1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.apache.camel.converter.jaxb; |
19 |
|
|
20 |
|
import org.apache.camel.converter.HasAnnotation; |
21 |
|
import org.apache.camel.converter.jaxp.XmlConverter; |
22 |
|
import org.w3c.dom.Document; |
23 |
|
|
24 |
|
import javax.xml.bind.JAXBContext; |
25 |
|
import javax.xml.bind.JAXBException; |
26 |
|
import javax.xml.bind.Marshaller; |
27 |
|
import javax.xml.bind.annotation.XmlRootElement; |
28 |
|
import javax.xml.bind.util.JAXBSource; |
29 |
|
import javax.xml.parsers.ParserConfigurationException; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
0 |
public class JaxbConverter { |
35 |
|
private XmlConverter jaxbConverter; |
36 |
|
|
37 |
|
public XmlConverter getJaxbConverter() { |
38 |
0 |
if (jaxbConverter == null) { |
39 |
0 |
jaxbConverter = new XmlConverter(); |
40 |
|
} |
41 |
0 |
return jaxbConverter; |
42 |
|
} |
43 |
|
|
44 |
|
public void setJaxbConverter(XmlConverter jaxbConverter) { |
45 |
0 |
this.jaxbConverter = jaxbConverter; |
46 |
0 |
} |
47 |
|
|
48 |
|
public static JAXBSource toSource(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException { |
49 |
0 |
JAXBContext context = createJaxbContext(value); |
50 |
0 |
return new JAXBSource(context, value); |
51 |
|
} |
52 |
|
|
53 |
|
public Document toDocument(@HasAnnotation(XmlRootElement.class)Object value) throws JAXBException, ParserConfigurationException { |
54 |
0 |
JAXBContext context = createJaxbContext(value); |
55 |
0 |
Marshaller marshaller = context.createMarshaller(); |
56 |
|
|
57 |
0 |
Document doc = getJaxbConverter().createDocument(); |
58 |
0 |
marshaller.marshal(value, doc); |
59 |
0 |
return doc; |
60 |
|
} |
61 |
|
|
62 |
|
protected static JAXBContext createJaxbContext(Object value) throws JAXBException { |
63 |
0 |
if (value == null) { |
64 |
0 |
throw new IllegalArgumentException("Cannot convert from null value to JAXBSource"); |
65 |
|
} |
66 |
0 |
JAXBContext context = JAXBContext.newInstance(value.getClass()); |
67 |
0 |
return context; |
68 |
|
} |
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
|
78 |
|
} |