KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > jbi > jaxp > StAXSourceTransformer


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.jbi.jaxp;
18
19 import org.w3c.dom.Document JavaDoc;
20 import org.w3c.dom.Element JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23
24 import javax.xml.parsers.ParserConfigurationException JavaDoc;
25 import javax.xml.stream.XMLInputFactory;
26 import javax.xml.stream.XMLOutputFactory;
27 import javax.xml.stream.XMLStreamException;
28 import javax.xml.stream.XMLStreamReader;
29 import javax.xml.transform.Source JavaDoc;
30 import javax.xml.transform.Transformer JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.dom.DOMResult JavaDoc;
33 import javax.xml.transform.dom.DOMSource JavaDoc;
34 import javax.xml.transform.sax.SAXSource JavaDoc;
35
36 import java.io.IOException JavaDoc;
37
38 /**
39  * An enhanced {@link org.apache.servicemix.jbi.jaxp.SourceTransformer} which adds support for converting from and to
40  * {@link StAXSource} instances. Since this class introduces a runtime dependency on StAX
41  * which some users may not use/require, this class is separated out from the core JAXP transformer.
42  *
43  * @version $Revision: 426415 $
44  */

45 public class StAXSourceTransformer extends SourceTransformer {
46
47     private XMLInputFactory inputFactory;
48     private XMLOutputFactory outputFactory;
49
50     /**
51      * Converts the source instance to a {@link javax.xml.transform.dom.DOMSource} or returns null if the conversion is not
52      * supported (making it easy to derive from this class to add new kinds of conversion).
53      */

54     public StaxSource toStaxSource(Source JavaDoc source) throws XMLStreamException {
55         if (source instanceof StaxSource) {
56             return (StaxSource) source;
57         }
58         else {
59             XMLInputFactory factory = getInputFactory();
60             XMLStreamReader reader = factory.createXMLStreamReader(source);
61             return new StaxSource(reader);
62         }
63     }
64     
65     public XMLStreamReader toXMLStreamReader(Source JavaDoc source) throws XMLStreamException, TransformerException JavaDoc {
66         // It seems that woodstox 2.9.3 throws some NPE in the servicemix-soap
67
// when using DOM, so use our own dom / stax parser
68
if (source instanceof DOMSource JavaDoc) {
69             Node JavaDoc n = ((DOMSource JavaDoc) source).getNode();
70             Element JavaDoc el = n instanceof Document JavaDoc ? ((Document JavaDoc) n).getDocumentElement() : n instanceof Element JavaDoc ? (Element JavaDoc) n : null;
71             if (el != null) {
72                 return new W3CDOMStreamReader(el);
73             }
74         }
75         XMLInputFactory factory = getInputFactory();
76         try {
77             return factory.createXMLStreamReader(source);
78         } catch (XMLStreamException e) {
79             return factory.createXMLStreamReader(toReaderFromSource(source));
80         }
81     }
82
83     public DOMSource JavaDoc toDOMSource(Source JavaDoc source) throws ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc, TransformerException JavaDoc {
84         DOMSource JavaDoc answer = super.toDOMSource(source);
85         if (answer == null && source instanceof StaxSource) {
86             answer = toDOMSourceFromStax((StaxSource) source);
87         }
88         return answer;
89     }
90
91     public SAXSource JavaDoc toSAXSource(Source JavaDoc source) throws IOException JavaDoc, SAXException JavaDoc {
92         SAXSource JavaDoc answer = super.toSAXSource(source);
93         if (answer == null && source instanceof StaxSource) {
94             answer = toSAXSourceFromStax((StaxSource) source);
95         }
96         return answer;
97     }
98
99     public DOMSource JavaDoc toDOMSourceFromStax(StaxSource source) throws TransformerException JavaDoc {
100         Transformer JavaDoc transformer = createTransfomer();
101         DOMResult JavaDoc result = new DOMResult JavaDoc();
102         transformer.transform(source, result);
103         return new DOMSource JavaDoc(result.getNode(), result.getSystemId());
104     }
105
106     public SAXSource JavaDoc toSAXSourceFromStax(StaxSource source) {
107         return (SAXSource JavaDoc) source;
108     }
109
110     // Properties
111
//-------------------------------------------------------------------------
112
public XMLInputFactory getInputFactory() {
113         if (inputFactory == null) {
114             inputFactory = createInputFactory();
115         }
116         return inputFactory;
117     }
118
119     public void setInputFactory(XMLInputFactory inputFactory) {
120         this.inputFactory = inputFactory;
121     }
122     
123     public XMLOutputFactory getOutputFactory() {
124         if (outputFactory == null) {
125             outputFactory = createOutputFactory();
126         }
127         return outputFactory;
128     }
129     
130     public void setOutputFactory(XMLOutputFactory outputFactory) {
131         this.outputFactory = outputFactory;
132     }
133
134     // Implementation methods
135
//-------------------------------------------------------------------------
136
protected XMLInputFactory createInputFactory() {
137         XMLInputFactory answer = XMLInputFactory.newInstance();
138         return answer;
139     }
140
141     protected XMLOutputFactory createOutputFactory() {
142         XMLOutputFactory answer = XMLOutputFactory.newInstance();
143         return answer;
144     }
145
146 }
147
Popular Tags