Clover Coverage Report
Coverage timestamp: Fri Feb 14 2014 01:49:02 UTC
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
50   151   17   5.56
14   105   0.34   9
9     1.89  
1    
 
  XmlNormalizerHandler       Line # 23 50 17 98.6% 0.98630136
 
  (1)
 
1    package org.qedeq.kernel.xml.test;
2   
3    import java.io.IOException;
4    import java.io.InputStream;
5    import java.io.OutputStream;
6    import java.io.PrintStream;
7    import java.util.HashMap;
8    import java.util.Iterator;
9    import java.util.Map;
10    import java.util.SortedMap;
11    import java.util.Stack;
12    import java.util.TreeMap;
13    import java.util.Map.Entry;
14   
15    import org.apache.xerces.parsers.SAXParser;
16    import org.qedeq.base.utility.Enumerator;
17    import org.qedeq.base.utility.StringUtility;
18    import org.xml.sax.Attributes;
19    import org.xml.sax.InputSource;
20    import org.xml.sax.SAXException;
21    import org.xml.sax.helpers.DefaultHandler;
22   
 
23    public class XmlNormalizerHandler extends DefaultHandler {
24   
25    private Map namespaces = new HashMap();
26   
27    private StringBuffer buffer = new StringBuffer();
28   
29    private StringBuffer tabs = new StringBuffer();
30   
31    final SAXParser parser = new SAXParser();
32   
33    Stack hasSubs = new Stack();
34   
35    private PrintStream ps;
36   
37   
 
38  266 toggle public final void parse(final InputStream is, final OutputStream os) throws SAXException, IOException {
39  266 ps = new PrintStream(os, false, "UTF-8");
40  266 parser.setContentHandler(this);
41  266 parser.parse(new InputSource(is));
42    }
43   
 
44  266 toggle public void startDocument() throws SAXException {
45  266 tabs.setLength(0);
46  266 namespaces.clear();
47  266 hasSubs.clear();
48  266 hasSubs.push(new Enumerator());
49    }
50   
 
51  266 toggle public void endDocument() throws SAXException {
52    // nothing to do
53    }
54   
55    /**
56    * Receive notification of the start of a Namespace mapping.
57    *
58    * <p>By default, do nothing. Application writers may override this
59    * method in a subclass to take specific actions at the start of
60    * each Namespace prefix scope (such as storing the prefix mapping).</p>
61    *
62    * @param prefix The Namespace prefix being declared.
63    * @param uri The Namespace URI mapped to the prefix.
64    * @exception org.xml.sax.SAXException Any SAX exception, possibly
65    * wrapping another exception.
66    * @see org.xml.sax.ContentHandler#startPrefixMapping
67    */
 
68  266 toggle public void startPrefixMapping (String prefix, String uri) {
69  266 namespaces.put(prefix, uri);
70  266 System.out.println(prefix + ":" + uri);
71    }
72   
 
73  72240 toggle public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
74  72240 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
75  39866 println(">");
76    }
77  72240 buffer.setLength(0);
78  72240 ((Enumerator) hasSubs.peek()).increaseNumber();
79  72240 hasSubs.push(new Enumerator());
80  72240 print(tabs.toString());
81  72240 print("<" + qName);
82  72240 int len = attributes.getLength();
83  72240 if (len > 0) {
84  44846 final SortedMap map = new TreeMap();
85  92104 for (int i = 0; i < len; i++) {
86  47258 map.put(attributes.getQName(i), attributes.getValue(i));
87    }
88  44846 Iterator it = map.entrySet().iterator();
89  92104 while (it.hasNext()) {
90  47258 print(" ");
91  47258 final Entry entry = (Entry) it.next();
92  47258 final String key = (String) entry.getKey();
93  47258 final String value = (String) entry.getValue();
94  47258 print(key);
95  47258 print("=\"");
96  47258 print(StringUtility.escapeXml(value));
97  47258 print("\"");
98    }
99    }
100  72240 tabs.append(" ");
101    }
102   
 
103  72240 toggle public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
104    throws SAXException {
105  72240 tabs.setLength(tabs.length() - 2);
106  72240 if (buffer.length() > 0 && buffer.toString().trim().length() > 0) {
107    // int i = 0;
108    // while (buffer.charAt(i) == 10 || buffer.charAt(i) == 13) {
109    // i++;
110    // }
111    // buffer.delete(0, i);
112    // i = buffer.length() - 1;
113    // while (buffer.charAt(i) == 10 || buffer.charAt(i) == 13) {
114    // i--;
115    // }
116    // buffer.delete(i + 1, buffer.length());
117  9904 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
118  9904 println(">");
119    }
120  9904 print(tabs.toString());
121  9904 print(" ");
122  9904 println(buffer.toString().trim());
123  9904 print(tabs.toString());
124  9904 println("</" + qName + ">");
125  9904 buffer.setLength(0);
126    } else {
127  62336 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
128  22736 println("/>");
129    } else {
130  39600 print(tabs.toString());
131  39600 println("</" + qName + ">");
132    }
133    }
134  72240 hasSubs.pop();
135    }
136   
 
137  158476 toggle public void characters(char[] ch, int start, int length) {
138  158476 buffer.append(ch, start, length);
139    }
140   
 
141  450082 toggle private void print(final String text) {
142    // System.out.print(text);
143  450082 ps.print(text);
144    }
145   
 
146  131914 toggle private void println(final String text) {
147    // System.out.println(text);
148  131914 ps.println(text);
149    }
150   
151    }