|
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.FileOutputStream; |
|
14 |
| import java.io.IOException; |
|
15 |
| import java.io.InputStream; |
|
16 |
| import java.io.InputStreamReader; |
|
17 |
| import java.io.OutputStream; |
|
18 |
| import java.io.Reader; |
|
19 |
| import java.io.Writer; |
|
20 |
| import java.net.URL; |
|
21 |
| import java.nio.charset.Charset; |
|
22 |
| import java.util.HashMap; |
|
23 |
| import java.util.Iterator; |
|
24 |
| import java.util.Map; |
|
25 |
| |
|
26 |
| import org.dom4j.Document; |
|
27 |
| import org.dom4j.DocumentException; |
|
28 |
| import org.dom4j.io.ElementModifier; |
|
29 |
| import org.dom4j.io.OutputFormat; |
|
30 |
| import org.dom4j.io.SAXModifier; |
|
31 |
| import org.dom4j.io.XMLWriter; |
|
32 |
| |
|
33 |
| import org.xml.sax.InputSource; |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| public class JAXBModifier extends JAXBSupport { |
|
46 |
| private SAXModifier modifier; |
|
47 |
| |
|
48 |
| private XMLWriter xmlWriter; |
|
49 |
| |
|
50 |
| private boolean pruneElements; |
|
51 |
| |
|
52 |
| private OutputFormat outputFormat; |
|
53 |
| |
|
54 |
| private HashMap modifiers = new HashMap(); |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
0
| public JAXBModifier(String contextPath) {
|
|
67 |
0
| super(contextPath);
|
|
68 |
0
| this.outputFormat = new OutputFormat();
|
|
69 |
| } |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
0
| public JAXBModifier(String contextPath, ClassLoader classloader) {
|
|
85 |
0
| super(contextPath, classloader);
|
|
86 |
0
| this.outputFormat = new OutputFormat();
|
|
87 |
| } |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
0
| public JAXBModifier(String contextPath, OutputFormat outputFormat) {
|
|
102 |
0
| super(contextPath);
|
|
103 |
0
| this.outputFormat = outputFormat;
|
|
104 |
| } |
|
105 |
| |
|
106 |
| |
|
107 |
| |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
| |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
0
| public JAXBModifier(String contextPath, ClassLoader classloader,
|
|
121 |
| OutputFormat outputFormat) { |
|
122 |
0
| super(contextPath, classloader);
|
|
123 |
0
| this.outputFormat = outputFormat;
|
|
124 |
| } |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
0
| public Document modify(File source) throws DocumentException, IOException {
|
|
141 |
0
| return installModifier().modify(source);
|
|
142 |
| } |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
| |
|
150 |
| |
|
151 |
| |
|
152 |
| |
|
153 |
| |
|
154 |
| |
|
155 |
| |
|
156 |
| |
|
157 |
| |
|
158 |
| |
|
159 |
| |
|
160 |
| |
|
161 |
0
| public Document modify(File source, Charset charset)
|
|
162 |
| throws DocumentException, IOException { |
|
163 |
0
| try {
|
|
164 |
0
| Reader reader = new InputStreamReader(new FileInputStream(source),
|
|
165 |
| charset); |
|
166 |
| |
|
167 |
0
| return installModifier().modify(reader);
|
|
168 |
| } catch (JAXBRuntimeException ex) { |
|
169 |
0
| Throwable cause = ex.getCause();
|
|
170 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
171 |
| } catch (FileNotFoundException ex) { |
|
172 |
0
| throw new DocumentException(ex.getMessage(), ex);
|
|
173 |
| } |
|
174 |
| } |
|
175 |
| |
|
176 |
| |
|
177 |
| |
|
178 |
| |
|
179 |
| |
|
180 |
| |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
0
| public Document modify(InputSource source) throws DocumentException,
|
|
191 |
| IOException { |
|
192 |
0
| try {
|
|
193 |
0
| return installModifier().modify(source);
|
|
194 |
| } catch (JAXBRuntimeException ex) { |
|
195 |
0
| Throwable cause = ex.getCause();
|
|
196 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
197 |
| } |
|
198 |
| } |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
0
| public Document modify(InputStream source) throws DocumentException,
|
|
215 |
| IOException { |
|
216 |
0
| try {
|
|
217 |
0
| return installModifier().modify(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 |
| |
|
236 |
| |
|
237 |
| |
|
238 |
| |
|
239 |
| |
|
240 |
0
| public Document modify(InputStream source, String systemId)
|
|
241 |
| throws DocumentException, IOException { |
|
242 |
0
| try {
|
|
243 |
0
| return installModifier().modify(source);
|
|
244 |
| } catch (JAXBRuntimeException ex) { |
|
245 |
0
| Throwable cause = ex.getCause();
|
|
246 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
247 |
| } |
|
248 |
| } |
|
249 |
| |
|
250 |
| |
|
251 |
| |
|
252 |
| |
|
253 |
| |
|
254 |
| |
|
255 |
| |
|
256 |
| |
|
257 |
| |
|
258 |
| |
|
259 |
| |
|
260 |
| |
|
261 |
| |
|
262 |
| |
|
263 |
| |
|
264 |
0
| public Document modify(Reader r) throws DocumentException, IOException {
|
|
265 |
0
| try {
|
|
266 |
0
| return installModifier().modify(r);
|
|
267 |
| } catch (JAXBRuntimeException ex) { |
|
268 |
0
| Throwable cause = ex.getCause();
|
|
269 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
270 |
| } |
|
271 |
| } |
|
272 |
| |
|
273 |
| |
|
274 |
| |
|
275 |
| |
|
276 |
| |
|
277 |
| |
|
278 |
| |
|
279 |
| |
|
280 |
| |
|
281 |
| |
|
282 |
| |
|
283 |
| |
|
284 |
| |
|
285 |
| |
|
286 |
| |
|
287 |
| |
|
288 |
| |
|
289 |
0
| public Document modify(Reader source, String systemId)
|
|
290 |
| throws DocumentException, IOException { |
|
291 |
0
| try {
|
|
292 |
0
| return installModifier().modify(source);
|
|
293 |
| } catch (JAXBRuntimeException ex) { |
|
294 |
0
| Throwable cause = ex.getCause();
|
|
295 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
296 |
| } |
|
297 |
| } |
|
298 |
| |
|
299 |
| |
|
300 |
| |
|
301 |
| |
|
302 |
| |
|
303 |
| |
|
304 |
| |
|
305 |
| |
|
306 |
| |
|
307 |
| |
|
308 |
| |
|
309 |
| |
|
310 |
| |
|
311 |
| |
|
312 |
| |
|
313 |
0
| public Document modify(String url) throws DocumentException, IOException {
|
|
314 |
0
| try {
|
|
315 |
0
| return installModifier().modify(url);
|
|
316 |
| } catch (JAXBRuntimeException ex) { |
|
317 |
0
| Throwable cause = ex.getCause();
|
|
318 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
319 |
| } |
|
320 |
| } |
|
321 |
| |
|
322 |
| |
|
323 |
| |
|
324 |
| |
|
325 |
| |
|
326 |
| |
|
327 |
| |
|
328 |
| |
|
329 |
| |
|
330 |
| |
|
331 |
| |
|
332 |
| |
|
333 |
| |
|
334 |
| |
|
335 |
| |
|
336 |
0
| public Document modify(URL source) throws DocumentException, IOException {
|
|
337 |
0
| try {
|
|
338 |
0
| return installModifier().modify(source);
|
|
339 |
| } catch (JAXBRuntimeException ex) { |
|
340 |
0
| Throwable cause = ex.getCause();
|
|
341 |
0
| throw new DocumentException(cause.getMessage(), cause);
|
|
342 |
| } |
|
343 |
| } |
|
344 |
| |
|
345 |
| |
|
346 |
| |
|
347 |
| |
|
348 |
| |
|
349 |
| |
|
350 |
| |
|
351 |
| |
|
352 |
| |
|
353 |
| |
|
354 |
0
| public void setOutput(File file) throws IOException {
|
|
355 |
0
| createXMLWriter().setOutputStream(new FileOutputStream(file));
|
|
356 |
| } |
|
357 |
| |
|
358 |
| |
|
359 |
| |
|
360 |
| |
|
361 |
| |
|
362 |
| |
|
363 |
| |
|
364 |
| |
|
365 |
| |
|
366 |
| |
|
367 |
0
| public void setOutput(OutputStream outputStream) throws IOException {
|
|
368 |
0
| createXMLWriter().setOutputStream(outputStream);
|
|
369 |
| } |
|
370 |
| |
|
371 |
| |
|
372 |
| |
|
373 |
| |
|
374 |
| |
|
375 |
| |
|
376 |
| |
|
377 |
| |
|
378 |
| |
|
379 |
| |
|
380 |
0
| public void setOutput(Writer writer) throws IOException {
|
|
381 |
0
| createXMLWriter().setWriter(writer);
|
|
382 |
| } |
|
383 |
| |
|
384 |
| |
|
385 |
| |
|
386 |
| |
|
387 |
| |
|
388 |
| |
|
389 |
| |
|
390 |
| |
|
391 |
| |
|
392 |
| |
|
393 |
0
| public void addObjectModifier(String path, JAXBObjectModifier mod) {
|
|
394 |
0
| modifiers.put(path, mod);
|
|
395 |
| } |
|
396 |
| |
|
397 |
| |
|
398 |
| |
|
399 |
| |
|
400 |
| |
|
401 |
| |
|
402 |
| |
|
403 |
| |
|
404 |
0
| public void removeObjectModifier(String path) {
|
|
405 |
0
| modifiers.remove(path);
|
|
406 |
0
| getModifier().removeModifier(path);
|
|
407 |
| } |
|
408 |
| |
|
409 |
| |
|
410 |
| |
|
411 |
| |
|
412 |
| |
|
413 |
0
| public void resetObjectModifiers() {
|
|
414 |
0
| modifiers.clear();
|
|
415 |
0
| getModifier().resetModifiers();
|
|
416 |
| } |
|
417 |
| |
|
418 |
| |
|
419 |
| |
|
420 |
| |
|
421 |
| |
|
422 |
| |
|
423 |
| |
|
424 |
0
| public boolean isPruneElements() {
|
|
425 |
0
| return pruneElements;
|
|
426 |
| } |
|
427 |
| |
|
428 |
| |
|
429 |
| |
|
430 |
| |
|
431 |
| |
|
432 |
| |
|
433 |
| |
|
434 |
| |
|
435 |
0
| public void setPruneElements(boolean pruneElements) {
|
|
436 |
0
| this.pruneElements = pruneElements;
|
|
437 |
| } |
|
438 |
| |
|
439 |
0
| private SAXModifier installModifier() throws IOException {
|
|
440 |
0
| modifier = new SAXModifier(isPruneElements());
|
|
441 |
| |
|
442 |
0
| modifier.resetModifiers();
|
|
443 |
| |
|
444 |
0
| Iterator modifierIt = modifiers.entrySet().iterator();
|
|
445 |
| |
|
446 |
0
| while (modifierIt.hasNext()) {
|
|
447 |
0
| Map.Entry entry = (Map.Entry) modifierIt.next();
|
|
448 |
0
| ElementModifier mod = new JAXBElementModifier(this,
|
|
449 |
| (JAXBObjectModifier) entry.getValue()); |
|
450 |
0
| getModifier().addModifier((String) entry.getKey(), mod);
|
|
451 |
| } |
|
452 |
| |
|
453 |
0
| modifier.setXMLWriter(getXMLWriter());
|
|
454 |
| |
|
455 |
0
| return modifier;
|
|
456 |
| } |
|
457 |
| |
|
458 |
0
| private SAXModifier getModifier() {
|
|
459 |
0
| if (this.modifier == null) {
|
|
460 |
0
| modifier = new SAXModifier(isPruneElements());
|
|
461 |
| } |
|
462 |
| |
|
463 |
0
| return modifier;
|
|
464 |
| } |
|
465 |
| |
|
466 |
0
| private XMLWriter getXMLWriter() {
|
|
467 |
0
| return xmlWriter;
|
|
468 |
| } |
|
469 |
| |
|
470 |
0
| private XMLWriter createXMLWriter() throws IOException {
|
|
471 |
0
| if (this.xmlWriter == null) {
|
|
472 |
0
| xmlWriter = new XMLWriter(outputFormat);
|
|
473 |
| } |
|
474 |
| |
|
475 |
0
| return xmlWriter;
|
|
476 |
| } |
|
477 |
| |
|
478 |
| private class JAXBElementModifier implements ElementModifier { |
|
479 |
| private JAXBModifier jaxbModifier; |
|
480 |
| |
|
481 |
| private JAXBObjectModifier objectModifier; |
|
482 |
| |
|
483 |
0
| public JAXBElementModifier(JAXBModifier jaxbModifier,
|
|
484 |
| JAXBObjectModifier objectModifier) { |
|
485 |
0
| this.jaxbModifier = jaxbModifier;
|
|
486 |
0
| this.objectModifier = objectModifier;
|
|
487 |
| } |
|
488 |
| |
|
489 |
0
| public org.dom4j.Element modifyElement(org.dom4j.Element element)
|
|
490 |
| throws Exception { |
|
491 |
0
| javax.xml.bind.Element originalObject = jaxbModifier
|
|
492 |
| .unmarshal(element); |
|
493 |