001: /*
002: * $Id: StAXStreamContentHandler.java,v 1.5 2004/07/05 23:15:11 cniles Exp $
003: *
004: * Copyright (c) 2004, Christian Niles, Unit12
005: * All rights reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions are met:
009: *
010: * * Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * * Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * * Neither the name of Christian Niles, Unit12, nor the names of its
018: * contributors may be used to endorse or promote products derived from
019: * this software without specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
022: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
023: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
024: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
025: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
026: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
027: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
028: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
029: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
030: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
031: * POSSIBILITY OF SUCH DAMAGE.
032: *
033: */
034: package javanet.staxutils;
035:
036: import java.util.Iterator;
037:
038: import javax.xml.stream.XMLStreamException;
039: import javax.xml.stream.XMLStreamWriter;
040:
041: import org.xml.sax.Attributes;
042: import org.xml.sax.SAXException;
043:
044: /**
045: * SAX ContentHandler that writes events to a StAX {@link XMLStreamWriter}.
046: *
047: * @author Christian Niles
048: * @version $Revision: 1.5 $
049: */
050: public class StAXStreamContentHandler extends StAXContentHandler {
051:
052: /** The StAX stream to which SAX events will be written. */
053: private XMLStreamWriter writer;
054:
055: public StAXStreamContentHandler() {
056:
057: }
058:
059: /**
060: * Constructs an instance that writes SAX events to the specified StAX
061: * stream.
062: *
063: * @param writer The StAX stream to which events will be written.
064: */
065: public StAXStreamContentHandler(XMLStreamWriter writer) {
066:
067: this .writer = writer;
068:
069: }
070:
071: /**
072: * Returns a reference to the {@link XMLStreamWriter}to which SAX events
073: * are written.
074: *
075: * @return The {@link XMLStreamWriter}to which SAX events are written.
076: */
077: public XMLStreamWriter getStreamWriter() {
078:
079: return writer;
080:
081: }
082:
083: /**
084: * Sets the {@link XMLStreamWriter}to which SAX events will be written.
085: *
086: * @param writer The {@link XMLStreamWriter}to which SAX events will be
087: * written.
088: */
089: public void setStreamWriter(XMLStreamWriter writer) {
090:
091: this .writer = writer;
092:
093: }
094:
095: public void startDocument() throws SAXException {
096:
097: super .startDocument();
098: try {
099:
100: writer.writeStartDocument();
101:
102: } catch (XMLStreamException e) {
103:
104: throw new SAXException(e);
105:
106: }
107:
108: }
109:
110: public void endDocument() throws SAXException {
111:
112: try {
113:
114: writer.writeEndDocument();
115:
116: } catch (XMLStreamException e) {
117:
118: throw new SAXException(e);
119:
120: }
121:
122: super .endDocument();
123:
124: }
125:
126: public void startElement(String uri, String localName,
127: String qName, Attributes attributes) throws SAXException {
128:
129: try {
130:
131: String[] qname = { null, null };
132: parseQName(qName, qname);
133:
134: writer.writeStartElement(qname[0], qname[1], uri);
135:
136: // copy namespaces
137: if (namespaces != null) {
138:
139: Iterator prefixes = namespaces.getDeclaredPrefixes();
140: while (prefixes.hasNext()) {
141:
142: String prefix = (String) prefixes.next();
143: String nsURI = namespaces.getNamespaceURI(prefix);
144:
145: if (prefix.length() == 0) {
146:
147: writer.setDefaultNamespace(nsURI);
148:
149: } else {
150:
151: writer.setPrefix(prefix, nsURI);
152:
153: }
154:
155: writer.writeNamespace(prefix, nsURI);
156:
157: }
158:
159: }
160:
161: // write attributes
162: for (int i = 0, s = attributes.getLength(); i < s; i++) {
163:
164: parseQName(attributes.getQName(i), qname);
165:
166: String attrPrefix = qname[0];
167: String attrLocal = qname[1];
168:
169: String attrQName = attributes.getQName(i);
170: String attrValue = attributes.getValue(i);
171: String attrURI = attributes.getURI(i);
172:
173: if ("xmlns".equals(attrQName)
174: || "xmlns".equals(attrPrefix)) {
175:
176: // namespace declaration disguised as an attribute. If the
177: // namespace has already been declared, skip it, otherwise
178: // write it as an namespace
179:
180: String nsURI = namespaces
181: .getNamespaceURI(attrPrefix);
182: if (nsURI == null) {
183:
184: if (attrPrefix.length() == 0) {
185:
186: writer.setDefaultNamespace(attrValue);
187:
188: } else {
189:
190: writer.setPrefix(attrPrefix, attrValue);
191:
192: }
193:
194: writer.writeNamespace(attrPrefix, attrValue);
195:
196: }
197:
198: } else if (attrPrefix.length() > 0) {
199:
200: writer.writeAttribute(attrPrefix, attrURI,
201: attrLocal, attrValue);
202:
203: } else {
204:
205: writer.writeAttribute(attrQName, attrValue);
206:
207: }
208:
209: }
210:
211: } catch (XMLStreamException e) {
212:
213: throw new SAXException(e);
214:
215: } finally {
216:
217: super .startElement(uri, localName, qName, attributes);
218:
219: }
220:
221: }
222:
223: public void endElement(String uri, String localName, String qName)
224: throws SAXException {
225:
226: try {
227:
228: writer.writeEndElement();
229:
230: } catch (XMLStreamException e) {
231:
232: throw new SAXException(e);
233:
234: } finally {
235:
236: super .endElement(uri, localName, qName);
237:
238: }
239:
240: }
241:
242: public void comment(char[] ch, int start, int length)
243: throws SAXException {
244:
245: super .comment(ch, start, length);
246: try {
247:
248: writer.writeComment(new String(ch, start, length));
249:
250: } catch (XMLStreamException e) {
251:
252: throw new SAXException(e);
253:
254: }
255:
256: }
257:
258: public void characters(char[] ch, int start, int length)
259: throws SAXException {
260:
261: super .characters(ch, start, length);
262: try {
263:
264: if (!isCDATA) {
265:
266: writer.writeCharacters(ch, start, length);
267:
268: }
269:
270: } catch (XMLStreamException e) {
271:
272: throw new SAXException(e);
273:
274: }
275:
276: }
277:
278: public void endCDATA() throws SAXException {
279:
280: try {
281:
282: writer.writeCData(CDATABuffer.toString());
283:
284: } catch (XMLStreamException e) {
285:
286: throw new SAXException(e);
287:
288: }
289:
290: super .endCDATA();
291:
292: }
293:
294: public void ignorableWhitespace(char[] ch, int start, int length)
295: throws SAXException {
296:
297: super .ignorableWhitespace(ch, start, length);
298: try {
299:
300: writer.writeCharacters(ch, start, length);
301:
302: } catch (XMLStreamException e) {
303:
304: throw new SAXException(e);
305:
306: }
307:
308: }
309:
310: public void processingInstruction(String target, String data)
311: throws SAXException {
312:
313: super .processingInstruction(target, data);
314: try {
315:
316: writer.writeProcessingInstruction(target, data);
317:
318: } catch (XMLStreamException e) {
319:
320: throw new SAXException(e);
321:
322: }
323:
324: }
325:
326: }
|