author | mkos |
Sun, 30 Dec 2012 00:00:00 +0100 | |
changeset 22678 | ac1ea46be942 |
parent 12009 | 4abb694f273a |
permissions | -rw-r--r-- |
12009 | 1 |
/* |
22678
ac1ea46be942
8029237: Update copyright year to match last edit in jaxws repository for 2012
mkos
parents:
12009
diff
changeset
|
2 |
* Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. |
12009 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
24 |
* |
|
25 |
* THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. |
|
26 |
*/ |
|
27 |
||
28 |
package com.sun.xml.internal.fastinfoset.stax.events; |
|
29 |
||
30 |
import java.util.Iterator; |
|
31 |
import javax.xml.namespace.QName; |
|
32 |
import javax.xml.namespace.NamespaceContext; |
|
33 |
import javax.xml.stream.XMLEventWriter; |
|
34 |
import javax.xml.stream.XMLEventReader; |
|
35 |
import javax.xml.stream.XMLStreamWriter; |
|
36 |
import javax.xml.stream.XMLStreamException; |
|
37 |
import javax.xml.stream.events.*; |
|
38 |
import com.sun.xml.internal.fastinfoset.CommonResourceBundle; |
|
39 |
||
40 |
public class StAXEventWriter implements XMLEventWriter { |
|
41 |
||
42 |
private XMLStreamWriter _streamWriter ; |
|
43 |
/** |
|
44 |
* |
|
45 |
* @param streamWriter |
|
46 |
*/ |
|
47 |
public StAXEventWriter(XMLStreamWriter streamWriter){ |
|
48 |
_streamWriter = streamWriter; |
|
49 |
} |
|
50 |
||
51 |
/** |
|
52 |
* Writes any cached events to the underlying output mechanism |
|
53 |
* @throws XMLStreamException |
|
54 |
*/ |
|
55 |
public void flush() throws XMLStreamException { |
|
56 |
_streamWriter.flush(); |
|
57 |
} |
|
58 |
/** |
|
59 |
* Frees any resources associated with this stream |
|
60 |
* @throws XMLStreamException |
|
61 |
*/ |
|
62 |
public void close() throws javax.xml.stream.XMLStreamException { |
|
63 |
_streamWriter.close(); |
|
64 |
} |
|
65 |
||
66 |
/** |
|
67 |
* |
|
68 |
* @param eventReader |
|
69 |
* @throws XMLStreamException |
|
70 |
*/ |
|
71 |
public void add(XMLEventReader eventReader) throws XMLStreamException { |
|
72 |
if(eventReader == null) throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.nullEventReader")); |
|
73 |
while(eventReader.hasNext()){ |
|
74 |
add(eventReader.nextEvent()); |
|
75 |
} |
|
76 |
} |
|
77 |
||
78 |
/** |
|
79 |
* Add an event to the output stream |
|
80 |
* Adding a START_ELEMENT will open a new namespace scope that |
|
81 |
* will be closed when the corresponding END_ELEMENT is written. |
|
82 |
* |
|
83 |
* @param event |
|
84 |
* @throws XMLStreamException |
|
85 |
*/ |
|
86 |
public void add(XMLEvent event) throws XMLStreamException { |
|
87 |
int type = event.getEventType(); |
|
88 |
switch(type){ |
|
89 |
case XMLEvent.DTD:{ |
|
90 |
DTD dtd = (DTD)event ; |
|
91 |
_streamWriter.writeDTD(dtd.getDocumentTypeDeclaration()); |
|
92 |
break; |
|
93 |
} |
|
94 |
case XMLEvent.START_DOCUMENT :{ |
|
95 |
StartDocument startDocument = (StartDocument)event ; |
|
96 |
_streamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), startDocument.getVersion()); |
|
97 |
break; |
|
98 |
} |
|
99 |
case XMLEvent.START_ELEMENT :{ |
|
100 |
StartElement startElement = event.asStartElement() ; |
|
101 |
QName qname = startElement.getName(); |
|
102 |
_streamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI()); |
|
103 |
||
104 |
Iterator iterator = startElement.getNamespaces(); |
|
105 |
while(iterator.hasNext()){ |
|
106 |
Namespace namespace = (Namespace)iterator.next(); |
|
107 |
_streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
|
108 |
} |
|
109 |
||
110 |
Iterator attributes = startElement.getAttributes(); |
|
111 |
while(attributes.hasNext()){ |
|
112 |
Attribute attribute = (Attribute)attributes.next(); |
|
113 |
QName name = attribute.getName(); |
|
114 |
_streamWriter.writeAttribute(name.getPrefix(), name.getNamespaceURI(), |
|
115 |
name.getLocalPart(),attribute.getValue()); |
|
116 |
} |
|
117 |
break; |
|
118 |
} |
|
119 |
case XMLEvent.NAMESPACE:{ |
|
120 |
Namespace namespace = (Namespace)event; |
|
121 |
_streamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
|
122 |
break ; |
|
123 |
} |
|
124 |
case XMLEvent.COMMENT: { |
|
125 |
Comment comment = (Comment)event ; |
|
126 |
_streamWriter.writeComment(comment.getText()); |
|
127 |
break; |
|
128 |
} |
|
129 |
case XMLEvent.PROCESSING_INSTRUCTION:{ |
|
130 |
ProcessingInstruction processingInstruction = (ProcessingInstruction)event ; |
|
131 |
_streamWriter.writeProcessingInstruction(processingInstruction.getTarget(), processingInstruction.getData()); |
|
132 |
break; |
|
133 |
} |
|
134 |
case XMLEvent.CHARACTERS:{ |
|
135 |
Characters characters = event.asCharacters(); |
|
136 |
//check if the CHARACTERS are CDATA |
|
137 |
if(characters.isCData()){ |
|
138 |
_streamWriter.writeCData(characters.getData()); |
|
139 |
} |
|
140 |
else{ |
|
141 |
_streamWriter.writeCharacters(characters.getData()); |
|
142 |
} |
|
143 |
break; |
|
144 |
} |
|
145 |
case XMLEvent.ENTITY_REFERENCE:{ |
|
146 |
EntityReference entityReference = (EntityReference)event ; |
|
147 |
_streamWriter.writeEntityRef(entityReference.getName()); |
|
148 |
break; |
|
149 |
} |
|
150 |
case XMLEvent.ATTRIBUTE:{ |
|
151 |
Attribute attribute = (Attribute)event; |
|
152 |
QName qname = attribute.getName(); |
|
153 |
_streamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), qname.getLocalPart(),attribute.getValue()); |
|
154 |
break; |
|
155 |
} |
|
156 |
case XMLEvent.CDATA:{ |
|
157 |
//there is no separate CDATA datatype but CDATA event can be reported |
|
158 |
//by using vendor specific CDATA property. |
|
159 |
Characters characters = (Characters)event; |
|
160 |
if(characters.isCData()){ |
|
161 |
_streamWriter.writeCData(characters.getData()); |
|
162 |
} |
|
163 |
break; |
|
164 |
} |
|
165 |
||
166 |
case XMLEvent.END_ELEMENT:{ |
|
167 |
_streamWriter.writeEndElement(); |
|
168 |
break; |
|
169 |
} |
|
170 |
case XMLEvent.END_DOCUMENT:{ |
|
171 |
_streamWriter.writeEndDocument(); |
|
172 |
break; |
|
173 |
} |
|
174 |
default: |
|
175 |
throw new XMLStreamException(CommonResourceBundle.getInstance().getString("message.eventTypeNotSupported", new Object[]{Util.getEventTypeString(type)})); |
|
176 |
//throw new XMLStreamException("Unknown Event type = " + type); |
|
177 |
}; |
|
178 |
||
179 |
} |
|
180 |
||
181 |
/** |
|
182 |
* Gets the prefix the uri is bound to |
|
183 |
* @param uri the uri to look up |
|
184 |
* @throws XMLStreamException |
|
185 |
*/ |
|
186 |
public String getPrefix(String uri) throws XMLStreamException { |
|
187 |
return _streamWriter.getPrefix(uri); |
|
188 |
} |
|
189 |
||
190 |
||
191 |
/** |
|
192 |
* Returns the current namespace context. |
|
193 |
* @return the current namespace context |
|
194 |
*/ |
|
195 |
public NamespaceContext getNamespaceContext() { |
|
196 |
return _streamWriter.getNamespaceContext(); |
|
197 |
} |
|
198 |
||
199 |
||
200 |
/** |
|
201 |
* Binds a URI to the default namespace |
|
202 |
* This URI is bound |
|
203 |
* in the scope of the current START_ELEMENT / END_ELEMENT pair. |
|
204 |
* If this method is called before a START_ELEMENT has been written |
|
205 |
* the uri is bound in the root scope. |
|
206 |
* @param uri the uri to bind to the default namespace |
|
207 |
* @throws XMLStreamException |
|
208 |
*/ |
|
209 |
public void setDefaultNamespace(String uri) throws XMLStreamException { |
|
210 |
_streamWriter.setDefaultNamespace(uri); |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Sets the current namespace context for prefix and uri bindings. |
|
215 |
* This context becomes the root namespace context for writing and |
|
216 |
* will replace the current root namespace context. Subsequent calls |
|
217 |
* to setPrefix and setDefaultNamespace will bind namespaces using |
|
218 |
* the context passed to the method as the root context for resolving |
|
219 |
* namespaces. |
|
220 |
* @param namespaceContext the namespace context to use for this writer |
|
221 |
* @throws XMLStreamException |
|
222 |
*/ |
|
223 |
public void setNamespaceContext(NamespaceContext namespaceContext) throws XMLStreamException { |
|
224 |
_streamWriter.setNamespaceContext(namespaceContext); |
|
225 |
} |
|
226 |
/** |
|
227 |
* Sets the prefix the uri is bound to. This prefix is bound |
|
228 |
* in the scope of the current START_ELEMENT / END_ELEMENT pair. |
|
229 |
* If this method is called before a START_ELEMENT has been written |
|
230 |
* the prefix is bound in the root scope. |
|
231 |
* @param prefix the prefix to bind to the uri |
|
232 |
* @param uri the uri to bind to the prefix |
|
233 |
* @throws XMLStreamException |
|
234 |
*/ |
|
235 |
public void setPrefix(String prefix, String uri) throws XMLStreamException { |
|
236 |
_streamWriter.setPrefix(prefix, uri); |
|
237 |
} |
|
238 |
||
239 |
} |