001: /*
002: * $Id: XMLEventWriterImpl.java,v 1.3 2006/05/12 13:30:52 sunithareddy Exp $
003: */
004:
005: /*
006: * The contents of this file are subject to the terms
007: * of the Common Development and Distribution License
008: * (the License). You may not use this file except in
009: * compliance with the License.
010: *
011: * You can obtain a copy of the license at
012: * https://glassfish.dev.java.net/public/CDDLv1.0.html.
013: * See the License for the specific language governing
014: * permissions and limitations under the License.
015: *
016: * When distributing Covered Code, include this CDDL
017: * Header Notice in each file and include the License file
018: * at https://glassfish.dev.java.net/public/CDDLv1.0.html.
019: * If applicable, add the following below the CDDL Header,
020: * with the fields enclosed by brackets [] replaced by
021: * you own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * [Name of File] [ver.__] [Date]
025: *
026: * Copyright 2006 Sun Microsystems Inc. All Rights Reserved
027: */
028:
029: package com.sun.xml.stream.writers;
030:
031: import java.util.Iterator;
032: import javax.xml.namespace.QName;
033: import javax.xml.stream.XMLEventWriter;
034: import javax.xml.stream.XMLStreamException;
035: import javax.xml.stream.events.Attribute;
036: import javax.xml.stream.events.Characters;
037: import javax.xml.stream.events.Comment;
038: import javax.xml.stream.events.DTD;
039: import javax.xml.stream.events.EntityReference;
040: import javax.xml.stream.events.Namespace;
041: import javax.xml.stream.events.ProcessingInstruction;
042: import javax.xml.stream.events.StartDocument;
043: import javax.xml.stream.events.StartElement;
044: import javax.xml.stream.events.XMLEvent;
045: import javax.xml.stream.XMLStreamWriter;
046:
047: /**
048: *
049: * @author Neeraj Bajaj, Sun Microsystems.
050: *
051: */
052: public class XMLEventWriterImpl implements XMLEventWriter {
053:
054: //delegate everything to XMLStreamWriter..
055: private XMLStreamWriter fStreamWriter;
056: private static final boolean DEBUG = false;
057:
058: /**
059: *
060: * @param streamWriter
061: */
062: public XMLEventWriterImpl(XMLStreamWriter streamWriter) {
063: fStreamWriter = streamWriter;
064: }
065:
066: /**
067: *
068: * @param xMLEventReader
069: * @throws XMLStreamException
070: */
071: public void add(javax.xml.stream.XMLEventReader xMLEventReader)
072: throws javax.xml.stream.XMLStreamException {
073: if (xMLEventReader == null)
074: throw new XMLStreamException(
075: "Event reader shouldn't be null");
076: while (xMLEventReader.hasNext()) {
077: add(xMLEventReader.nextEvent());
078: }
079: }
080:
081: /**
082: *
083: * @param xMLEvent
084: * @throws XMLStreamException
085: */
086: public void add(javax.xml.stream.events.XMLEvent xMLEvent)
087: throws javax.xml.stream.XMLStreamException {
088: int type = xMLEvent.getEventType();
089: switch (type) {
090: case XMLEvent.DTD: {
091: DTD dtd = (DTD) xMLEvent;
092: if (DEBUG)
093: System.out.println("Adding DTD = " + dtd.toString());
094: fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
095: break;
096: }
097: case XMLEvent.START_DOCUMENT: {
098: StartDocument startDocument = (StartDocument) xMLEvent;
099: if (DEBUG)
100: System.out.println("Adding StartDocument = "
101: + startDocument.toString());
102: try {
103: fStreamWriter.writeStartDocument(startDocument
104: .getCharacterEncodingScheme(), startDocument
105: .getVersion());
106: } catch (XMLStreamException e) {
107: fStreamWriter.writeStartDocument(startDocument
108: .getVersion());
109: }
110: break;
111: }
112: case XMLEvent.START_ELEMENT: {
113: StartElement startElement = xMLEvent.asStartElement();
114: if (DEBUG)
115: System.out.println("Adding startelement = "
116: + startElement.toString());
117: QName qname = startElement.getName();
118: fStreamWriter.writeStartElement(qname.getPrefix(), qname
119: .getLocalPart(), qname.getNamespaceURI());
120:
121: //getNamespaces() Returns an Iterator of namespaces declared on this element. This Iterator does not contain
122: //previously declared namespaces unless they appear on the current START_ELEMENT. Therefore
123: //this list may contain redeclared namespaces and duplicate namespace declarations. Use the
124: //getNamespaceContext() method to get the current context of namespace declarations.
125:
126: //so we should be using getNamespaces() to write namespace declarations for this START_ELEMENT
127: Iterator iterator = startElement.getNamespaces();
128: while (iterator.hasNext()) {
129: Namespace namespace = (Namespace) iterator.next();
130: fStreamWriter.writeNamespace(namespace.getPrefix(),
131: namespace.getNamespaceURI());
132: }
133: //REVISIT: What about writing attributes ?
134: Iterator attributes = startElement.getAttributes();
135: while (attributes.hasNext()) {
136: Attribute attribute = (Attribute) attributes.next();
137: QName aqname = attribute.getName();
138: fStreamWriter.writeAttribute(aqname.getPrefix(), aqname
139: .getNamespaceURI(), aqname.getLocalPart(),
140: attribute.getValue());
141: }
142: break;
143: }
144: case XMLEvent.NAMESPACE: {
145: Namespace namespace = (Namespace) xMLEvent;
146: if (DEBUG)
147: System.out.println("Adding namespace = "
148: + namespace.toString());
149: fStreamWriter.writeNamespace(namespace.getPrefix(),
150: namespace.getNamespaceURI());
151: break;
152: }
153: case XMLEvent.COMMENT: {
154: Comment comment = (Comment) xMLEvent;
155: if (DEBUG)
156: System.out.println("Adding comment = "
157: + comment.toString());
158: fStreamWriter.writeComment(comment.getText());
159: break;
160: }
161: case XMLEvent.PROCESSING_INSTRUCTION: {
162: ProcessingInstruction processingInstruction = (ProcessingInstruction) xMLEvent;
163: if (DEBUG)
164: System.out.println("Adding processing instruction = "
165: + processingInstruction.toString());
166: fStreamWriter.writeProcessingInstruction(
167: processingInstruction.getTarget(),
168: processingInstruction.getData());
169: break;
170: }
171: case XMLEvent.CHARACTERS: {
172: Characters characters = xMLEvent.asCharacters();
173: if (DEBUG)
174: System.out.println("Adding characters = "
175: + characters.toString());
176: //check if the CHARACTERS are CDATA
177: if (characters.isCData()) {
178: fStreamWriter.writeCData(characters.getData());
179: } else {
180: fStreamWriter.writeCharacters(characters.getData());
181: }
182: break;
183: }
184: case XMLEvent.ENTITY_REFERENCE: {
185: EntityReference entityReference = (EntityReference) xMLEvent;
186: if (DEBUG)
187: System.out.println("Adding Entity Reference = "
188: + entityReference.toString());
189: fStreamWriter.writeEntityRef(entityReference.getName());
190: break;
191: }
192: case XMLEvent.ATTRIBUTE: {
193: Attribute attribute = (Attribute) xMLEvent;
194: if (DEBUG)
195: System.out.println("Adding Attribute = "
196: + attribute.toString());
197: QName qname = attribute.getName();
198: fStreamWriter.writeAttribute(qname.getPrefix(), qname
199: .getNamespaceURI(), qname.getLocalPart(), attribute
200: .getValue());
201: break;
202: }
203: case XMLEvent.CDATA: {
204: //there is no separate CDATA datatype but CDATA event can be reported
205: //by using vendor specific CDATA property.
206: Characters characters = (Characters) xMLEvent;
207: if (DEBUG)
208: System.out.println("Adding characters = "
209: + characters.toString());
210: if (characters.isCData()) {
211: fStreamWriter.writeCData(characters.getData());
212: }
213: break;
214: }
215: //xxx: Why there isn't any event called Notation.
216: //case XMLEvent.NOTATION_DECLARATION:{
217: //}
218:
219: case XMLEvent.END_ELEMENT: {
220: //we dont need to typecast it.. just call writeEndElement() and fStreamWriter will take care of it.
221: //EndElement endElement = (EndElement)xMLEvent;
222: fStreamWriter.writeEndElement();
223: break;
224: }
225: case XMLEvent.END_DOCUMENT: {
226: //we dont need to typecast just call writeEndDocument() and fStreamWriter will take care rest.
227: //EndDocument endDocument = (EndDocument)xMLEvent;
228: fStreamWriter.writeEndDocument();
229: break;
230: }
231: //throw new XMLStreamException("Unknown Event type = " + type);
232: }
233: ;
234:
235: }
236:
237: /**
238: *
239: * @throws XMLStreamException
240: */
241: public void close() throws javax.xml.stream.XMLStreamException {
242: fStreamWriter.close();
243: }
244:
245: /**
246: *
247: * @throws XMLStreamException will inturn call flush on the stream to which data is being
248: * written.
249: */
250: public void flush() throws javax.xml.stream.XMLStreamException {
251: fStreamWriter.flush();
252: }
253:
254: /**
255: *
256: * @return
257: */
258: public javax.xml.namespace.NamespaceContext getNamespaceContext() {
259: return fStreamWriter.getNamespaceContext();
260: }
261:
262: /**
263: *
264: * @param namespaceURI Namespace URI
265: * @throws XMLStreamException
266: * @return prefix associated with the URI.
267: */
268: public String getPrefix(String namespaceURI)
269: throws javax.xml.stream.XMLStreamException {
270: return fStreamWriter.getPrefix(namespaceURI);
271: }
272:
273: /**
274: *
275: * @param uri Namespace URI
276: * @throws XMLStreamException
277: */
278: public void setDefaultNamespace(String uri)
279: throws javax.xml.stream.XMLStreamException {
280: fStreamWriter.setDefaultNamespace(uri);
281: }
282:
283: /**
284: *
285: * @param namespaceContext Namespace Context
286: * @throws XMLStreamException
287: */
288: public void setNamespaceContext(
289: javax.xml.namespace.NamespaceContext namespaceContext)
290: throws javax.xml.stream.XMLStreamException {
291: fStreamWriter.setNamespaceContext(namespaceContext);
292: }
293:
294: /**
295: *
296: * @param prefix namespace prefix associated with the uri.
297: * @param uri Namespace URI
298: * @throws XMLStreamException
299: */
300: public void setPrefix(String prefix, String uri)
301: throws javax.xml.stream.XMLStreamException {
302: fStreamWriter.setPrefix(prefix, uri);
303: }
304:
305: }//XMLEventWriterImpl
|