1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.filter.xml.internal.parameter

File XStreamParameterManager.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

10
32
3
1
149
89
11
0.34
10.67
3
3.67

Classes

Class Line # Actions
XStreamParameterManager 60 32 0% 11 12
0.7333333573.3%
 

Contributing tests

This file is covered by 806 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.filter.xml.internal.parameter;
21   
22    import java.io.InputStream;
23    import java.lang.reflect.Type;
24    import java.util.ArrayList;
25    import java.util.Collection;
26    import java.util.LinkedHashMap;
27    import java.util.List;
28    import java.util.Map;
29    import java.util.Objects;
30   
31    import javax.inject.Inject;
32    import javax.inject.Singleton;
33    import javax.xml.stream.XMLStreamException;
34    import javax.xml.stream.XMLStreamWriter;
35   
36    import org.slf4j.Logger;
37    import org.w3c.dom.Element;
38    import org.xwiki.component.annotation.Component;
39    import org.xwiki.component.phase.Initializable;
40    import org.xwiki.component.phase.InitializationException;
41    import org.xwiki.component.util.ReflectionUtils;
42    import org.xwiki.filter.FilterEventParameters;
43    import org.xwiki.filter.xml.internal.XMLUtils;
44   
45    import com.thoughtworks.xstream.XStream;
46    import com.thoughtworks.xstream.converters.DataHolder;
47    import com.thoughtworks.xstream.core.MapBackedDataHolder;
48    import com.thoughtworks.xstream.io.xml.DomReader;
49    import com.thoughtworks.xstream.io.xml.StaxDriver;
50    import com.thoughtworks.xstream.io.xml.StaxWriter;
51   
52    /**
53    * XStream based implementation of {@link ParameterManager}.
54    *
55    * @version $Id: fc2f20a389ba1deb80c18f4e99fee9606d0a44d2 $
56    * @since 5.2M1
57    */
58    @Component
59    @Singleton
 
60    public class XStreamParameterManager implements ParameterManager, Initializable
61    {
62    /**
63    * The name of the type field containing the default type of the parameter.
64    */
65    public static final String DDEFAULTTYPE_NAME = "defaultType";
66   
67    /**
68    * The logger.
69    */
70    @Inject
71    private Logger logger;
72   
73    /**
74    * The XStream entry point.
75    */
76    private XStream xstream;
77   
78    /**
79    * Used to create xml stream writers.
80    */
81    private StaxDriver staxDriver;
82   
 
83  845 toggle @Override
84    public void initialize() throws InitializationException
85    {
86  845 this.staxDriver = new StaxDriver();
87  845 this.xstream = new XStream(this.staxDriver);
88   
89  845 this.xstream.setMarshallingStrategy(new XMLTreeMarshallingStrategy());
90   
91  845 this.xstream.addDefaultImplementation(LinkedHashMap.class, Map.class);
92  845 this.xstream.addDefaultImplementation(ArrayList.class, Collection.class);
93  845 this.xstream.addDefaultImplementation(ArrayList.class, List.class);
94   
95  845 this.xstream.registerConverter(new XMLFilterElementParametersConverter(this.xstream.getMapper()));
96  845 this.xstream.registerConverter(new InputStreamConverter());
97   
98  845 this.xstream.alias("parameters", FilterEventParameters.class);
99  845 this.xstream.alias("map", LinkedHashMap.class);
100  845 this.xstream.alias("input-stream", InputStream.class);
101    }
102   
 
103  1418 toggle @Override
104    public void serialize(Type type, Object object, XMLStreamWriter xmlStreamWriter)
105    {
106  1418 Class<?> typeClass = ReflectionUtils.getTypeClass(type);
107  1418 if (typeClass != null && Objects.equals(XMLUtils.emptyValue(typeClass), object)) {
108  0 return;
109    }
110   
111  1418 StaxWriter staxWriter;
112  1418 try {
113  1418 staxWriter = this.staxDriver.createStaxWriter(xmlStreamWriter, false);
114    } catch (XMLStreamException e) {
115    // Should never happen since that when sending start document event
116  0 this.logger.error("Failed to create new instance of StaxWriter", e);
117   
118  0 return;
119    }
120   
121  1418 DataHolder dataHolder = new MapBackedDataHolder();
122  1418 if (type != Object.class) {
123  1408 dataHolder.put(DDEFAULTTYPE_NAME, type);
124    }
125   
126  1418 this.xstream.marshal(object, staxWriter, dataHolder);
127    }
128   
 
129  955 toggle @Override
130    public Object unSerialize(Type type, Element rootElement) throws ClassNotFoundException
131    {
132  955 if (type != null && !rootElement.hasChildNodes()) {
133  0 Object value = XMLUtils.emptyValue(ReflectionUtils.getTypeClass(type));
134  0 if (value != null) {
135  0 return value;
136    }
137    }
138   
139  955 DataHolder dataHolder = new MapBackedDataHolder();
140   
141  955 if (type == Object.class) {
142  0 dataHolder.put(DDEFAULTTYPE_NAME, String.class);
143    } else {
144  955 dataHolder.put(DDEFAULTTYPE_NAME, type);
145    }
146   
147  955 return this.xstream.unmarshal(new DomReader(rootElement), null, dataHolder);
148    }
149    }