1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
package com.jcabi.xml; |
31 | |
|
32 | |
import java.io.File; |
33 | |
import java.io.IOException; |
34 | |
import java.io.InputStream; |
35 | |
import java.io.StringWriter; |
36 | |
import java.net.URI; |
37 | |
import java.net.URL; |
38 | |
import java.util.ArrayList; |
39 | |
import java.util.Collections; |
40 | |
import java.util.List; |
41 | |
import javax.xml.namespace.NamespaceContext; |
42 | |
import javax.xml.namespace.QName; |
43 | |
import javax.xml.parsers.DocumentBuilderFactory; |
44 | |
import javax.xml.parsers.ParserConfigurationException; |
45 | |
import javax.xml.transform.OutputKeys; |
46 | |
import javax.xml.transform.Source; |
47 | |
import javax.xml.transform.Transformer; |
48 | |
import javax.xml.transform.TransformerConfigurationException; |
49 | |
import javax.xml.transform.TransformerException; |
50 | |
import javax.xml.transform.TransformerFactory; |
51 | |
import javax.xml.transform.dom.DOMResult; |
52 | |
import javax.xml.transform.dom.DOMSource; |
53 | |
import javax.xml.transform.stream.StreamResult; |
54 | |
import javax.xml.xpath.XPath; |
55 | |
import javax.xml.xpath.XPathConstants; |
56 | |
import javax.xml.xpath.XPathExpressionException; |
57 | |
import javax.xml.xpath.XPathFactory; |
58 | |
import lombok.EqualsAndHashCode; |
59 | |
import org.w3c.dom.Document; |
60 | |
import org.w3c.dom.Node; |
61 | |
import org.w3c.dom.NodeList; |
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | 2 | @EqualsAndHashCode(of = { "xml", "leaf" }) |
75 | |
@SuppressWarnings("PMD.ExcessiveImports") |
76 | |
public final class XMLDocument implements XML { |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | 1 | private static final XPathFactory XFACTORY = |
82 | |
XPathFactory.newInstance(); |
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | 1 | private static final TransformerFactory TFACTORY = |
88 | |
TransformerFactory.newInstance(); |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | 1 | private static final DocumentBuilderFactory DFACTORY = |
94 | |
DocumentBuilderFactory.newInstance(); |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
private final transient XPathContext context; |
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
private final transient String xml; |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
private final transient boolean leaf; |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
private final transient Object cache; |
117 | |
|
118 | |
static { |
119 | 1 | if (XMLDocument.DFACTORY.getClass().getName().contains("xerces")) { |
120 | |
try { |
121 | 1 | XMLDocument.DFACTORY.setFeature( |
122 | |
|
123 | |
"http://apache.org/xml/features/nonvalidating/load-external-dtd", |
124 | |
false |
125 | |
); |
126 | 0 | } catch (final ParserConfigurationException ex) { |
127 | 0 | throw new IllegalStateException(ex); |
128 | 1 | } |
129 | |
} |
130 | 1 | XMLDocument.DFACTORY.setNamespaceAware(true); |
131 | 1 | } |
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
public XMLDocument(final String text) { |
155 | 408 | this( |
156 | |
new DomParser(XMLDocument.DFACTORY, text).document(), |
157 | |
new XPathContext(), |
158 | |
false |
159 | |
); |
160 | 408 | } |
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
public XMLDocument(final Node node) { |
173 | 57 | this(node, new XPathContext(), !(node instanceof Document)); |
174 | 57 | } |
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
public XMLDocument(final Source source) { |
189 | 209 | this(XMLDocument.transform(source), new XPathContext(), false); |
190 | 209 | } |
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
public XMLDocument(final File file) throws IOException { |
206 | 1 | this(new TextResource(file).toString()); |
207 | 1 | } |
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
public XMLDocument(final URL url) throws IOException { |
223 | 3 | this(new TextResource(url).toString()); |
224 | 3 | } |
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
public XMLDocument(final URI uri) throws IOException { |
240 | 0 | this(new TextResource(uri).toString()); |
241 | 0 | } |
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public XMLDocument(final InputStream stream) throws IOException { |
260 | 1 | this(new TextResource(stream).toString()); |
261 | 1 | stream.close(); |
262 | 1 | } |
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
private XMLDocument(final Node node, final XPathContext ctx, |
271 | 51151 | final boolean lfe) { |
272 | 51149 | this.xml = XMLDocument.asString(node); |
273 | 51152 | this.context = ctx; |
274 | 51152 | this.leaf = lfe; |
275 | 51152 | this.cache = node; |
276 | 51152 | } |
277 | |
|
278 | |
@Override |
279 | |
public String toString() { |
280 | 263 | return this.xml; |
281 | |
} |
282 | |
|
283 | |
@Override |
284 | |
public Node node() { |
285 | 1312 | final Node castCache = Node.class.cast(this.cache); |
286 | |
final Node answer; |
287 | 1310 | if (castCache instanceof Document) { |
288 | 1202 | answer = castCache.cloneNode(true); |
289 | |
} else { |
290 | 108 | answer = this.createImportedNode(castCache); |
291 | |
} |
292 | 1312 | return answer; |
293 | |
} |
294 | |
|
295 | |
@Override |
296 | |
@SuppressWarnings |
297 | |
( |
298 | |
{ |
299 | |
"PMD.ExceptionAsFlowControl", |
300 | |
"PMD.PreserveStackTrace" |
301 | |
} |
302 | |
) |
303 | |
public List<String> xpath(final String query) { |
304 | |
List<String> items; |
305 | |
try { |
306 | 59 | final NodeList nodes = this.fetch(query, NodeList.class); |
307 | 56 | items = new ArrayList<String>(nodes.getLength()); |
308 | 113 | for (int idx = 0; idx < nodes.getLength(); ++idx) { |
309 | 57 | final int type = nodes.item(idx).getNodeType(); |
310 | 57 | if (type != Node.TEXT_NODE && type != Node.ATTRIBUTE_NODE |
311 | |
&& type != Node.CDATA_SECTION_NODE) { |
312 | 0 | throw new IllegalArgumentException( |
313 | |
String.format( |
314 | |
|
315 | |
"Only text() nodes or attributes are retrievable with xpath() '%s': %d", |
316 | |
query, type |
317 | |
) |
318 | |
); |
319 | |
} |
320 | 57 | items.add(nodes.item(idx).getNodeValue()); |
321 | |
} |
322 | 3 | } catch (final XPathExpressionException ex) { |
323 | |
try { |
324 | 3 | items = Collections.singletonList( |
325 | |
this.fetch(query, String.class) |
326 | |
); |
327 | 1 | } catch (final XPathExpressionException exp) { |
328 | 1 | throw new IllegalArgumentException( |
329 | |
|
330 | |
String.format( |
331 | |
"invalid XPath query '%s' at %s", |
332 | |
query, XMLDocument.XFACTORY.getClass().getName() |
333 | |
), exp |
334 | |
); |
335 | 2 | } |
336 | 56 | } |
337 | 58 | return new ListWrapper<String>(items, this.node(), query); |
338 | |
} |
339 | |
|
340 | |
@Override |
341 | |
public XML registerNs(final String prefix, final Object uri) { |
342 | 1 | return new XMLDocument( |
343 | |
this.node(), this.context.add(prefix, uri), this.leaf |
344 | |
); |
345 | |
} |
346 | |
|
347 | |
@Override |
348 | |
@SuppressWarnings("PMD.AvoidInstantiatingObjectsInLoops") |
349 | |
public List<XML> nodes(final String query) { |
350 | |
final List<XML> items; |
351 | |
try { |
352 | 320 | final NodeList nodes = this.fetch(query, NodeList.class); |
353 | 319 | items = new ArrayList<XML>(nodes.getLength()); |
354 | 50585 | for (int idx = 0; idx < nodes.getLength(); ++idx) { |
355 | 50267 | items.add( |
356 | |
new XMLDocument( |
357 | |
nodes.item(idx), |
358 | |
this.context, true |
359 | |
) |
360 | |
); |
361 | |
} |
362 | 0 | } catch (final XPathExpressionException ex) { |
363 | 0 | throw new IllegalArgumentException( |
364 | |
String.format( |
365 | |
"invalid XPath query '%s' by %s", |
366 | |
query, XMLDocument.XFACTORY.getClass().getName() |
367 | |
), ex |
368 | |
); |
369 | 320 | } |
370 | 320 | return new ListWrapper<XML>(items, this.node(), query); |
371 | |
} |
372 | |
|
373 | |
@Override |
374 | |
public XML merge(final NamespaceContext ctx) { |
375 | 209 | return new XMLDocument(this.node(), this.context.merge(ctx), this.leaf); |
376 | |
} |
377 | |
|
378 | |
|
379 | |
|
380 | |
|
381 | |
|
382 | |
|
383 | |
private Node createImportedNode(final Node node) { |
384 | |
final Document document; |
385 | |
try { |
386 | 108 | document = DFACTORY.newDocumentBuilder().newDocument(); |
387 | 0 | } catch (final ParserConfigurationException ex) { |
388 | 0 | throw new IllegalStateException(ex); |
389 | 108 | } |
390 | 108 | final Node imported = document.importNode(node, true); |
391 | 108 | document.appendChild(imported); |
392 | 108 | return imported; |
393 | |
} |
394 | |
|
395 | |
|
396 | |
|
397 | |
|
398 | |
|
399 | |
|
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
|
407 | |
|
408 | |
|
409 | |
@SuppressWarnings("unchecked") |
410 | |
private <T> T fetch(final String query, final Class<T> type) |
411 | |
throws XPathExpressionException { |
412 | |
final XPath xpath; |
413 | 382 | synchronized (XMLDocument.class) { |
414 | 382 | xpath = XMLDocument.XFACTORY.newXPath(); |
415 | 382 | } |
416 | 382 | xpath.setNamespaceContext(this.context); |
417 | |
final QName qname; |
418 | 382 | if (type.equals(String.class)) { |
419 | 3 | qname = XPathConstants.STRING; |
420 | 379 | } else if (type.equals(NodeList.class)) { |
421 | 379 | qname = XPathConstants.NODESET; |
422 | |
} else { |
423 | 0 | throw new IllegalArgumentException( |
424 | |
String.format( |
425 | |
"Unsupported type: %s", type.getName() |
426 | |
) |
427 | |
); |
428 | |
} |
429 | 382 | return (T) xpath.evaluate(query, this.node(), qname); |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
private static String asString(final Node node) { |
439 | 51151 | final StringWriter writer = new StringWriter(); |
440 | |
try { |
441 | |
final Transformer trans; |
442 | 51147 | synchronized (XMLDocument.class) { |
443 | 51156 | trans = XMLDocument.TFACTORY.newTransformer(); |
444 | 51156 | } |
445 | |
|
446 | 51156 | trans.setOutputProperty(OutputKeys.INDENT, "yes"); |
447 | 51156 | trans.setOutputProperty(OutputKeys.VERSION, "1.0"); |
448 | 51156 | if (!(node instanceof Document)) { |
449 | 50272 | trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes"); |
450 | |
} |
451 | 51156 | synchronized (node) { |
452 | 51156 | trans.transform( |
453 | |
new DOMSource(node), |
454 | |
new StreamResult(writer) |
455 | |
); |
456 | 51149 | } |
457 | 0 | } catch (final TransformerConfigurationException ex) { |
458 | 0 | throw new IllegalStateException(ex); |
459 | 0 | } catch (final TransformerException ex) { |
460 | 0 | throw new IllegalArgumentException(ex); |
461 | 51152 | } |
462 | 51153 | return writer.toString(); |
463 | |
} |
464 | |
|
465 | |
|
466 | |
|
467 | |
|
468 | |
|
469 | |
|
470 | |
private static Node transform(final Source source) { |
471 | 209 | final DOMResult result = new DOMResult(); |
472 | |
try { |
473 | |
final Transformer trans; |
474 | 209 | synchronized (XMLDocument.class) { |
475 | 209 | trans = XMLDocument.TFACTORY.newTransformer(); |
476 | 209 | } |
477 | 209 | trans.transform(source, result); |
478 | 0 | } catch (final TransformerConfigurationException ex) { |
479 | 0 | throw new IllegalStateException(ex); |
480 | 0 | } catch (final TransformerException ex) { |
481 | 0 | throw new IllegalStateException(ex); |
482 | 209 | } |
483 | 209 | return result.getNode(); |
484 | |
} |
485 | |
|
486 | |
} |