001: /* $Id: ContentHandlerToXMLEventWriter.java,v 1.4 2005/10/25 18:36:24 ryan_shoemaker Exp $
002: *
003: * Copyright (c) 2004, Sun Microsystems, Inc.
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * 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
014: * copyright notice, this list of conditions and the following
015: * disclaimer in the documentation and/or other materials provided
016: * with the distribution.
017: *
018: * * Neither the name of Sun Microsystems, Inc. nor the names of its
019: * contributors may be used to endorse or promote products derived
020: * from this software without specific prior written permission.
021: *
022: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
023: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
024: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
025: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
026: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
027: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
028: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
029: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
030: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
032: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033: */
034: package javanet.staxutils;
035:
036: import org.xml.sax.Attributes;
037: import org.xml.sax.Locator;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.helpers.DefaultHandler;
040:
041: import javax.xml.stream.Location;
042: import javax.xml.stream.XMLEventFactory;
043: import javax.xml.stream.XMLEventWriter;
044: import javax.xml.stream.XMLStreamException;
045: import javax.xml.stream.events.Characters;
046: import java.util.HashMap;
047: import java.util.Stack;
048:
049: /**
050: * This is a simple utility class that adapts SAX events into StAX
051: * {@link javax.xml.stream.XMLEventWriter} events, bridging between
052: * the two parser technologies.
053: *
054: * This ContentHandler does not own the XMLEventWriter. Therefore, it will
055: * not close or flush the writer at any point.
056: *
057: * @author Ryan.Shoemaker@Sun.COM
058: * @version 1.0
059: */
060: public class ContentHandlerToXMLEventWriter extends DefaultHandler {
061:
062: // SAX events will be sent to this XMLEventWriter
063: private final XMLEventWriter staxWriter;
064:
065: // factory for StAX events
066: private final XMLEventFactory staxEventFactory;
067:
068: // SAX locator
069: private Locator locator = null;
070:
071: // StAX location
072: private Location location = null;
073:
074: // storage for prefix bindings
075: private final Stack prefixBindings;
076:
077: // name to entity decl map
078: // map<String:EntityDeclaration>
079: private final HashMap entityMap;
080:
081: public ContentHandlerToXMLEventWriter(XMLEventWriter staxCore) {
082: this .staxWriter = staxCore;
083: staxEventFactory = XMLEventFactory.newInstance();
084:
085: prefixBindings = new Stack(); // default of 10 seems reasonable
086:
087: entityMap = new HashMap();
088: }
089:
090: /*
091: * (non-Javadoc)
092: *
093: * @see org.xml.sax.ContentHandler#endDocument()
094: */
095: public void endDocument() throws SAXException {
096: try {
097: staxWriter.add(staxEventFactory.createEndDocument());
098: staxWriter.flush();
099: } catch (XMLStreamException e) {
100: throw new SAXException(e);
101: }
102: }
103:
104: /*
105: * (non-Javadoc)
106: *
107: * @see org.xml.sax.ContentHandler#startDocument()
108: */
109: public void startDocument() throws SAXException {
110: try {
111: staxWriter.add(staxEventFactory.createStartDocument());
112: } catch (XMLStreamException e) {
113: throw new SAXException(e);
114: }
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.xml.sax.ContentHandler#characters(char[], int, int)
121: */
122: public void characters(char[] ch, int start, int length)
123: throws SAXException {
124:
125: try {
126: // TODO: is there a way to reuse an event?
127: Characters event = staxEventFactory
128: .createCharacters(new String(ch, start, length));
129: staxWriter.add(event);
130: } catch (XMLStreamException e) {
131: throw new SAXException(e);
132: }
133:
134: }
135:
136: /*
137: * (non-Javadoc)
138: *
139: * @see org.xml.sax.ContentHandler#ignorableWhitespace(char[], int, int)
140: */
141: public void ignorableWhitespace(char[] ch, int start, int length)
142: throws SAXException {
143:
144: characters(ch, start, length);
145: }
146:
147: /*
148: * (non-Javadoc)
149: *
150: * @see org.xml.sax.ContentHandler#endPrefixMapping(java.lang.String)
151: */
152: public void endPrefixMapping(String prefix) throws SAXException {
153: // TODO: no-op?
154:
155: // I think we can ignore these SAX events because StAX
156: // automatically scopes the prefix bindings.
157: }
158:
159: public void unparsedEntityDecl(String name, String publicId,
160: String systemId, String notationName) throws SAXException {
161: // store unparsed entity decls so we can report them properly in skippedEntity
162: entityMap.put(name, new EntityDeclarationImpl(location, name,
163: publicId, systemId, notationName, null));
164: }
165:
166: /*
167: * (non-Javadoc)
168: *
169: * @see org.xml.sax.ContentHandler#skippedEntity(java.lang.String)
170: */
171: public void skippedEntity(String name) throws SAXException {
172: try {
173: // if the entity isn't knows, then pass null through
174: // for the EntityDeclaration
175: staxWriter.add(staxEventFactory.createEntityReference(name,
176: (EntityDeclarationImpl) entityMap.get(name)));
177: } catch (XMLStreamException e) {
178: throw new SAXException(e);
179: }
180: }
181:
182: /*
183: * (non-Javadoc)
184: *
185: * @see org.xml.sax.ContentHandler#setDocumentLocator(org.xml.sax.Locator)
186: */
187: public void setDocumentLocator(final Locator locator) {
188: this .locator = locator;
189: staxEventFactory.setLocation(new Location() {
190: public int getLineNumber() {
191: return locator.getLineNumber();
192: }
193:
194: public int getColumnNumber() {
195: return locator.getColumnNumber();
196: }
197:
198: public int getCharacterOffset() {
199: return -1;
200: }
201:
202: public String getPublicId() {
203: return locator.getPublicId();
204: }
205:
206: public String getSystemId() {
207: return locator.getSystemId();
208: }
209: });
210: }
211:
212: /*
213: * (non-Javadoc)
214: *
215: * @see org.xml.sax.ContentHandler#processingInstruction(java.lang.String,
216: * java.lang.String)
217: */
218: public void processingInstruction(String target, String data)
219: throws SAXException {
220:
221: try {
222: staxWriter.add(staxEventFactory
223: .createProcessingInstruction(target, data));
224: } catch (XMLStreamException e) {
225: throw new SAXException(e);
226: }
227:
228: }
229:
230: /*
231: * (non-Javadoc)
232: *
233: * @see org.xml.sax.ContentHandler#startPrefixMapping(java.lang.String,
234: * java.lang.String)
235: */
236: public void startPrefixMapping(String prefix, String uri)
237: throws SAXException {
238:
239: if (prefix.equals("xml")) {
240: return;
241: }
242:
243: // defend against parsers that pass null in for "xmlns" prefix
244: if (prefix == null) {
245: prefix = "";
246: }
247:
248: prefixBindings.add(prefix);
249: prefixBindings.add(uri);
250: }
251:
252: /*
253: * (non-Javadoc)
254: *
255: * @see org.xml.sax.ContentHandler#endElement(java.lang.String,
256: * java.lang.String, java.lang.String)
257: */
258: public void endElement(String namespaceURI, String localName,
259: String qName) throws SAXException {
260:
261: try {
262: // TODO: is this all we have to do?
263: staxWriter.add(staxEventFactory.createEndElement(
264: getPrefix(qName), namespaceURI, localName));
265: } catch (XMLStreamException e) {
266: throw new SAXException(e);
267: }
268: }
269:
270: /*
271: * (non-Javadoc)
272: *
273: * @see org.xml.sax.ContentHandler#startElement(java.lang.String,
274: * java.lang.String, java.lang.String, org.xml.sax.Attributes)
275: */
276: public void startElement(String namespaceURI, String localName,
277: String qName, Attributes atts) throws SAXException {
278:
279: try {
280: staxWriter.add(staxEventFactory.createStartElement(
281: getPrefix(qName), namespaceURI, localName));
282:
283: String uri, prefix;
284: while (prefixBindings.size() != 0) {
285: uri = (String) prefixBindings.pop();
286: prefix = (String) prefixBindings.pop();
287: if (prefix.length() == 0) {
288: staxWriter.setDefaultNamespace(uri);
289: } else {
290: staxWriter.setPrefix(prefix, uri);
291: }
292:
293: // this method handles "", null, and "xmlns" prefixes properly
294: if (prefix == null || "".equals(prefix)
295: || "xmlns".equals(prefix)) {
296: staxWriter.add(staxEventFactory
297: .createNamespace(uri));
298: } else {
299: staxWriter.add(staxEventFactory.createNamespace(
300: prefix, uri));
301: }
302: }
303:
304: writeAttributes(atts);
305: } catch (XMLStreamException e) {
306: throw new SAXException(e);
307: }
308:
309: }
310:
311: /**
312: * Generate a StAX writeAttribute event for each attribute
313: *
314: * @param atts
315: * attributes from the SAX event
316: */
317: private void writeAttributes(Attributes atts)
318: throws XMLStreamException {
319: for (int i = 0; i < atts.getLength(); i++) {
320: final String prefix = getPrefix(atts.getQName(i));
321: if (!prefix.equals("xmlns")) { // defend againts broken transformers that report xmlns decls as attrs
322: staxWriter.add(staxEventFactory.createAttribute(prefix,
323: atts.getURI(i), atts.getLocalName(i), atts
324: .getValue(i)));
325: }
326: }
327: }
328:
329: /**
330: * Pull the prefix off of the specified QName.
331: *
332: * @param qName
333: * the QName
334: * @return the prefix or the empty string if it doesn't exist.
335: */
336: private String getPrefix(String qName) {
337: int idx = qName.indexOf(':');
338: if (idx == -1) {
339: return "";
340: } else {
341: return qName.substring(0, idx);
342: }
343: }
344:
345: }
|