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.jaxb;
018    
019    import javax.xml.namespace.NamespaceContext;
020    import javax.xml.stream.XMLStreamException;
021    import javax.xml.stream.XMLStreamWriter;
022    
023    /**
024     * {@link XMLStreamWriter} wrapper that filters out non-XML characters, see
025     * {@link NonXmlCharFilterer} for details. Filtering applies to
026     * <ul>
027     * <li>Characters</li>
028     * <li>CData</li>
029     * <li>Attributes</li>
030     * <li>Comments</li>
031     * </ul>
032     * 
033     * @see XMLStreamWriter
034     */
035    public class FilteringXmlStreamWriter implements XMLStreamWriter {
036        NonXmlCharFilterer nonXmlCharFilterer = new NonXmlCharFilterer();
037    
038        private XMLStreamWriter writer;
039    
040        /**
041         * @param writer
042         *            target writer to wrap.
043         */
044        public FilteringXmlStreamWriter(XMLStreamWriter writer) {
045            this.writer = writer;
046        }
047    
048        /**
049         * This method applies filtering before delegating call to {@link #writer}.
050         */
051        public void writeAttribute(String prefix, String namespaceURI, String localName, String value)
052            throws XMLStreamException {
053            String filteredValue = nonXmlCharFilterer.filter(value);
054            writer.writeAttribute(prefix, namespaceURI, localName, filteredValue);
055        }
056    
057        /**
058         * This method applies filtering before delegating call to {@link #writer}.
059         */
060        public void writeAttribute(String namespaceURI, String localName, String value)
061            throws XMLStreamException {
062            String filteredValue = nonXmlCharFilterer.filter(value);
063            writer.writeAttribute(namespaceURI, localName, filteredValue);
064        }
065    
066        /**
067         * This method applies filtering before delegating call to {@link #writer}.
068         */
069        public void writeAttribute(String localName, String value) throws XMLStreamException {
070            String filteredValue = nonXmlCharFilterer.filter(value);
071            writer.writeAttribute(localName, filteredValue);
072        }
073    
074        /**
075         * This method applies filtering before delegating call to {@link #writer}.
076         */
077        public void writeCData(String data) throws XMLStreamException {
078            String filteredData = nonXmlCharFilterer.filter(data);
079            writer.writeCData(filteredData);
080        }
081    
082        /**
083         * This method applies filtering before delegating call to {@link #writer}.
084         */
085        public void writeCharacters(char[] text, int start, int len) throws XMLStreamException {
086            nonXmlCharFilterer.filter(text, start, len);
087            writer.writeCharacters(text, start, len);
088        }
089    
090        /**
091         * This method applies filtering before delegating call to {@link #writer}.
092         */
093        public void writeCharacters(String text) throws XMLStreamException {
094            String filteredText = nonXmlCharFilterer.filter(text);
095            writer.writeCharacters(filteredText);
096        }
097    
098        /**
099         * This method applies filtering before delegating call to {@link #writer}.
100         */
101        public void writeComment(String data) throws XMLStreamException {
102            String filteredData = nonXmlCharFilterer.filter(data);
103            writer.writeComment(filteredData);
104        }
105    
106        public void close() throws XMLStreamException {
107            writer.close();
108        }
109    
110        public void flush() throws XMLStreamException {
111            writer.flush();
112        }
113    
114        public NamespaceContext getNamespaceContext() {
115            return writer.getNamespaceContext();
116        }
117    
118        public String getPrefix(String uri) throws XMLStreamException {
119            return writer.getPrefix(uri);
120        }
121    
122        public Object getProperty(String name) throws IllegalArgumentException {
123            return writer.getProperty(name);
124        }
125    
126        public void setDefaultNamespace(String uri) throws XMLStreamException {
127            writer.setDefaultNamespace(uri);
128        }
129    
130        public void setNamespaceContext(NamespaceContext context) throws XMLStreamException {
131            writer.setNamespaceContext(context);
132        }
133    
134        public void setPrefix(String prefix, String uri) throws XMLStreamException {
135            writer.setPrefix(prefix, uri);
136        }
137    
138        public void writeDefaultNamespace(String namespaceURI) throws XMLStreamException {
139            writer.writeDefaultNamespace(namespaceURI);
140        }
141    
142        public void writeDTD(String dtd) throws XMLStreamException {
143            writer.writeDTD(dtd);
144        }
145    
146        public void writeEmptyElement(String prefix, String localName, String namespaceURI)
147            throws XMLStreamException {
148            writer.writeEmptyElement(prefix, localName, namespaceURI);
149        }
150    
151        public void writeEmptyElement(String namespaceURI, String localName) throws XMLStreamException {
152            writer.writeEmptyElement(namespaceURI, localName);
153        }
154    
155        public void writeEmptyElement(String localName) throws XMLStreamException {
156            writer.writeEmptyElement(localName);
157        }
158    
159        public void writeEndDocument() throws XMLStreamException {
160            writer.writeEndDocument();
161        }
162    
163        public void writeEndElement() throws XMLStreamException {
164            writer.writeEndElement();
165        }
166    
167        public void writeEntityRef(String name) throws XMLStreamException {
168            writer.writeEntityRef(name);
169        }
170    
171        public void writeNamespace(String prefix, String namespaceURI) throws XMLStreamException {
172            writer.writeNamespace(prefix, namespaceURI);
173        }
174    
175        public void writeProcessingInstruction(String target, String data) throws XMLStreamException {
176            writer.writeProcessingInstruction(target, data);
177        }
178    
179        public void writeProcessingInstruction(String target) throws XMLStreamException {
180            writer.writeProcessingInstruction(target);
181        }
182    
183        public void writeStartDocument() throws XMLStreamException {
184            writer.writeStartDocument();
185        }
186    
187        public void writeStartDocument(String encoding, String version) throws XMLStreamException {
188            writer.writeStartDocument(encoding, version);
189        }
190    
191        public void writeStartDocument(String version) throws XMLStreamException {
192            writer.writeStartDocument(version);
193        }
194    
195        public void writeStartElement(String prefix, String localName, String namespaceURI)
196            throws XMLStreamException {
197            writer.writeStartElement(prefix, localName, namespaceURI);
198        }
199    
200        public void writeStartElement(String namespaceURI, String localName) throws XMLStreamException {
201            writer.writeStartElement(namespaceURI, localName);
202        }
203    
204        public void writeStartElement(String localName) throws XMLStreamException {
205            writer.writeStartElement(localName);
206        }
207    
208    }