diff --git a/oxm/src/main/java/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.java b/oxm/src/main/java/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.java
index 914cabbb..2212f327 100644
--- a/oxm/src/main/java/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.java
+++ b/oxm/src/main/java/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.java
@@ -28,6 +28,8 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.oxm.XmlMappingException;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
/**
* Abstract base class for implementations of the Marshaller and Unmarshaller interfaces that
@@ -64,9 +66,19 @@ public abstract class AbstractJaxbMarshaller
/** Sets the JAXB Context path. */
public void setContextPath(String contextPath) {
+ Assert.notNull(contextPath, "'contextPath' must not be null");
this.contextPath = contextPath;
}
+ /**
+ * Sets multiple JAXB Context paths. The given array of context paths is converted to a colon-delimited string, as
+ * supported by JAXB.
+ */
+ public void setContextPaths(String[] contextPaths) {
+ Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
+ this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
+ }
+
/**
* Sets the JAXB Marshaller properties. These properties will be set on the underlying JAXB
* Marshaller, and allow for features such as indentation.
diff --git a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java
index 388b5c78..f403d0d9 100644
--- a/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java
+++ b/oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java
@@ -22,7 +22,7 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
-import org.springframework.beans.factory.InitializingBean;
+import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
/**
@@ -38,7 +38,7 @@ import org.springframework.util.StringUtils;
* @see #setValidating(boolean)
* @since 1.0.0
*/
-public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements InitializingBean {
+public class Jaxb1Marshaller extends AbstractJaxbMarshaller {
private boolean validating = false;
@@ -48,7 +48,26 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements Initializ
}
public boolean supports(Class clazz) {
- return Element.class.isAssignableFrom(clazz);
+ if (!Element.class.isAssignableFrom(clazz)) {
+ return false;
+ }
+ if (StringUtils.hasLength(getContextPath())) {
+ String className = ClassUtils.getQualifiedName(clazz);
+ int lastDotIndex = className.lastIndexOf('.');
+ if (lastDotIndex == -1) {
+ return false;
+ }
+ String packageName = className.substring(0, lastDotIndex);
+ String[] contextPaths = StringUtils.tokenizeToStringArray(getContextPath(), ":");
+ for (int i = 0; i < contextPaths.length; i++) {
+ if (contextPaths[i].equals(packageName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+ return false;
+
}
protected final JAXBContext createJaxbContext() throws JAXBException {
diff --git a/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java b/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java
index 063ae1f5..74e3018a 100644
--- a/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java
+++ b/oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java
@@ -31,7 +31,7 @@ public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
protected final Marshaller createMarshaller() throws Exception {
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
- marshaller.setContextPath(CONTEXT_PATH);
+ marshaller.setContextPaths(new String[]{CONTEXT_PATH});
marshaller.afterPropertiesSet();
return marshaller;
}