1. Project Clover database Sat Feb 2 2019 06:45:20 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 83 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  272 toggle protected StringKeyMapExtensionPropertySerializer(String type,
55    Map<String, ExtensionPropertySerializer> serializerById,
56    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
57    {
58  272 super(type);
59   
60  272 this.serializerById = serializerById;
61  272 this.serializerByClass = serializerByClass;
62    }
63   
64    /**
65    * @param serializerById the serializers by type id
66    * @param serializerByClass the serializers by class
67    */
 
68  272 toggle public StringKeyMapExtensionPropertySerializer(Map<String, ExtensionPropertySerializer> serializerById,
69    Map<Class<?>, ExtensionPropertySerializer> serializerByClass)
70    {
71  272 this("strinkeygmap", serializerById, serializerByClass);
72    }
73   
 
74  1964 toggle @Override
75    public M toValue(Element element)
76    {
77  1964 M map = (M) new HashMap();
78   
79  1964 NodeList featuresNodes = element.getChildNodes();
80   
81  9674 for (int i = 0; i < featuresNodes.getLength(); ++i) {
82  7710 Node node = featuresNodes.item(i);
83   
84  7710 if (node.getNodeType() == Node.ELEMENT_NODE) {
85  2873 String key = node.getNodeName();
86  2873 Object value = CollectionExtensionPropertySerializer.toValue((Element) node, this.serializerById);
87   
88  2873 map.put(key, value);
89    }
90    }
91   
92  1964 return map;
93    }
94   
 
95  1207 toggle @Override
96    public Element toElement(Document document, String elementName, M elementValue)
97    {
98  1207 Element element = createRootElement(document, elementName);
99   
100  1207 Set<Map.Entry> set = elementValue.entrySet();
101  1207 for (Map.Entry entry : set) {
102  1932 if (entry.getKey() != null) {
103  1932 Element subElement =
104    CollectionExtensionPropertySerializer.toElement(entry.getValue(), document, entry.getKey()
105    .toString(), this.serializerByClass);
106   
107  1932 if (subElement != null) {
108  1932 element.appendChild(subElement);
109    }
110    }
111    }
112   
113  1207 return element;
114    }
115    }