001    /**
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.camel.converter.jaxp;
018    
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    import java.io.Reader;
022    import java.io.Writer;
023    
024    import javax.xml.stream.XMLEventReader;
025    import javax.xml.stream.XMLEventWriter;
026    import javax.xml.stream.XMLInputFactory;
027    import javax.xml.stream.XMLOutputFactory;
028    import javax.xml.stream.XMLStreamException;
029    import javax.xml.stream.XMLStreamReader;
030    import javax.xml.stream.XMLStreamWriter;
031    import javax.xml.transform.Result;
032    import javax.xml.transform.Source;
033    
034    import org.apache.camel.Converter;
035    import org.apache.camel.Exchange;
036    import org.apache.camel.converter.IOConverter;
037    
038    /**
039     * A converter of StAX objects
040     *
041     * @version $Revision: 904440 $
042     */
043    @Converter
044    public class StaxConverter {
045        private XMLInputFactory inputFactory;
046        private XMLOutputFactory outputFactory;
047    
048        @Converter
049        public XMLEventWriter createXMLEventWriter(OutputStream out, Exchange exchange) throws XMLStreamException {
050            return getOutputFactory().createXMLEventWriter(out, IOConverter.getCharsetName(exchange));
051        }
052        
053        @Deprecated
054        public XMLEventWriter createXMLEventWriter(OutputStream out) throws XMLStreamException {
055            return getOutputFactory().createXMLEventWriter(out);
056        }
057    
058        @Converter
059        public XMLEventWriter createXMLEventWriter(Writer writer) throws XMLStreamException {
060            return getOutputFactory().createXMLEventWriter(writer);
061        }
062    
063        @Converter
064        public XMLEventWriter createXMLEventWriter(Result result) throws XMLStreamException {
065            return getOutputFactory().createXMLEventWriter(result);
066        }
067        
068        @Deprecated
069        public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream) throws XMLStreamException {
070            return getOutputFactory().createXMLStreamWriter(outputStream);
071        }
072    
073        @Converter
074        public XMLStreamWriter createXMLStreamWriter(OutputStream outputStream, Exchange exchange) throws XMLStreamException {
075            return getOutputFactory().createXMLStreamWriter(outputStream, IOConverter.getCharsetName(exchange));
076        }
077    
078        @Converter
079        public XMLStreamWriter createXMLStreamWriter(Writer writer) throws XMLStreamException {
080            return getOutputFactory().createXMLStreamWriter(writer);
081        }
082    
083        @Converter
084        public XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException {
085            return getOutputFactory().createXMLStreamWriter(result);
086        }
087        
088        @Deprecated
089        public XMLStreamReader createXMLStreamReader(InputStream in) throws XMLStreamException {
090            return getInputFactory().createXMLStreamReader(in);
091        }
092        
093        @Converter
094        public XMLStreamReader createXMLStreamReader(InputStream in, Exchange exchange) throws XMLStreamException {
095            return getInputFactory().createXMLStreamReader(in, IOConverter.getCharsetName(exchange));
096        }
097    
098        @Converter
099        public XMLStreamReader createXMLStreamReader(Reader in) throws XMLStreamException {
100            return getInputFactory().createXMLStreamReader(in);
101        }
102    
103        @Converter
104        public XMLStreamReader createXMLStreamReader(Source in) throws XMLStreamException {
105            return getInputFactory().createXMLStreamReader(in);
106        }
107        
108        @Deprecated
109        public XMLEventReader createXMLEventReader(InputStream in) throws XMLStreamException {
110            return getInputFactory().createXMLEventReader(in);
111        }
112    
113        @Converter
114        public XMLEventReader createXMLEventReader(InputStream in, Exchange exchange) throws XMLStreamException {
115            return getInputFactory().createXMLEventReader(in, IOConverter.getCharsetName(exchange));
116        }
117    
118        @Converter
119        public XMLEventReader createXMLEventReader(Reader in) throws XMLStreamException {
120            return getInputFactory().createXMLEventReader(in);
121        }
122    
123        @Converter
124        public XMLEventReader createXMLEventReader(XMLStreamReader in) throws XMLStreamException {
125            return getInputFactory().createXMLEventReader(in);
126        }
127    
128        @Converter
129        public XMLEventReader createXMLEventReader(Source in) throws XMLStreamException {
130            return getInputFactory().createXMLEventReader(in);
131        }
132    
133        // Properties
134        //-------------------------------------------------------------------------
135    
136        public XMLInputFactory getInputFactory() {
137            if (inputFactory == null) {
138                inputFactory = XMLInputFactory.newInstance();
139            }
140            return inputFactory;
141        }
142    
143        public void setInputFactory(XMLInputFactory inputFactory) {
144            this.inputFactory = inputFactory;
145        }
146    
147        public XMLOutputFactory getOutputFactory() {
148            if (outputFactory == null) {
149                outputFactory = XMLOutputFactory.newInstance();
150            }
151            return outputFactory;
152        }
153    
154        public void setOutputFactory(XMLOutputFactory outputFactory) {
155            this.outputFactory = outputFactory;
156        }
157    }