001: /*
002: * Copyright 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: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
026: */
027:
028: /*
029: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
030: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
031: *
032: * This code is free software; you can redistribute it and/or modify it
033: * under the terms of the GNU General Public License version 2 only, as
034: * published by the Free Software Foundation. Sun designates this
035: * particular file as subject to the "Classpath" exception as provided
036: * by Sun in the LICENSE file that accompanied this code.
037: *
038: * This code is distributed in the hope that it will be useful, but WITHOUT
039: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
040: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
041: * version 2 for more details (a copy is included in the LICENSE file that
042: * accompanied this code).
043: *
044: * You should have received a copy of the GNU General Public License version
045: * 2 along with this work; if not, write to the Free Software Foundation,
046: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
047: *
048: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
049: * CA 95054 USA or visit www.sun.com if you need additional information or
050: * have any questions.
051: *
052: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
053: *
054: */
055:
056: package com.sun.xml.internal.fastinfoset.stax.events;
057:
058: import java.util.Iterator;
059: import javax.xml.namespace.QName;
060: import javax.xml.namespace.NamespaceContext;
061: import javax.xml.stream.XMLEventWriter;
062: import javax.xml.stream.XMLEventReader;
063: import javax.xml.stream.XMLStreamWriter;
064: import javax.xml.stream.XMLStreamException;
065: import javax.xml.stream.events.*;
066: import com.sun.xml.internal.fastinfoset.stax.events.Util;
067: import com.sun.xml.internal.fastinfoset.CommonResourceBundle;
068:
069: public class StAXEventWriter implements XMLEventWriter {
070:
071: private XMLStreamWriter _streamWriter;
072:
073: /**
074: *
075: * @param streamWriter
076: */
077: public StAXEventWriter(XMLStreamWriter streamWriter) {
078: _streamWriter = streamWriter;
079: }
080:
081: /**
082: * Writes any cached events to the underlying output mechanism
083: * @throws XMLStreamException
084: */
085: public void flush() throws XMLStreamException {
086: _streamWriter.flush();
087: }
088:
089: /**
090: * Frees any resources associated with this stream
091: * @throws XMLStreamException
092: */
093: public void close() throws javax.xml.stream.XMLStreamException {
094: _streamWriter.close();
095: }
096:
097: /**
098: *
099: * @param eventReader
100: * @throws XMLStreamException
101: */
102: public void add(XMLEventReader eventReader)
103: throws XMLStreamException {
104: if (eventReader == null)
105: throw new XMLStreamException(CommonResourceBundle
106: .getInstance().getString("message.nullEventReader"));
107: while (eventReader.hasNext()) {
108: add(eventReader.nextEvent());
109: }
110: }
111:
112: /**
113: * Add an event to the output stream
114: * Adding a START_ELEMENT will open a new namespace scope that
115: * will be closed when the corresponding END_ELEMENT is written.
116: *
117: * @param event
118: * @throws XMLStreamException
119: */
120: public void add(XMLEvent event) throws XMLStreamException {
121: int type = event.getEventType();
122: switch (type) {
123: case XMLEvent.DTD: {
124: DTD dtd = (DTD) event;
125: _streamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
126: break;
127: }
128: case XMLEvent.START_DOCUMENT: {
129: StartDocument startDocument = (StartDocument) event;
130: _streamWriter.writeStartDocument(startDocument
131: .getCharacterEncodingScheme(), startDocument
132: .getVersion());
133: break;
134: }
135: case XMLEvent.START_ELEMENT: {
136: StartElement startElement = event.asStartElement();
137: QName qname = startElement.getName();
138: _streamWriter.writeStartElement(qname.getPrefix(), qname
139: .getLocalPart(), qname.getNamespaceURI());
140:
141: Iterator iterator = startElement.getNamespaces();
142: while (iterator.hasNext()) {
143: Namespace namespace = (Namespace) iterator.next();
144: _streamWriter.writeNamespace(namespace.getPrefix(),
145: namespace.getNamespaceURI());
146: }
147:
148: Iterator attributes = startElement.getAttributes();
149: while (attributes.hasNext()) {
150: Attribute attribute = (Attribute) attributes.next();
151: QName name = attribute.getName();
152: _streamWriter.writeAttribute(name.getPrefix(), name
153: .getNamespaceURI(), name.getLocalPart(),
154: attribute.getValue());
155: }
156: break;
157: }
158: case XMLEvent.NAMESPACE: {
159: Namespace namespace = (Namespace) event;
160: _streamWriter.writeNamespace(namespace.getPrefix(),
161: namespace.getNamespaceURI());
162: break;
163: }
164: case XMLEvent.COMMENT: {
165: Comment comment = (Comment) event;
166: _streamWriter.writeComment(comment.getText());
167: break;
168: }
169: case XMLEvent.PROCESSING_INSTRUCTION: {
170: ProcessingInstruction processingInstruction = (ProcessingInstruction) event;
171: _streamWriter.writeProcessingInstruction(
172: processingInstruction.getTarget(),
173: processingInstruction.getData());
174: break;
175: }
176: case XMLEvent.CHARACTERS: {
177: Characters characters = event.asCharacters();
178: //check if the CHARACTERS are CDATA
179: if (characters.isCData()) {
180: _streamWriter.writeCData(characters.getData());
181: } else {
182: _streamWriter.writeCharacters(characters.getData());
183: }
184: break;
185: }
186: case XMLEvent.ENTITY_REFERENCE: {
187: EntityReference entityReference = (EntityReference) event;
188: _streamWriter.writeEntityRef(entityReference.getName());
189: break;
190: }
191: case XMLEvent.ATTRIBUTE: {
192: Attribute attribute = (Attribute) event;
193: QName qname = attribute.getName();
194: _streamWriter.writeAttribute(qname.getPrefix(), qname
195: .getNamespaceURI(), qname.getLocalPart(), attribute
196: .getValue());
197: break;
198: }
199: case XMLEvent.CDATA: {
200: //there is no separate CDATA datatype but CDATA event can be reported
201: //by using vendor specific CDATA property.
202: Characters characters = (Characters) event;
203: if (characters.isCData()) {
204: _streamWriter.writeCData(characters.getData());
205: }
206: break;
207: }
208:
209: case XMLEvent.END_ELEMENT: {
210: _streamWriter.writeEndElement();
211: break;
212: }
213: case XMLEvent.END_DOCUMENT: {
214: _streamWriter.writeEndDocument();
215: break;
216: }
217: default:
218: throw new XMLStreamException(CommonResourceBundle
219: .getInstance().getString(
220: "message.eventTypeNotSupported",
221: new Object[] { Util
222: .getEventTypeString(type) }));
223: //throw new XMLStreamException("Unknown Event type = " + type);
224: }
225: ;
226:
227: }
228:
229: /**
230: * Gets the prefix the uri is bound to
231: * @param uri the uri to look up
232: * @throws XMLStreamException
233: */
234: public String getPrefix(String uri) throws XMLStreamException {
235: return _streamWriter.getPrefix(uri);
236: }
237:
238: /**
239: * Returns the current namespace context.
240: * @return the current namespace context
241: */
242: public NamespaceContext getNamespaceContext() {
243: return _streamWriter.getNamespaceContext();
244: }
245:
246: /**
247: * Binds a URI to the default namespace
248: * This URI is bound
249: * in the scope of the current START_ELEMENT / END_ELEMENT pair.
250: * If this method is called before a START_ELEMENT has been written
251: * the uri is bound in the root scope.
252: * @param uri the uri to bind to the default namespace
253: * @throws XMLStreamException
254: */
255: public void setDefaultNamespace(String uri)
256: throws XMLStreamException {
257: _streamWriter.setDefaultNamespace(uri);
258: }
259:
260: /**
261: * Sets the current namespace context for prefix and uri bindings.
262: * This context becomes the root namespace context for writing and
263: * will replace the current root namespace context. Subsequent calls
264: * to setPrefix and setDefaultNamespace will bind namespaces using
265: * the context passed to the method as the root context for resolving
266: * namespaces.
267: * @param namespaceContext the namespace context to use for this writer
268: * @throws XMLStreamException
269: */
270: public void setNamespaceContext(NamespaceContext namespaceContext)
271: throws XMLStreamException {
272: _streamWriter.setNamespaceContext(namespaceContext);
273: }
274:
275: /**
276: * Sets the prefix the uri is bound to. This prefix is bound
277: * in the scope of the current START_ELEMENT / END_ELEMENT pair.
278: * If this method is called before a START_ELEMENT has been written
279: * the prefix is bound in the root scope.
280: * @param prefix the prefix to bind to the uri
281: * @param uri the uri to bind to the prefix
282: * @throws XMLStreamException
283: */
284: public void setPrefix(String prefix, String uri)
285: throws XMLStreamException {
286: _streamWriter.setPrefix(prefix, uri);
287: }
288:
289: }
|