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 |
|
|
|
|
| 98.6% |
Uncovered Elements: 1 (73) |
Complexity: 17 |
Complexity Density: 0.34 |
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
38 |
260
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0.25 |
|
44 |
260
|
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 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
51 |
260
|
public void endDocument() throws SAXException {... |
52 |
|
|
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
@param |
63 |
|
@param |
64 |
|
@exception |
65 |
|
|
66 |
|
@see |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
68 |
260
|
public void startPrefixMapping (String prefix, String uri) {... |
69 |
260
|
namespaces.put(prefix, uri); |
70 |
260
|
System.out.println(prefix + ":" + uri); |
71 |
|
} |
72 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (31) |
Complexity: 5 |
Complexity Density: 0.22 |
|
73 |
72044
|
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 |
|
|
|
|
| 95.2% |
Uncovered Elements: 1 (21) |
Complexity: 5 |
Complexity Density: 0.33 |
|
103 |
72044
|
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 |
|
|
108 |
|
|
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
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 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
137 |
157962
|
public void characters(char[] ch, int start, int length) {... |
138 |
157962
|
buffer.append(ch, start, length); |
139 |
|
} |
140 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
141 |
448742
|
private void print(final String text) {... |
142 |
|
|
143 |
448742
|
ps.print(text); |
144 |
|
} |
145 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
131484
|
private void println(final String text) {... |
147 |
|
|
148 |
131484
|
ps.println(text); |
149 |
|
} |
150 |
|
|
151 |
|
} |