1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.xml.transform;
18
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.io.Reader;
23 import java.io.Writer;
24 import javax.xml.stream.XMLEventReader;
25 import javax.xml.stream.XMLEventWriter;
26 import javax.xml.stream.XMLStreamException;
27 import javax.xml.stream.XMLStreamReader;
28 import javax.xml.stream.XMLStreamWriter;
29 import javax.xml.transform.Result;
30 import javax.xml.transform.Source;
31 import javax.xml.transform.dom.DOMResult;
32 import javax.xml.transform.dom.DOMSource;
33 import javax.xml.transform.sax.SAXResult;
34 import javax.xml.transform.sax.SAXSource;
35 import javax.xml.transform.stax.StAXResult;
36 import javax.xml.transform.stax.StAXSource;
37 import javax.xml.transform.stream.StreamResult;
38 import javax.xml.transform.stream.StreamSource;
39
40 import org.w3c.dom.Document;
41 import org.w3c.dom.Node;
42 import org.xml.sax.ContentHandler;
43 import org.xml.sax.InputSource;
44 import org.xml.sax.SAXException;
45 import org.xml.sax.XMLReader;
46 import org.xml.sax.ext.LexicalHandler;
47
48 import org.springframework.util.Assert;
49 import org.springframework.xml.JaxpVersion;
50
51
52
53
54
55
56
57 public abstract class TraxUtils {
58
59
60
61
62
63
64
65 public static boolean isStaxSource(Source source) {
66 if (source instanceof StaxSource) {
67 return true;
68 }
69 else if (JaxpVersion.isAtLeastJaxp14()) {
70 return Jaxp14StaxHandler.isStaxSource(source);
71 }
72 else {
73 return false;
74 }
75 }
76
77
78
79
80
81
82
83 public static boolean isStaxResult(Result result) {
84 if (result instanceof StaxResult) {
85 return true;
86 }
87 else if (JaxpVersion.isAtLeastJaxp14()) {
88 return Jaxp14StaxHandler.isStaxResult(result);
89 }
90 else {
91 return false;
92 }
93 }
94
95
96
97
98
99
100
101
102
103 public static XMLStreamReader getXMLStreamReader(Source source) {
104 if (source instanceof StaxSource) {
105 return ((StaxSource) source).getXMLStreamReader();
106 }
107 else if (JaxpVersion.isAtLeastJaxp14()) {
108 return Jaxp14StaxHandler.getXMLStreamReader(source);
109 }
110 else {
111 throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
112 }
113 }
114
115
116
117
118
119
120
121
122
123 public static XMLEventReader getXMLEventReader(Source source) {
124 if (source instanceof StaxSource) {
125 return ((StaxSource) source).getXMLEventReader();
126 }
127 else if (JaxpVersion.isAtLeastJaxp14()) {
128 return Jaxp14StaxHandler.getXMLEventReader(source);
129 }
130 else {
131 throw new IllegalArgumentException("Source '" + source + "' is neither StaxSource nor StAXSource");
132 }
133 }
134
135
136
137
138
139
140
141
142
143 public static XMLStreamWriter getXMLStreamWriter(Result result) {
144 if (result instanceof StaxResult) {
145 return ((StaxResult) result).getXMLStreamWriter();
146 }
147 else if (JaxpVersion.isAtLeastJaxp14()) {
148 return Jaxp14StaxHandler.getXMLStreamWriter(result);
149 }
150 else {
151 throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
152 }
153 }
154
155
156
157
158
159
160
161
162
163 public static XMLEventWriter getXMLEventWriter(Result result) {
164 if (result instanceof StaxResult) {
165 return ((StaxResult) result).getXMLEventWriter();
166 }
167 else if (JaxpVersion.isAtLeastJaxp14()) {
168 return Jaxp14StaxHandler.getXMLEventWriter(result);
169 }
170 else {
171 throw new IllegalArgumentException("Result '" + result + "' is neither StaxResult nor StAXResult");
172 }
173 }
174
175
176
177
178
179
180
181
182 public static Source createStaxSource(XMLStreamReader streamReader) {
183 if (JaxpVersion.isAtLeastJaxp14()) {
184 return Jaxp14StaxHandler.createStaxSource(streamReader);
185 }
186 else {
187 return new StaxSource(streamReader);
188 }
189 }
190
191
192
193
194
195
196
197
198
199 public static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException {
200 if (JaxpVersion.isAtLeastJaxp14()) {
201 return Jaxp14StaxHandler.createStaxSource(eventReader);
202 }
203 else {
204 return new StaxSource(eventReader);
205 }
206 }
207
208
209
210
211
212
213
214 public static Document getDocument(DOMSource source) {
215 Node node = source.getNode();
216 if (node instanceof Document) {
217 return (Document) node;
218 }
219 else if (node != null) {
220 return node.getOwnerDocument();
221 }
222 else {
223 return null;
224 }
225 }
226
227
228
229
230
231
232
233
234 public static void doWithSource(Source source, SourceCallback callback)
235 throws XMLStreamException, IOException, SAXException {
236 if (source instanceof DOMSource) {
237 callback.domSource(((DOMSource) source).getNode());
238 }
239 else if (isStaxSource(source)) {
240 XMLStreamReader streamReader = getXMLStreamReader(source);
241 if (streamReader != null) {
242 callback.staxSource(streamReader);
243 }
244 else {
245 XMLEventReader eventReader = getXMLEventReader(source);
246 if (eventReader != null) {
247 callback.staxSource(eventReader);
248 }
249 else {
250 throw new IllegalArgumentException(
251 "StAX source contains neither XMLStreamReader nor XMLEventReader");
252 }
253 }
254 }
255 else if (source instanceof SAXSource) {
256 SAXSource saxSource = (SAXSource) source;
257 callback.saxSource(saxSource.getXMLReader(), saxSource.getInputSource());
258 }
259 else if (source instanceof StreamSource) {
260 StreamSource streamSource = (StreamSource) source;
261 if (streamSource.getInputStream() != null) {
262 callback.streamSource(streamSource.getInputStream());
263 }
264 else if (streamSource.getReader() != null) {
265 callback.streamSource(streamSource.getReader());
266 }
267 else {
268 throw new IllegalArgumentException("StreamSource contains neither InputStream nor Reader");
269 }
270 }
271 else {
272 throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
273 }
274 }
275
276
277
278
279
280
281
282
283 public static void doWithResult(Result result, ResultCallback callback)
284 throws XMLStreamException, IOException, SAXException {
285 if (result instanceof DOMResult) {
286 callback.domResult(((DOMResult) result).getNode());
287 }
288 else if (isStaxResult(result)) {
289 XMLStreamWriter streamWriter = getXMLStreamWriter(result);
290 if (streamWriter != null) {
291 callback.staxResult(streamWriter);
292 }
293 else {
294 XMLEventWriter eventWriter = getXMLEventWriter(result);
295 if (eventWriter != null) {
296 callback.staxResult(eventWriter);
297 }
298 else {
299 throw new IllegalArgumentException(
300 "StAX result contains neither XMLStreamWriter nor XMLEventWriter");
301 }
302 }
303 }
304 else if (result instanceof SAXResult) {
305 SAXResult saxSource = (SAXResult) result;
306 callback.saxResult(saxSource.getHandler(), saxSource.getLexicalHandler());
307 }
308 else if (result instanceof StreamResult) {
309 StreamResult streamSource = (StreamResult) result;
310 if (streamSource.getOutputStream() != null) {
311 callback.streamResult(streamSource.getOutputStream());
312 }
313 else if (streamSource.getWriter() != null) {
314 callback.streamResult(streamSource.getWriter());
315 }
316 else {
317 throw new IllegalArgumentException("StreamResult contains neither OutputStream nor Writer");
318 }
319 }
320 else {
321 throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
322 }
323 }
324
325
326
327
328
329
330 public interface SourceCallback {
331
332
333
334
335
336
337 void domSource(Node node);
338
339
340
341
342
343
344
345 void saxSource(XMLReader reader, InputSource inputSource) throws IOException, SAXException;
346
347
348
349
350
351
352
353 void staxSource(XMLEventReader eventReader) throws XMLStreamException;
354
355
356
357
358
359
360
361 void staxSource(XMLStreamReader streamReader) throws XMLStreamException;
362
363
364
365
366
367
368 void streamSource(InputStream inputStream) throws IOException;
369
370
371
372
373
374
375 void streamSource(Reader reader) throws IOException;
376 }
377
378
379
380
381
382
383 public interface ResultCallback {
384
385
386
387
388
389
390 void domResult(Node node);
391
392
393
394
395
396
397
398
399 void saxResult(ContentHandler contentHandler, LexicalHandler lexicalHandler) throws IOException, SAXException;
400
401
402
403
404
405
406
407 void staxResult(XMLEventWriter eventWriter) throws XMLStreamException;
408
409
410
411
412
413
414
415 void staxResult(XMLStreamWriter streamWriter) throws XMLStreamException;
416
417
418
419
420
421
422 void streamResult(OutputStream outputStream) throws IOException;
423
424
425
426
427
428
429 void streamResult(Writer writer) throws IOException;
430 }
431
432
433 private static class Jaxp14StaxHandler {
434
435 private static Source createStaxSource(XMLStreamReader streamReader) {
436 return new StAXSource(streamReader);
437 }
438
439 private static Source createStaxSource(XMLEventReader eventReader) throws XMLStreamException {
440 return new StAXSource(eventReader);
441 }
442
443 private static boolean isStaxSource(Source source) {
444 return source instanceof StAXSource;
445 }
446
447 private static boolean isStaxResult(Result result) {
448 return result instanceof StAXResult;
449 }
450
451 private static XMLStreamReader getXMLStreamReader(Source source) {
452 Assert.isInstanceOf(StAXSource.class, source);
453 return ((StAXSource) source).getXMLStreamReader();
454 }
455
456 private static XMLEventReader getXMLEventReader(Source source) {
457 Assert.isInstanceOf(StAXSource.class, source);
458 return ((StAXSource) source).getXMLEventReader();
459 }
460
461 private static XMLStreamWriter getXMLStreamWriter(Result result) {
462 Assert.isInstanceOf(StAXResult.class, result);
463 return ((StAXResult) result).getXMLStreamWriter();
464 }
465
466 private static XMLEventWriter getXMLEventWriter(Result result) {
467 Assert.isInstanceOf(StAXResult.class, result);
468 return ((StAXResult) result).getXMLEventWriter();
469 }
470 }
471
472
473 }