1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.extension.repository.internal

File CollectionExtensionPropertySerializer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

18
37
8
1
182
93
17
0.46
4.62
8
2.12

Classes

Class Line # Actions
CollectionExtensionPropertySerializer 37 37 0% 17 7
0.888888988.9%
 

Contributing tests

This file is covered by 94 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.extension.repository.internal;
21   
22    import java.util.ArrayList;
23    import java.util.Collection;
24    import java.util.Map;
25   
26    import org.w3c.dom.Document;
27    import org.w3c.dom.Element;
28    import org.w3c.dom.Node;
29    import org.w3c.dom.NodeList;
30   
31    /**
32    * Serialize and unserialize {@link Collection} properties.
33    *
34    * @param <C> the type of the property value
35    * @version $Id: d52366c498f59123a0b0eaeb2c20a4a3c23d91e8 $
36    */
 
37    public class CollectionExtensionPropertySerializer<C extends Collection> extends AbstractExtensionPropertySerializer<C>
38    {
39    /**
40    * The serializers by type id.
41    */
42    protected Map<String, ExtensionPropertySerializer> serializerById;
43   
44    /**
45    * The serializers by class.
46    */
47    protected Map<Class<?>, ExtensionPropertySerializer> serializerByClass;
48   
49    /**
50    * @param type the type of the property
51    * @param serializerById the serializers by type id
52    * @param serializerByClass the serializers by class
53    */
 
54  554 toggle protected CollectionExtensionPropertySerializer(String type,
55    Map<String, ExtensionPropertySerializer> serializerById,
56    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
57    {
58  554 super(type);
59   
60  554 this.serializerById = serializerById;
61  554 this.serializerByClass = serializerByClass;
62    }
63   
64    /**
65    * @param serializerById the serializers by type id
66    * @param serializerByClass the serializers by class
67    */
 
68  277 toggle public CollectionExtensionPropertySerializer(Map<String, ExtensionPropertySerializer> serializerById,
69    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
70    {
71  277 this("collection", serializerById, serializerByClass);
72    }
73   
74    /**
75    * @return a new collection
76    */
 
77  1 toggle protected C createCollection()
78    {
79  1 return (C) new ArrayList();
80    }
81   
82    /**
83    * @param <T> the type of the expected value
84    * @param element the element to unserialize
85    * @param serializerById the serializers by type id
86    * @return the unserialized property value
87    */
 
88  682254 toggle public static <T> T toValue(Element element, Map<String, ExtensionPropertySerializer> serializerById)
89    {
90  682254 if (element != null) {
91  682254 String type = element.getAttribute("type");
92   
93  682254 ExtensionPropertySerializer<?> serializer = serializerById.get(type);
94   
95  682254 if (serializer != null) {
96  682254 return (T) serializer.toValue(element);
97    }
98    }
99   
100  0 return null;
101    }
102   
103    /**
104    * @param valueClass the class of the value to serialize
105    * @param serializerByClass the serializers by class
106    * @return the serializer for the provided class
107    */
 
108  3116937 toggle public static ExtensionPropertySerializer getSerializerByClass(Class<?> valueClass,
109    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
110    {
111  3116937 for (Map.Entry<Class<?>, ExtensionPropertySerializer> entry : serializerByClass.entrySet()) {
112  9460778 if (entry.getKey().isAssignableFrom(valueClass)) {
113  2409279 return entry.getValue();
114    }
115    }
116   
117  707658 return null;
118    }
119   
120    /**
121    * @param value the value to serialize
122    * @param document the document used to create new {@link Element}
123    * @param elementName the name of the element to create
124    * @param serializerByClass the serializers by class
125    * @return the serialized property {@link Element}
126    */
 
127  3763912 toggle public static Element toElement(Object value, Document document, String elementName,
128    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
129    {
130  3763912 if (value != null) {
131  3116937 ExtensionPropertySerializer serializer = getSerializerByClass(value.getClass(), serializerByClass);
132   
133  3116937 if (serializer != null) {
134  2409279 return serializer.toElement(document, elementName, value);
135    }
136    }
137   
138  1354633 return null;
139    }
140   
 
141  458 toggle @Override
142    public C toValue(Element element)
143    {
144  458 C collection = createCollection();
145   
146  458 NodeList featuresNodes = element.getChildNodes();
147   
148  2156 for (int i = 0; i < featuresNodes.getLength(); ++i) {
149  1698 Node node = featuresNodes.item(i);
150   
151  1698 if (node.getNodeType() == Node.ELEMENT_NODE) {
152  620 Object value = toValue((Element) node, this.serializerById);
153   
154  620 if (value == null) {
155  0 return null;
156    } else {
157  620 collection.add(value);
158    }
159    }
160    }
161   
162  458 return collection;
163    }
164   
 
165  2 toggle @Override
166    public Element toElement(Document document, String elementName, C elementValue)
167    {
168  2 Element element = createRootElement(document, elementName);
169   
170  2 for (Object subValue : elementValue) {
171  4 Element subElement = toElement(subValue, document, elementName, this.serializerByClass);
172   
173  4 if (subElement == null) {
174  0 return null;
175    }
176   
177  4 element.appendChild(subElement);
178    }
179   
180  2 return element;
181    }
182    }