001: /*
002: * Copyright 2005-2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.xml.internal.stream.writers;
027:
028: import java.util.Iterator;
029: import javax.xml.namespace.QName;
030: import javax.xml.stream.XMLEventWriter;
031: import javax.xml.stream.XMLStreamException;
032: import javax.xml.stream.events.Attribute;
033: import javax.xml.stream.events.Characters;
034: import javax.xml.stream.events.Comment;
035: import javax.xml.stream.events.DTD;
036: import javax.xml.stream.events.EntityReference;
037: import javax.xml.stream.events.Namespace;
038: import javax.xml.stream.events.ProcessingInstruction;
039: import javax.xml.stream.events.StartDocument;
040: import javax.xml.stream.events.StartElement;
041: import javax.xml.stream.events.XMLEvent;
042: import javax.xml.stream.XMLStreamWriter;
043:
044: /**
045: *
046: * @author Neeraj Bajaj, Sun Microsystems.
047: *
048: */
049: public class XMLEventWriterImpl implements XMLEventWriter {
050:
051: //delegate everything to XMLStreamWriter..
052: private XMLStreamWriter fStreamWriter;
053: private static final boolean DEBUG = false;
054:
055: /**
056: *
057: * @param streamWriter
058: */
059: public XMLEventWriterImpl(XMLStreamWriter streamWriter) {
060: fStreamWriter = streamWriter;
061: }
062:
063: /**
064: *
065: * @param xMLEventReader
066: * @throws XMLStreamException
067: */
068: public void add(javax.xml.stream.XMLEventReader xMLEventReader)
069: throws javax.xml.stream.XMLStreamException {
070: if (xMLEventReader == null)
071: throw new XMLStreamException(
072: "Event reader shouldn't be null");
073: while (xMLEventReader.hasNext()) {
074: add(xMLEventReader.nextEvent());
075: }
076: }
077:
078: /**
079: *
080: * @param xMLEvent
081: * @throws XMLStreamException
082: */
083: public void add(javax.xml.stream.events.XMLEvent xMLEvent)
084: throws javax.xml.stream.XMLStreamException {
085: int type = xMLEvent.getEventType();
086: switch (type) {
087: case XMLEvent.DTD: {
088: DTD dtd = (DTD) xMLEvent;
089: if (DEBUG)
090: System.out.println("Adding DTD = " + dtd.toString());
091: fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
092: break;
093: }
094: case XMLEvent.START_DOCUMENT: {
095: StartDocument startDocument = (StartDocument) xMLEvent;
096: if (DEBUG)
097: System.out.println("Adding StartDocument = "
098: + startDocument.toString());
099: try {
100: fStreamWriter.writeStartDocument(startDocument
101: .getCharacterEncodingScheme(), startDocument
102: .getVersion());
103: } catch (XMLStreamException e) {
104: fStreamWriter.writeStartDocument(startDocument
105: .getVersion());
106: }
107: break;
108: }
109: case XMLEvent.START_ELEMENT: {
110: StartElement startElement = xMLEvent.asStartElement();
111: if (DEBUG)
112: System.out.println("Adding startelement = "
113: + startElement.toString());
114: QName qname = startElement.getName();
115: fStreamWriter.writeStartElement(qname.getPrefix(), qname
116: .getLocalPart(), qname.getNamespaceURI());
117:
118: //getNamespaces() Returns an Iterator of namespaces declared on this element. This Iterator does not contain
119: //previously declared namespaces unless they appear on the current START_ELEMENT. Therefore
120: //this list may contain redeclared namespaces and duplicate namespace declarations. Use the
121: //getNamespaceContext() method to get the current context of namespace declarations.
122:
123: //so we should be using getNamespaces() to write namespace declarations for this START_ELEMENT
124: Iterator iterator = startElement.getNamespaces();
125: while (iterator.hasNext()) {
126: Namespace namespace = (Namespace) iterator.next();
127: fStreamWriter.writeNamespace(namespace.getPrefix(),
128: namespace.getNamespaceURI());
129: }
130: //REVISIT: What about writing attributes ?
131: Iterator attributes = startElement.getAttributes();
132: while (attributes.hasNext()) {
133: Attribute attribute = (Attribute) attributes.next();
134: QName aqname = attribute.getName();
135: fStreamWriter.writeAttribute(aqname.getPrefix(), aqname
136: .getNamespaceURI(), aqname.getLocalPart(),
137: attribute.getValue());
138: }
139: break;
140: }
141: case XMLEvent.NAMESPACE: {
142: Namespace namespace = (Namespace) xMLEvent;
143: if (DEBUG)
144: System.out.println("Adding namespace = "
145: + namespace.toString());
146: fStreamWriter.writeNamespace(namespace.getPrefix(),
147: namespace.getNamespaceURI());
148: break;
149: }
150: case XMLEvent.COMMENT: {
151: Comment comment = (Comment) xMLEvent;
152: if (DEBUG)
153: System.out.println("Adding comment = "
154: + comment.toString());
155: fStreamWriter.writeComment(comment.getText());
156: break;
157: }
158: case XMLEvent.PROCESSING_INSTRUCTION: {
159: ProcessingInstruction processingInstruction = (ProcessingInstruction) xMLEvent;
160: if (DEBUG)
161: System.out.println("Adding processing instruction = "
162: + processingInstruction.toString());
163: fStreamWriter.writeProcessingInstruction(
164: processingInstruction.getTarget(),
165: processingInstruction.getData());
166: break;
167: }
168: case XMLEvent.CHARACTERS: {
169: Characters characters = xMLEvent.asCharacters();
170: if (DEBUG)
171: System.out.println("Adding characters = "
172: + characters.toString());
173: //check if the CHARACTERS are CDATA
174: if (characters.isCData()) {
175: fStreamWriter.writeCData(characters.getData());
176: } else {
177: fStreamWriter.writeCharacters(characters.getData());
178: }
179: break;
180: }
181: case XMLEvent.ENTITY_REFERENCE: {
182: EntityReference entityReference = (EntityReference) xMLEvent;
183: if (DEBUG)
184: System.out.println("Adding Entity Reference = "
185: + entityReference.toString());
186: fStreamWriter.writeEntityRef(entityReference.getName());
187: break;
188: }
189: case XMLEvent.ATTRIBUTE: {
190: Attribute attribute = (Attribute) xMLEvent;
191: if (DEBUG)
192: System.out.println("Adding Attribute = "
193: + attribute.toString());
194: QName qname = attribute.getName();
195: fStreamWriter.writeAttribute(qname.getPrefix(), qname
196: .getNamespaceURI(), qname.getLocalPart(), attribute
197: .getValue());
198: break;
199: }
200: case XMLEvent.CDATA: {
201: //there is no separate CDATA datatype but CDATA event can be reported
202: //by using vendor specific CDATA property.
203: Characters characters = (Characters) xMLEvent;
204: if (DEBUG)
205: System.out.println("Adding characters = "
206: + characters.toString());
207: if (characters.isCData()) {
208: fStreamWriter.writeCData(characters.getData());
209: }
210: break;
211: }
212: //xxx: Why there isn't any event called Notation.
213: //case XMLEvent.NOTATION_DECLARATION:{
214: //}
215:
216: case XMLEvent.END_ELEMENT: {
217: //we dont need to typecast it.. just call writeEndElement() and fStreamWriter will take care of it.
218: //EndElement endElement = (EndElement)xMLEvent;
219: fStreamWriter.writeEndElement();
220: break;
221: }
222: case XMLEvent.END_DOCUMENT: {
223: //we dont need to typecast just call writeEndDocument() and fStreamWriter will take care rest.
224: //EndDocument endDocument = (EndDocument)xMLEvent;
225: fStreamWriter.writeEndDocument();
226: break;
227: }
228: //throw new XMLStreamException("Unknown Event type = " + type);
229: }
230: ;
231:
232: }
233:
234: /**
235: *
236: * @throws XMLStreamException
237: */
238: public void close() throws javax.xml.stream.XMLStreamException {
239: fStreamWriter.close();
240: }
241:
242: /**
243: *
244: * @throws XMLStreamException will inturn call flush on the stream to which data is being
245: * written.
246: */
247: public void flush() throws javax.xml.stream.XMLStreamException {
248: fStreamWriter.flush();
249: }
250:
251: /**
252: *
253: * @return
254: */
255: public javax.xml.namespace.NamespaceContext getNamespaceContext() {
256: return fStreamWriter.getNamespaceContext();
257: }
258:
259: /**
260: *
261: * @param namespaceURI Namespace URI
262: * @throws XMLStreamException
263: * @return prefix associated with the URI.
264: */
265: public String getPrefix(String namespaceURI)
266: throws javax.xml.stream.XMLStreamException {
267: return fStreamWriter.getPrefix(namespaceURI);
268: }
269:
270: /**
271: *
272: * @param uri Namespace URI
273: * @throws XMLStreamException
274: */
275: public void setDefaultNamespace(String uri)
276: throws javax.xml.stream.XMLStreamException {
277: fStreamWriter.setDefaultNamespace(uri);
278: }
279:
280: /**
281: *
282: * @param namespaceContext Namespace Context
283: * @throws XMLStreamException
284: */
285: public void setNamespaceContext(
286: javax.xml.namespace.NamespaceContext namespaceContext)
287: throws javax.xml.stream.XMLStreamException {
288: fStreamWriter.setNamespaceContext(namespaceContext);
289: }
290:
291: /**
292: *
293: * @param prefix namespace prefix associated with the uri.
294: * @param uri Namespace URI
295: * @throws XMLStreamException
296: */
297: public void setPrefix(String prefix, String uri)
298: throws javax.xml.stream.XMLStreamException {
299: fStreamWriter.setPrefix(prefix, uri);
300: }
301:
302: }//XMLEventWriterImpl
|