File tree Expand file tree Collapse file tree 3 files changed +35
-4
lines changed
main/java/org/springframework/oxm/jaxb
test/java/org/springframework/oxm/jaxb Expand file tree Collapse file tree 3 files changed +35
-4
lines changed Original file line number Diff line number Diff line change 2828import org .apache .commons .logging .LogFactory ;
2929import org .springframework .beans .factory .InitializingBean ;
3030import 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.
Original file line number Diff line number Diff line change 2222import javax .xml .transform .Result ;
2323import javax .xml .transform .Source ;
2424
25- import org .springframework .beans . factory . InitializingBean ;
25+ import org .springframework .util . ClassUtils ;
2626import org .springframework .util .StringUtils ;
2727
2828/**
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 {
Original file line number Diff line number Diff 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 }
You can’t perform that action at this time.
0 commit comments