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.InputStream;
20 import java.io.OutputStream;
21 import java.io.Reader;
22 import java.io.Writer;
23 import javax.xml.stream.XMLEventReader;
24 import javax.xml.stream.XMLEventWriter;
25 import javax.xml.stream.XMLStreamReader;
26 import javax.xml.stream.XMLStreamWriter;
27 import javax.xml.transform.Result;
28 import javax.xml.transform.Source;
29 import javax.xml.transform.dom.DOMResult;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.sax.SAXResult;
32 import javax.xml.transform.sax.SAXSource;
33 import javax.xml.transform.stax.StAXResult;
34 import javax.xml.transform.stax.StAXSource;
35 import javax.xml.transform.stream.StreamResult;
36 import javax.xml.transform.stream.StreamSource;
37
38 import org.springframework.util.StringUtils;
39 import org.springframework.util.xml.StaxUtils;
40
41 import org.w3c.dom.Document;
42 import org.w3c.dom.Node;
43 import org.xml.sax.ContentHandler;
44 import org.xml.sax.InputSource;
45 import org.xml.sax.XMLReader;
46 import org.xml.sax.ext.LexicalHandler;
47
48
49
50
51
52
53
54 public abstract class TraxUtils {
55
56
57
58
59
60
61
62 public static Document getDocument(DOMSource source) {
63 Node node = source.getNode();
64 if (node instanceof Document) {
65 return (Document) node;
66 }
67 else if (node != null) {
68 return node.getOwnerDocument();
69 }
70 else {
71 return null;
72 }
73 }
74
75
76
77
78
79
80
81
82 public static void doWithSource(Source source, SourceCallback callback) throws Exception {
83 if (source instanceof DOMSource) {
84 callback.domSource(((DOMSource) source).getNode());
85 return;
86 }
87 else if (StaxUtils.isStaxSource(source)) {
88 XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(source);
89 if (streamReader != null) {
90 callback.staxSource(streamReader);
91 return;
92 }
93 else {
94 XMLEventReader eventReader = StaxUtils.getXMLEventReader(source);
95 if (eventReader != null) {
96 callback.staxSource(eventReader);
97 return;
98 }
99 }
100 }
101 else if (source instanceof SAXSource) {
102 SAXSource saxSource = (SAXSource) source;
103 callback.saxSource(saxSource.getXMLReader(), saxSource.getInputSource());
104 return;
105 }
106 else if (source instanceof StreamSource) {
107 StreamSource streamSource = (StreamSource) source;
108 if (streamSource.getInputStream() != null) {
109 callback.streamSource(streamSource.getInputStream());
110 return;
111 }
112 else if (streamSource.getReader() != null) {
113 callback.streamSource(streamSource.getReader());
114 return;
115 }
116 }
117 if (StringUtils.hasLength(source.getSystemId())) {
118 String systemId = source.getSystemId();
119 callback.source(systemId);
120 }
121 else {
122 throw new IllegalArgumentException("Unknown Source type: " + source.getClass());
123 }
124 }
125
126
127
128
129
130
131
132
133 public static void doWithResult(Result result, ResultCallback callback) throws Exception {
134 if (result instanceof DOMResult) {
135 callback.domResult(((DOMResult) result).getNode());
136 return;
137 }
138 else if (StaxUtils.isStaxResult(result)) {
139 XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(result);
140 if (streamWriter != null) {
141 callback.staxResult(streamWriter);
142 return;
143 }
144 else {
145 XMLEventWriter eventWriter = StaxUtils.getXMLEventWriter(result);
146 if (eventWriter != null) {
147 callback.staxResult(eventWriter);
148 return;
149 }
150 }
151 }
152 else if (result instanceof SAXResult) {
153 SAXResult saxSource = (SAXResult) result;
154 callback.saxResult(saxSource.getHandler(), saxSource.getLexicalHandler());
155 return;
156 }
157 else if (result instanceof StreamResult) {
158 StreamResult streamSource = (StreamResult) result;
159 if (streamSource.getOutputStream() != null) {
160 callback.streamResult(streamSource.getOutputStream());
161 return;
162 }
163 else if (streamSource.getWriter() != null) {
164 callback.streamResult(streamSource.getWriter());
165 return;
166 }
167 }
168 if (StringUtils.hasLength(result.getSystemId())) {
169 String systemId = result.getSystemId();
170 callback.result(systemId);
171 }
172 else {
173 throw new IllegalArgumentException("Unknown Result type: " + result.getClass());
174 }
175 }
176
177
178
179
180
181
182 public interface SourceCallback {
183
184
185
186
187
188
189 void domSource(Node node) throws Exception;
190
191
192
193
194
195
196
197 void saxSource(XMLReader reader, InputSource inputSource) throws Exception;
198
199
200
201
202
203
204
205 void staxSource(XMLEventReader eventReader) throws Exception;
206
207
208
209
210
211
212
213 void staxSource(XMLStreamReader streamReader) throws Exception;
214
215
216
217
218
219
220 void streamSource(InputStream inputStream) throws Exception;
221
222
223
224
225
226
227 void streamSource(Reader reader) throws Exception;
228
229
230
231
232
233
234 void source(String systemId) throws Exception;
235
236
237 }
238
239
240
241
242
243
244 public interface ResultCallback {
245
246
247
248
249
250
251 void domResult(Node node) throws Exception;
252
253
254
255
256
257
258
259
260 void saxResult(ContentHandler contentHandler, LexicalHandler lexicalHandler) throws Exception;
261
262
263
264
265
266
267
268 void staxResult(XMLEventWriter eventWriter) throws Exception;
269
270
271
272
273
274
275
276 void staxResult(XMLStreamWriter streamWriter) throws Exception;
277
278
279
280
281
282
283 void streamResult(OutputStream outputStream) throws Exception;
284
285
286
287
288
289
290 void streamResult(Writer writer) throws Exception;
291
292
293
294
295
296
297 void result(String systemId) throws Exception;
298
299 }
300
301
302 }