author | joehw |
Thu, 15 Dec 2016 13:57:04 -0800 | |
changeset 42802 | 2a03abb03c06 |
parent 41432 | c4036f85c0c5 |
permissions | -rw-r--r-- |
12005 | 1 |
/* |
41432
c4036f85c0c5
8139584: XMLStreamWriterImpl does not write 'standalone' property
joehw
parents:
25868
diff
changeset
|
2 |
* Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved. |
12005 | 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 |
||
26 |
package com.sun.xml.internal.stream.writers; |
|
27 |
||
28 |
import java.util.Iterator; |
|
42802 | 29 |
import javax.xml.namespace.NamespaceContext; |
12005 | 30 |
import javax.xml.namespace.QName; |
42802 | 31 |
import javax.xml.stream.XMLEventReader; |
12005 | 32 |
import javax.xml.stream.XMLEventWriter; |
33 |
import javax.xml.stream.XMLStreamException; |
|
42802 | 34 |
import javax.xml.stream.XMLStreamWriter; |
12005 | 35 |
import javax.xml.stream.events.Attribute; |
36 |
import javax.xml.stream.events.Characters; |
|
37 |
import javax.xml.stream.events.Comment; |
|
38 |
import javax.xml.stream.events.DTD; |
|
39 |
import javax.xml.stream.events.EntityReference; |
|
40 |
import javax.xml.stream.events.Namespace; |
|
41 |
import javax.xml.stream.events.ProcessingInstruction; |
|
42 |
import javax.xml.stream.events.StartDocument; |
|
43 |
import javax.xml.stream.events.StartElement; |
|
44 |
import javax.xml.stream.events.XMLEvent; |
|
45 |
||
46 |
/** |
|
47 |
* |
|
42802 | 48 |
* @author Neeraj Bajaj, Sun Microsystems. |
12005 | 49 |
* |
50 |
*/ |
|
42802 | 51 |
public class XMLEventWriterImpl implements XMLEventWriter { |
12005 | 52 |
|
53 |
//delegate everything to XMLStreamWriter.. |
|
42802 | 54 |
private final XMLStreamWriterBase fStreamWriter; |
12005 | 55 |
private static final boolean DEBUG = false; |
42802 | 56 |
|
12005 | 57 |
/** |
58 |
* |
|
59 |
* @param streamWriter |
|
60 |
*/ |
|
42802 | 61 |
public XMLEventWriterImpl(XMLStreamWriter streamWriter) { |
62 |
fStreamWriter = (XMLStreamWriterBase) streamWriter; |
|
12005 | 63 |
} |
64 |
||
65 |
/** |
|
66 |
* |
|
67 |
* @param xMLEventReader |
|
68 |
* @throws XMLStreamException |
|
69 |
*/ |
|
42802 | 70 |
public void add(XMLEventReader xMLEventReader) throws XMLStreamException { |
71 |
if (xMLEventReader == null) { |
|
72 |
throw new XMLStreamException("Event reader shouldn't be null"); |
|
73 |
} |
|
74 |
while (xMLEventReader.hasNext()) { |
|
12005 | 75 |
add(xMLEventReader.nextEvent()); |
76 |
} |
|
77 |
} |
|
78 |
||
79 |
/** |
|
80 |
* |
|
81 |
* @param xMLEvent |
|
82 |
* @throws XMLStreamException |
|
83 |
*/ |
|
42802 | 84 |
public void add(XMLEvent xMLEvent) throws XMLStreamException { |
12005 | 85 |
int type = xMLEvent.getEventType(); |
42802 | 86 |
switch (type) { |
87 |
case XMLEvent.DTD: { |
|
88 |
DTD dtd = (DTD) xMLEvent; |
|
89 |
if (DEBUG) { |
|
90 |
System.out.println("Adding DTD = " + dtd.toString()); |
|
91 |
} |
|
12005 | 92 |
fStreamWriter.writeDTD(dtd.getDocumentTypeDeclaration()); |
93 |
break; |
|
94 |
} |
|
42802 | 95 |
case XMLEvent.START_DOCUMENT: { |
96 |
StartDocument startDocument = (StartDocument) xMLEvent; |
|
97 |
if (DEBUG) { |
|
98 |
System.out.println("Adding StartDocument = " + startDocument.toString()); |
|
99 |
} |
|
12005 | 100 |
try { |
42802 | 101 |
fStreamWriter.writeStartDocument(startDocument.getCharacterEncodingScheme(), |
102 |
startDocument.getVersion(), |
|
103 |
startDocument.isStandalone(), startDocument.standaloneSet()); |
|
104 |
} catch (XMLStreamException e) { |
|
12005 | 105 |
fStreamWriter.writeStartDocument(startDocument.getVersion()); |
106 |
} |
|
107 |
break; |
|
108 |
} |
|
42802 | 109 |
case XMLEvent.START_ELEMENT: { |
110 |
StartElement startElement = xMLEvent.asStartElement(); |
|
111 |
if (DEBUG) { |
|
112 |
System.out.println("Adding startelement = " + startElement.toString()); |
|
113 |
} |
|
12005 | 114 |
QName qname = startElement.getName(); |
42802 | 115 |
fStreamWriter.writeStartElement(qname.getPrefix(), qname.getLocalPart(), |
116 |
qname.getNamespaceURI()); |
|
12005 | 117 |
|
42802 | 118 |
/* |
119 |
getNamespaces() Returns an Iterator of namespaces declared on this element. |
|
120 |
This Iterator does not contain previously declared namespaces unless they |
|
121 |
appear on the current START_ELEMENT. Therefore this list may contain redeclared |
|
122 |
namespaces and duplicate namespace declarations. Use the getNamespaceContext() |
|
123 |
method to get the current context of namespace declarations. We should be |
|
124 |
using getNamespaces() to write namespace declarations for this START_ELEMENT |
|
125 |
*/ |
|
126 |
Iterator<? extends Namespace> iterator = startElement.getNamespaces(); |
|
127 |
while (iterator.hasNext()) { |
|
128 |
Namespace namespace = iterator.next(); |
|
12005 | 129 |
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
130 |
} |
|
131 |
//REVISIT: What about writing attributes ? |
|
42802 | 132 |
Iterator<? extends Attribute> attributes = startElement.getAttributes(); |
133 |
while (attributes.hasNext()) { |
|
134 |
Attribute attribute = attributes.next(); |
|
12005 | 135 |
QName aqname = attribute.getName(); |
42802 | 136 |
fStreamWriter.writeAttribute(aqname.getPrefix(), aqname.getNamespaceURI(), |
137 |
aqname.getLocalPart(), attribute.getValue()); |
|
12005 | 138 |
} |
139 |
break; |
|
140 |
} |
|
42802 | 141 |
case XMLEvent.NAMESPACE: { |
142 |
Namespace namespace = (Namespace) xMLEvent; |
|
143 |
if (DEBUG) { |
|
144 |
System.out.println("Adding namespace = " + namespace.toString()); |
|
145 |
} |
|
12005 | 146 |
fStreamWriter.writeNamespace(namespace.getPrefix(), namespace.getNamespaceURI()); |
42802 | 147 |
break; |
12005 | 148 |
} |
149 |
case XMLEvent.COMMENT: { |
|
42802 | 150 |
Comment comment = (Comment) xMLEvent; |
151 |
if (DEBUG) { |
|
152 |
System.out.println("Adding comment = " + comment.toString()); |
|
153 |
} |
|
12005 | 154 |
fStreamWriter.writeComment(comment.getText()); |
155 |
break; |
|
156 |
} |
|
42802 | 157 |
case XMLEvent.PROCESSING_INSTRUCTION: { |
158 |
ProcessingInstruction processingInstruction = (ProcessingInstruction) xMLEvent; |
|
159 |
if (DEBUG) { |
|
160 |
System.out.println("Adding processing instruction = " + processingInstruction.toString()); |
|
161 |
} |
|
162 |
fStreamWriter.writeProcessingInstruction(processingInstruction.getTarget(), |
|
163 |
processingInstruction.getData()); |
|
12005 | 164 |
break; |
165 |
} |
|
42802 | 166 |
case XMLEvent.CHARACTERS: { |
12005 | 167 |
Characters characters = xMLEvent.asCharacters(); |
42802 | 168 |
if (DEBUG) { |
169 |
System.out.println("Adding characters = " + characters.toString()); |
|
170 |
} |
|
12005 | 171 |
//check if the CHARACTERS are CDATA |
42802 | 172 |
if (characters.isCData()) { |
12005 | 173 |
fStreamWriter.writeCData(characters.getData()); |
42802 | 174 |
} else { |
12005 | 175 |
fStreamWriter.writeCharacters(characters.getData()); |
176 |
} |
|
177 |
break; |
|
178 |
} |
|
42802 | 179 |
case XMLEvent.ENTITY_REFERENCE: { |
180 |
EntityReference entityReference = (EntityReference) xMLEvent; |
|
181 |
if (DEBUG) { |
|
182 |
System.out.println("Adding Entity Reference = " + entityReference.toString()); |
|
183 |
} |
|
12005 | 184 |
fStreamWriter.writeEntityRef(entityReference.getName()); |
185 |
break; |
|
186 |
} |
|
42802 | 187 |
case XMLEvent.ATTRIBUTE: { |
188 |
Attribute attribute = (Attribute) xMLEvent; |
|
189 |
if (DEBUG) { |
|
190 |
System.out.println("Adding Attribute = " + attribute.toString()); |
|
191 |
} |
|
12005 | 192 |
QName qname = attribute.getName(); |
42802 | 193 |
fStreamWriter.writeAttribute(qname.getPrefix(), qname.getNamespaceURI(), |
194 |
qname.getLocalPart(), attribute.getValue()); |
|
12005 | 195 |
break; |
196 |
} |
|
42802 | 197 |
case XMLEvent.CDATA: { |
12005 | 198 |
//there is no separate CDATA datatype but CDATA event can be reported |
199 |
//by using vendor specific CDATA property. |
|
42802 | 200 |
Characters characters = (Characters) xMLEvent; |
201 |
if (DEBUG) { |
|
202 |
System.out.println("Adding characters = " + characters.toString()); |
|
203 |
} |
|
204 |
if (characters.isCData()) { |
|
12005 | 205 |
fStreamWriter.writeCData(characters.getData()); |
206 |
} |
|
207 |
break; |
|
208 |
} |
|
209 |
//xxx: Why there isn't any event called Notation. |
|
210 |
//case XMLEvent.NOTATION_DECLARATION:{ |
|
211 |
//} |
|
212 |
||
42802 | 213 |
case XMLEvent.END_ELEMENT: { |
12005 | 214 |
fStreamWriter.writeEndElement(); |
215 |
break; |
|
216 |
} |
|
42802 | 217 |
case XMLEvent.END_DOCUMENT: { |
12005 | 218 |
fStreamWriter.writeEndDocument(); |
219 |
break; |
|
220 |
} |
|
221 |
//throw new XMLStreamException("Unknown Event type = " + type); |
|
222 |
}; |
|
223 |
||
224 |
} |
|
225 |
||
226 |
/** |
|
227 |
* |
|
228 |
* @throws XMLStreamException |
|
229 |
*/ |
|
42802 | 230 |
public void close() throws XMLStreamException { |
12005 | 231 |
fStreamWriter.close(); |
232 |
} |
|
233 |
||
234 |
/** |
|
235 |
* |
|
42802 | 236 |
* @throws XMLStreamException will inturn call flush on the stream to which |
237 |
* data is being written. |
|
12005 | 238 |
*/ |
42802 | 239 |
public void flush() throws XMLStreamException { |
12005 | 240 |
fStreamWriter.flush(); |
241 |
} |
|
242 |
||
243 |
/** |
|
244 |
* |
|
245 |
* @return |
|
246 |
*/ |
|
42802 | 247 |
public NamespaceContext getNamespaceContext() { |
12005 | 248 |
return fStreamWriter.getNamespaceContext(); |
249 |
} |
|
250 |
||
251 |
/** |
|
252 |
* |
|
253 |
* @param namespaceURI Namespace URI |
|
254 |
* @throws XMLStreamException |
|
255 |
* @return prefix associated with the URI. |
|
256 |
*/ |
|
42802 | 257 |
public String getPrefix(String namespaceURI) throws XMLStreamException { |
12005 | 258 |
return fStreamWriter.getPrefix(namespaceURI); |
259 |
} |
|
260 |
||
261 |
/** |
|
262 |
* |
|
263 |
* @param uri Namespace URI |
|
264 |
* @throws XMLStreamException |
|
265 |
*/ |
|
42802 | 266 |
public void setDefaultNamespace(String uri) throws XMLStreamException { |
12005 | 267 |
fStreamWriter.setDefaultNamespace(uri); |
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* |
|
272 |
* @param namespaceContext Namespace Context |
|
273 |
* @throws XMLStreamException |
|
274 |
*/ |
|
42802 | 275 |
public void setNamespaceContext(NamespaceContext namespaceContext) |
276 |
throws XMLStreamException { |
|
12005 | 277 |
fStreamWriter.setNamespaceContext(namespaceContext); |
278 |
} |
|
279 |
||
280 |
/** |
|
281 |
* |
|
282 |
* @param prefix namespace prefix associated with the uri. |
|
283 |
* @param uri Namespace URI |
|
284 |
* @throws XMLStreamException |
|
285 |
*/ |
|
42802 | 286 |
public void setPrefix(String prefix, String uri) throws XMLStreamException { |
12005 | 287 |
fStreamWriter.setPrefix(prefix, uri); |
288 |
} |
|
289 |
||
290 |
}//XMLEventWriterImpl |