KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xml > stream > XMLEventWriterImpl


1 /*
2 * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3 *
4 * This file is part of Resin(R) Open Source
5 *
6 * Each copy or derived work must preserve the copyright notice and this
7 * notice unmodified.
8 *
9 * Resin Open Source is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * Resin Open Source is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17 * of NON-INFRINGEMENT. See the GNU General Public License for more
18 * details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with Resin Open Source; if not, write to the
22 *
23 * Free Software Foundation, Inc.
24 * 59 Temple Place, Suite 330
25 * Boston, MA 02111-1307 USA
26 *
27 * @author Emil Ong
28 */

29
30 package com.caucho.xml.stream;
31
32 import javax.xml.namespace.NamespaceContext JavaDoc;
33 import javax.xml.namespace.QName JavaDoc;
34 import javax.xml.stream.XMLEventReader;
35 import javax.xml.stream.XMLEventWriter;
36 import javax.xml.stream.XMLStreamConstants;
37 import javax.xml.stream.XMLStreamException;
38 import javax.xml.stream.XMLStreamWriter;
39 import javax.xml.stream.events.*;
40
41 public class XMLEventWriterImpl implements XMLEventWriter, XMLStreamConstants {
42   private XMLStreamWriter _out;
43
44   public XMLEventWriterImpl(XMLStreamWriter out)
45   {
46     _out = out;
47   }
48
49   public void add(XMLEvent event) throws XMLStreamException
50   {
51     // Order important: Namespace extends Attribute, so it comes first
52
if (event instanceof Namespace) {
53       Namespace namespace = (Namespace) event;
54       
55       if (namespace.isDefaultNamespaceDeclaration())
56         _out.writeDefaultNamespace(namespace.getNamespaceURI());
57       else
58         _out.writeNamespace(namespace.getPrefix(),
59                             namespace.getNamespaceURI());
60
61     }
62     else if (event instanceof Attribute) {
63       Attribute attribute = (Attribute) event;
64       QName JavaDoc name = attribute.getName();
65
66       if (name.getPrefix() != null && ! "".equals(name.getPrefix())) {
67         _out.writeAttribute(name.getPrefix(), name.getNamespaceURI(),
68                             name.getLocalPart(), attribute.getValue());
69       }
70       else if (name.getNamespaceURI() != null &&
71                ! "".equals(name.getNamespaceURI())) {
72         _out.writeAttribute(name.getNamespaceURI(), name.getLocalPart(),
73                             attribute.getValue());
74       }
75       else
76         _out.writeAttribute(name.getLocalPart(), attribute.getValue());
77     }
78     else if (event instanceof Characters) {
79       Characters characters = (Characters) event;
80
81       switch (characters.getEventType()) {
82         case CDATA:
83           _out.writeCData(characters.getData());
84           break;
85
86         case SPACE:
87         case CHARACTERS:
88         default:
89           _out.writeCharacters(characters.getData());
90           break;
91       }
92     }
93     else if (event instanceof Comment) {
94       Comment comment = (Comment) event;
95
96       _out.writeComment(comment.getText());
97     }
98     else if (event instanceof DTD) {
99       DTD dtd = (DTD) event;
100
101       _out.writeDTD(dtd.getDocumentTypeDeclaration());
102     }
103     else if (event instanceof EndDocument) {
104       _out.writeEndDocument();
105     }
106     else if (event instanceof EndElement) {
107       _out.writeEndElement();
108     }
109     else if (event instanceof EntityDeclaration) {
110       throw new UnsupportedOperationException JavaDoc();
111     }
112     else if (event instanceof EntityReference) {
113       throw new UnsupportedOperationException JavaDoc();
114     }
115     else if (event instanceof NotationDeclaration) {
116       throw new UnsupportedOperationException JavaDoc();
117     }
118     else if (event instanceof ProcessingInstruction) {
119       ProcessingInstruction pi = (ProcessingInstruction) event;
120
121       if (pi.getData() == null || "".equals(pi.getData()))
122         _out.writeProcessingInstruction(pi.getTarget());
123       else
124         _out.writeProcessingInstruction(pi.getTarget(), pi.getData());
125
126     }
127     else if (event instanceof StartDocument) {
128       StartDocument startDocument = (StartDocument) event;
129
130       if (startDocument.encodingSet()) {
131         _out.writeStartDocument(startDocument.getCharacterEncodingScheme(),
132                                 startDocument.getVersion());
133       }
134       else if (startDocument.getVersion() != null &&
135                ! "".equals(startDocument.getVersion())) {
136         _out.writeStartDocument(startDocument.getVersion());
137       }
138       else
139         _out.writeStartDocument();
140     }
141     else if (event instanceof StartElement) {
142       StartElement startElement = (StartElement) event;
143       QName JavaDoc name = startElement.getName();
144
145       if (name.getPrefix() != null && ! "".equals(name.getPrefix())) {
146         _out.writeStartElement(name.getPrefix(), name.getNamespaceURI(),
147                                name.getLocalPart());
148       }
149       else if (name.getNamespaceURI() != null &&
150                ! "".equals(name.getNamespaceURI())) {
151         _out.writeStartElement(name.getNamespaceURI(), name.getLocalPart());
152       }
153       else
154         _out.writeStartElement(name.getLocalPart());
155     }
156     else
157       throw new XMLStreamException();
158   }
159
160   public void add(XMLEventReader reader) throws XMLStreamException
161   {
162     while (reader.hasNext())
163       add(reader.nextEvent());
164   }
165
166   public void close() throws XMLStreamException
167   {
168     _out.close();
169   }
170
171   public void flush() throws XMLStreamException
172   {
173     _out.flush();
174   }
175
176   public NamespaceContext JavaDoc getNamespaceContext()
177   {
178     return _out.getNamespaceContext();
179   }
180
181   public String JavaDoc getPrefix(String JavaDoc uri) throws XMLStreamException
182   {
183     return _out.getPrefix(uri);
184   }
185
186   public void setDefaultNamespace(String JavaDoc uri) throws XMLStreamException
187   {
188     _out.setDefaultNamespace(uri);
189   }
190
191   public void setNamespaceContext(NamespaceContext JavaDoc context)
192     throws XMLStreamException
193   {
194     _out.setNamespaceContext(context);
195   }
196
197   public void setPrefix(String JavaDoc prefix, String JavaDoc uri) throws XMLStreamException
198   {
199     _out.setPrefix(prefix, uri);
200   }
201
202 }
203
Popular Tags