Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 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  260 toggle public final void parse(final InputStream is, final OutputStream os) throws SAXException, IOException {
39  260 ps = new PrintStream(os, false, "UTF-8");
40  260 parser.setContentHandler(this);
41  260 parser.parse(new InputSource(is));
42    }
43   
 
44  260 toggle public void startDocument() throws SAXException {
45  260 tabs.setLength(0);
46  260 namespaces.clear();
47  260 hasSubs.clear();
48  260 hasSubs.push(new Enumerator());
49    }
50   
 
51  260 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  260 toggle public void startPrefixMapping (String prefix, String uri) {
69  260 namespaces.put(prefix, uri);
70  260 System.out.println(prefix + ":" + uri);
71    }
72   
 
73  72044 toggle public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
74  72044 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
75  39732 println(">");
76    }
77  72044 buffer.setLength(0);
78  72044 ((Enumerator) hasSubs.peek()).increaseNumber();
79  72044 hasSubs.push(new Enumerator());
80  72044 print(tabs.toString());
81  72044 print("<" + qName);
82  72044 int len = attributes.getLength();
83  72044 if (len > 0) {
84  44730 final SortedMap map = new TreeMap();
85  91854 for (int i = 0; i < len; i++) {
86  47124 map.put(attributes.getQName(i), attributes.getValue(i));
87    }
88  44730 Iterator it = map.entrySet().iterator();
89  91854 while (it.hasNext()) {
90  47124 print(" ");
91  47124 final Entry entry = (Entry) it.next();
92  47124 final String key = (String) entry.getKey();
93  47124 final String value = (String) entry.getValue();
94  47124 print(key);
95  47124 print("=\"");
96  47124 print(StringUtility.escapeXml(value));
97  47124 print("\"");
98    }
99    }
100  72044 tabs.append(" ");
101    }
102   
 
103  72044 toggle public void endElement(java.lang.String uri, java.lang.String localName, java.lang.String qName)
104    throws SAXException {
105  72044 tabs.setLength(tabs.length() - 2);
106  72044 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  9854 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
118  9854 println(">");
119    }
120  9854 print(tabs.toString());
121  9854 print(" ");
122  9854 println(buffer.toString().trim());
123  9854 print(tabs.toString());
124  9854 println("</" + qName + ">");
125  9854 buffer.setLength(0);
126    } else {
127  62190 if (((Enumerator) hasSubs.peek()).getNumber() == 0) {
128  22718 println("/>");
129    } else {
130  39472 print(tabs.toString());
131  39472 println("</" + qName + ">");
132    }
133    }
134  72044 hasSubs.pop();
135    }
136   
 
137  157962 toggle public void characters(char[] ch, int start, int length) {
138  157962 buffer.append(ch, start, length);
139    }
140   
 
141  448742 toggle private void print(final String text) {
142    // System.out.print(text);
143  448742 ps.print(text);
144    }
145   
 
146  131484 toggle private void println(final String text) {
147    // System.out.println(text);
148  131484 ps.println(text);
149    }
150   
151    }