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

File StringKeyMapExtensionPropertySerializer.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

8
21
4
1
115
58
8
0.38
5.25
4
2

Classes

Class Line # Actions
StringKeyMapExtensionPropertySerializer 37 21 0% 8 2
0.9393939493.9%
 

Contributing tests

This file is covered by 82 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.HashMap;
23    import java.util.Map;
24    import java.util.Set;
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 Map} properties.
33    *
34    * @param <M> the type of the property value
35    * @version $Id: 4a07a6f0d007ffa32ba3c4b0e9ea64f670ae00ef $
36    */
 
37    public class StringKeyMapExtensionPropertySerializer<M extends Map> extends AbstractExtensionPropertySerializer<M>
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  277 toggle protected StringKeyMapExtensionPropertySerializer(String type,
55    Map<String, ExtensionPropertySerializer> serializerById,
56    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
57    {
58  277 super(type);
59   
60  277 this.serializerById = serializerById;
61  277 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 StringKeyMapExtensionPropertySerializer(Map<String, ExtensionPropertySerializer> serializerById,
69    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
70    {
71  277 this("strinkeygmap", serializerById, serializerByClass);
72    }
73   
 
74  163 toggle @Override
75    public M toValue(Element element)
76    {
77  163 M map = (M) new HashMap();
78   
79  163 NodeList featuresNodes = element.getChildNodes();
80   
81  652 for (int i = 0; i < featuresNodes.getLength(); ++i) {
82  489 Node node = featuresNodes.item(i);
83   
84  489 if (node.getNodeType() == Node.ELEMENT_NODE) {
85  163 String key = node.getNodeName();
86  163 Object value = CollectionExtensionPropertySerializer.toValue((Element) node, this.serializerById);
87   
88  163 map.put(key, value);
89    }
90    }
91   
92  163 return map;
93    }
94   
 
95  396 toggle @Override
96    public Element toElement(Document document, String elementName, M elementValue)
97    {
98  396 Element element = createRootElement(document, elementName);
99   
100  396 Set<Map.Entry> set = elementValue.entrySet();
101  396 for (Map.Entry entry : set) {
102  610 if (entry.getKey() != null) {
103  610 Element subElement =
104    CollectionExtensionPropertySerializer.toElement(entry.getValue(), document, entry.getKey()
105    .toString(), this.serializerByClass);
106   
107  610 if (subElement != null) {
108  610 element.appendChild(subElement);
109    }
110    }
111    }
112   
113  396 return element;
114    }
115    }