1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.converter.jaxp; |
19 |
|
|
20 |
|
|
21 |
|
import java.io.IOException; |
22 |
|
import java.io.InputStream; |
23 |
|
import java.io.InputStreamReader; |
24 |
|
import java.io.Reader; |
25 |
|
import java.io.StringReader; |
26 |
|
import java.io.StringWriter; |
27 |
|
import java.io.ByteArrayInputStream; |
28 |
|
import java.io.File; |
29 |
|
import java.lang.reflect.Constructor; |
30 |
|
|
31 |
|
import javax.xml.parsers.DocumentBuilder; |
32 |
|
import javax.xml.parsers.DocumentBuilderFactory; |
33 |
|
import javax.xml.parsers.ParserConfigurationException; |
34 |
|
import javax.xml.transform.OutputKeys; |
35 |
|
import javax.xml.transform.Result; |
36 |
|
import javax.xml.transform.Source; |
37 |
|
import javax.xml.transform.Transformer; |
38 |
|
import javax.xml.transform.TransformerConfigurationException; |
39 |
|
import javax.xml.transform.TransformerException; |
40 |
|
import javax.xml.transform.TransformerFactory; |
41 |
|
import javax.xml.transform.dom.DOMResult; |
42 |
|
import javax.xml.transform.dom.DOMSource; |
43 |
|
import javax.xml.transform.sax.SAXSource; |
44 |
|
import javax.xml.transform.stream.StreamResult; |
45 |
|
import javax.xml.transform.stream.StreamSource; |
46 |
|
|
47 |
|
import org.w3c.dom.Document; |
48 |
|
import org.w3c.dom.Element; |
49 |
|
import org.w3c.dom.Node; |
50 |
|
import org.xml.sax.InputSource; |
51 |
|
import org.xml.sax.SAXException; |
52 |
|
import org.xml.sax.XMLReader; |
53 |
|
import org.apache.camel.util.ObjectHelper; |
54 |
|
import org.apache.camel.Converter; |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
@Converter |
62 |
|
public class XmlConverter { |
63 |
|
public static final String DEFAULT_CHARSET_PROPERTY = "org.apache.camel.default.charset"; |
64 |
|
|
65 |
1 |
public static String defaultCharset = ObjectHelper.getSystemProperty(DEFAULT_CHARSET_PROPERTY, "UTF-8"); |
66 |
|
|
67 |
|
private DocumentBuilderFactory documentBuilderFactory; |
68 |
|
private TransformerFactory transformerFactory; |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private static final Class dom2SaxClass; |
78 |
|
|
79 |
|
static { |
80 |
1 |
Class cl = null; |
81 |
|
try { |
82 |
1 |
cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX"); |
83 |
0 |
} catch (Throwable t) {} |
84 |
1 |
dom2SaxClass = cl; |
85 |
1 |
} |
86 |
|
|
87 |
|
|
88 |
21 |
public XmlConverter() { |
89 |
21 |
} |
90 |
|
|
91 |
0 |
public XmlConverter(DocumentBuilderFactory documentBuilderFactory) { |
92 |
0 |
this.documentBuilderFactory = documentBuilderFactory; |
93 |
0 |
} |
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
|
98 |
|
|
99 |
|
public void toResult(Source source, Result result) throws TransformerException { |
100 |
1 |
if (source == null) { |
101 |
0 |
return; |
102 |
|
} |
103 |
1 |
Transformer transformer = createTransfomer(); |
104 |
1 |
if (transformer == null) { |
105 |
0 |
throw new TransformerException("Could not create a transformer - JAXP is misconfigured!"); |
106 |
|
} |
107 |
1 |
transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset); |
108 |
1 |
transformer.transform(source, result); |
109 |
1 |
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
@Converter |
115 |
|
public BytesSource toSource(byte[] data) { |
116 |
0 |
return new BytesSource(data); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
@Converter |
124 |
|
public StringSource toSource(String data) { |
125 |
2 |
return new StringSource(data); |
126 |
|
} |
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
|
131 |
|
@Converter |
132 |
|
public DOMSource toSource(Document document) { |
133 |
0 |
return new DOMSource(document); |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
@Converter |
140 |
|
public String toString(Source source) throws TransformerException { |
141 |
1 |
if (source == null) { |
142 |
0 |
return null; |
143 |
1 |
} else if (source instanceof StringSource) { |
144 |
0 |
return ((StringSource) source).getText(); |
145 |
1 |
} else if (source instanceof BytesSource) { |
146 |
0 |
return new String(((BytesSource) source).getData()); |
147 |
|
} else { |
148 |
1 |
StringWriter buffer = new StringWriter(); |
149 |
1 |
toResult(source, new StreamResult(buffer)); |
150 |
1 |
return buffer.toString(); |
151 |
|
} |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@Converter |
158 |
|
public String toString(Node node) throws TransformerException { |
159 |
1 |
return toString(new DOMSource(node)); |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
@Converter |
167 |
|
public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException, SAXException, TransformerException { |
168 |
0 |
if (source instanceof DOMSource) { |
169 |
0 |
return (DOMSource) source; |
170 |
|
} |
171 |
0 |
else if (source instanceof SAXSource) { |
172 |
0 |
return toDOMSourceFromSAX((SAXSource) source); |
173 |
|
} |
174 |
0 |
else if (source instanceof StreamSource) { |
175 |
0 |
return toDOMSourceFromStream((StreamSource) source); |
176 |
|
} |
177 |
|
else { |
178 |
0 |
return null; |
179 |
|
} |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
|
@Converter |
187 |
|
public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException { |
188 |
0 |
if (source instanceof SAXSource) { |
189 |
0 |
return (SAXSource) source; |
190 |
|
} |
191 |
0 |
else if (source instanceof DOMSource) { |
192 |
0 |
return toSAXSourceFromDOM((DOMSource) source); |
193 |
|
} |
194 |
0 |
else if (source instanceof StreamSource) { |
195 |
0 |
return toSAXSourceFromStream((StreamSource) source); |
196 |
|
} |
197 |
|
else { |
198 |
0 |
return null; |
199 |
|
} |
200 |
|
} |
201 |
|
|
202 |
|
@Converter |
203 |
|
public StreamSource toStreamSource(Source source) throws TransformerException { |
204 |
0 |
if (source instanceof StreamSource) { |
205 |
0 |
return (StreamSource) source; |
206 |
0 |
} else if (source instanceof DOMSource) { |
207 |
0 |
return toStreamSourceFromDOM((DOMSource) source); |
208 |
0 |
} else if (source instanceof SAXSource) { |
209 |
0 |
return toStreamSourceFromSAX((SAXSource) source); |
210 |
|
} else { |
211 |
0 |
return null; |
212 |
|
} |
213 |
|
} |
214 |
|
|
215 |
|
@Converter |
216 |
|
public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException { |
217 |
0 |
InputSource inputSource = source.getInputSource(); |
218 |
0 |
if (inputSource != null) { |
219 |
0 |
if (inputSource.getCharacterStream() != null) { |
220 |
0 |
return new StreamSource(inputSource.getCharacterStream()); |
221 |
|
} |
222 |
0 |
if (inputSource.getByteStream() != null) { |
223 |
0 |
return new StreamSource(inputSource.getByteStream()); |
224 |
|
} |
225 |
|
} |
226 |
0 |
String result = toString(source); |
227 |
0 |
return new StringSource(result); |
228 |
|
} |
229 |
|
|
230 |
|
@Converter |
231 |
|
public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException { |
232 |
0 |
String result = toString(source); |
233 |
0 |
return new StringSource(result); |
234 |
|
} |
235 |
|
|
236 |
|
@Converter |
237 |
|
public SAXSource toSAXSourceFromStream(StreamSource source) { |
238 |
|
InputSource inputSource; |
239 |
0 |
if (source.getReader() != null) { |
240 |
0 |
inputSource = new InputSource(source.getReader()); |
241 |
0 |
} else { |
242 |
0 |
inputSource = new InputSource(source.getInputStream()); |
243 |
|
} |
244 |
0 |
inputSource.setSystemId(source.getSystemId()); |
245 |
0 |
inputSource.setPublicId(source.getPublicId()); |
246 |
0 |
return new SAXSource(inputSource); |
247 |
|
} |
248 |
|
|
249 |
|
@Converter |
250 |
|
public Reader toReaderFromSource(Source src) throws TransformerException { |
251 |
0 |
StreamSource stSrc = toStreamSource(src); |
252 |
0 |
Reader r = stSrc.getReader(); |
253 |
0 |
if (r == null) { |
254 |
0 |
r = new InputStreamReader(stSrc.getInputStream()); |
255 |
|
} |
256 |
0 |
return r; |
257 |
|
} |
258 |
|
|
259 |
|
@Converter |
260 |
|
public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException { |
261 |
0 |
DocumentBuilder builder = createDocumentBuilder(); |
262 |
0 |
String systemId = source.getSystemId(); |
263 |
0 |
Document document = null; |
264 |
0 |
Reader reader = source.getReader(); |
265 |
0 |
if (reader != null) { |
266 |
0 |
document = builder.parse(new InputSource(reader)); |
267 |
0 |
} else { |
268 |
0 |
InputStream inputStream = source.getInputStream(); |
269 |
0 |
if (inputStream != null) { |
270 |
0 |
InputSource inputsource = new InputSource(inputStream); |
271 |
0 |
inputsource.setSystemId(systemId); |
272 |
0 |
document = builder.parse(inputsource); |
273 |
0 |
} |
274 |
|
else { |
275 |
0 |
throw new IOException("No input stream or reader available"); |
276 |
|
} |
277 |
|
} |
278 |
0 |
return new DOMSource(document, systemId); |
279 |
|
} |
280 |
|
|
281 |
|
@Converter |
282 |
|
public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException { |
283 |
0 |
if (dom2SaxClass != null) { |
284 |
|
try { |
285 |
0 |
Constructor cns = dom2SaxClass.getConstructor(new Class[] { Node.class }); |
286 |
0 |
XMLReader converter = (XMLReader) cns.newInstance(new Object[] { source.getNode() }); |
287 |
0 |
return new SAXSource(converter, new InputSource()); |
288 |
0 |
} catch (Exception e) { |
289 |
0 |
throw new TransformerException(e); |
290 |
|
} |
291 |
|
} else { |
292 |
0 |
String str = toString(source); |
293 |
0 |
StringReader reader = new StringReader(str); |
294 |
0 |
return new SAXSource(new InputSource(reader)); |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
|
@Converter |
299 |
|
public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException { |
300 |
0 |
return new DOMSource(toDOMNodeFromSAX(source)); |
301 |
|
} |
302 |
|
|
303 |
|
@Converter |
304 |
|
public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException { |
305 |
0 |
DOMResult result = new DOMResult(); |
306 |
0 |
toResult(source, result); |
307 |
0 |
return result.getNode(); |
308 |
|
} |
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
|
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
@Converter |
317 |
|
public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException { |
318 |
0 |
DOMSource domSrc = toDOMSource(source); |
319 |
0 |
return domSrc != null ? domSrc.getNode() : null; |
320 |
|
} |
321 |
|
|
322 |
|
|
323 |
|
|
324 |
|
|
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
332 |
|
@Converter |
333 |
|
public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException { |
334 |
0 |
Node node = toDOMNode(source); |
335 |
0 |
return toDOMElement(node); |
336 |
|
} |
337 |
|
|
338 |
|
|
339 |
|
|
340 |
|
|
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
|
346 |
|
|
347 |
|
@Converter |
348 |
|
public Element toDOMElement(Node node) throws TransformerException { |
349 |
|
|
350 |
0 |
if (node instanceof Document) { |
351 |
0 |
return ((Document) node).getDocumentElement(); |
352 |
|
|
353 |
0 |
} else if (node instanceof Element) { |
354 |
0 |
return (Element) node; |
355 |
|
|
356 |
|
} else { |
357 |
0 |
throw new TransformerException("Unable to convert DOM node to an Element"); |
358 |
|
} |
359 |
|
} |
360 |
|
|
361 |
|
|
362 |
|
|
363 |
|
|
364 |
|
|
365 |
|
|
366 |
|
|
367 |
|
@Converter |
368 |
|
public Document toDOMDocument(byte[] data) throws IOException, SAXException, ParserConfigurationException { |
369 |
24 |
DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); |
370 |
24 |
return documentBuilder.parse(new ByteArrayInputStream(data)); |
371 |
|
} |
372 |
|
|
373 |
|
|
374 |
|
|
375 |
|
|
376 |
|
|
377 |
|
|
378 |
|
|
379 |
|
@Converter |
380 |
|
public Document toDOMDocument(InputStream in) throws IOException, SAXException, ParserConfigurationException { |
381 |
0 |
DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); |
382 |
0 |
return documentBuilder.parse(in); |
383 |
|
} |
384 |
|
|
385 |
|
|
386 |
|
|
387 |
|
|
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
@Converter |
392 |
|
public Document toDOMDocument(InputSource in) throws IOException, SAXException, ParserConfigurationException { |
393 |
0 |
DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); |
394 |
0 |
return documentBuilder.parse(in); |
395 |
|
} |
396 |
|
|
397 |
|
|
398 |
|
|
399 |
|
|
400 |
|
|
401 |
|
|
402 |
|
|
403 |
|
@Converter |
404 |
|
public Document toDOMDocument(String text) throws IOException, SAXException, ParserConfigurationException { |
405 |
24 |
return toDOMDocument(text.getBytes()); |
406 |
|
} |
407 |
|
|
408 |
|
|
409 |
|
|
410 |
|
|
411 |
|
|
412 |
|
|
413 |
|
|
414 |
|
@Converter |
415 |
|
public Document toDOMDocument(File file) throws IOException, SAXException, ParserConfigurationException { |
416 |
0 |
DocumentBuilder documentBuilder = getDocumentBuilderFactory().newDocumentBuilder(); |
417 |
0 |
return documentBuilder.parse(file); |
418 |
|
} |
419 |
|
|
420 |
|
|
421 |
|
|
422 |
|
|
423 |
|
|
424 |
|
|
425 |
|
|
426 |
|
|
427 |
|
|
428 |
|
|
429 |
|
|
430 |
|
|
431 |
|
@Converter |
432 |
|
public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException { |
433 |
0 |
Node node = toDOMNode(source); |
434 |
0 |
return toDOMDocument(node); |
435 |
|
} |
436 |
|
|
437 |
|
|
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
|
445 |
|
|
446 |
|
|
447 |
|
|
448 |
|
|
449 |
|
@Converter |
450 |
|
public Document toDOMDocument(Node node) throws ParserConfigurationException, TransformerException { |
451 |
|
|
452 |
0 |
if (node instanceof Document) { |
453 |
0 |
return (Document) node; |
454 |
|
|
455 |
0 |
} else if (node instanceof Element) { |
456 |
0 |
Element elem = (Element) node; |
457 |
|
|
458 |
0 |
if (elem.getOwnerDocument().getDocumentElement() == elem) { |
459 |
0 |
return elem.getOwnerDocument(); |
460 |
|
|
461 |
|
} else { |
462 |
0 |
Document doc = createDocument(); |
463 |
0 |
doc.appendChild(doc.importNode(node, true)); |
464 |
0 |
return doc; |
465 |
|
} |
466 |
|
|
467 |
|
} else { |
468 |
0 |
throw new TransformerException("Unable to convert DOM node to a Document"); |
469 |
|
} |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
|
|
474 |
|
public DocumentBuilderFactory getDocumentBuilderFactory() { |
475 |
24 |
if (documentBuilderFactory == null) { |
476 |
18 |
documentBuilderFactory = createDocumentBuilderFactory(); |
477 |
|
} |
478 |
24 |
return documentBuilderFactory; |
479 |
|
} |
480 |
|
|
481 |
|
public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) { |
482 |
0 |
this.documentBuilderFactory = documentBuilderFactory; |
483 |
0 |
} |
484 |
|
|
485 |
|
|
486 |
|
|
487 |
|
|
488 |
|
public DocumentBuilderFactory createDocumentBuilderFactory() { |
489 |
18 |
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
490 |
18 |
factory.setNamespaceAware(true); |
491 |
18 |
factory.setIgnoringElementContentWhitespace(true); |
492 |
18 |
factory.setIgnoringComments(true); |
493 |
18 |
return factory; |
494 |
|
} |
495 |
|
|
496 |
|
|
497 |
|
public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException { |
498 |
0 |
DocumentBuilderFactory factory = getDocumentBuilderFactory(); |
499 |
0 |
return factory.newDocumentBuilder(); |
500 |
|
} |
501 |
|
|
502 |
|
public Document createDocument() throws ParserConfigurationException { |
503 |
0 |
DocumentBuilder builder = createDocumentBuilder(); |
504 |
0 |
return builder.newDocument(); |
505 |
|
} |
506 |
|
|
507 |
|
public TransformerFactory getTransformerFactory() { |
508 |
2 |
if (transformerFactory == null) { |
509 |
2 |
transformerFactory = createTransformerFactory(); |
510 |
|
} |
511 |
2 |
return transformerFactory; |
512 |
|
} |
513 |
|
|
514 |
|
public void setTransformerFactory(TransformerFactory transformerFactory) { |
515 |
0 |
this.transformerFactory = transformerFactory; |
516 |
0 |
} |
517 |
|
|
518 |
|
public Transformer createTransfomer() throws TransformerConfigurationException { |
519 |
1 |
TransformerFactory factory = getTransformerFactory(); |
520 |
1 |
return factory.newTransformer(); |
521 |
|
} |
522 |
|
|
523 |
|
public TransformerFactory createTransformerFactory() { |
524 |
2 |
TransformerFactory answer = TransformerFactory.newInstance(); |
525 |
2 |
return answer; |
526 |
|
} |
527 |
|
|
528 |
|
} |