1
2
3
4
5
6
7
8 package it.imolinfo.jbi4corba.jbi.processor.transform;
9
10 import it.imolinfo.jbi4corba.Logger;
11 import it.imolinfo.jbi4corba.LoggerFactory;
12 import it.imolinfo.jbi4corba.jbi.Messages;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.Reader;
18 import java.io.StringReader;
19 import java.io.StringWriter;
20 import java.lang.reflect.Constructor;
21
22 import javax.jbi.messaging.MessagingException;
23 import javax.jbi.messaging.NormalizedMessage;
24 import javax.xml.parsers.DocumentBuilder;
25 import javax.xml.parsers.DocumentBuilderFactory;
26 import javax.xml.parsers.ParserConfigurationException;
27 import javax.xml.stream.XMLInputFactory;
28 import javax.xml.stream.XMLStreamException;
29 import javax.xml.stream.XMLStreamReader;
30 import javax.xml.transform.OutputKeys;
31 import javax.xml.transform.Result;
32 import javax.xml.transform.Source;
33 import javax.xml.transform.Transformer;
34 import javax.xml.transform.TransformerConfigurationException;
35 import javax.xml.transform.TransformerException;
36 import javax.xml.transform.TransformerFactory;
37 import javax.xml.transform.dom.DOMResult;
38 import javax.xml.transform.dom.DOMSource;
39 import javax.xml.transform.sax.SAXSource;
40 import javax.xml.transform.stream.StreamResult;
41 import javax.xml.transform.stream.StreamSource;
42
43 import org.apache.cxf.staxutils.W3CDOMStreamReader;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.xml.sax.InputSource;
48 import org.xml.sax.SAXException;
49 import org.xml.sax.XMLReader;
50
51
52
53
54
55
56
57 @SuppressWarnings("unchecked")
58 public class SourceTransformer {
59
60
61
62
63
64
65
66
67
68 private static final Class dom2SaxClass;
69
70
71 private static final Logger LOG
72 = LoggerFactory.getLogger(SourceTransformer.class);
73 private static final Messages MESSAGES =
74 Messages.getMessages(SourceTransformer.class);
75
76
77
78
79 private static final String DEFAULT_CHARSET_PROPERTY = "org.apache.servicemix.default.charset";
80
81
82 private static String defaultCharset = System.getProperty(DEFAULT_CHARSET_PROPERTY, "UTF-8");
83
84
85 private DocumentBuilderFactory documentBuilderFactory;
86
87
88 private TransformerFactory transformerFactory;
89
90
91 private XMLInputFactory inputFactory;
92
93
94
95
96 public SourceTransformer() {
97 }
98
99
100
101
102
103
104 public SourceTransformer(DocumentBuilderFactory documentBuilderFactory) {
105 this.documentBuilderFactory = documentBuilderFactory;
106 }
107
108
109
110
111
112
113
114
115
116 public void toResult(Source source, Result result) throws TransformerException {
117 if (source == null) {
118 return;
119 }
120 Transformer transformer = createTransfomer();
121 if (transformer == null) {
122 String msg=MESSAGES.getString("CRB001000_Could_not_create_transformer_JAXP_misconfigured");
123 LOG.error(msg);
124 throw new TransformerException(msg);
125
126
127 }
128 transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
129 transformer.transform(source, result);
130 }
131
132
133
134
135
136
137
138
139
140
141
142 public String toString(Source source) throws TransformerException {
143 if (source == null) {
144 return null;
145 } else if (source instanceof StringSource) {
146 return ((StringSource) source).getText();
147 } else {
148 StringWriter buffer = new StringWriter();
149 toResult(source, new StreamResult(buffer));
150 return buffer.toString();
151 }
152 }
153
154
155
156
157
158
159
160
161
162
163
164 public String toString(Node node) throws TransformerException {
165 return toString(new DOMSource(node));
166 }
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 public String contentToString(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
187 return toString(message.getContent());
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210 public DOMSource toDOMSource(Source source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
211 if (source instanceof DOMSource) {
212 return (DOMSource) source;
213 }
214 else if (source instanceof SAXSource) {
215 return toDOMSourceFromSAX((SAXSource) source);
216 }
217 else if (source instanceof StreamSource) {
218 return toDOMSourceFromStream((StreamSource) source);
219 }
220 else {
221 return null;
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244 public Source toDOMSource(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
245 Node node = toDOMNode(message);
246 return new DOMSource(node);
247 }
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265 public SAXSource toSAXSource(Source source) throws IOException, SAXException, TransformerException {
266 if (source instanceof SAXSource) {
267 return (SAXSource) source;
268 }
269 else if (source instanceof DOMSource) {
270 return toSAXSourceFromDOM((DOMSource) source);
271 }
272 else if (source instanceof StreamSource) {
273 return toSAXSourceFromStream((StreamSource) source);
274 }
275 else {
276 return null;
277 }
278 }
279
280
281
282
283
284
285
286
287
288
289
290
291 public StreamSource toStreamSource(Source source) throws TransformerException {
292 if (source instanceof StreamSource) {
293 return (StreamSource) source;
294 } else if (source instanceof DOMSource) {
295 return toStreamSourceFromDOM((DOMSource) source);
296 } else if (source instanceof SAXSource) {
297 return toStreamSourceFromSAX((SAXSource) source);
298 } else {
299 return null;
300 }
301 }
302
303
304
305
306
307
308
309
310
311
312
313 public StreamSource toStreamSourceFromSAX(SAXSource source) throws TransformerException {
314 InputSource inputSource = source.getInputSource();
315 if (inputSource != null) {
316 if (inputSource.getCharacterStream() != null) {
317 return new StreamSource(inputSource.getCharacterStream());
318 }
319 if (inputSource.getByteStream() != null) {
320 return new StreamSource(inputSource.getByteStream());
321 }
322 }
323 String result = toString(source);
324 return new StringSource(result);
325 }
326
327
328
329
330
331
332
333
334
335
336
337
338 public StreamSource toStreamSourceFromDOM(DOMSource source) throws TransformerException {
339 String result = toString(source);
340 return new StringSource(result);
341 }
342
343
344
345
346
347
348
349
350 public SAXSource toSAXSourceFromStream(StreamSource source) {
351 InputSource inputSource;
352 if (source.getReader() != null) {
353 inputSource = new InputSource(source.getReader());
354 } else {
355 inputSource = new InputSource(source.getInputStream());
356 }
357 inputSource.setSystemId(source.getSystemId());
358 inputSource.setPublicId(source.getPublicId());
359 return new SAXSource(inputSource);
360 }
361
362
363
364
365
366
367
368
369
370
371
372
373 public Reader toReaderFromSource(Source src) throws TransformerException {
374 StreamSource stSrc = toStreamSource(src);
375 Reader r = stSrc.getReader();
376 if (r == null) {
377 r = new InputStreamReader(stSrc.getInputStream());
378 }
379 return r;
380 }
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397 public DOMSource toDOMSourceFromStream(StreamSource source) throws ParserConfigurationException, IOException, SAXException {
398 DocumentBuilder builder = createDocumentBuilder();
399 String systemId = source.getSystemId();
400 Document document = null;
401 Reader reader = source.getReader();
402 if (reader != null) {
403 document = builder.parse(new InputSource(reader));
404 } else {
405 InputStream inputStream = source.getInputStream();
406 if (inputStream != null) {
407 InputSource inputsource = new InputSource(inputStream);
408 inputsource.setSystemId(systemId);
409 document = builder.parse(inputsource);
410 }
411 else {
412 throw new IOException("No input stream or reader available");
413 }
414 }
415 return new DOMSource(document, systemId);
416 }
417
418
419
420
421
422 static {
423 Class cl = null;
424 try {
425 cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX");
426 } catch (Throwable t) {
427
428 String msg=MESSAGES.getString("CRB001001_Static_inizializer_do_nothing",
429 new Object[] {t.getMessage()});
430
431 LOG.debug(msg);
432 }
433 dom2SaxClass = cl;
434 }
435
436
437
438
439
440
441
442
443
444
445
446
447 public SAXSource toSAXSourceFromDOM(DOMSource source) throws TransformerException {
448 if (dom2SaxClass != null) {
449 try {
450 Constructor cns = dom2SaxClass.getConstructor(new Class[] { Node.class });
451 XMLReader converter = (XMLReader) cns.newInstance(new Object[] { source.getNode() });
452 return new SAXSource(converter, new InputSource());
453 } catch (Exception e) {
454 String msg=MESSAGES.getString("CRB001002_To_SAX_source_from_DOM");
455 LOG.error(msg,e);
456 throw new TransformerException(msg,e);
457
458 }
459 } else {
460 String str = toString(source);
461 StringReader reader = new StringReader(str);
462 return new SAXSource(new InputSource(reader));
463 }
464 }
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483 public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
484 return new DOMSource(toDOMNodeFromSAX(source));
485 }
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504 public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
505 DOMResult result = new DOMResult();
506 toResult(source, result);
507 return result.getNode();
508 }
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528 public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
529 DOMSource domSrc = toDOMSource(source);
530 if (domSrc != null) {
531 return domSrc.getNode();
532 } else {
533 return null;
534 }
535 }
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 public Node toDOMNode(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
559 Source content = message.getContent();
560 Node node = toDOMNode(content);
561 return node;
562 }
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580 public Element toDOMElement(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
581 Node node = toDOMNode(message);
582 return toDOMElement(node);
583 }
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603 public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
604 Node node = toDOMNode(source);
605 return toDOMElement(node);
606 }
607
608
609
610
611
612
613
614
615
616
617
618
619 public Element toDOMElement(Node node) throws TransformerException {
620
621 if (node instanceof Document) {
622 return ((Document) node).getDocumentElement();
623
624 } else if (node instanceof Element) {
625 return (Element) node;
626
627 } else {
628 String msg=MESSAGES.getString("CRB001003_Unable_to_convert_DOM_node_to_an_Element");
629 LOG.error(msg);
630 throw new TransformerException(msg);
631
632 }
633 }
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654 public Document toDOMDocument(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
655 Node node = toDOMNode(message);
656 return toDOMDocument(node);
657 }
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677 public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
678 Node node = toDOMNode(source);
679 return toDOMDocument(node);
680 }
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698 public Document toDOMDocument(Node node) throws ParserConfigurationException, TransformerException {
699
700 if (node instanceof Document) {
701 return (Document) node;
702
703 } else if (node instanceof Element) {
704 Element elem = (Element) node;
705
706 if (elem.getOwnerDocument().getDocumentElement() == elem) {
707 return elem.getOwnerDocument();
708
709 } else {
710 Document doc = createDocument();
711 doc.appendChild(doc.importNode(node, true));
712 return doc;
713 }
714
715 } else {
716 String msg=MESSAGES.getString("CRB001004_Unable_to_convert_DOM_node_to_a_Document");
717 LOG.error(msg);
718 throw new TransformerException(msg);
719 }
720 }
721
722
723
724
725
726
727
728
729 public DocumentBuilderFactory getDocumentBuilderFactory() {
730 if (documentBuilderFactory == null) {
731 documentBuilderFactory = createDocumentBuilderFactory();
732 }
733 return documentBuilderFactory;
734 }
735
736
737
738
739
740
741
742 public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) {
743 this.documentBuilderFactory = documentBuilderFactory;
744 }
745
746
747
748
749
750
751
752
753
754 public DocumentBuilderFactory createDocumentBuilderFactory() {
755 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
756 factory.setNamespaceAware(true);
757 factory.setIgnoringElementContentWhitespace(true);
758 factory.setIgnoringComments(true);
759 return factory;
760 }
761
762
763
764
765
766
767
768
769
770
771 public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
772 DocumentBuilderFactory factory = getDocumentBuilderFactory();
773 factory.setNamespaceAware(true);
774 return factory.newDocumentBuilder();
775 }
776
777
778
779
780
781
782
783
784
785 public Document createDocument() throws ParserConfigurationException {
786 DocumentBuilder builder = createDocumentBuilder();
787 return builder.newDocument();
788 }
789
790
791
792
793
794
795 public TransformerFactory getTransformerFactory() {
796 if (transformerFactory == null) {
797 transformerFactory = createTransformerFactory();
798 }
799 return transformerFactory;
800 }
801
802
803
804
805
806
807
808 public void setTransformerFactory(TransformerFactory transformerFactory) {
809 this.transformerFactory = transformerFactory;
810 }
811
812
813
814
815
816
817
818
819
820 public Transformer createTransfomer() throws TransformerConfigurationException {
821 TransformerFactory factory = getTransformerFactory();
822 return factory.newTransformer();
823 }
824
825
826
827
828
829
830 public TransformerFactory createTransformerFactory() {
831 TransformerFactory answer = TransformerFactory.newInstance();
832 return answer;
833 }
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848 public XMLStreamReader toXMLStreamReader(Source source) throws XMLStreamException, TransformerException {
849
850
851
852
853
854
855 if (source instanceof DOMSource) {
856 Node n = ((DOMSource) source).getNode();
857
858 Element el = null;
859 if (n instanceof Document) {
860 el = ((Document) n).getDocumentElement();
861 } else if (n instanceof Element ) {
862 el =(Element) n;
863 }
864 if (el != null) {
865 return new W3CDOMStreamReader(el);
866 }
867 }
868 XMLInputFactory factory = getInputFactory();
869 try {
870 return factory.createXMLStreamReader(source);
871 } catch (XMLStreamException e) {
872 return factory.createXMLStreamReader(toReaderFromSource(source));
873 }
874 }
875
876
877
878
879
880
881
882
883 protected XMLInputFactory createInputFactory() {
884 XMLInputFactory answer = XMLInputFactory.newInstance();
885 return answer;
886 }
887
888
889
890
891
892
893
894
895 public XMLInputFactory getInputFactory() {
896 if (inputFactory == null) {
897 inputFactory = createInputFactory();
898 }
899 return inputFactory;
900 }
901 }