1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.oxm.jaxb;
18
19 import java.util.Iterator;
20 import java.util.Map;
21 import javax.xml.bind.JAXBContext;
22 import javax.xml.bind.JAXBException;
23 import javax.xml.bind.Marshaller;
24 import javax.xml.bind.Unmarshaller;
25 import javax.xml.bind.ValidationEventHandler;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.oxm.XmlMappingException;
31 import org.springframework.util.Assert;
32 import org.springframework.util.StringUtils;
33
34
35
36
37
38
39
40
41
42
43
44
45
46 public abstract class AbstractJaxbMarshaller
47 implements org.springframework.oxm.Marshaller, org.springframework.oxm.Unmarshaller, InitializingBean {
48
49
50 protected final Log logger = LogFactory.getLog(getClass());
51
52 private String contextPath;
53
54 private Map marshallerProperties;
55
56 private Map unmarshallerProperties;
57
58 private JAXBContext jaxbContext;
59
60 private ValidationEventHandler validationEventHandler;
61
62
63 protected String getContextPath() {
64 return contextPath;
65 }
66
67
68 public void setContextPath(String contextPath) {
69 Assert.notNull(contextPath, "'contextPath' must not be null");
70 this.contextPath = contextPath;
71 }
72
73
74
75
76
77 public void setContextPaths(String[] contextPaths) {
78 Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
79 this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93 public void setMarshallerProperties(Map properties) {
94 this.marshallerProperties = properties;
95 }
96
97
98
99
100
101
102
103
104 public void setUnmarshallerProperties(Map properties) {
105 this.unmarshallerProperties = properties;
106 }
107
108
109
110
111
112
113
114 public void setValidationEventHandler(ValidationEventHandler validationEventHandler) {
115 this.validationEventHandler = validationEventHandler;
116 }
117
118
119 public JAXBContext getJaxbContext() {
120 return jaxbContext;
121 }
122
123 public final void afterPropertiesSet() throws Exception {
124 try {
125 jaxbContext = createJaxbContext();
126 }
127 catch (JAXBException ex) {
128 throw convertJaxbException(ex);
129 }
130 }
131
132
133
134
135
136
137
138
139
140
141
142 protected XmlMappingException convertJaxbException(JAXBException ex) {
143 return JaxbUtils.convertJaxbException(ex);
144 }
145
146
147 protected Marshaller createMarshaller() {
148 try {
149 Marshaller marshaller = jaxbContext.createMarshaller();
150 if (marshallerProperties != null) {
151 for (Iterator iterator = marshallerProperties.keySet().iterator(); iterator.hasNext();) {
152 String name = (String) iterator.next();
153 marshaller.setProperty(name, marshallerProperties.get(name));
154 }
155 }
156 if (validationEventHandler != null) {
157 marshaller.setEventHandler(validationEventHandler);
158 }
159 initJaxbMarshaller(marshaller);
160 return marshaller;
161 }
162 catch (JAXBException ex) {
163 throw convertJaxbException(ex);
164 }
165 }
166
167
168 protected Unmarshaller createUnmarshaller() {
169 try {
170 Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
171 if (unmarshallerProperties != null) {
172 for (Iterator iterator = unmarshallerProperties.keySet().iterator(); iterator.hasNext();) {
173 String name = (String) iterator.next();
174 unmarshaller.setProperty(name, unmarshallerProperties.get(name));
175 }
176 }
177 if (validationEventHandler != null) {
178 unmarshaller.setEventHandler(validationEventHandler);
179 }
180 initJaxbUnmarshaller(unmarshaller);
181 return unmarshaller;
182 }
183 catch (JAXBException ex) {
184 throw convertJaxbException(ex);
185 }
186 }
187
188
189
190
191
192
193
194 protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
195 }
196
197
198
199
200
201
202
203 protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
204 }
205
206
207 protected abstract JAXBContext createJaxbContext() throws Exception;
208 }