Fixed #SWS-88: Allow CastorMarshaller to accept more than one mapping file

This commit is contained in:
Arjen Poutsma
2007-02-14 15:36:45 +00:00
parent e1c721f125
commit 1edf7347fe
3 changed files with 169 additions and 27 deletions

View File

@@ -18,22 +18,22 @@
<target name="generate-test-jaxb" depends="generate-test-init">
<taskdef name="xjc" classname="com.sun.tools.xjc.XJCTask"
classpathref="maven.test.classpath"/>
classpathref="maven.plugin.classpath"/>
<xjc target="${generate.target.directory}"
package="org.springframework.oxm.jaxb1"
schema="${schema}">
<produces dir="${generate.target.directory}" includes="**/*.java"/>
<produces dir="${generate.target.directory}" includes="**/jaxb1/**/*.java"/>
</xjc>
</target>
<target name="generate-test-xmlbeans">
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="maven.test.classpath"/>
<xmlbean schema="${schema}" classgendir="${basedir}/target/test-classes" classpathref="maven.test.classpath"/>
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="maven.plugin.classpath"/>
<xmlbean schema="${schema}" classgendir="${basedir}/target/test-classes" classpathref="maven.plugin.classpath"/>
</target>
<target name="generate-test-castor" depends="generate-test-init">
<taskdef name="castor-srcgen" classname="org.exolab.castor.tools.ant.taskdefs.CastorSourceGenTask"
classpathref="maven.test.classpath"/>
classpathref="maven.plugin.classpath"/>
<castor-srcgen types="j2" warnings="false" file="${schema}"
todir="${generate.target.directory}" package="org.springframework.oxm.castor"/>
</target>

View File

@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<parent>
<artifactId>spring-ws</artifactId>
<groupId>org.springframework.ws</groupId>
@@ -30,7 +31,7 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<target name="generate-test-sources" />
<target name="generate-test-sources"/>
</ant>
</tasks>
<testSourceRoot>${project.build.directory}/generated-sources/test/java</testSourceRoot>
@@ -45,7 +46,7 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<target name="test-compile" />
<target name="test-compile"/>
</ant>
</tasks>
</configuration>
@@ -54,6 +55,128 @@
</goals>
</execution>
</executions>
<dependencies>
<!-- JAXB dependencies -->
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-libs</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>com.sun.msv.datatype.xsd</groupId>
<artifactId>xsdlib</artifactId>
<version>20060615</version>
</dependency>
<!-- XMLBeans dependencies -->
<dependency>
<groupId>xmlbeans</groupId>
<artifactId>xbean</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>stax</groupId>
<artifactId>stax-api</artifactId>
<version>1.0.1</version>
</dependency>
<!-- Castor dependencies -->
<dependency>
<groupId>org.codehaus.castor</groupId>
<artifactId>castor</artifactId>
<version>1.0.5</version>
<exclusions>
<exclusion>
<groupId>adaptx</groupId>
<artifactId>adaptx</artifactId>
</exclusion>
<exclusion>
<groupId>ant</groupId>
<artifactId>ant</artifactId>
</exclusion>
<exclusion>
<groupId>cglib</groupId>
<artifactId>cglib-full</artifactId>
</exclusion>
<exclusion>
<groupId>com.cenqua.clover</groupId>
<artifactId>clover</artifactId>
</exclusion>
<exclusion>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
</exclusion>
<exclusion>
<groupId>jakarta-regexp</groupId>
<artifactId>jakarta-regexp</artifactId>
</exclusion>
<exclusion>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
</exclusion>
<exclusion>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
</exclusion>
<exclusion>
<groupId>ldapsdk</groupId>
<artifactId>ldapsdk</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>xerces</groupId>
<artifactId>xerces</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.8.1</version>
</dependency>
<!-- Apache Data Binding dependencies -->
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-adb-codegen</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-kernel</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.ws.commons.schema</groupId>
<artifactId>XmlSchema</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<artifactId>axiom-impl</artifactId>
<groupId>org.apache.ws.commons.axiom</groupId>
<version>1.2.1</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
@@ -153,11 +276,6 @@
<artifactId>jaxb-libs</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.msv.datatype.xsd</groupId>
<artifactId>xsdlib</artifactId>

View File

@@ -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;
* <p/>
* If a target class is specified using <code>setTargetClass</code>, the <code>CastorMarshaller</code> 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 <code>setMappingLocation</code>.
* provide a mapping file using <code>setMappingLocations</code>.
* <p/>
* Due to Castor's API, it is required to set the encoding used for writing to output streams. It defaults to
* <code>UTF-8</code>.
@@ -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 <code>XMLClassDescriptorResolver</code>. Subclasses can override this to create a custom
* resolver.
* <p/>
* 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;
}
/**