diff --git a/oxm/build-maven2.xml b/oxm/build-maven2.xml index 378f1832..759663b5 100644 --- a/oxm/build-maven2.xml +++ b/oxm/build-maven2.xml @@ -18,22 +18,22 @@ + classpathref="maven.plugin.classpath"/> - + - - + + + classpathref="maven.plugin.classpath"/> diff --git a/oxm/pom.xml b/oxm/pom.xml index 17321c92..0abc5f5f 100644 --- a/oxm/pom.xml +++ b/oxm/pom.xml @@ -1,5 +1,6 @@ - + spring-ws org.springframework.ws @@ -30,7 +31,7 @@ - + ${project.build.directory}/generated-sources/test/java @@ -45,7 +46,7 @@ - + @@ -54,6 +55,128 @@ + + + + javax.xml.bind + jaxb-api + 1.0 + + + com.sun.xml.bind + jaxb-xjc + 1.0.6 + + + com.sun.xml.bind + jaxb-impl + 1.0.6 + + + com.sun.xml.bind + jaxb-libs + 1.0.6 + + + com.sun.msv.datatype.xsd + xsdlib + 20060615 + + + + xmlbeans + xbean + 2.1.0 + + + stax + stax-api + 1.0.1 + + + + org.codehaus.castor + castor + 1.0.5 + + + adaptx + adaptx + + + ant + ant + + + cglib + cglib-full + + + com.cenqua.clover + clover + + + oro + oro + + + jakarta-regexp + jakarta-regexp + + + javax.servlet + servlet-api + + + javax.transaction + jta + + + ldapsdk + ldapsdk + + + log4j + log4j + + + xerces + xerces + + + + + xerces + xercesImpl + 2.8.1 + + + + org.apache.axis2 + axis2-adb + 1.1.1 + + + org.apache.axis2 + axis2-adb-codegen + 1.1.1 + + + org.apache.axis2 + axis2-kernel + 1.1.1 + + + org.apache.ws.commons.schema + XmlSchema + 1.2 + + + axiom-impl + org.apache.ws.commons.axiom + 1.2.1 + + @@ -153,11 +276,6 @@ jaxb-libs test - - com.sun.xml.bind - jaxb-xjc - test - com.sun.msv.datatype.xsd xsdlib diff --git a/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java b/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java index 5f4bfbc5..e8c2b4af 100644 --- a/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java +++ b/oxm/src/main/java/org/springframework/oxm/castor/CastorMarshaller.java @@ -42,6 +42,8 @@ import org.springframework.beans.factory.InitializingBean; import org.springframework.core.io.Resource; import org.springframework.oxm.AbstractMarshaller; import org.springframework.oxm.XmlMappingException; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StringUtils; import org.springframework.xml.dom.DomContentHandler; import org.springframework.xml.stream.StaxEventContentHandler; import org.springframework.xml.stream.StaxEventXmlReader; @@ -61,7 +63,7 @@ import org.xml.sax.ext.LexicalHandler; *

* If a target class is specified using setTargetClass, the CastorMarshaller can only be used * to unmarshall XML that represents that specific class. If you want to unmarshall multiple classes, you have to - * provide a mapping file using setMappingLocation. + * provide a mapping file using setMappingLocations. *

* Due to Castor's API, it is required to set the encoding used for writing to output streams. It defaults to * UTF-8. @@ -70,6 +72,7 @@ import org.xml.sax.ext.LexicalHandler; * @see #setEncoding(String) * @see #setTargetClass(Class) * @see #setMappingLocation(org.springframework.core.io.Resource) + * @see #setMappingLocations(org.springframework.core.io.Resource[]) */ public class CastorMarshaller extends AbstractMarshaller implements InitializingBean { @@ -78,7 +81,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing */ public static final String DEFAULT_ENCODING = "UTF-8"; - private Resource mappingLocation; + private Resource[] mappingLocations; private String encoding; @@ -115,20 +118,27 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } /** - * Sets the location of the Castor XML Mapping file. + * Sets the locations of the Castor XML Mapping files. */ public void setMappingLocation(Resource mappingLocation) { - this.mappingLocation = mappingLocation; + mappingLocations = new Resource[]{mappingLocation}; + } + + /** + * Sets the locations of the Castor XML Mapping files. + */ + public void setMappingLocations(Resource[] mappingLocations) { + this.mappingLocations = mappingLocations; } public final void afterPropertiesSet() throws IOException { - if (mappingLocation != null && targetClass != null) { - throw new IllegalArgumentException("Cannot set both the 'mappingLocation' and 'targetClass' property. " + - "Set targetClass for unmarshalling a single class, and 'mappingLocation' for multiple classes'"); + if (mappingLocations != null && targetClass != null) { + throw new IllegalArgumentException("Cannot set both the 'mappingLocations' and 'targetClass' property. " + + "Set targetClass for unmarshalling a single class, and 'mappingLocations' for multiple classes'"); } if (logger.isInfoEnabled()) { - if (mappingLocation != null) { - logger.info("Configured using " + mappingLocation); + if (mappingLocations != null) { + logger.info("Configured using " + StringUtils.arrayToCommaDelimitedString(mappingLocations)); } else if (targetClass != null) { logger.info("Configured for target class [" + targetClass.getName() + "]"); @@ -138,19 +148,32 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing } } try { - createClassDescriptorResolver(); + classDescriptorResolver = createClassDescriptorResolver(mappingLocations, targetClass); } catch (MappingException ex) { throw new CastorSystemException("Could not load Castor mapping: " + ex.getMessage(), ex); } } - private void createClassDescriptorResolver() throws MappingException, IOException { - classDescriptorResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory + /** + * Creates the Castor XMLClassDescriptorResolver. Subclasses can override this to create a custom + * resolver. + *

+ * The default implementation loads mapping files if defined, or loads the target class if not defined. + * + * @return the created resolver + * @throws MappingException when the mapping file cannot be loaded + * @throws IOException in case of I/O errors + */ + protected XMLClassDescriptorResolver createClassDescriptorResolver(Resource[] mappingLocations, Class targetClass) + throws MappingException, IOException { + XMLClassDescriptorResolver classDescriptorResolver = (XMLClassDescriptorResolver) ClassDescriptorResolverFactory .createClassDescriptorResolver(BindingType.XML); - if (mappingLocation != null) { + if (!ObjectUtils.isEmpty(mappingLocations)) { Mapping mapping = new Mapping(); - mapping.loadMapping(new InputSource(mappingLocation.getInputStream())); + for (int i = 0; i < mappingLocations.length; i++) { + mapping.loadMapping(new InputSource(mappingLocations[i].getInputStream())); + } MappingUnmarshaller mappingUnmarshaller = new MappingUnmarshaller(); MappingLoader mappingLoader = mappingUnmarshaller.getMappingLoader(mapping, BindingType.XML); classDescriptorResolver.setMappingLoader(mappingLoader); @@ -159,6 +182,7 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing else if (targetClass != null) { classDescriptorResolver.setClassLoader(targetClass.getClassLoader()); } + return classDescriptorResolver; } /**