001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.xml.stream;
031:
032: import javax.xml.namespace.NamespaceContext;
033: import javax.xml.namespace.QName;
034: import javax.xml.stream.XMLEventReader;
035: import javax.xml.stream.XMLEventWriter;
036: import static javax.xml.stream.XMLStreamConstants.*;
037: import javax.xml.stream.XMLStreamException;
038: import javax.xml.stream.XMLStreamWriter;
039: import javax.xml.stream.events.*;
040:
041: import java.util.Iterator;
042:
043: public class XMLEventWriterImpl implements XMLEventWriter {
044: private XMLStreamWriter _out;
045:
046: public XMLEventWriterImpl(XMLStreamWriter out) {
047: _out = out;
048: }
049:
050: public XMLStreamWriter getXMLStreamWriter() {
051: return _out;
052: }
053:
054: public void add(XMLEvent event) throws XMLStreamException {
055: // Order important: Namespace extends Attribute, so it comes first
056: if (event instanceof Namespace) {
057: Namespace namespace = (Namespace) event;
058:
059: if (namespace.isDefaultNamespaceDeclaration())
060: _out.writeDefaultNamespace(namespace.getNamespaceURI());
061: else
062: _out.writeNamespace(namespace.getPrefix(), namespace
063: .getNamespaceURI());
064:
065: } else if (event instanceof Attribute) {
066: Attribute attribute = (Attribute) event;
067: QName name = attribute.getName();
068:
069: if (name.getPrefix() != null
070: && !"".equals(name.getPrefix())) {
071: _out.writeAttribute(name.getPrefix(), name
072: .getNamespaceURI(), name.getLocalPart(),
073: attribute.getValue());
074: } else if (name.getNamespaceURI() != null
075: && !"".equals(name.getNamespaceURI())) {
076: _out.writeAttribute(name.getNamespaceURI(), name
077: .getLocalPart(), attribute.getValue());
078: } else
079: _out.writeAttribute(name.getLocalPart(), attribute
080: .getValue());
081: } else if (event instanceof Characters) {
082: Characters characters = (Characters) event;
083:
084: switch (characters.getEventType()) {
085: case CDATA:
086: _out.writeCData(characters.getData());
087: break;
088:
089: case SPACE:
090: case CHARACTERS:
091: default:
092: _out.writeCharacters(characters.getData());
093: break;
094: }
095: } else if (event instanceof Comment) {
096: Comment comment = (Comment) event;
097:
098: _out.writeComment(comment.getText());
099: } else if (event instanceof DTD) {
100: DTD dtd = (DTD) event;
101:
102: _out.writeDTD(dtd.getDocumentTypeDeclaration());
103: } else if (event instanceof EndDocument) {
104: _out.writeEndDocument();
105: } else if (event instanceof EndElement) {
106: _out.writeEndElement();
107: } else if (event instanceof EntityDeclaration) {
108: throw new UnsupportedOperationException();
109: } else if (event instanceof EntityReference) {
110: throw new UnsupportedOperationException();
111: } else if (event instanceof NotationDeclaration) {
112: throw new UnsupportedOperationException();
113: } else if (event instanceof ProcessingInstruction) {
114: ProcessingInstruction pi = (ProcessingInstruction) event;
115:
116: if (pi.getData() == null || "".equals(pi.getData()))
117: _out.writeProcessingInstruction(pi.getTarget());
118: else
119: _out.writeProcessingInstruction(pi.getTarget(), pi
120: .getData());
121:
122: } else if (event instanceof StartDocument) {
123: StartDocument startDocument = (StartDocument) event;
124:
125: if (startDocument.encodingSet()) {
126: _out.writeStartDocument(startDocument
127: .getCharacterEncodingScheme(), startDocument
128: .getVersion());
129: } else if (startDocument.getVersion() != null
130: && !"".equals(startDocument.getVersion())) {
131: _out.writeStartDocument(startDocument.getVersion());
132: } else
133: _out.writeStartDocument();
134: } else if (event instanceof StartElement) {
135: StartElement startElement = (StartElement) event;
136: QName name = startElement.getName();
137:
138: // Namespaces
139: // We do namespaces first because the element itself may need one
140: // xml/300w should catch this
141: Iterator namespaces = startElement.getNamespaces();
142:
143: while (namespaces.hasNext())
144: add((Namespace) namespaces.next());
145:
146: if (name.getPrefix() != null
147: && !"".equals(name.getPrefix())) {
148: _out.writeStartElement(name.getPrefix(), name
149: .getLocalPart(), name.getNamespaceURI());
150: } else if (name.getNamespaceURI() != null
151: && !"".equals(name.getNamespaceURI())) {
152: _out.writeStartElement(name.getNamespaceURI(), name
153: .getLocalPart());
154: } else
155: _out.writeStartElement(name.getLocalPart());
156:
157: // Attributes
158: Iterator attributes = startElement.getAttributes();
159:
160: while (attributes.hasNext())
161: add((Attribute) attributes.next());
162: } else
163: throw new XMLStreamException();
164: }
165:
166: public void add(XMLEventReader reader) throws XMLStreamException {
167: while (reader.hasNext())
168: add(reader.nextEvent());
169: }
170:
171: public void close() throws XMLStreamException {
172: _out.close();
173: }
174:
175: public void flush() throws XMLStreamException {
176: _out.flush();
177: }
178:
179: public NamespaceContext getNamespaceContext() {
180: return _out.getNamespaceContext();
181: }
182:
183: public String getPrefix(String uri) throws XMLStreamException {
184: return _out.getPrefix(uri);
185: }
186:
187: public void setDefaultNamespace(String uri)
188: throws XMLStreamException {
189: _out.setDefaultNamespace(uri);
190: }
191:
192: public void setNamespaceContext(NamespaceContext context)
193: throws XMLStreamException {
194: _out.setNamespaceContext(context);
195: }
196:
197: public void setPrefix(String prefix, String uri)
198: throws XMLStreamException {
199: _out.setPrefix(prefix, uri);
200: }
201:
202: }
|