Skip to content

Commit cf00834

Browse files
committed
Improved Jaxb1Marshaller.supports() to use context path.
Added setContextPaths to AbstractJaxbMarshaller.
1 parent 91f2133 commit cf00834

File tree

3 files changed

+35
-4
lines changed

3 files changed

+35
-4
lines changed

oxm/src/main/java/org/springframework/oxm/jaxb/AbstractJaxbMarshaller.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
import org.apache.commons.logging.LogFactory;
2929
import org.springframework.beans.factory.InitializingBean;
3030
import org.springframework.oxm.XmlMappingException;
31+
import org.springframework.util.Assert;
32+
import org.springframework.util.StringUtils;
3133

3234
/**
3335
* Abstract base class for implementations of the <code>Marshaller</code> and <code>Unmarshaller</code> interfaces that
@@ -64,9 +66,19 @@ protected String getContextPath() {
6466

6567
/** Sets the JAXB Context path. */
6668
public void setContextPath(String contextPath) {
69+
Assert.notNull(contextPath, "'contextPath' must not be null");
6770
this.contextPath = contextPath;
6871
}
6972

73+
/**
74+
* Sets multiple JAXB Context paths. The given array of context paths is converted to a colon-delimited string, as
75+
* supported by JAXB.
76+
*/
77+
public void setContextPaths(String[] contextPaths) {
78+
Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
79+
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
80+
}
81+
7082
/**
7183
* Sets the JAXB <code>Marshaller</code> properties. These properties will be set on the underlying JAXB
7284
* <code>Marshaller</code>, and allow for features such as indentation.

oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb1Marshaller.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import javax.xml.transform.Result;
2323
import javax.xml.transform.Source;
2424

25-
import org.springframework.beans.factory.InitializingBean;
25+
import org.springframework.util.ClassUtils;
2626
import org.springframework.util.StringUtils;
2727

2828
/**
@@ -38,7 +38,7 @@
3838
* @see #setValidating(boolean)
3939
* @since 1.0.0
4040
*/
41-
public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements InitializingBean {
41+
public class Jaxb1Marshaller extends AbstractJaxbMarshaller {
4242

4343
private boolean validating = false;
4444

@@ -48,7 +48,26 @@ public void setValidating(boolean validating) {
4848
}
4949

5050
public boolean supports(Class clazz) {
51-
return Element.class.isAssignableFrom(clazz);
51+
if (!Element.class.isAssignableFrom(clazz)) {
52+
return false;
53+
}
54+
if (StringUtils.hasLength(getContextPath())) {
55+
String className = ClassUtils.getQualifiedName(clazz);
56+
int lastDotIndex = className.lastIndexOf('.');
57+
if (lastDotIndex == -1) {
58+
return false;
59+
}
60+
String packageName = className.substring(0, lastDotIndex);
61+
String[] contextPaths = StringUtils.tokenizeToStringArray(getContextPath(), ":");
62+
for (int i = 0; i < contextPaths.length; i++) {
63+
if (contextPaths[i].equals(packageName)) {
64+
return true;
65+
}
66+
}
67+
return false;
68+
}
69+
return false;
70+
5271
}
5372

5473
protected final JAXBContext createJaxbContext() throws JAXBException {

oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb1MarshallerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class Jaxb1MarshallerTest extends AbstractJaxbMarshallerTestCase {
3131

3232
protected final Marshaller createMarshaller() throws Exception {
3333
Jaxb1Marshaller marshaller = new Jaxb1Marshaller();
34-
marshaller.setContextPath(CONTEXT_PATH);
34+
marshaller.setContextPaths(new String[]{CONTEXT_PATH});
3535
marshaller.afterPropertiesSet();
3636
return marshaller;
3737
}

0 commit comments

Comments
 (0)