/
StAXWriter.java
354 lines (318 loc) · 10.9 KB
/
StAXWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
/*
* StAXWriter.java
*
* Created on March 22, 2004, 2:59 PM
*/
package com.sun.xml.rpc.streaming;
import java.io.OutputStream;
import java.io.StringReader;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;
import com.sun.xml.rpc.util.exception.LocalizableExceptionAdapter;
import com.sun.xml.rpc.util.xml.CDATA;
/**
* An implementation of XMLWriter that uses StAX
*/
public class StAXWriter extends XMLWriterBase {
private final static String XML_VERSION = "1.0";
// the stream writer that does the work
private XMLStreamWriter writer;
// only use the one passed in
private PrefixFactory prefixFactory;
// make sure we close the document
private boolean documentEnded = false;
private static XMLOutputFactory outputFactory = null;
/*
* Create the XMLStreamWriter and start the document
*/
public StAXWriter(OutputStream out, String enc, boolean declare) {
try {
// Need to init factory if no writer is passed
if (outputFactory == null) {
outputFactory = XMLOutputFactory.newInstance();
}
writer = outputFactory.createXMLStreamWriter(out, enc);
if (declare) {
writer.writeStartDocument(enc, XML_VERSION);
}
}
catch (XMLStreamException e) {
throw wrapException(e);
}
}
public StAXWriter(OutputStream out, String enc, boolean declare,
XMLStreamWriter writer)
{
try {
this.writer = writer;
if (declare) {
writer.writeStartDocument(enc, XML_VERSION);
}
}
catch (XMLStreamException e) {
throw wrapException(e);
}
}
/**
* Returns the StAX XMLStreamWriter that is being used.
*/
public XMLStreamWriter getXMLStreamWriter() {
return writer;
}
public void startElement(String localName, String uri) {
try {
if (uri.equals("")) {
writer.writeStartElement(localName);
} else {
boolean mustDeclarePrefix = false;
String aPrefix = getPrefix(uri);
if (aPrefix == null) {
aPrefix = prefixFactory.getPrefix(uri);
mustDeclarePrefix = true;
}
writer.writeStartElement(aPrefix, localName, uri);
if (mustDeclarePrefix) {
writeNamespaceDeclaration(aPrefix, uri);
}
}
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void startElement(String localName, String uri, String prefix) {
try {
if (uri.equals("")) {
writer.writeStartElement(localName);
} else {
boolean mustDeclarePrefix = false;
String other = writer.getPrefix(uri);
if (other == null) {
mustDeclarePrefix = true;
}
else if (!other.equals(prefix)) {
mustDeclarePrefix = true;
writer.setPrefix(prefix, uri);
}
writer.writeStartElement(prefix, localName, uri);
if (mustDeclarePrefix) {
writer.writeNamespace(prefix, uri);
}
}
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void writeAttribute(String localName, String uri, String value) {
try {
if (uri.equals("")) {
writer.writeAttribute(localName, value);
} else {
boolean mustDeclarePrefix = false;
String aPrefix = getPrefix(uri);
if (aPrefix == null) {
mustDeclarePrefix = true;
if (prefixFactory != null) {
aPrefix = prefixFactory.getPrefix(uri);
}
if (aPrefix == null) {
throw new XMLWriterException(
"xmlwriter.noPrefixForURI",
uri);
}
}
writer.writeAttribute(aPrefix, uri, localName, value);
if (mustDeclarePrefix) {
writeNamespaceDeclaration(aPrefix, uri);
}
}
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/*
* Currently, all attributes are quoted by XMLStreamWriter
*/
public void writeAttributeUnquoted(String localName, String uri,
String value)
{
writeAttribute(localName, uri, value);
}
public void writeChars(String chars) {
try {
writer.writeCharacters(chars);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void writeChars(CDATA chars) {
writeChars(chars.getText());
}
/**
* If the first character of the string is an opening bracket,
* then this method assumes that the string is xml and it
* parses the string to write out the correct xml representation.
* This is because there is no api in stax to write unescaped
* characters.
*/
public void writeCharsUnquoted(String chars) {
try {
if (chars.charAt(0) == '<') {
parseAndWriteXML(chars);
return;
}
writer.writeCharacters(chars);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/**
* If the first character of the string is an opening bracket,
* then this method assumes that the string is xml and it
* parses the string to write out the correct xml representation.
* This is because there is no api in stax to write unescaped
* characters.
*/
public void writeCharsUnquoted(char[] buf, int offset, int len) {
try {
if (buf[offset] == '<') {
parseAndWriteXML(new String(buf, offset, len));
return;
}
writer.writeCharacters(buf, offset, len);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void writeComment(String comment) {
try {
writer.writeComment(comment);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/*
* currently using setPrefix rather than writeNamespace due
* to bug in impl. will revisit after switching to zephyr
*/
public void writeNamespaceDeclaration(String prefix, String uri) {
try {
writer.writeNamespace(prefix, uri);
// Must call setPrefix() as well
if (prefix == null || prefix.length() == 0 || prefix.equals("xmlns")) {
writer.setDefaultNamespace(uri);
}
else {
writer.setPrefix(prefix, uri);
}
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
/*
* Check namespace context for prefix before checking
* prefix factory.
*/
public void writeNamespaceDeclaration(String uri) {
// String aPrefix = writer.getNamespaceContext().getPrefix(uri);
try {
String aPrefix = writer.getPrefix(uri);
if (aPrefix == null) {
if (prefixFactory == null) {
throw new XMLWriterException("xmlwriter.noPrefixForURI", uri);
} else {
aPrefix = prefixFactory.getPrefix(uri);
}
}
writeNamespaceDeclaration(aPrefix, uri);
}
catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void endElement() {
try {
writer.writeEndElement();
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void close() {
try {
if (!documentEnded) {
writer.writeEndDocument();
}
writer.close();
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public void flush() {
try {
writer.flush();
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public String getPrefix(String uri) {
try {
return writer.getPrefix(uri);
} catch (XMLStreamException e) {
throw wrapException(e);
}
}
public String getURI(String prefix) {
return writer.getNamespaceContext().getNamespaceURI(prefix);
}
public PrefixFactory getPrefixFactory() {
return prefixFactory;
}
public void setPrefixFactory(PrefixFactory factory) {
prefixFactory = factory;
}
/*
* see writeCharsUnquoted()
*/
private void parseAndWriteXML(String xml) throws XMLStreamException
{
XMLReader reader = new StAXReader(new StringReader(xml), true);
int state = XMLReader.BOF;
do {
state = reader.next();
switch(state) {
case XMLReader.START:
QName elementName = reader.getName();
startElement(elementName.getLocalPart(),
elementName.getNamespaceURI(),
elementName.getPrefix());
break;
case XMLReader.END:
endElement();
break;
case XMLReader.CHARS:
writeChars(reader.getValue());
}
} while(state != XMLReader.EOF);
}
private XMLWriterException wrapException(XMLStreamException e) {
return new XMLWriterException(
"xmlwriter.ioException",
new LocalizableExceptionAdapter(e));
}
}