This commit is contained in:
Arjen Poutsma
2007-11-10 13:33:03 +00:00
parent 4965d354e9
commit 7ca4e47c60
3 changed files with 114 additions and 83 deletions

View File

@@ -1,4 +1,5 @@
<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>
@@ -26,8 +27,8 @@
<configuration>
<tasks>
<ant antfile="${basedir}/build-maven2.xml" inheritRefs="true">
<property name="maven.test.skip" value="${maven.test.skip}" />
<target name="generate-test-sources" />
<property name="maven.test.skip" value="${maven.test.skip}"/>
<target name="generate-test-sources"/>
</ant>
</tasks>
<testSourceRoot>${project.build.directory}/generated-sources/test/java</testSourceRoot>
@@ -99,13 +100,13 @@
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.3</version>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.1.3</version>
<version>2.1.5</version>
<scope>test</scope>
</dependency>
<!-- XStream -->

View File

@@ -16,22 +16,34 @@
package org.springframework.oxm.jaxb;
import org.springframework.core.io.Resource;
import org.springframework.oxm.GenericMarshaller;
import org.springframework.oxm.GenericUnmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.oxm.mime.MimeMarshaller;
import org.springframework.oxm.mime.MimeUnmarshaller;
import org.springframework.util.*;
import org.springframework.xml.transform.StaxResult;
import org.springframework.xml.transform.StaxSource;
import org.springframework.xml.validation.SchemaLoaderUtils;
import java.awt.Image;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.UUID;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.xml.XMLConstants;
import javax.xml.bind.*;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
@@ -43,18 +55,23 @@ import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.validation.Schema;
import java.awt.*;
import java.io.*;
import java.lang.reflect.GenericArrayType;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.*;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.oxm.GenericMarshaller;
import org.springframework.oxm.GenericUnmarshaller;
import org.springframework.oxm.XmlMappingException;
import org.springframework.oxm.mime.MimeContainer;
import org.springframework.oxm.mime.MimeMarshaller;
import org.springframework.oxm.mime.MimeUnmarshaller;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.xml.transform.StaxResult;
import org.springframework.xml.transform.StaxSource;
import org.springframework.xml.validation.SchemaLoaderUtils;
/**
* Implementation of the <code>Marshaller</code> interface for JAXB 2.0.
@@ -77,7 +94,9 @@ import java.util.*;
* @since 1.0.0
*/
public class Jaxb2Marshaller extends AbstractJaxbMarshaller
implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller {
implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware {
private ClassLoader classLoader;
private Resource[] schemaResources;
@@ -97,6 +116,10 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
private boolean mtomEnabled = false;
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
/**
* Sets the <code>XmlAdapter</code>s to be registered with the JAXB <code>Marshaller</code> and
* <code>Unmarshaller</code>
@@ -123,9 +146,7 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
this.jaxbContextProperties = jaxbContextProperties;
}
/**
* Sets the <code>Marshaller.Listener</code> to be registered with the JAXB <code>Marshaller</code>.
*/
/** Sets the <code>Marshaller.Listener</code> to be registered with the JAXB <code>Marshaller</code>. */
public void setMarshallerListener(Marshaller.Listener marshallerListener) {
this.marshallerListener = marshallerListener;
}
@@ -148,23 +169,17 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
this.schemaLanguage = schemaLanguage;
}
/**
* Sets the schema resource to use for validation.
*/
/** Sets the schema resource to use for validation. */
public void setSchema(Resource schemaResource) {
schemaResources = new Resource[]{schemaResource};
}
/**
* Sets the schema resources to use for validation.
*/
/** Sets the schema resources to use for validation. */
public void setSchemas(Resource[] schemaResources) {
this.schemaResources = schemaResources;
}
/**
* Sets the <code>Unmarshaller.Listener</code> to be registered with the JAXB <code>Unmarshaller</code>.
*/
/** Sets the <code>Unmarshaller.Listener</code> to be registered with the JAXB <code>Unmarshaller</code>. */
public void setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener) {
this.unmarshallerListener = unmarshallerListener;
}
@@ -172,7 +187,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
public boolean supports(Type type) {
if (type instanceof Class) {
return supportsInternal((Class) type, true);
} else if (type instanceof ParameterizedType) {
}
else if (type instanceof ParameterizedType) {
ParameterizedType parameterizedType = (ParameterizedType) type;
if (JAXBElement.class.equals(parameterizedType.getRawType())) {
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1,
@@ -187,7 +203,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
else if (typeArgument instanceof GenericArrayType) {
GenericArrayType genericArrayType = (GenericArrayType) typeArgument;
return genericArrayType.getGenericComponentType().equals(Byte.TYPE);
} else if (!supports(typeArgument)) {
}
else if (!supports(typeArgument)) {
return false;
}
return true;
@@ -197,31 +214,18 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
private boolean isPrimitiveType(Class clazz) {
return (Boolean.class.equals(clazz) ||
Byte.class.equals(clazz) ||
Short.class.equals(clazz) ||
Integer.class.equals(clazz) ||
Long.class.equals(clazz) ||
Float.class.equals(clazz) ||
Double.class.equals(clazz) ||
byte[].class.equals(clazz));
return (Boolean.class.equals(clazz) || Byte.class.equals(clazz) || Short.class.equals(clazz) ||
Integer.class.equals(clazz) || Long.class.equals(clazz) || Float.class.equals(clazz) ||
Double.class.equals(clazz) || byte[].class.equals(clazz));
}
private boolean isStandardType(Class clazz) {
return (String.class.equals(clazz) ||
BigInteger.class.equals(clazz) ||
BigDecimal.class.equals(clazz) ||
Calendar.class.isAssignableFrom(clazz) ||
Date.class.isAssignableFrom(clazz) ||
QName.class.equals(clazz) ||
URI.class.equals(clazz) ||
XMLGregorianCalendar.class.isAssignableFrom(clazz) ||
Duration.class.isAssignableFrom(clazz) ||
Object.class.equals(clazz) ||
Image.class.isAssignableFrom(clazz) ||
DataHandler.class.equals(clazz) ||
Source.class.isAssignableFrom(clazz) ||
UUID.class.equals(clazz));
return (String.class.equals(clazz) || BigInteger.class.equals(clazz) || BigDecimal.class.equals(clazz) ||
Calendar.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) ||
QName.class.equals(clazz) || URI.class.equals(clazz) ||
XMLGregorianCalendar.class.isAssignableFrom(clazz) || Duration.class.isAssignableFrom(clazz) ||
Object.class.equals(clazz) || Image.class.isAssignableFrom(clazz) || DataHandler.class.equals(clazz) ||
Source.class.isAssignableFrom(clazz) || UUID.class.equals(clazz));
}
public boolean supports(Class clazz) {
@@ -249,7 +253,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
}
return false;
} else if (!ObjectUtils.isEmpty(classesToBeBound)) {
}
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
return Arrays.asList(classesToBeBound).contains(clazz);
}
return false;
@@ -276,9 +281,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
if (StringUtils.hasLength(getContextPath())) {
return createJaxbContextFromContextPath();
} else if (!ObjectUtils.isEmpty(classesToBeBound)) {
}
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
return createJaxbContextFromClasses();
} else {
}
else {
throw new IllegalArgumentException("setting either contextPath or classesToBeBound is required");
}
}
@@ -288,10 +295,18 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
logger.info("Creating JAXBContext with context path [" + getContextPath() + "]");
}
if (jaxbContextProperties != null) {
return JAXBContext
.newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), jaxbContextProperties);
} else {
return JAXBContext.newInstance(getContextPath());
if (classLoader != null) {
return JAXBContext
.newInstance(getContextPath(), classLoader, jaxbContextProperties);
}
else {
return JAXBContext
.newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), jaxbContextProperties);
}
}
else {
return classLoader != null ? JAXBContext.newInstance(getContextPath(), classLoader) :
JAXBContext.newInstance(getContextPath());
}
}
@@ -302,7 +317,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
if (jaxbContextProperties != null) {
return JAXBContext.newInstance(classesToBeBound, jaxbContextProperties);
} else {
}
else {
return JAXBContext.newInstance(classesToBeBound);
}
}
@@ -355,7 +371,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
if (result instanceof StaxResult) {
marshalStaxResult(marshaller, graph, (StaxResult) result);
} else {
}
else {
marshaller.marshal(graph, result);
}
}
@@ -368,9 +385,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
throws JAXBException {
if (staxResult.getXMLStreamWriter() != null) {
jaxbMarshaller.marshal(graph, staxResult.getXMLStreamWriter());
} else if (staxResult.getXMLEventWriter() != null) {
}
else if (staxResult.getXMLEventWriter() != null) {
jaxbMarshaller.marshal(graph, staxResult.getXMLEventWriter());
} else {
}
else {
throw new IllegalArgumentException("StaxResult contains neither XMLStreamWriter nor XMLEventConsumer");
}
}
@@ -391,7 +410,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
}
if (source instanceof StaxSource) {
return unmarshalStaxSource(unmarshaller, (StaxSource) source);
} else {
}
else {
return unmarshaller.unmarshal(source);
}
}
@@ -403,9 +423,11 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
private Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, StaxSource staxSource) throws JAXBException {
if (staxSource.getXMLStreamReader() != null) {
return jaxbUnmarshaller.unmarshal(staxSource.getXMLStreamReader());
} else if (staxSource.getXMLEventReader() != null) {
}
else if (staxSource.getXMLEventReader() != null) {
return jaxbUnmarshaller.unmarshal(staxSource.getXMLEventReader());
} else {
}
else {
throw new IllegalArgumentException("StaxSource contains neither XMLStreamReader nor XMLEventReader");
}
}

View File

@@ -22,6 +22,7 @@ import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
@@ -38,10 +39,16 @@ import org.springframework.util.StringUtils;
* @see #setValidating(boolean)
* @since 1.0.0
*/
public class Jaxb1Marshaller extends AbstractJaxbMarshaller {
public class Jaxb1Marshaller extends AbstractJaxbMarshaller implements BeanClassLoaderAware {
private boolean validating = false;
private ClassLoader classLoader;
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
/** Set if the JAXB <code>Unmarshaller</code> should validate the incoming document. Default is <code>false</code>. */
public void setValidating(boolean validating) {
this.validating = validating;
@@ -77,7 +84,8 @@ public class Jaxb1Marshaller extends AbstractJaxbMarshaller {
if (logger.isInfoEnabled()) {
logger.info("Creating JAXBContext with context path [" + getContextPath() + "]");
}
return JAXBContext.newInstance(getContextPath());
return classLoader != null ? JAXBContext.newInstance(getContextPath(), classLoader) :
JAXBContext.newInstance(getContextPath());
}
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {