Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../../img/srcFileCovDistChart7.png 76% of files have more coverage
29   111   13   3.22
2   77   0.45   9
9     1.44  
1    
 
  AbstractSerializer       Line # 30 29 0% 13 14 65% 0.65
 
  (4)
 
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.xdomxml.internal.renderer.parameters;
21   
22    import java.util.Map;
23    import java.util.regex.Pattern;
24   
25    import org.xml.sax.Attributes;
26    import org.xml.sax.ContentHandler;
27    import org.xml.sax.SAXException;
28    import org.xml.sax.helpers.AttributesImpl;
29   
 
30    public abstract class AbstractSerializer
31    {
32    public static final Pattern VALIDNAME = Pattern.compile("[A-Za-z][A-Za-z0-9:_.-]*");
33   
34    public static final Attributes EMPTY_ATTRIBUTES = new AttributesImpl();
35   
 
36  2 toggle public void serializeParameter(String name, Map<String, String> map, ContentHandler contentHandler)
37    {
38  2 startElement(name, EMPTY_ATTRIBUTES, contentHandler);
39  2 for (Map.Entry<String, String> entry : map.entrySet()) {
40  4 serializeParameter(entry.getKey(), entry.getValue(), contentHandler);
41    }
42  2 endElement(name, contentHandler);
43    }
44   
 
45  0 toggle public void serializeParameter(String name, char value, ContentHandler contentHandler)
46    {
47  0 serializeParameter(name, String.valueOf(value), contentHandler);
48    }
49   
 
50  0 toggle public void serializeParameter(String name, int value, ContentHandler contentHandler)
51    {
52  0 serializeParameter(name, String.valueOf(value), contentHandler);
53    }
54   
 
55  0 toggle public void serializeParameter(String name, boolean value, ContentHandler contentHandler)
56    {
57  0 serializeParameter(name, String.valueOf(value), contentHandler);
58    }
59   
 
60  75 toggle public void serializeParameter(String name, String value, ContentHandler contentHandler)
61    {
62  75 String nodeName;
63  75 Attributes attributes;
64   
65  75 if (isValidNodeName(name)) {
66  75 nodeName = name;
67  75 attributes = EMPTY_ATTRIBUTES;
68    } else {
69  0 nodeName = "entry";
70  0 AttributesImpl attributesImpl = new AttributesImpl();
71  0 attributesImpl.addAttribute(null, null, "name", null, name);
72  0 attributes = attributesImpl;
73    }
74   
75  75 startElement(nodeName, attributes, contentHandler);
76  75 characters(value, contentHandler);
77  75 endElement(nodeName, contentHandler);
78    }
79   
 
80  75 toggle public boolean isValidNodeName(String name)
81    {
82  75 return VALIDNAME.matcher(name).matches();
83    }
84   
 
85  189 toggle public void startElement(String elementName, Attributes attributes, ContentHandler contentHandler)
86    {
87  189 try {
88  189 contentHandler.startElement("", elementName, elementName, attributes);
89    } catch (SAXException e) {
90  0 throw new RuntimeException("Failed to send sax event", e);
91    }
92    }
93   
 
94  75 toggle public void characters(String str, ContentHandler contentHandler)
95    {
96  75 try {
97  75 contentHandler.characters(str.toCharArray(), 0, str.length());
98    } catch (SAXException e) {
99  0 throw new RuntimeException("Failed to send sax event", e);
100    }
101    }
102   
 
103  189 toggle public void endElement(String elementName, ContentHandler contentHandler)
104    {
105  189 try {
106  189 contentHandler.endElement("", elementName, elementName);
107    } catch (SAXException e) {
108  0 throw new RuntimeException("Failed to send sax event", e);
109    }
110    }
111    }