001: /*
002: * $Id: XMLStreamEventWriter.java,v 1.1 2004/07/05 23:16:16 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 javax.xml.stream.XMLStreamException;
037: import javax.xml.stream.XMLStreamWriter;
038: import javax.xml.stream.events.StartElement;
039: import javax.xml.stream.events.XMLEvent;
040:
041: import javanet.staxutils.events.ExtendedXMLEvent;
042: import javanet.staxutils.io.XMLWriterUtils;
043:
044: /**
045: * {@link javax.xml.stream.XMLEventWriter} that writes events to a
046: * {@link XMLStreamWriter}.
047: *
048: * @author Christian Niles
049: * @version $Revision: 1.1 $
050: */
051: public class XMLStreamEventWriter extends BaseXMLEventWriter {
052:
053: /** The underlying stream. */
054: private XMLStreamWriter writer;
055:
056: /**
057: * Constructs a <code>XMLEventStreamWriter</code> that writes events to the
058: * given stream.
059: *
060: * @param writer The {@link XMLStreamWriter} to which the events will be
061: * written.
062: */
063: public XMLStreamEventWriter(XMLStreamWriter writer) {
064:
065: super (null, writer.getNamespaceContext());
066: this .writer = writer;
067:
068: }
069:
070: public synchronized void flush() throws XMLStreamException {
071:
072: super .flush();
073:
074: // send any cached start element
075: if (savedStart != null) {
076:
077: XMLWriterUtils.writeStartElement(savedStart, false, writer);
078:
079: }
080:
081: writer.flush();
082:
083: }
084:
085: public synchronized void close() throws XMLStreamException {
086:
087: super .close();
088: writer.close();
089:
090: }
091:
092: /**
093: * Saved reference to most recent start element. This is used to properly
094: * write empty elements. Each startElement event is saved here until the
095: * next event is received, at which point it will be written as a start or
096: * empty element if it is followed directly by an EndElement.
097: */
098: private StartElement savedStart;
099:
100: protected synchronized void sendEvent(XMLEvent event)
101: throws XMLStreamException {
102:
103: // Check if we have a cached start tag. If we do, then we should
104: // check if this event is actually an end tag, so we can properly
105: // write an empty element.
106: if (savedStart != null) {
107:
108: StartElement start = savedStart;
109: savedStart = null;
110:
111: if (event.getEventType() == XMLEvent.END_ELEMENT) {
112:
113: // this end tag directly followed a start tag, so send
114: // the underlying writer an empty start element
115: XMLWriterUtils.writeStartElement(start, true, writer);
116: return;
117:
118: } else {
119:
120: // element has content, so send a regular start tag
121: XMLWriterUtils.writeStartElement(start, false, writer);
122:
123: }
124:
125: }
126:
127: if (event.isStartElement()) {
128:
129: // cache the event
130: savedStart = event.asStartElement();
131:
132: } else if (event instanceof ExtendedXMLEvent) {
133:
134: // let the event handle itself
135: ((ExtendedXMLEvent) event).writeEvent(writer);
136:
137: } else {
138:
139: // write the event to the stream
140: XMLWriterUtils.writeEvent(event, writer);
141:
142: }
143:
144: }
145:
146: }
|