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

File AbstractSerializer.java

 

Coverage histogram

../../../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

8
40
9
1
128
92
17
0.43
4.44
9
1.89

Classes

Class Line # Actions
AbstractSerializer 30 40 0% 17 17
0.701754470.2%
 

Contributing tests

This file is covered by 2 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.rendering.xdomxml10.internal.renderer.parameter;
21   
22    import java.util.Map;
23   
24    import org.xml.sax.Attributes;
25    import org.xml.sax.ContentHandler;
26    import org.xml.sax.SAXException;
27    import org.xml.sax.helpers.AttributesImpl;
28    import org.xwiki.rendering.xdomxml10.internal.XDOMXMLConstants;
29   
 
30    public abstract class AbstractSerializer
31    {
32    public static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
33   
34    // TODO: support more than strings
 
35  2 toggle public void serializeParameter(String name, Map< ? , ? > map, boolean type, ContentHandler contentHandler)
36    {
37  2 Attributes attributes;
38   
39  2 if (type) {
40  2 AttributesImpl attributesImpl = new AttributesImpl();
41  2 attributesImpl.addAttribute(null, null, "type", null, "stringmap");
42  2 attributes = attributesImpl;
43    } else {
44  0 attributes = EMPTY_ATTRIBUTES;
45    }
46   
47  2 startElement(name, attributes, contentHandler);
48  2 for (Map.Entry< ? , ? > entry : map.entrySet()) {
49  2 if (entry.getValue() != null && entry.getKey() != null) {
50  2 serializeParameter(entry.getKey().toString(), entry.getValue().toString(), null, contentHandler);
51    }
52    }
53  2 endElement(name, contentHandler);
54    }
55   
 
56  0 toggle public void serializeParameter(String name, char value, boolean type, ContentHandler contentHandler)
57    {
58  0 serializeParameter(name, String.valueOf(value), "char", contentHandler);
59    }
60   
 
61  0 toggle public void serializeParameter(String name, int value, boolean type, ContentHandler contentHandler)
62    {
63  0 serializeParameter(name, String.valueOf(value), "int", contentHandler);
64    }
65   
 
66  0 toggle public void serializeParameter(String name, boolean value, boolean type, ContentHandler contentHandler)
67    {
68  0 serializeParameter(name, String.valueOf(value), "bool", contentHandler);
69    }
70   
 
71  8 toggle public void serializeParameter(String name, String value, String type, ContentHandler contentHandler)
72    {
73  8 String nodeName;
74  8 Attributes attributes;
75   
76  8 if (isValidNodeName(name)) {
77  8 nodeName = name;
78  8 if (type == null) {
79  6 attributes = EMPTY_ATTRIBUTES;
80    } else {
81  2 AttributesImpl attributesImpl = new AttributesImpl();
82  2 attributesImpl.addAttribute(null, null, "type", null, type);
83  2 attributes = attributesImpl;
84    }
85    } else {
86  0 nodeName = "entry";
87  0 AttributesImpl attributesImpl = new AttributesImpl();
88  0 attributesImpl.addAttribute(null, null, "name", null, name);
89  0 attributes = attributesImpl;
90    }
91   
92  8 startElement(nodeName, attributes, contentHandler);
93  8 characters(value, contentHandler);
94  8 endElement(nodeName, contentHandler);
95    }
96   
 
97  8 toggle public boolean isValidNodeName(String name)
98    {
99  8 return XDOMXMLConstants.VALID_ELEMENTNAME.matcher(name).matches();
100    }
101   
 
102  18 toggle public void startElement(String elementName, Attributes attributes, ContentHandler contentHandler)
103    {
104  18 try {
105  18 contentHandler.startElement("", elementName, elementName, attributes);
106    } catch (SAXException e) {
107  0 throw new RuntimeException("Failed to send sax event", e);
108    }
109    }
110   
 
111  8 toggle public void characters(String str, ContentHandler contentHandler)
112    {
113  8 try {
114  8 contentHandler.characters(str.toCharArray(), 0, str.length());
115    } catch (SAXException e) {
116  0 throw new RuntimeException("Failed to send sax event", e);
117    }
118    }
119   
 
120  18 toggle public void endElement(String elementName, ContentHandler contentHandler)
121    {
122  18 try {
123  18 contentHandler.endElement("", elementName, elementName);
124    } catch (SAXException e) {
125  0 throw new RuntimeException("Failed to send sax event", e);
126    }
127    }
128    }