1
2
3
4
5
6
7
8 package it.imolinfo.jbi4ejb.processor.transform;
9
10 import it.imolinfo.jbi4ejb.Logger;
11 import it.imolinfo.jbi4ejb.LoggerFactory;
12 import it.imolinfo.jbi4ejb.configuration.InterfaceExtractorUtil;
13 import it.imolinfo.jbi4ejb.jbi.Messages;
14
15 import java.io.IOException;
16 import java.io.InputStream;
17 import java.io.InputStreamReader;
18 import java.io.Reader;
19 import java.io.StringReader;
20 import java.io.StringWriter;
21 import java.lang.reflect.Constructor;
22
23 import javax.jbi.messaging.MessagingException;
24 import javax.jbi.messaging.NormalizedMessage;
25 import javax.xml.parsers.DocumentBuilder;
26 import javax.xml.parsers.DocumentBuilderFactory;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.stream.XMLInputFactory;
29 import javax.xml.stream.XMLStreamException;
30 import javax.xml.stream.XMLStreamReader;
31 import javax.xml.transform.OutputKeys;
32 import javax.xml.transform.Result;
33 import javax.xml.transform.Source;
34 import javax.xml.transform.Transformer;
35 import javax.xml.transform.TransformerConfigurationException;
36 import javax.xml.transform.TransformerException;
37 import javax.xml.transform.TransformerFactory;
38 import javax.xml.transform.dom.DOMResult;
39 import javax.xml.transform.dom.DOMSource;
40 import javax.xml.transform.sax.SAXSource;
41 import javax.xml.transform.stream.StreamResult;
42 import javax.xml.transform.stream.StreamSource;
43
44 import org.codehaus.xfire.util.stax.W3CDOMStreamReader;
45 import org.w3c.dom.Document;
46 import org.w3c.dom.Element;
47 import org.w3c.dom.Node;
48 import org.xml.sax.InputSource;
49 import org.xml.sax.SAXException;
50 import org.xml.sax.XMLReader;
51
52
53
54
55
56
57
58
59
60 public class SourceTransformer {
61
62
63
64
65
66
67
68
69
70 private static final Class dom2SaxClass;
71
72
73 private static final Logger LOG
74 = LoggerFactory.getLogger(SourceTransformer.class);
75 private static final Messages MESSAGES
76 = Messages.getMessages(SourceTransformer.class);
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
117 public void toResult(Source source, Result result) throws TransformerException {
118 if (source == null) {
119 return;
120 }
121 Transformer transformer = createTransfomer();
122 if (transformer == null) {
123 String msg=MESSAGES.getString("EJB000701_Could_not_create_transformer");
124 LOG.error(msg);
125 throw new TransformerException(msg);
126 }
127 transformer.setOutputProperty(OutputKeys.ENCODING, defaultCharset);
128 transformer.transform(source, result);
129 }
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 String msg=MESSAGES.getString("EJB000702_No_input_stream_or_reader_available");
413 LOG.error(msg);
414 throw new IOException(msg);
415 }
416 }
417 return new DOMSource(document, systemId);
418 }
419
420
421
422
423
424 static {
425 Class cl = null;
426 try {
427 cl = Class.forName("org.apache.xalan.xsltc.trax.DOM2SAX");
428 } catch (Throwable t) {
429
430 LOG.error("EJB000703_Static_initializer", new Object[]{t.getMessage()});
431
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("EJB000704_Exception_in_toSAXSourceFromDOM", new Object[]{e.getMessage()});
455 LOG.error(msg,e);
456 throw new TransformerException(msg,e);
457 }
458 } else {
459 String str = toString(source);
460 StringReader reader = new StringReader(str);
461 return new SAXSource(new InputSource(reader));
462 }
463 }
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482 public DOMSource toDOMSourceFromSAX(SAXSource source) throws IOException, SAXException, ParserConfigurationException, TransformerException {
483 return new DOMSource(toDOMNodeFromSAX(source));
484 }
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503 public Node toDOMNodeFromSAX(SAXSource source) throws ParserConfigurationException, IOException, SAXException, TransformerException {
504 DOMResult result = new DOMResult();
505 toResult(source, result);
506 return result.getNode();
507 }
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527 public Node toDOMNode(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
528 DOMSource domSrc = toDOMSource(source);
529 if (domSrc != null) {
530 return domSrc.getNode();
531 } else {
532 return null;
533 }
534 }
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557 public Node toDOMNode(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
558 Source content = message.getContent();
559 Node node = toDOMNode(content);
560 return node;
561 }
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579 public Element toDOMElement(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
580 Node node = toDOMNode(message);
581 return toDOMElement(node);
582 }
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602 public Element toDOMElement(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
603 Node node = toDOMNode(source);
604 return toDOMElement(node);
605 }
606
607
608
609
610
611
612
613
614
615
616
617
618 public Element toDOMElement(Node node) throws TransformerException {
619
620 if (node instanceof Document) {
621 return ((Document) node).getDocumentElement();
622
623 } else if (node instanceof Element) {
624 return (Element) node;
625
626 } else {
627 String msg=MESSAGES.getString("EJB000705_Unable_to_convert_DOM_node_to_Element");
628 LOG.error(msg);
629 throw new TransformerException(msg);
630 }
631 }
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652 public Document toDOMDocument(NormalizedMessage message) throws MessagingException, TransformerException, ParserConfigurationException, IOException, SAXException {
653 Node node = toDOMNode(message);
654 return toDOMDocument(node);
655 }
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675 public Document toDOMDocument(Source source) throws TransformerException, ParserConfigurationException, IOException, SAXException {
676 Node node = toDOMNode(source);
677 return toDOMDocument(node);
678 }
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696 public Document toDOMDocument(Node node) throws ParserConfigurationException, TransformerException {
697
698 if (node instanceof Document) {
699 return (Document) node;
700
701 } else if (node instanceof Element) {
702 Element elem = (Element) node;
703
704 if (elem.getOwnerDocument().getDocumentElement() == elem) {
705 return elem.getOwnerDocument();
706
707 } else {
708 Document doc = createDocument();
709 doc.appendChild(doc.importNode(node, true));
710 return doc;
711 }
712
713 } else {
714 String msg=MESSAGES.getString("EJB000706_Unable_to_convert_DOM_node_to_Document");
715 LOG.error(msg);
716 throw new TransformerException(msg);
717 }
718 }
719
720
721
722
723
724
725
726
727 public DocumentBuilderFactory getDocumentBuilderFactory() {
728 if (documentBuilderFactory == null) {
729 documentBuilderFactory = createDocumentBuilderFactory();
730 }
731 return documentBuilderFactory;
732 }
733
734
735
736
737
738
739
740 public void setDocumentBuilderFactory(DocumentBuilderFactory documentBuilderFactory) {
741 this.documentBuilderFactory = documentBuilderFactory;
742 }
743
744
745
746
747
748
749
750
751
752 public DocumentBuilderFactory createDocumentBuilderFactory() {
753 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
754 factory.setNamespaceAware(true);
755 factory.setIgnoringElementContentWhitespace(true);
756 factory.setIgnoringComments(true);
757 return factory;
758 }
759
760
761
762
763
764
765
766
767
768
769 public DocumentBuilder createDocumentBuilder() throws ParserConfigurationException {
770 DocumentBuilderFactory factory = getDocumentBuilderFactory();
771 return factory.newDocumentBuilder();
772 }
773
774
775
776
777
778
779
780
781
782 public Document createDocument() throws ParserConfigurationException {
783 DocumentBuilder builder = createDocumentBuilder();
784 return builder.newDocument();
785 }
786
787
788
789
790
791
792 public TransformerFactory getTransformerFactory() {
793 if (transformerFactory == null) {
794 transformerFactory = createTransformerFactory();
795 }
796 return transformerFactory;
797 }
798
799
800
801
802
803
804
805 public void setTransformerFactory(TransformerFactory transformerFactory) {
806 this.transformerFactory = transformerFactory;
807 }
808
809
810
811
812
813
814
815
816
817 public Transformer createTransfomer() throws TransformerConfigurationException {
818 TransformerFactory factory = getTransformerFactory();
819 return factory.newTransformer();
820 }
821
822
823
824
825
826
827 public TransformerFactory createTransformerFactory() {
828 TransformerFactory answer = TransformerFactory.newInstance();
829 return answer;
830 }
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845 public XMLStreamReader toXMLStreamReader(Source source) throws XMLStreamException, TransformerException {
846
847
848
849
850
851
852 if (source instanceof DOMSource) {
853 Node n = ((DOMSource) source).getNode();
854
855 Element el = null;
856 if (n instanceof Document) {
857 el = ((Document) n).getDocumentElement();
858 } else if (n instanceof Element ) {
859 el =(Element) n;
860 }
861 if (el != null) {
862 return new W3CDOMStreamReader(el);
863 }
864 }
865 XMLInputFactory factory = getInputFactory();
866 try {
867 return factory.createXMLStreamReader(source);
868 } catch (XMLStreamException e) {
869 return factory.createXMLStreamReader(toReaderFromSource(source));
870 }
871 }
872
873
874
875
876
877
878
879
880 protected XMLInputFactory createInputFactory() {
881 XMLInputFactory answer = XMLInputFactory.newInstance();
882 return answer;
883 }
884
885
886
887
888
889
890
891
892 public XMLInputFactory getInputFactory() {
893 if (inputFactory == null) {
894 inputFactory = createInputFactory();
895 }
896 return inputFactory;
897 }
898 }