1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.qedeq.kernel.xml.tracker; |
16 |
|
|
17 |
|
import java.io.File; |
18 |
|
import java.io.FileInputStream; |
19 |
|
import java.io.IOException; |
20 |
|
import java.io.InputStream; |
21 |
|
import java.io.Reader; |
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.xml.parsers.ParserConfigurationException; |
28 |
|
import javax.xml.parsers.SAXParser; |
29 |
|
import javax.xml.parsers.SAXParserFactory; |
30 |
|
|
31 |
|
import org.qedeq.base.io.IoUtility; |
32 |
|
import org.qedeq.base.io.SourceArea; |
33 |
|
import org.qedeq.base.io.SourcePosition; |
34 |
|
import org.qedeq.base.io.TextInput; |
35 |
|
import org.qedeq.base.trace.Trace; |
36 |
|
import org.qedeq.base.utility.Enumerator; |
37 |
|
import org.qedeq.kernel.xml.handler.common.SimpleHandler; |
38 |
|
import org.xml.sax.Attributes; |
39 |
|
import org.xml.sax.InputSource; |
40 |
|
import org.xml.sax.SAXException; |
41 |
|
import org.xml.sax.XMLReader; |
42 |
|
|
43 |
|
import com.sun.syndication.io.XmlReader; |
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@author |
59 |
|
|
|
|
| 70.3% |
Uncovered Elements: 62 (209) |
Complexity: 51 |
Complexity Density: 0.33 |
|
60 |
|
public final class XPathLocationParser extends SimpleHandler { |
61 |
|
|
62 |
|
|
63 |
|
private static final Class CLASS = XPathLocationParser.class; |
64 |
|
|
65 |
|
|
66 |
|
private static final String NAMESPACES_FEATURE_ID = "http://xml.org/sax/features/namespaces"; |
67 |
|
|
68 |
|
|
69 |
|
private static final String VALIDATION_FEATURE_ID = "http://xml.org/sax/features/validation"; |
70 |
|
|
71 |
|
|
72 |
|
private final XMLReader reader; |
73 |
|
|
74 |
|
|
75 |
|
private final SimpleXPath find; |
76 |
|
|
77 |
|
|
78 |
|
private SimpleXPath current; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private SimpleXPath summary; |
83 |
|
|
84 |
|
|
85 |
|
private File xmlFile; |
86 |
|
|
87 |
|
|
88 |
|
private final List elements; |
89 |
|
|
90 |
|
|
91 |
|
private int level; |
92 |
|
|
93 |
|
|
94 |
|
private SourcePosition startDelta; |
95 |
|
|
96 |
|
|
97 |
|
private SourcePosition endDelta; |
98 |
|
|
99 |
|
|
100 |
|
private SourcePosition start; |
101 |
|
|
102 |
|
|
103 |
|
private SourcePosition end; |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
@param |
109 |
|
@param |
110 |
|
@return |
111 |
|
@throws |
112 |
|
@throws |
113 |
|
@throws |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
@param |
126 |
|
@param |
127 |
|
@param |
128 |
|
|
129 |
|
@param |
130 |
|
|
131 |
|
@param |
132 |
|
@return |
133 |
|
|
|
|
| 75% |
Uncovered Elements: 5 (20) |
Complexity: 8 |
Complexity Density: 0.5 |
|
134 |
22
|
public static SourceArea findSourceArea(final String address, final SimpleXPath xpath,... |
135 |
|
final SourcePosition startDelta, final SourcePosition endDelta, final File file) { |
136 |
22
|
final String method = "findSourceArea(String, SimpleXPath, SourcePosition, SourcePosition, File)"; |
137 |
22
|
final String message = "Could not find \"" + xpath + "\" within \"" + file + "\""; |
138 |
22
|
try { |
139 |
22
|
XPathLocationParser parser = new XPathLocationParser(xpath, startDelta, endDelta); |
140 |
22
|
parser.parse(file); |
141 |
21
|
if (parser.getStart() == null || parser.getEnd() == null) { |
142 |
1
|
Trace.fatal(CLASS, method, message, null); |
143 |
1
|
if (Boolean.TRUE.toString().equalsIgnoreCase( |
144 |
|
System.getProperty("qedeq.test.xmlLocationFailures"))) { |
145 |
0
|
throw new RuntimeException(message); |
146 |
|
} |
147 |
1
|
return new SourceArea(address); |
148 |
|
} |
149 |
20
|
return new SourceArea(address, parser.getStart(), parser.getEnd()); |
150 |
|
} catch (ParserConfigurationException e) { |
151 |
0
|
Trace.fatal(CLASS, method, message, e); |
152 |
|
} catch (SAXException e) { |
153 |
0
|
Trace.fatal(CLASS, method, message, e); |
154 |
|
} catch (IOException e) { |
155 |
1
|
Trace.fatal(CLASS, method, message, e); |
156 |
|
} catch (RuntimeException e) { |
157 |
0
|
Trace.fatal(CLASS, method, message, e); |
158 |
|
} |
159 |
1
|
return null; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
@param |
168 |
|
@param |
169 |
|
@return |
170 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
171 |
16
|
public static SourceArea findSourceArea(final File file, final SimpleXPath xpath) {... |
172 |
16
|
return findSourceArea(file.toString(), xpath, null, null, file); |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@param |
180 |
|
|
181 |
|
@param |
182 |
|
|
183 |
|
@throws |
184 |
|
@throws |
185 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (20) |
Complexity: 2 |
Complexity Density: 0.11 |
|
186 |
22
|
public XPathLocationParser(final SimpleXPath xpath, final SourcePosition startDelta,... |
187 |
|
final SourcePosition endDelta) throws ParserConfigurationException, |
188 |
|
SAXException { |
189 |
22
|
super(); |
190 |
|
|
191 |
22
|
this.find = xpath; |
192 |
22
|
this.startDelta = startDelta; |
193 |
22
|
this.endDelta = endDelta; |
194 |
22
|
elements = new ArrayList(20); |
195 |
22
|
level = 0; |
196 |
|
|
197 |
22
|
final String factoryImpl = System.getProperty("javax.xml.parsers.SAXParserFactory"); |
198 |
22
|
if (factoryImpl == null) { |
199 |
2
|
System.setProperty("javax.xml.parsers.SAXParserFactory", |
200 |
|
"org.apache.xerces.jaxp.SAXParserFactoryImpl"); |
201 |
|
} |
202 |
22
|
SAXParserFactory factory = SAXParserFactory.newInstance(); |
203 |
22
|
factory.setNamespaceAware(false); |
204 |
22
|
factory.setValidating(false); |
205 |
|
|
206 |
22
|
factory.setFeature(NAMESPACES_FEATURE_ID, false); |
207 |
22
|
factory.setFeature(VALIDATION_FEATURE_ID, false); |
208 |
|
|
209 |
22
|
final SAXParser parser = factory.newSAXParser(); |
210 |
|
|
211 |
22
|
reader = parser.getXMLReader(); |
212 |
|
|
213 |
|
|
214 |
22
|
reader.setFeature(NAMESPACES_FEATURE_ID, false); |
215 |
22
|
reader.setFeature(VALIDATION_FEATURE_ID, false); |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
@param |
222 |
|
@throws |
223 |
|
@throws |
224 |
|
|
|
|
| 84.6% |
Uncovered Elements: 2 (13) |
Complexity: 3 |
Complexity Density: 0.23 |
|
225 |
22
|
public final void parse(final File file) throws IOException, SAXException {... |
226 |
22
|
xmlFile = file; |
227 |
22
|
elements.clear(); |
228 |
22
|
level = 0; |
229 |
22
|
InputStream stream = null; |
230 |
22
|
try { |
231 |
22
|
current = new SimpleXPath(); |
232 |
22
|
summary = new SimpleXPath(); |
233 |
22
|
reader.setContentHandler(this); |
234 |
|
|
235 |
|
|
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
248 |
|
|
249 |
|
|
250 |
22
|
stream = new FileInputStream(file); |
251 |
21
|
reader.parse(new InputSource(stream)); |
252 |
|
} catch (XPathLocationFoundException e) { |
253 |
|
|
254 |
|
} catch (SAXException e) { |
255 |
0
|
Trace.trace(CLASS, this, "parse", e); |
256 |
0
|
throw e; |
257 |
|
} finally { |
258 |
22
|
IoUtility.close(stream); |
259 |
|
} |
260 |
|
} |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
@see |
266 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
267 |
1
|
public void endDocument() throws SAXException {... |
268 |
1
|
elements.clear(); |
269 |
1
|
level = 0; |
270 |
|
} |
271 |
|
|
272 |
|
|
273 |
|
|
274 |
|
|
275 |
|
@see |
276 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
277 |
21
|
public void startDocument() throws SAXException {... |
278 |
21
|
elements.clear(); |
279 |
21
|
level = 0; |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
@see |
286 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
287 |
4043
|
public void characters(final char[] ch, final int start, final int length) throws SAXException {... |
288 |
|
|
289 |
|
} |
290 |
|
|
291 |
|
|
292 |
|
|
293 |
|
|
294 |
|
@see |
295 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
296 |
0
|
public void ignorableWhitespace(final char[] ch, final int start, final int length)... |
297 |
|
throws SAXException { |
298 |
|
|
299 |
|
} |
300 |
|
|
301 |
|
|
302 |
|
|
303 |
|
|
304 |
|
@see |
305 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
306 |
0
|
public void endPrefixMapping(final String prefix) throws SAXException {... |
307 |
|
|
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
@see |
314 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
315 |
0
|
public void skippedEntity(final String name) throws SAXException {... |
316 |
|
|
317 |
|
} |
318 |
|
|
319 |
|
|
320 |
|
|
321 |
|
|
322 |
|
@see |
323 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
324 |
0
|
public void processingInstruction(final String target, final String data) throws SAXException {... |
325 |
|
|
326 |
|
} |
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
@see |
332 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
333 |
0
|
public void startPrefixMapping(final String prefix, final String uri) throws SAXException {... |
334 |
|
|
335 |
|
} |
336 |
|
|
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
@see |
341 |
|
|
342 |
|
|
|
|
| 58.9% |
Uncovered Elements: 30 (73) |
Complexity: 13 |
Complexity Density: 0.23 |
|
343 |
1732
|
public void startElement(final String namespaceURI, final String localName, final String qName,... |
344 |
|
final Attributes atts) throws SAXException { |
345 |
1732
|
final String method = "startElement(String, String, Attributes)"; |
346 |
1732
|
level++; |
347 |
1732
|
summary.addElement("*", addOccurence("*")); |
348 |
1732
|
current.addElement(qName, addOccurence(qName)); |
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
|
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
1732
|
if (getLocator() == null) { |
366 |
0
|
throw new SAXException("Locator unexpectedly null"); |
367 |
|
} |
368 |
1732
|
if (find.matchesElements(current, summary)) { |
369 |
20
|
Trace.trace(CLASS, this, method, "matching elements"); |
370 |
20
|
Trace.param(CLASS, this, method, qName, current); |
371 |
20
|
TextInput xml = null; |
372 |
20
|
Reader xmlReader = null; |
373 |
20
|
try { |
374 |
20
|
xmlReader = new XmlReader(xmlFile); |
375 |
20
|
xml = new TextInput(xmlReader); |
376 |
|
|
377 |
|
|
378 |
|
} catch (IOException io) { |
379 |
0
|
Trace.fatal(CLASS, this, method, "File \"" + xmlFile + "\" should be readable", io); |
380 |
0
|
if (getLocator() == null) { |
381 |
0
|
throw new SAXException("Locator unexpectedly null"); |
382 |
|
} |
383 |
|
|
384 |
0
|
start = new SourcePosition( |
385 |
|
getLocator().getLineNumber(), getLocator().getColumnNumber()); |
386 |
0
|
return; |
387 |
|
} |
388 |
20
|
try { |
389 |
20
|
xml.setRow(getLocator().getLineNumber()); |
390 |
20
|
xml.setColumn(getLocator().getColumnNumber()); |
391 |
20
|
if (startDelta != null) { |
392 |
0
|
xml.skipWhiteSpace(); |
393 |
0
|
final String cdata = "<![CDATA["; |
394 |
0
|
final String read = xml.readString(cdata.length()); |
395 |
0
|
final int cdataLength = (cdata.equals(read) ? cdata.length() : 0); |
396 |
0
|
start = addDelta(xml, cdataLength, startDelta); |
397 |
0
|
end = addDelta(xml, cdataLength, endDelta); |
398 |
0
|
return; |
399 |
|
} |
400 |
20
|
try { |
401 |
20
|
xml.skipBackToBeginOfXmlTag(); |
402 |
|
} catch (RuntimeException e) { |
403 |
0
|
Trace.trace(CLASS, this, method, e); |
404 |
|
} |
405 |
20
|
start = new SourcePosition(xml.getRow(), xml.getColumn()); |
406 |
20
|
if (find.getAttribute() != null) { |
407 |
8
|
xml.read(); |
408 |
8
|
xml.readNextXmlName(); |
409 |
8
|
String tag; |
410 |
8
|
do { |
411 |
8
|
xml.skipWhiteSpace(); |
412 |
8
|
int row = xml.getRow(); |
413 |
8
|
int col = xml.getColumn(); |
414 |
8
|
try { |
415 |
8
|
tag = xml.readNextXmlName(); |
416 |
|
} catch (IllegalArgumentException e) { |
417 |
0
|
break; |
418 |
|
} |
419 |
8
|
if (tag.equals(find.getAttribute())) { |
420 |
8
|
start = new SourcePosition(row, col); |
421 |
8
|
xml.readNextAttributeValue(); |
422 |
8
|
end = new SourcePosition(xml.getRow(), xml.getColumn()); |
423 |
8
|
throw new XPathLocationFoundException(); |
424 |
|
} |
425 |
0
|
try { |
426 |
0
|
xml.readNextAttributeValue(); |
427 |
|
} catch (IllegalArgumentException e) { |
428 |
0
|
break; |
429 |
|
} |
430 |
|
} while (true); |
431 |
|
|
432 |
0
|
if (end == null) { |
433 |
0
|
end = new SourcePosition(xml.getRow(), xml.getColumn()); |
434 |
0
|
throw new XPathLocationFoundException(); |
435 |
|
} |
436 |
|
} |
437 |
|
} finally { |
438 |
20
|
IoUtility.close(xml); |
439 |
|
} |
440 |
|
} |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
@param |
447 |
|
@param |
448 |
|
@param |
449 |
|
@return |
450 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
451 |
0
|
private SourcePosition addDelta(final TextInput xml, final int cdataLength,... |
452 |
|
final SourcePosition delta) { |
453 |
0
|
xml.setRow(getLocator().getLineNumber()); |
454 |
0
|
xml.setColumn(getLocator().getColumnNumber()); |
455 |
0
|
if (delta.getRow() == 1 && cdataLength > 0) { |
456 |
0
|
xml.addColumn(cdataLength + delta.getColumn() - 1); |
457 |
|
} else { |
458 |
0
|
xml.addPosition(delta); |
459 |
|
} |
460 |
0
|
return new SourcePosition(xml.getRow(), xml.getColumn()); |
461 |
|
} |
462 |
|
|
463 |
|
|
464 |
|
|
465 |
|
|
466 |
|
@param |
467 |
|
@return |
468 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (18) |
Complexity: 4 |
Complexity Density: 0.33 |
|
469 |
3464
|
private int addOccurence(final String name) {... |
470 |
4218
|
while (level < elements.size()) { |
471 |
754
|
elements.remove(elements.size() - 1); |
472 |
|
} |
473 |
4373
|
while (level > elements.size()) { |
474 |
909
|
elements.add(new HashMap()); |
475 |
|
} |
476 |
3464
|
final Map levelMap = (Map) elements.get(level - 1); |
477 |
3464
|
final Enumerator counter; |
478 |
3464
|
if (levelMap.containsKey(name)) { |
479 |
1208
|
counter = (Enumerator) levelMap.get(name); |
480 |
1208
|
counter.increaseNumber(); |
481 |
|
} else { |
482 |
2256
|
counter = new Enumerator(1); |
483 |
2256
|
levelMap.put(name, counter); |
484 |
|
} |
485 |
3464
|
return counter.getNumber(); |
486 |
|
} |
487 |
|
|
488 |
|
|
489 |
|
|
490 |
|
|
491 |
|
@see |
492 |
|
|
493 |
|
|
|
|
| 65.6% |
Uncovered Elements: 11 (32) |
Complexity: 7 |
Complexity Density: 0.27 |
|
494 |
1636
|
public void endElement(final String namespaceURI, final String localName, final String qName)... |
495 |
|
throws SAXException { |
496 |
1636
|
final String method = "endElement(String, String, Attributes)"; |
497 |
1636
|
level--; |
498 |
1636
|
if (getLocator() == null) { |
499 |
0
|
current.deleteLastElement(); |
500 |
0
|
summary.deleteLastElement(); |
501 |
0
|
throw new SAXException("Locator unexpectly null"); |
502 |
|
} |
503 |
1636
|
if (find.matchesElements(current, summary) && find.getAttribute() == null |
504 |
|
&& startDelta == null) { |
505 |
12
|
TextInput xml = null; |
506 |
12
|
Reader xmlReader = null; |
507 |
12
|
try { |
508 |
12
|
xmlReader = new XmlReader(xmlFile); |
509 |
12
|
xml = new TextInput(xmlReader); |
510 |
|
|
511 |
|
|
512 |
|
} catch (IOException io) { |
513 |
0
|
Trace.fatal(CLASS, this, method, "File \"" + xmlFile + "\" should be readable", io); |
514 |
0
|
if (getLocator() == null) { |
515 |
0
|
throw new SAXException("Locator unexpectedly null"); |
516 |
|
} |
517 |
|
|
518 |
0
|
start = new SourcePosition(getLocator().getLineNumber(), |
519 |
|
getLocator().getColumnNumber()); |
520 |
0
|
return; |
521 |
|
} finally { |
522 |
12
|
IoUtility.close(xmlReader); |
523 |
|
} |
524 |
12
|
try { |
525 |
12
|
xml.setRow(getLocator().getLineNumber()); |
526 |
12
|
xml.setColumn(getLocator().getColumnNumber()); |
527 |
|
|
528 |
12
|
end = new SourcePosition(xml.getRow(), xml.getColumn()); |
529 |
12
|
throw new XPathLocationFoundException(); |
530 |
|
} finally { |
531 |
12
|
IoUtility.close(xml); |
532 |
|
} |
533 |
|
} |
534 |
1624
|
current.deleteLastElement(); |
535 |
1624
|
summary.deleteLastElement(); |
536 |
|
} |
537 |
|
|
538 |
|
|
539 |
|
|
540 |
|
|
541 |
|
@return |
542 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
543 |
41
|
private SourcePosition getStart() {... |
544 |
41
|
return start; |
545 |
|
} |
546 |
|
|
547 |
|
|
548 |
|
|
549 |
|
|
550 |
|
@return |
551 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
552 |
40
|
private SourcePosition getEnd() {... |
553 |
40
|
return end; |
554 |
|
} |
555 |
|
|
556 |
|
} |