|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| package org.dom4j.jaxb; |
|
9 |
| |
|
10 |
| import java.io.File; |
|
11 |
| import java.io.FileInputStream; |
|
12 |
| import java.io.FileNotFoundException; |
|
13 |
| import java.io.InputStream; |
|
14 |
| import java.io.InputStreamReader; |
|
15 |
| import java.io.Reader; |
|
16 |
| import java.net.URL; |
|
17 |
| import java.nio.charset.Charset; |
|
18 |
| |
|
19 |
| import org.dom4j.Document; |
|
20 |
| import org.dom4j.DocumentException; |
|
21 |
| import org.dom4j.Element; |
|
22 |
| import org.dom4j.ElementHandler; |
|
23 |
| import org.dom4j.ElementPath; |
|
24 |
| import org.dom4j.io.SAXReader; |
|
25 |
| |
|
26 |
| import org.xml.sax.InputSource; |
|
27 |
| |
|
28 |
| |
|
29 |
| |
|
30 |
| |
|
31 |
| |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| public class JAXBReader extends JAXBSupport { |
|
40 |
| private SAXReader reader; |
|
41 |
| |
|
42 |
| private boolean pruneElements; |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
0
| public JAXBReader(String contextPath) {
|
|
55 |
0
| super(contextPath);
|
|
56 |
| } |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
0
| public JAXBReader(String contextPath, ClassLoader classloader) {
|
|
72 |
0
| super(contextPath, classloader);
|
|
73 |
| } |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
0
| public Document read(File source) throws DocumentException {
|
|
87 |
0
| return getReader().read(source);
|
|
88 |
| } |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
0
| public Document read(File file, Charset charset) throws DocumentException {
|
|
105 |
0
| try {
|
|
106 |
0
| Reader xmlReader = new InputStreamReader(new FileInputStream(file),
|
|
107 |
| charset); |
|
108 |
| |
|
109 |
0
| return getReader().read(xmlReader);
|
|
110 |
| } catch (JAXBRuntimeException ex) { |
|
111 |
0
| Throwable cause = ex.getCause();
|
|
112 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
113 |
| } catch (FileNotFoundException ex) { |
|
114 |
0
| throw new DocumentException(ex.getMessage(), ex);
|
|
115 |
| } |
|
116 |
| } |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
0
| public Document read(InputSource source) throws DocumentException {
|
|
130 |
0
| try {
|
|
131 |
0
| return getReader().read(source);
|
|
132 |
| } catch (JAXBRuntimeException ex) { |
|
133 |
0
| Throwable cause = ex.getCause();
|
|
134 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
135 |
| } |
|
136 |
| } |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
0
| public Document read(InputStream source) throws DocumentException {
|
|
150 |
0
| try {
|
|
151 |
0
| return getReader().read(source);
|
|
152 |
| } catch (JAXBRuntimeException ex) { |
|
153 |
0
| Throwable cause = ex.getCause();
|
|
154 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
155 |
| } |
|
156 |
| } |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
| |
|
162 |
| |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
| |
|
171 |
0
| public Document read(InputStream source, String systemId)
|
|
172 |
| throws DocumentException { |
|
173 |
0
| try {
|
|
174 |
0
| return getReader().read(source);
|
|
175 |
| } catch (JAXBRuntimeException ex) { |
|
176 |
0
| Throwable cause = ex.getCause();
|
|
177 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
178 |
| } |
|
179 |
| } |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
0
| public Document read(Reader source) throws DocumentException {
|
|
193 |
0
| try {
|
|
194 |
0
| return getReader().read(source);
|
|
195 |
| } catch (JAXBRuntimeException ex) { |
|
196 |
0
| Throwable cause = ex.getCause();
|
|
197 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
0
| public Document read(Reader source, String systemId)
|
|
215 |
| throws DocumentException { |
|
216 |
0
| try {
|
|
217 |
0
| return getReader().read(source);
|
|
218 |
| } catch (JAXBRuntimeException ex) { |
|
219 |
0
| Throwable cause = ex.getCause();
|
|
220 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
221 |
| } |
|
222 |
| } |
|
223 |
| |
|
224 |
| |
|
225 |
| |
|
226 |
| |
|
227 |
| |
|
228 |
| |
|
229 |
| |
|
230 |
| |
|
231 |
| |
|
232 |
| |
|
233 |
| |
|
234 |
| |
|
235 |
0
| public Document read(String source) throws DocumentException {
|
|
236 |
0
| try {
|
|
237 |
0
| return getReader().read(source);
|
|
238 |
| } catch (JAXBRuntimeException ex) { |
|
239 |
0
| Throwable cause = ex.getCause();
|
|
240 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
241 |
| } |
|
242 |
| } |
|
243 |
| |
|
244 |
| |
|
245 |
| |
|
246 |
| |
|
247 |
| |
|
248 |
| |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
0
| public Document read(URL source) throws DocumentException {
|
|
256 |
0
| try {
|
|
257 |
0
| return getReader().read(source);
|
|
258 |
| } catch (JAXBRuntimeException ex) { |
|
259 |
0
| Throwable cause = ex.getCause();
|
|
260 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
261 |
| } |
|
262 |
| } |
|
263 |
| |
|
264 |
| |
|
265 |
| |
|
266 |
| |
|
267 |
| |
|
268 |
| |
|
269 |
| |
|
270 |
| |
|
271 |
| |
|
272 |
| |
|
273 |
| |
|
274 |
0
| public void addObjectHandler(String path, JAXBObjectHandler handler) {
|
|
275 |
0
| ElementHandler eHandler = new UnmarshalElementHandler(this, handler);
|
|
276 |
0
| getReader().addHandler(path, eHandler);
|
|
277 |
| } |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
0
| public void removeObjectHandler(String path) {
|
|
287 |
0
| getReader().removeHandler(path);
|
|
288 |
| } |
|
289 |
| |
|
290 |
| |
|
291 |
| |
|
292 |
| |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
| |
|
300 |
0
| public void addHandler(String path, ElementHandler handler) {
|
|
301 |
0
| getReader().addHandler(path, handler);
|
|
302 |
| } |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
0
| public void removeHandler(String path) {
|
|
312 |
0
| getReader().removeHandler(path);
|
|
313 |
| } |
|
314 |
| |
|
315 |
| |
|
316 |
| |
|
317 |
| |
|
318 |
| |
|
319 |
0
| public void resetHandlers() {
|
|
320 |
0
| getReader().resetHandlers();
|
|
321 |
| } |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
0
| public boolean isPruneElements() {
|
|
329 |
0
| return pruneElements;
|
|
330 |
| } |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
| |
|
337 |
| |
|
338 |
0
| public void setPruneElements(boolean pruneElements) {
|
|
339 |
0
| this.pruneElements = pruneElements;
|
|
340 |
| |
|
341 |
0
| if (pruneElements) {
|
|
342 |
0
| getReader().setDefaultHandler(new PruningElementHandler());
|
|
343 |
| } |
|
344 |
| } |
|
345 |
| |
|
346 |
0
| private SAXReader getReader() {
|
|
347 |
0
| if (reader == null) {
|
|
348 |
0
| reader = new SAXReader();
|
|
349 |
| } |
|
350 |
| |
|
351 |
0
| return reader;
|
|
352 |
| } |
|
353 |
| |
|
354 |
| private class UnmarshalElementHandler implements ElementHandler { |
|
355 |
| private JAXBReader jaxbReader; |
|
356 |
| |
|
357 |
| private JAXBObjectHandler handler; |
|
358 |
| |
|
359 |
0
| public UnmarshalElementHandler(JAXBReader documentReader,
|
|
360 |
| JAXBObjectHandler handler) { |
|
361 |
0
| this.jaxbReader = documentReader;
|
|
362 |
0
| this.handler = handler;
|
|
363 |
| } |
|
364 |
| |
|
365 |
0
| public void onStart(ElementPath elementPath) {
|
|
366 |
| } |
|
367 |
| |
|
368 |
0
| public void onEnd(ElementPath elementPath) {
|
|
369 |
0
| try {
|
|
370 |
0
| org.dom4j.Element elem = elementPath.getCurrent();
|
|
371 |
| |
|
372 |
0
| javax.xml.bind.Element jaxbObject
|
|
373 |
| = (javax.xml.bind.Element) jaxbReader.unmarshal(elem); |
|
374 |
| |
|
375 |
0
| if (jaxbReader.isPruneElements()) {
|
|
376 |
0
| elem.detach();
|
|
377 |
| } |
|
378 |
| |
|
379 |
0
| handler.handleObject(jaxbObject);
|
|
380 |
| } catch (Exception ex) { |
|
381 |
0
| throw new JAXBRuntimeException(ex);
|
|
382 |
| } |
|
383 |
| } |
|
384 |
| } |
|
385 |
| |
|
386 |
| private class PruningElementHandler implements ElementHandler { |
|
387 |
0
| public PruningElementHandler() {
|
|
388 |
| } |
|
389 |
| |
|
390 |
0
| public void onStart(ElementPath parm1) {
|
|
391 |
| } |
|
392 |
| |
|
393 |
0
| public void onEnd(ElementPath elementPath) {
|
|
394 |
0
| Element elem = elementPath.getCurrent();
|
|
395 |
0
| elem.detach();
|
|
396 |
0
| elem = null;
|
|
397 |
| } |
|
398 |
| } |
|
399 |
| } |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
| |
|
405 |
| |
|
406 |
| |
|
407 |
| |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
| |
|
414 |
| |
|
415 |
| |
|
416 |
| |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
| |
|
425 |
| |
|
426 |
| |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
| |
|
436 |
| |