KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > xml > fastinfoset > stax > StAXEventWriter


1 /*
2  * Fast Infoset ver. 0.1 software ("Software")
3  *
4  * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Software is licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License. You may
8  * obtain a copy of the License at:
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15  * License for the specific language governing permissions and limitations.
16  *
17  * Sun supports and benefits from the global community of open source
18  * developers, and thanks the community for its important contributions and
19  * open standards-based technology, which Sun has adopted into many of its
20  * products.
21  *
22  * Please note that portions of Software may be provided with notices and
23  * open source licenses from such communities and third parties that govern the
24  * use of those portions, and any licenses granted hereunder do not alter any
25  * rights and obligations you may have under such open source licenses,
26  * however, the disclaimer of warranty and limitation of liability provisions
27  * in this License will apply to all Software in this distribution.
28  *
29  * You acknowledge that the Software is not designed, licensed or intended
30  * for use in the design, construction, operation or maintenance of any nuclear
31  * facility.
32  *
33  * Apache License
34  * Version 2.0, January 2004
35  * http://www.apache.org/licenses/
36  *
37  */

38
39 package com.sun.xml.fastinfoset.stax;
40
41 import java.util.Iterator JavaDoc;
42 import javax.xml.namespace.QName JavaDoc;
43 import javax.xml.namespace.NamespaceContext JavaDoc;
44 import javax.xml.stream.XMLStreamConstants;
45 import javax.xml.stream.XMLEventWriter;
46 import javax.xml.stream.XMLEventReader;
47 import javax.xml.stream.XMLStreamWriter;
48 import javax.xml.stream.XMLStreamException;
49 import javax.xml.stream.events.*;
50 import com.sun.xml.fastinfoset.util.Util;
51 import com.sun.xml.fastinfoset.CommonResourceBundle;
52
53 public class StAXEventWriter implements XMLEventWriter {
54     
55     private XMLStreamWriter _streamWriter ;
56     /**
57      *
58      * @param streamWriter
59      */

60     public StAXEventWriter(XMLStreamWriter streamWriter){
61         _streamWriter = streamWriter;
62     }
63
64     /**
65     * Writes any cached events to the underlying output mechanism
66     * @throws XMLStreamException
67     */

68     public void flush() throws XMLStreamException {
69         _streamWriter.flush();
70     }
71     /**
72     * Frees any resources associated with this stream
73     * @throws XMLStreamException
74     */

75     public void close() throws javax.xml.stream.XMLStreamException {
76         _streamWriter.close();
77     }
78     
79     /**
80      *
81      * @param eventReader
82      * @throws XMLStreamException
83      */

84     public void add(XMLEventReader eventReader) throws XMLStreamException {
85         if(eventReader == null) throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullEventReader"));
86         while(eventReader.hasNext()){
87             add(eventReader.nextEvent());
88         }
89     }
90     
91     /**
92     * Add an event to the output stream
93     * Adding a START_ELEMENT will open a new namespace scope that
94     * will be closed when the corresponding END_ELEMENT is written.
95     *
96     * @param event
97     * @throws XMLStreamException
98     */

99     public void add(XMLEvent event) throws XMLStreamException {
100         int type = event.getEventType();
101         switch(type){
102             case XMLEvent.DTD:{
103                 DTD dtd = (DTD)event ;
104                 _streamWriter.writeDTD(dtd.getDocumentTypeDeclaration());
105                 break;
106             }
107             case XMLEvent.START_DOCUMENT :{
108                 StartDocument startDocument = (StartDocument)event ;
109                 _streamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion());
110                 break;
111             }
112             case XMLEvent.START_ELEMENT :{
113                 StartElement startElement = event.asStartElement() ;
114                 QName JavaDoc qname = startElement.getName();
115                 _streamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
116                 
117                 Iterator JavaDoc iterator = startElement.getNamespaces();
118                 while(iterator.hasNext()){
119                     Namespace namespace = (Namespace)iterator.next();
120                     _streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
121                 }
122
123                 Iterator JavaDoc attributes = startElement.getAttributes();
124                 while(attributes.hasNext()){
125                     Attribute attribute = (Attribute)attributes.next();
126                     QName JavaDoc name = attribute.getName();
127                     _streamWriter.writeAttribute(name.getPrefix(), name.getNamespaceURI(),
128                                                 name.getLocalPart(),attribute.getValue());
129                 }
130                 break;
131             }
132             case XMLEvent.NAMESPACE:{
133                 Namespace namespace = (Namespace)event;
134                 _streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI());
135                 break ;
136             }
137             case XMLEvent.COMMENT: {
138                 Comment comment = (Comment)event ;
139                 _streamWriter.writeComment(comment.getText());
140                 break;
141             }
142             case XMLEvent.PROCESSING_INSTRUCTION:{
143                 ProcessingInstruction processingInstruction = (ProcessingInstruction)event ;
144                 _streamWriter.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData());
145                 break;
146             }
147             case XMLEvent.CHARACTERS:{
148                 Characters characters = event.asCharacters();
149                 //check if the CHARACTERS are CDATA
150
if(characters.isCData()){
151                     _streamWriter.writeCData(characters.getData());
152                 }
153                 else{
154                     _streamWriter.writeCharacters(characters.getData());
155                 }
156                 break;
157             }
158             case XMLEvent.ENTITY_REFERENCE:{
159                 EntityReference entityReference = (EntityReference)event ;
160                 _streamWriter.writeEntityRef(entityReference.getName());
161                 break;
162             }
163             case XMLEvent.ATTRIBUTE:{
164                 Attribute attribute = (Attribute)event;
165                 QName JavaDoc qname = attribute.getName();
166                 _streamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(),attribute.getValue());
167                 break;
168             }
169             case XMLEvent.CDATA:{
170                 //there is no separate CDATA datatype but CDATA event can be reported
171
//by using vendor specific CDATA property.
172
Characters characters = (Characters)event;
173                 if(characters.isCData()){
174                     _streamWriter.writeCData(characters.getData());
175                 }
176                 break;
177             }
178             
179             case XMLEvent.END_ELEMENT:{
180                 _streamWriter.writeEndElement();
181                 break;
182             }
183             case XMLEvent.END_DOCUMENT:{
184                 _streamWriter.writeEndDocument();
185                 break;
186             }
187             default:
188                 throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.eventTypeNotSupported", new Object JavaDoc[]{Util.getEventTypeString(type)}));
189             //throw new XMLStreamException("Unknown Event type = " + type);
190
};
191         
192     }
193     
194     /**
195     * Gets the prefix the uri is bound to
196     * @param uri the uri to look up
197     * @throws XMLStreamException
198     */

