1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
package org.apache.camel.converter.jaxb; |
18 |
|
|
19 |
|
import java.io.InputStream; |
20 |
|
import java.io.Reader; |
21 |
|
import java.io.StringReader; |
22 |
|
import java.io.StringWriter; |
23 |
|
|
24 |
|
import javax.xml.bind.JAXBContext; |
25 |
|
import javax.xml.bind.JAXBException; |
26 |
|
import javax.xml.bind.Unmarshaller; |
27 |
|
import javax.xml.bind.Marshaller; |
28 |
|
import javax.xml.bind.annotation.XmlRootElement; |
29 |
|
import javax.xml.bind.util.JAXBSource; |
30 |
|
import javax.xml.transform.Source; |
31 |
|
|
32 |
|
import org.apache.camel.RuntimeCamelException; |
33 |
|
import org.apache.camel.TypeConverter; |
34 |
|
import org.apache.camel.spi.TypeConverterAware; |
35 |
|
import org.apache.commons.logging.Log; |
36 |
|
import org.apache.commons.logging.LogFactory; |
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
|
41 |
1 |
public class FallbackTypeConverter implements TypeConverter, TypeConverterAware { |
42 |
1 |
private static final transient Log LOG = LogFactory.getLog(FallbackTypeConverter.class); |
43 |
|
private TypeConverter parentTypeConverter; |
44 |
1 |
private boolean prettyPrint = true; |
45 |
|
|
46 |
|
public boolean isPrettyPrint() { |
47 |
0 |
return prettyPrint; |
48 |
|
} |
49 |
|
|
50 |
|
public void setPrettyPrint(boolean prettyPrint) { |
51 |
0 |
this.prettyPrint = prettyPrint; |
52 |
0 |
} |
53 |
|
|
54 |
|
public void setTypeConverter(TypeConverter parentTypeConverter) { |
55 |
1 |
this.parentTypeConverter = parentTypeConverter; |
56 |
1 |
} |
57 |
|
|
58 |
|
public <T> T convertTo(Class<T> type, Object value) { |
59 |
|
try { |
60 |
1 |
if (isJaxbType(type)) { |
61 |
1 |
return unmarshall(type, value); |
62 |
|
} |
63 |
0 |
if (value != null) { |
64 |
0 |
if (isJaxbType(value.getClass())) { |
65 |
0 |
return marshall(type, value); |
66 |
|
} |
67 |
|
} |
68 |
0 |
return null; |
69 |
0 |
} catch (JAXBException e) { |
70 |
0 |
throw new RuntimeCamelException(e); |
71 |
|
} |
72 |
|
} |
73 |
|
|
74 |
|
protected <T> boolean isJaxbType(Class<T> type) { |
75 |
1 |
XmlRootElement element = type.getAnnotation(XmlRootElement.class); |
76 |
1 |
boolean jaxbType = element != null; |
77 |
1 |
return jaxbType; |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
protected <T> T unmarshall(Class<T> type, Object value) throws JAXBException { |
84 |
1 |
JAXBContext context = createContext(type); |
85 |
1 |
Unmarshaller unmarshaller = context.createUnmarshaller(); |
86 |
|
|
87 |
1 |
if (parentTypeConverter != null) { |
88 |
1 |
InputStream inputStream = parentTypeConverter.convertTo(InputStream.class, value); |
89 |
1 |
if (inputStream != null) { |
90 |
1 |
Object unmarshalled = unmarshaller.unmarshal(inputStream); |
91 |
1 |
return type.cast(unmarshalled); |
92 |
|
} |
93 |
0 |
Reader reader = parentTypeConverter.convertTo(Reader.class, value); |
94 |
0 |
if (reader != null) { |
95 |
0 |
Object unmarshalled = unmarshaller.unmarshal(reader); |
96 |
0 |
return type.cast(unmarshalled); |
97 |
|
} |
98 |
0 |
Source source = parentTypeConverter.convertTo(Source.class, value); |
99 |
0 |
if (source != null) { |
100 |
0 |
Object unmarshalled = unmarshaller.unmarshal(source); |
101 |
0 |
return type.cast(unmarshalled); |
102 |
|
} |
103 |
|
} |
104 |
0 |
if (value instanceof String) { |
105 |
0 |
value = new StringReader((String)value); |
106 |
|
} |
107 |
0 |
if (value instanceof InputStream) { |
108 |
0 |
Object unmarshalled = unmarshaller.unmarshal((InputStream)value); |
109 |
0 |
return type.cast(unmarshalled); |
110 |
|
} |
111 |
0 |
if (value instanceof Reader) { |
112 |
0 |
Object unmarshalled = unmarshaller.unmarshal((Reader)value); |
113 |
0 |
return type.cast(unmarshalled); |
114 |
|
} |
115 |
0 |
return null; |
116 |
|
} |
117 |
|
|
118 |
|
protected <T> T marshall(Class<T> type, Object value) throws JAXBException { |
119 |
0 |
if (parentTypeConverter != null) { |
120 |
|
|
121 |
|
|
122 |
0 |
JAXBContext context = createContext(value.getClass()); |
123 |
0 |
JAXBSource source = new JAXBSource(context, value); |
124 |
0 |
T answer = parentTypeConverter.convertTo(type, source); |
125 |
0 |
if (answer == null) { |
126 |
|
|
127 |
0 |
StringWriter buffer = new StringWriter(); |
128 |
0 |
Marshaller marshaller = context.createMarshaller(); |
129 |
0 |
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isPrettyPrint() ? Boolean.TRUE : Boolean.FALSE); |
130 |
0 |
marshaller.marshal(value, buffer); |
131 |
0 |
return parentTypeConverter.convertTo(type, buffer.toString()); |
132 |
|
} |
133 |
0 |
return answer; |
134 |
|
} |
135 |
|
|
136 |
|
|
137 |
0 |
return null; |
138 |
|
} |
139 |
|
|
140 |
|
protected <T> JAXBContext createContext(Class<T> type) throws JAXBException { |
141 |
1 |
JAXBContext context = JAXBContext.newInstance(type); |
142 |
1 |
return context; |
143 |
|
} |
144 |
|
} |