1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.xml; |
21 |
|
|
22 |
|
import java.util.Stack; |
23 |
|
|
24 |
|
import org.xml.sax.Attributes; |
25 |
|
import org.xml.sax.SAXException; |
26 |
|
import org.xml.sax.helpers.DefaultHandler; |
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
@version |
47 |
|
@since |
48 |
|
|
|
|
| 75.8% |
Uncovered Elements: 23 (95) |
Complexity: 26 |
Complexity Density: 0.45 |
|
49 |
|
public class ExtractHandler extends DefaultHandler |
50 |
|
{ |
51 |
|
|
52 |
|
|
53 |
|
|
|
|
| 71.4% |
Uncovered Elements: 2 (7) |
Complexity: 3 |
Complexity Density: 0.75 |
|
54 |
|
private static class XMLTag |
55 |
|
{ |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
private String qName; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private Attributes atts; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
@param |
70 |
|
@param |
71 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
72 |
6 |
XMLTag(String qName, Attributes atts)... |
73 |
|
{ |
74 |
6 |
this.qName = qName; |
75 |
6 |
this.atts = atts; |
76 |
|
} |
77 |
|
|
78 |
|
|
79 |
|
@return |
80 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
2 |
public String getQName()... |
82 |
|
{ |
83 |
2 |
return this.qName; |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
@return |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
89 |
0 |
public Attributes getAtts()... |
90 |
|
{ |
91 |
0 |
return this.atts; |
92 |
|
} |
93 |
|
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
private int lowerBound; |
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
private int upperBound; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
private int counter; |
109 |
|
|
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
private Stack<XMLTag> openedTags = new Stack<XMLTag>(); |
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
private StringBuilder result = new StringBuilder(); |
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
private boolean finished; |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
@param |
132 |
|
@throws |
133 |
|
|
|
|
| 63.6% |
Uncovered Elements: 4 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
134 |
4 |
public ExtractHandler(int start, int length) throws SAXException... |
135 |
|
{ |
136 |
4 |
super(); |
137 |
4 |
if (start < 0) { |
138 |
0 |
throw new SAXException("Start must be greater than or equal to 0"); |
139 |
|
} |
140 |
4 |
if (length <= 0) { |
141 |
0 |
throw new SAXException("Length must be greater than 0"); |
142 |
|
} |
143 |
4 |
this.lowerBound = start; |
144 |
4 |
this.upperBound = this.lowerBound + length; |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
@return |
149 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
4 |
public String getResult()... |
151 |
|
{ |
152 |
4 |
return this.result.toString(); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
|
@return |
157 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
158 |
2 |
public boolean isFinished()... |
159 |
|
{ |
160 |
2 |
return this.finished; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@param |
167 |
|
@param |
168 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
169 |
6 |
private void openTag(String qName, Attributes atts)... |
170 |
|
{ |
171 |
6 |
this.result.append('<').append(qName); |
172 |
6 |
for (int i = 0; i < atts.getLength(); i++) { |
173 |
0 |
this.result.append(' ').append(atts.getQName(i)).append("=\"").append(atts.getValue(i)).append('\"'); |
174 |
|
} |
175 |
6 |
this.result.append('>'); |
176 |
|
} |
177 |
|
|
178 |
|
|
179 |
|
|
180 |
|
|
181 |
|
@see |
182 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
183 |
0 |
private void openTags()... |
184 |
|
{ |
185 |
0 |
for (XMLTag tag : this.openedTags) { |
186 |
0 |
openTag(tag.getQName(), tag.getAtts()); |
187 |
|
} |
188 |
|
} |
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
@see |
194 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 2 |
Complexity Density: 1 |
|
195 |
4 |
private void closeTags()... |
196 |
|
{ |
197 |
6 |
while (!this.openedTags.isEmpty()) { |
198 |
2 |
closeTag(this.openedTags.pop().getQName()); |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
@param |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
207 |
6 |
private void closeTag(String qName)... |
208 |
|
{ |
209 |
6 |
this.result.append("</").append(qName).append('>'); |
210 |
|
} |
211 |
|
|
212 |
|
|
213 |
|
@return |
214 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
215 |
14 |
private boolean isExtracting()... |
216 |
|
{ |
217 |
14 |
return this.lowerBound <= this.counter && this.counter <= this.upperBound; |
218 |
|
} |
219 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
220 |
4 |
@Override... |
221 |
|
public void startDocument() throws SAXException |
222 |
|
{ |
223 |
4 |
super.startDocument(); |
224 |
4 |
this.counter = 0; |
225 |
4 |
this.openedTags.clear(); |
226 |
4 |
this.result.setLength(0); |
227 |
4 |
this.finished = false; |
228 |
|
} |
229 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
230 |
6 |
@Override... |
231 |
|
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException |
232 |
|
{ |
233 |
6 |
this.openedTags.push(new XMLTag(qName, atts)); |
234 |
6 |
if (isExtracting()) { |
235 |
6 |
openTag(qName, atts); |
236 |
|
} |
237 |
|
} |
238 |
|
|
|
|
| 67.6% |
Uncovered Elements: 11 (34) |
Complexity: 7 |
Complexity Density: 0.29 |
|
239 |
6 |
@Override... |
240 |
|
public void characters(char[] ch, int start, int length) throws SAXException |
241 |
|
{ |
242 |
6 |
int offset = this.lowerBound - this.counter; |
243 |
6 |
if (offset > 0) { |
244 |
0 |
if (offset > length) { |
245 |
0 |
this.counter += length; |
246 |
0 |
return; |
247 |
|
} else { |
248 |
0 |
this.counter = this.lowerBound; |
249 |
0 |
openTags(); |
250 |
0 |
characters(ch, start + offset, length - offset); |
251 |
0 |
return; |
252 |
|
} |
253 |
|
} |
254 |
6 |
int remainingLength = this.upperBound - this.counter; |
255 |
6 |
if (remainingLength <= length) { |
256 |
2 |
String content = String.valueOf(ch, start, remainingLength); |
257 |
2 |
int spaceIndex = remainingLength; |
258 |
|
|
259 |
2 |
if (length > remainingLength && ch[start + remainingLength] != ' ') { |
260 |
2 |
spaceIndex = content.lastIndexOf(' '); |
261 |
|
} |
262 |
2 |
if (spaceIndex >= 0) { |
263 |
1 |
this.counter += spaceIndex; |
264 |
1 |
this.result.append(content.substring(0, spaceIndex)); |
265 |
|
} else { |
266 |
1 |
this.counter = this.upperBound; |
267 |
1 |
this.result.append(content); |
268 |
|
} |
269 |
2 |
endDocument(); |
270 |
2 |
throw new SAXException("Length limit reached"); |
271 |
|
} else { |
272 |
4 |
this.counter += length; |
273 |
4 |
this.result.append(ch, start, length); |
274 |
|
} |
275 |
|
} |
276 |
|
|
|
|
| 80% |
Uncovered Elements: 1 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
277 |
4 |
@Override... |
278 |
|
public void endElement(String namespaceURI, String localName, String qName) throws SAXException |
279 |
|
{ |
280 |
|
|
281 |
|
|
282 |
4 |
this.openedTags.pop(); |
283 |
4 |
if (isExtracting()) { |
284 |
4 |
closeTag(qName); |
285 |
|
} |
286 |
|
} |
287 |
|
|
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
288 |
4 |
@Override... |
289 |
|
public void endDocument() throws SAXException |
290 |
|
{ |
291 |
4 |
super.endDocument(); |
292 |
|
|
293 |
4 |
if (isExtracting()) { |
294 |
4 |
closeTags(); |
295 |
|
} |
296 |
|
|
297 |
4 |
this.finished = true; |
298 |
|
} |
299 |
|
} |