199     public String JavaDoc getPrefix(String JavaDoc uri) throws XMLStreamException {
200         return _streamWriter.getPrefix(uri);
201     }
202     
203     
204     /**
205     * Returns the current namespace context.
206     * @return the current namespace context
207     */

208     public NamespaceContext JavaDoc getNamespaceContext() {
209         return _streamWriter.getNamespaceContext();
210     }
211     
212     
213     /**
214     * Binds a URI to the default namespace
215     * This URI is bound
216     * in the scope of the current START_ELEMENT / END_ELEMENT pair.
217     * If this method is called before a START_ELEMENT has been written
218     * the uri is bound in the root scope.
219     * @param uri the uri to bind to the default namespace
220     * @throws XMLStreamException
221     */

222     public void setDefaultNamespace(String JavaDoc uri) throws XMLStreamException {
223         _streamWriter.setDefaultNamespace(uri);
224     }
225     
226     /**
227     * Sets the current namespace context for prefix and uri bindings.
228     * This context becomes the root namespace context for writing and
229     * will replace the current root namespace context. Subsequent calls
230     * to setPrefix and setDefaultNamespace will bind namespaces using
231     * the context passed to the method as the root context for resolving
232     * namespaces.
233     * @param context the namespace context to use for this writer
234     * @throws XMLStreamException
235     */

236     public void setNamespaceContext(NamespaceContext JavaDoc namespaceContext) throws XMLStreamException {
237         _streamWriter.setNamespaceContext(namespaceContext);
238     }
239     /**
240     * Sets the prefix the uri is bound to. This prefix is bound
241     * in the scope of the current START_ELEMENT / END_ELEMENT pair.
242     * If this method is called before a START_ELEMENT has been written
243     * the prefix is bound in the root scope.
244     * @param prefix the prefix to bind to the uri
245     * @param uri the uri to bind to the prefix
246     * @throws XMLStreamException
247     */

248     public void setPrefix(String JavaDoc prefix, String JavaDoc uri) throws XMLStreamException {
249         _streamWriter.setPrefix(prefix, uri);
250     }
251         
252 }
253
Popular Tags