01: /*
02: * Copyright 2005 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.springframework.oxm.jaxb;
17:
18: import javax.xml.bind.Element;
19: import javax.xml.bind.JAXBContext;
20: import javax.xml.bind.JAXBException;
21: import javax.xml.bind.Unmarshaller;
22: import javax.xml.transform.Result;
23: import javax.xml.transform.Source;
24:
25: import org.springframework.beans.factory.InitializingBean;
26: import org.springframework.util.StringUtils;
27:
28: /**
29: * Implementation of the <code>Marshaller</code> interface for JAXB 1.0.
30: * <p/>
31: * The typical usage will be to set the <code>contextPath</code> property on this bean, possibly customize the
32: * marshaller and unmarshaller by setting properties, and validations, and to refer to it.
33: *
34: * @author Arjen Poutsma
35: * @see #setContextPath(String)
36: * @see #setMarshallerProperties(java.util.Map)
37: * @see #setUnmarshallerProperties(java.util.Map)
38: * @see #setValidating(boolean)
39: * @since 1.0.0
40: */
41: public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements
42: InitializingBean {
43:
44: private boolean validating = false;
45:
46: /** Set if the JAXB <code>Unmarshaller</code> should validate the incoming document. Default is <code>false</code>. */
47: public void setValidating(boolean validating) {
48: this .validating = validating;
49: }
50:
51: public boolean supports(Class clazz) {
52: return Element.class.isAssignableFrom(clazz);
53: }
54:
55: protected final JAXBContext createJaxbContext()
56: throws JAXBException {
57: if (!StringUtils.hasLength(getContextPath())) {
58: throw new IllegalArgumentException(
59: "contextPath is required");
60: }
61: if (logger.isInfoEnabled()) {
62: logger.info("Creating JAXBContext with context path ["
63: + getContextPath() + "]");
64: }
65: return JAXBContext.newInstance(getContextPath());
66: }
67:
68: protected void initJaxbUnmarshaller(Unmarshaller unmarshaller)
69: throws JAXBException {
70: unmarshaller.setValidating(validating);
71: }
72:
73: public void marshal(Object graph, Result result) {
74: try {
75: createMarshaller().marshal(graph, result);
76: } catch (JAXBException ex) {
77: throw convertJaxbException(ex);
78: }
79: }
80:
81: public Object unmarshal(Source source) {
82: try {
83: return createUnmarshaller().unmarshal(source);
84: } catch (JAXBException ex) {
85: throw convertJaxbException(ex);
86: }
87: }
88: }
|