- Removed GenericMarshaller & GenericUnmarshaller
- Made Marshaller and Unmarshaller generic
This commit is contained in:
parent
56bdfae39a
commit
e966fd9605
|
@ -33,7 +33,6 @@
|
||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module" module-name="jms" />
|
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library">
|
||||||
<library>
|
<library>
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
|
@ -45,7 +44,6 @@
|
||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module" module-name="web-servlet" />
|
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library">
|
||||||
<library>
|
<library>
|
||||||
<CLASSES>
|
<CLASSES>
|
||||||
|
@ -112,15 +110,6 @@
|
||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
<orderEntry type="module-library">
|
|
||||||
<library>
|
|
||||||
<CLASSES>
|
|
||||||
<root url="jar://$APPLICATION_HOME_DIR$/lib/junit-4.5.jar!/" />
|
|
||||||
</CLASSES>
|
|
||||||
<JAVADOC />
|
|
||||||
<SOURCES />
|
|
||||||
</library>
|
|
||||||
</orderEntry>
|
|
||||||
<orderEntry type="module" module-name="context" />
|
<orderEntry type="module" module-name="context" />
|
||||||
<orderEntry type="module-library">
|
<orderEntry type="module-library">
|
||||||
<library>
|
<library>
|
||||||
|
@ -155,11 +144,180 @@
|
||||||
</SOURCES>
|
</SOURCES>
|
||||||
</library>
|
</library>
|
||||||
</orderEntry>
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$IVY_CACHE$/org.junit/com.springsource.org.junit/4.5.0/com.springsource.org.junit-4.5.0.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES>
|
||||||
|
<root url="jar://$IVY_CACHE$/org.junit/com.springsource.org.junit/4.5.0/com.springsource.org.junit-sources-4.5.0.jar!/" />
|
||||||
|
</SOURCES>
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
|
<orderEntry type="module-library">
|
||||||
|
<library>
|
||||||
|
<CLASSES>
|
||||||
|
<root url="jar://$IVY_CACHE$/javax.xml.bind/com.springsource.javax.xml.bind/2.1.7/com.springsource.javax.xml.bind-2.1.7.jar!/" />
|
||||||
|
</CLASSES>
|
||||||
|
<JAVADOC />
|
||||||
|
<SOURCES>
|
||||||
|
<root url="jar://$IVY_CACHE$/javax.xml.bind/com.springsource.javax.xml.bind/2.1.7/com.springsource.javax.xml.bind-sources-2.1.7.jar!/" />
|
||||||
|
</SOURCES>
|
||||||
|
</library>
|
||||||
|
</orderEntry>
|
||||||
</component>
|
</component>
|
||||||
<component name="copyright">
|
<component name="copyright">
|
||||||
<Base>
|
<Base>
|
||||||
<setting name="state" value="1" />
|
<setting name="state" value="1" />
|
||||||
</Base>
|
</Base>
|
||||||
|
<LanguageOptions name="HTML">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="1" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="JAVA">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="2" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="JSP">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="1" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="JavaScript">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="1" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="Properties">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="1" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="XML">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright (c) &#36;today.year, Your Corporation. All Rights Reserved." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="1" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
|
<LanguageOptions name="__TEMPLATE__">
|
||||||
|
<option name="templateOptions">
|
||||||
|
<value>
|
||||||
|
<option name="block" value="true" />
|
||||||
|
<option name="separateBefore" value="false" />
|
||||||
|
<option name="separateAfter" value="false" />
|
||||||
|
<option name="prefixLines" value="true" />
|
||||||
|
<option name="lenBefore" value="80" />
|
||||||
|
<option name="lenAfter" value="80" />
|
||||||
|
<option name="box" value="false" />
|
||||||
|
<option name="filler" value=" " />
|
||||||
|
</value>
|
||||||
|
</option>
|
||||||
|
<option name="notice" value="Copyright 2002-&#36;today.year the original author or authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License." />
|
||||||
|
<option name="keyword" value="Copyright" />
|
||||||
|
<option name="fileTypeOverride" value="4" />
|
||||||
|
<option name="relativeBefore" value="true" />
|
||||||
|
<option name="addBlankAfter" value="true" />
|
||||||
|
<option name="fileLocation" value="1" />
|
||||||
|
<option name="useAlternate" value="false" />
|
||||||
|
</LanguageOptions>
|
||||||
</component>
|
</component>
|
||||||
</module>
|
</module>
|
||||||
|
|
||||||
|
|
|
@ -1,40 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2007 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.oxm;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extension of the {@link Marshaller} interface that supports Java 5 generics. More specifically, this marshaller adds
|
|
||||||
* support for the new {@link Type} hierarchy, returned by methods such as {@link Method#getGenericParameterTypes()} and
|
|
||||||
* {@link Method#getGenericReturnType()}.
|
|
||||||
*
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
public interface GenericMarshaller extends Marshaller {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates whether this marshaller can marshal instances of the supplied type.
|
|
||||||
*
|
|
||||||
* @param type the type that this marshaller is being asked if it can marshal
|
|
||||||
* @return <code>true</code> if this marshaller can indeed marshal instances of the supplied type; <code>false</code>
|
|
||||||
* otherwise
|
|
||||||
*/
|
|
||||||
boolean supports(Type type);
|
|
||||||
}
|
|
|
@ -1,40 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2007 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.oxm;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.lang.reflect.Type;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Extension of the {@link Unmarshaller} interface that supports Java 5 generics. More specifically, this unmarshaller
|
|
||||||
* adds support for the new {@link Type} hierarchy, returned by methods such as {@link
|
|
||||||
* Method#getGenericParameterTypes()} and {@link Method#getGenericReturnType()}.
|
|
||||||
*
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
public interface GenericUnmarshaller extends Unmarshaller {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Indicates whether this unmarshaller can unmarshal instances of the supplied type.
|
|
||||||
*
|
|
||||||
* @param type the type that this unmarshaller is being asked if it can marshal
|
|
||||||
* @return <code>true</code> if this unmarshaller can indeed unmarshal to the supplied type; <code>false</code>
|
|
||||||
* otherwise
|
|
||||||
*/
|
|
||||||
boolean supports(Type type);
|
|
||||||
}
|
|
|
@ -46,6 +46,6 @@ public interface Marshaller {
|
||||||
* @return <code>true</code> if this marshaller can indeed marshal instances of the supplied class; <code>false</code>
|
* @return <code>true</code> if this marshaller can indeed marshal instances of the supplied class; <code>false</code>
|
||||||
* otherwise
|
* otherwise
|
||||||
*/
|
*/
|
||||||
boolean supports(Class clazz);
|
boolean supports(Class<?> clazz);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -44,6 +44,6 @@ public interface Unmarshaller {
|
||||||
* @return <code>true</code> if this unmarshaller can indeed unmarshal to the supplied class; <code>false</code>
|
* @return <code>true</code> if this unmarshaller can indeed unmarshal to the supplied class; <code>false</code>
|
||||||
* otherwise
|
* otherwise
|
||||||
*/
|
*/
|
||||||
boolean supports(Class clazz);
|
boolean supports(Class<?> clazz);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,9 @@ import org.springframework.util.xml.StaxUtils;
|
||||||
*/
|
*/
|
||||||
public class CastorMarshaller extends AbstractMarshaller implements InitializingBean {
|
public class CastorMarshaller extends AbstractMarshaller implements InitializingBean {
|
||||||
|
|
||||||
/** The default encoding used for stream access. */
|
/**
|
||||||
|
* The default encoding used for stream access.
|
||||||
|
*/
|
||||||
public static final String DEFAULT_ENCODING = "UTF-8";
|
public static final String DEFAULT_ENCODING = "UTF-8";
|
||||||
|
|
||||||
private Resource[] mappingLocations;
|
private Resource[] mappingLocations;
|
||||||
|
@ -96,7 +98,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
|
|
||||||
private Properties namespaceMappings;
|
private Properties namespaceMappings;
|
||||||
|
|
||||||
/** Returns whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field. */
|
/**
|
||||||
|
* Returns whether the Castor {@link Unmarshaller} should ignore attributes that do not match a specific field.
|
||||||
|
*/
|
||||||
public boolean getIgnoreExtraAttributes() {
|
public boolean getIgnoreExtraAttributes() {
|
||||||
return ignoreExtraAttributes;
|
return ignoreExtraAttributes;
|
||||||
}
|
}
|
||||||
|
@ -111,7 +115,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
this.ignoreExtraAttributes = ignoreExtraAttributes;
|
this.ignoreExtraAttributes = ignoreExtraAttributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns whether the Castor {@link Unmarshaller} should ignore elements that do not match a specific field. */
|
/**
|
||||||
|
* Returns whether the Castor {@link Unmarshaller} should ignore elements that do not match a specific field.
|
||||||
|
*/
|
||||||
public boolean getIgnoreExtraElements() {
|
public boolean getIgnoreExtraElements() {
|
||||||
return ignoreExtraElements;
|
return ignoreExtraElements;
|
||||||
}
|
}
|
||||||
|
@ -126,7 +132,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
this.ignoreExtraElements = ignoreExtraElements;
|
this.ignoreExtraElements = ignoreExtraElements;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace. */
|
/**
|
||||||
|
* Returns whether the Castor {@link Unmarshaller} should preserve "ignorable" whitespace.
|
||||||
|
*/
|
||||||
public boolean getWhitespacePreserve() {
|
public boolean getWhitespacePreserve() {
|
||||||
return whitespacePreserve;
|
return whitespacePreserve;
|
||||||
}
|
}
|
||||||
|
@ -140,7 +148,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
this.whitespacePreserve = whitespacePreserve;
|
this.whitespacePreserve = whitespacePreserve;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns whether this marshaller should validate in- and outgoing documents. */
|
/**
|
||||||
|
* Returns whether this marshaller should validate in- and outgoing documents.
|
||||||
|
*/
|
||||||
public boolean isValidating() {
|
public boolean isValidating() {
|
||||||
return validating;
|
return validating;
|
||||||
}
|
}
|
||||||
|
@ -154,7 +164,9 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
this.validating = validating;
|
this.validating = validating;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns the namespace mappings. Property names are interpreted as namespace prefixes; values are namespace URIs. */
|
/**
|
||||||
|
* Returns the namespace mappings. Property names are interpreted as namespace prefixes; values are namespace URIs.
|
||||||
|
*/
|
||||||
public Properties getNamespaceMappings() {
|
public Properties getNamespaceMappings() {
|
||||||
return namespaceMappings;
|
return namespaceMappings;
|
||||||
}
|
}
|
||||||
|
@ -177,12 +189,16 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
this.encoding = encoding;
|
this.encoding = encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the locations of the Castor XML Mapping files. */
|
/**
|
||||||
|
* Sets the locations of the Castor XML Mapping files.
|
||||||
|
*/
|
||||||
public void setMappingLocation(Resource mappingLocation) {
|
public void setMappingLocation(Resource mappingLocation) {
|
||||||
mappingLocations = new Resource[]{mappingLocation};
|
mappingLocations = new Resource[]{mappingLocation};
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the locations of the Castor XML Mapping files. */
|
/**
|
||||||
|
* Sets the locations of the Castor XML Mapping files.
|
||||||
|
*/
|
||||||
public void setMappingLocations(Resource[] mappingLocations) {
|
public void setMappingLocations(Resource[] mappingLocations) {
|
||||||
this.mappingLocations = mappingLocations;
|
this.mappingLocations = mappingLocations;
|
||||||
}
|
}
|
||||||
|
@ -223,8 +239,10 @@ public class CastorMarshaller extends AbstractMarshaller implements Initializing
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns <code>true</code> for all classes, i.e. Castor supports arbitrary classes. */
|
/**
|
||||||
public boolean supports(Class clazz) {
|
* Returns <code>true</code> for all classes, i.e. Castor supports arbitrary classes.
|
||||||
|
*/
|
||||||
|
public boolean supports(Class<?> clazz) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,204 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2006 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.oxm.jaxb;
|
|
||||||
|
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
|
||||||
import javax.xml.bind.JAXBContext;
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.Marshaller;
|
|
||||||
import javax.xml.bind.Unmarshaller;
|
|
||||||
import javax.xml.bind.ValidationEventHandler;
|
|
||||||
|
|
||||||
import org.apache.commons.logging.Log;
|
|
||||||
import org.apache.commons.logging.LogFactory;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.InitializingBean;
|
|
||||||
import org.springframework.oxm.XmlMappingException;
|
|
||||||
import org.springframework.util.Assert;
|
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Abstract base class for implementations of the <code>Marshaller</code> and <code>Unmarshaller</code> interfaces that
|
|
||||||
* use JAXB. This base class is responsible for creating JAXB marshallers from a <code>JAXBContext</code>. <p/> JAXB 2.0
|
|
||||||
* added breaking API changes, so specific subclasses must be used for JAXB 1.0 and 2.0 (<code>Jaxb1Marshaller</code>
|
|
||||||
* and <code>Jaxb2Marshaller</code> respectivaly).
|
|
||||||
*
|
|
||||||
* @author Arjen Poutsma
|
|
||||||
* @see Jaxb2Marshaller
|
|
||||||
* @since 3.0
|
|
||||||
*/
|
|
||||||
public abstract class AbstractJaxbMarshaller
|
|
||||||
implements org.springframework.oxm.Marshaller, org.springframework.oxm.Unmarshaller, InitializingBean {
|
|
||||||
|
|
||||||
/** Logger available to subclasses. */
|
|
||||||
protected final Log logger = LogFactory.getLog(getClass());
|
|
||||||
|
|
||||||
private String contextPath;
|
|
||||||
|
|
||||||
private Map marshallerProperties;
|
|
||||||
|
|
||||||
private Map unmarshallerProperties;
|
|
||||||
|
|
||||||
private JAXBContext jaxbContext;
|
|
||||||
|
|
||||||
private ValidationEventHandler validationEventHandler;
|
|
||||||
|
|
||||||
/** Returns the JAXB Context path. */
|
|
||||||
protected String getContextPath() {
|
|
||||||
return contextPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Sets the JAXB Context path. */
|
|
||||||
public void setContextPath(String contextPath) {
|
|
||||||
Assert.notNull(contextPath, "'contextPath' must not be null");
|
|
||||||
this.contextPath = contextPath;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets multiple JAXB Context paths. The given array of context paths is converted to a colon-delimited string, as
|
|
||||||
* supported by JAXB.
|
|
||||||
*/
|
|
||||||
public void setContextPaths(String[] contextPaths) {
|
|
||||||
Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
|
|
||||||
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the JAXB <code>Marshaller</code> properties. These properties will be set on the underlying JAXB
|
|
||||||
* <code>Marshaller</code>, and allow for features such as indentation.
|
|
||||||
*
|
|
||||||
* @param properties the properties
|
|
||||||
* @see javax.xml.bind.Marshaller#setProperty(String,Object)
|
|
||||||
* @see javax.xml.bind.Marshaller#JAXB_ENCODING
|
|
||||||
* @see javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT
|
|
||||||
* @see javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
|
|
||||||
* @see javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION
|
|
||||||
*/
|
|
||||||
public void setMarshallerProperties(Map properties) {
|
|
||||||
this.marshallerProperties = properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the JAXB <code>Unmarshaller</code> properties. These properties will be set on the underlying JAXB
|
|
||||||
* <code>Unmarshaller</code>.
|
|
||||||
*
|
|
||||||
* @param properties the properties
|
|
||||||
* @see javax.xml.bind.Unmarshaller#setProperty(String,Object)
|
|
||||||
*/
|
|
||||||
public void setUnmarshallerProperties(Map properties) {
|
|
||||||
this.unmarshallerProperties = properties;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Sets the JAXB validation event handler. This event handler will be called by JAXB if any validation errors are
|
|
||||||
* encountered during calls to any of the marshal API's.
|
|
||||||
*
|
|
||||||
* @param validationEventHandler the event handler
|
|
||||||
*/
|
|
||||||
public void setValidationEventHandler(ValidationEventHandler validationEventHandler) {
|
|
||||||
this.validationEventHandler = validationEventHandler;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns the {@link JAXBContext} created in {@link #afterPropertiesSet()}. */
|
|
||||||
public JAXBContext getJaxbContext() {
|
|
||||||
return jaxbContext;
|
|
||||||
}
|
|
||||||
|
|
||||||
public final void afterPropertiesSet() throws Exception {
|
|
||||||
try {
|
|
||||||
jaxbContext = createJaxbContext();
|
|
||||||
}
|
|
||||||
catch (JAXBException ex) {
|
|
||||||
throw convertJaxbException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Convert the given <code>JAXBException</code> to an appropriate exception from the
|
|
||||||
* <code>org.springframework.oxm</code> hierarchy. <p/> The default implementation delegates to <code>JaxbUtils</code>.
|
|
||||||
* Can be overridden in subclasses.
|
|
||||||
*
|
|
||||||
* @param ex <code>JAXBException</code> that occured
|
|
||||||
* @return the corresponding <code>XmlMappingException</code> instance
|
|
||||||
* @see JaxbUtils#convertJaxbException
|
|
||||||
*/
|
|
||||||
protected XmlMappingException convertJaxbException(JAXBException ex) {
|
|
||||||
return JaxbUtils.convertJaxbException(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe. */
|
|
||||||
protected Marshaller createMarshaller() {
|
|
||||||
try {
|
|
||||||
Marshaller marshaller = jaxbContext.createMarshaller();
|
|
||||||
if (marshallerProperties != null) {
|
|
||||||
for (Iterator iterator = marshallerProperties.keySet().iterator(); iterator.hasNext();) {
|
|
||||||
String name = (String) iterator.next();
|
|
||||||
marshaller.setProperty(name, marshallerProperties.get(name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (validationEventHandler != null) {
|
|
||||||
marshaller.setEventHandler(validationEventHandler);
|
|
||||||
}
|
|
||||||
initJaxbMarshaller(marshaller);
|
|
||||||
return marshaller;
|
|
||||||
}
|
|
||||||
catch (JAXBException ex) {
|
|
||||||
throw convertJaxbException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Returns a newly created JAXB unmarshaller. JAXB unmarshallers are not necessarily thread safe. */
|
|
||||||
protected Unmarshaller createUnmarshaller() {
|
|
||||||
try {
|
|
||||||
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
|
||||||
if (unmarshallerProperties != null) {
|
|
||||||
for (Iterator iterator = unmarshallerProperties.keySet().iterator(); iterator.hasNext();) {
|
|
||||||
String name = (String) iterator.next();
|
|
||||||
unmarshaller.setProperty(name, unmarshallerProperties.get(name));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (validationEventHandler != null) {
|
|
||||||
unmarshaller.setEventHandler(validationEventHandler);
|
|
||||||
}
|
|
||||||
initJaxbUnmarshaller(unmarshaller);
|
|
||||||
return unmarshaller;
|
|
||||||
}
|
|
||||||
catch (JAXBException ex) {
|
|
||||||
throw convertJaxbException(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called
|
|
||||||
* after creation of JAXB <code>Marshaller</code>, and after the respective properties have been set. <p/> Default
|
|
||||||
* implementation does nothing.
|
|
||||||
*/
|
|
||||||
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Template method that can overridden by concrete JAXB marshallers for custom initialization behavior. Gets called
|
|
||||||
* after creation of JAXB <code>Unmarshaller</code>, and after the respective properties have been set. <p/> Default
|
|
||||||
* implementation does nothing.
|
|
||||||
*/
|
|
||||||
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Template method that returns a newly created JAXB context. Called from <code>afterPropertiesSet()</code>. */
|
|
||||||
protected abstract JAXBContext createJaxbContext() throws Exception;
|
|
||||||
}
|
|
|
@ -16,24 +16,16 @@
|
||||||
|
|
||||||
package org.springframework.oxm.jaxb;
|
package org.springframework.oxm.jaxb;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
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.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Date;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import javax.activation.DataHandler;
|
import javax.activation.DataHandler;
|
||||||
|
@ -44,14 +36,11 @@ import javax.xml.bind.JAXBElement;
|
||||||
import javax.xml.bind.JAXBException;
|
import javax.xml.bind.JAXBException;
|
||||||
import javax.xml.bind.Marshaller;
|
import javax.xml.bind.Marshaller;
|
||||||
import javax.xml.bind.Unmarshaller;
|
import javax.xml.bind.Unmarshaller;
|
||||||
|
import javax.xml.bind.ValidationEventHandler;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
import javax.xml.bind.annotation.XmlRootElement;
|
||||||
import javax.xml.bind.annotation.XmlType;
|
|
||||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||||
import javax.xml.bind.attachment.AttachmentMarshaller;
|
import javax.xml.bind.attachment.AttachmentMarshaller;
|
||||||
import javax.xml.bind.attachment.AttachmentUnmarshaller;
|
import javax.xml.bind.attachment.AttachmentUnmarshaller;
|
||||||
import javax.xml.datatype.Duration;
|
|
||||||
import javax.xml.datatype.XMLGregorianCalendar;
|
|
||||||
import javax.xml.namespace.QName;
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
import javax.xml.stream.XMLEventReader;
|
||||||
import javax.xml.stream.XMLEventWriter;
|
import javax.xml.stream.XMLEventWriter;
|
||||||
import javax.xml.stream.XMLStreamReader;
|
import javax.xml.stream.XMLStreamReader;
|
||||||
|
@ -62,15 +51,16 @@ import javax.xml.transform.sax.SAXSource;
|
||||||
import javax.xml.validation.Schema;
|
import javax.xml.validation.Schema;
|
||||||
import javax.xml.validation.SchemaFactory;
|
import javax.xml.validation.SchemaFactory;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.xml.sax.InputSource;
|
import org.xml.sax.InputSource;
|
||||||
import org.xml.sax.SAXException;
|
import org.xml.sax.SAXException;
|
||||||
import org.xml.sax.XMLReader;
|
import org.xml.sax.XMLReader;
|
||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
import org.xml.sax.helpers.XMLReaderFactory;
|
||||||
|
|
||||||
import org.springframework.beans.factory.BeanClassLoaderAware;
|
import org.springframework.beans.factory.BeanClassLoaderAware;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.core.io.Resource;
|
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.XmlMappingException;
|
||||||
import org.springframework.oxm.mime.MimeContainer;
|
import org.springframework.oxm.mime.MimeContainer;
|
||||||
import org.springframework.oxm.mime.MimeMarshaller;
|
import org.springframework.oxm.mime.MimeMarshaller;
|
||||||
|
@ -84,25 +74,43 @@ import org.springframework.util.xml.SaxUtils;
|
||||||
import org.springframework.util.xml.StaxUtils;
|
import org.springframework.util.xml.StaxUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implementation of the <code>Marshaller</code> interface for JAXB 2.0. <p/> The typical usage will be to set either
|
* Implementation of the <code>Marshaller</code> interface for JAXB 2.0.
|
||||||
* the <code>contextPath</code> or the <code>classesToBeBound</code> property on this bean, possibly customize the
|
*
|
||||||
* marshaller and unmarshaller by setting properties, schemas, adapters, and listeners, and to refer to it.
|
* <p>The typical usage will be to set either the <code>contextPath</code> or the <code>classesToBeBound</code> property
|
||||||
|
* on this bean, possibly customize the marshaller and unmarshaller by setting properties, schemas, adapters, and
|
||||||
|
* listeners, and to refer to it.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
* @see #setContextPath(String)
|
* @see #setContextPath(String)
|
||||||
* @see #setClassesToBeBound(Class[])
|
* @see #setClassesToBeBound(Class[])
|
||||||
* @see #setJaxbContextProperties(java.util.Map)
|
* @see #setJaxbContextProperties(Map)
|
||||||
* @see #setMarshallerProperties(java.util.Map)
|
* @see #setMarshallerProperties(Map)
|
||||||
* @see #setUnmarshallerProperties(java.util.Map)
|
* @see #setUnmarshallerProperties(Map)
|
||||||
* @see #setSchema(org.springframework.core.io.Resource)
|
* @see #setSchema(Resource)
|
||||||
* @see #setSchemas(org.springframework.core.io.Resource[])
|
* @see #setSchemas(Resource[])
|
||||||
* @see #setMarshallerListener(javax.xml.bind.Marshaller.Listener)
|
* @see #setMarshallerListener(Marshaller.Listener)
|
||||||
* @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener)
|
* @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener)
|
||||||
* @see #setAdapters(javax.xml.bind.annotation.adapters.XmlAdapter[])
|
* @see #setAdapters(XmlAdapter[])
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, BeanClassLoaderAware, InitializingBean {
|
||||||
implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware {
|
|
||||||
|
private static final String CID = "cid:";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logger available to subclasses.
|
||||||
|
*/
|
||||||
|
private static final Log logger = LogFactory.getLog(Jaxb2Marshaller.class);
|
||||||
|
|
||||||
|
private String contextPath;
|
||||||
|
|
||||||
|
private Map<String, Object> marshallerProperties;
|
||||||
|
|
||||||
|
private Map<String, Object> unmarshallerProperties;
|
||||||
|
|
||||||
|
private JAXBContext jaxbContext;
|
||||||
|
|
||||||
|
private ValidationEventHandler validationEventHandler;
|
||||||
|
|
||||||
private ClassLoader classLoader;
|
private ClassLoader classLoader;
|
||||||
|
|
||||||
|
@ -136,6 +144,23 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
this.adapters = adapters;
|
this.adapters = adapters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the JAXB Context path.
|
||||||
|
*/
|
||||||
|
public void setContextPath(String contextPath) {
|
||||||
|
Assert.hasText(contextPath, "'contextPath' must not be null");
|
||||||
|
this.contextPath = contextPath;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets multiple JAXB Context paths. The given array of context paths is converted to a colon-delimited string, as
|
||||||
|
* supported by JAXB.
|
||||||
|
*/
|
||||||
|
public void setContextPaths(String[] contextPaths) {
|
||||||
|
Assert.notEmpty(contextPaths, "'contextPaths' must not be empty");
|
||||||
|
this.contextPath = StringUtils.arrayToDelimitedString(contextPaths, ":");
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the list of java classes to be recognized by a newly created JAXBContext. Setting this property or
|
* Sets the list of java classes to be recognized by a newly created JAXBContext. Setting this property or
|
||||||
* <code>contextPath</code> is required.
|
* <code>contextPath</code> is required.
|
||||||
|
@ -154,7 +179,9 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
this.jaxbContextProperties = jaxbContextProperties;
|
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) {
|
public void setMarshallerListener(Marshaller.Listener marshallerListener) {
|
||||||
this.marshallerListener = marshallerListener;
|
this.marshallerListener = marshallerListener;
|
||||||
}
|
}
|
||||||
|
@ -177,110 +204,80 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
this.schemaLanguage = schemaLanguage;
|
this.schemaLanguage = schemaLanguage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the schema resource to use for validation. */
|
/**
|
||||||
|
* Sets the schema resource to use for validation.
|
||||||
|
*/
|
||||||
public void setSchema(Resource schemaResource) {
|
public void setSchema(Resource schemaResource) {
|
||||||
schemaResources = new 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) {
|
public void setSchemas(Resource[] schemaResources) {
|
||||||
this.schemaResources = 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) {
|
public void setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener) {
|
||||||
this.unmarshallerListener = unmarshallerListener;
|
this.unmarshallerListener = unmarshallerListener;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supports(Type type) {
|
/**
|
||||||
if (type instanceof Class) {
|
* Sets the JAXB <code>Marshaller</code> properties. These properties will be set on the underlying JAXB
|
||||||
return supportsInternal((Class) type, true);
|
* <code>Marshaller</code>, and allow for features such as indentation.
|
||||||
}
|
*
|
||||||
else if (type instanceof ParameterizedType) {
|
* @param properties the properties
|
||||||
ParameterizedType parameterizedType = (ParameterizedType) type;
|
* @see Marshaller#setProperty(String,Object)
|
||||||
if (JAXBElement.class.equals(parameterizedType.getRawType())) {
|
* @see Marshaller#JAXB_ENCODING
|
||||||
Assert.isTrue(parameterizedType.getActualTypeArguments().length == 1,
|
* @see Marshaller#JAXB_FORMATTED_OUTPUT
|
||||||
"Invalid amount of parameterized types in JAXBElement");
|
* @see Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
|
||||||
Type typeArgument = parameterizedType.getActualTypeArguments()[0];
|
* @see Marshaller#JAXB_SCHEMA_LOCATION
|
||||||
if (typeArgument instanceof Class) {
|
*/
|
||||||
Class clazz = (Class) typeArgument;
|
public void setMarshallerProperties(Map<String, Object> properties) {
|
||||||
if (!isPrimitiveType(clazz) && !isStandardType(clazz) && !supportsInternal(clazz, false)) {
|
this.marshallerProperties = properties;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (typeArgument instanceof GenericArrayType) {
|
|
||||||
GenericArrayType genericArrayType = (GenericArrayType) typeArgument;
|
|
||||||
return genericArrayType.getGenericComponentType().equals(Byte.TYPE);
|
|
||||||
}
|
|
||||||
else if (!supports(typeArgument)) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isPrimitiveType(Class clazz) {
|
/**
|
||||||
return (Boolean.class.equals(clazz) || Byte.class.equals(clazz) || Short.class.equals(clazz) ||
|
* Sets the JAXB <code>Unmarshaller</code> properties. These properties will be set on the underlying JAXB
|
||||||
Integer.class.equals(clazz) || Long.class.equals(clazz) || Float.class.equals(clazz) ||
|
* <code>Unmarshaller</code>.
|
||||||
Double.class.equals(clazz) || byte[].class.equals(clazz));
|
*
|
||||||
|
* @param properties the properties
|
||||||
|
* @see javax.xml.bind.Unmarshaller#setProperty(String,Object)
|
||||||
|
*/
|
||||||
|
public void setUnmarshallerProperties(Map<String, Object> properties) {
|
||||||
|
this.unmarshallerProperties = properties;
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean isStandardType(Class clazz) {
|
/**
|
||||||
return (String.class.equals(clazz) || BigInteger.class.equals(clazz) || BigDecimal.class.equals(clazz) ||
|
* Sets the JAXB validation event handler. This event handler will be called by JAXB if any validation errors are
|
||||||
Calendar.class.isAssignableFrom(clazz) || Date.class.isAssignableFrom(clazz) ||
|
* encountered during calls to any of the marshal API's.
|
||||||
QName.class.equals(clazz) || URI.class.equals(clazz) ||
|
*
|
||||||
XMLGregorianCalendar.class.isAssignableFrom(clazz) || Duration.class.isAssignableFrom(clazz) ||
|
* @param validationEventHandler the event handler
|
||||||
Object.class.equals(clazz) || Image.class.isAssignableFrom(clazz) || DataHandler.class.equals(clazz) ||
|
*/
|
||||||
Source.class.isAssignableFrom(clazz) || UUID.class.equals(clazz));
|
public void setValidationEventHandler(ValidationEventHandler validationEventHandler) {
|
||||||
|
this.validationEventHandler = validationEventHandler;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supports(Class clazz) {
|
public final void afterPropertiesSet() throws Exception {
|
||||||
return supportsInternal(clazz, true);
|
if (StringUtils.hasLength(contextPath) && !ObjectUtils.isEmpty(classesToBeBound)) {
|
||||||
|
throw new IllegalArgumentException("specify either contextPath or classesToBeBound property; not both");
|
||||||
}
|
}
|
||||||
|
try {
|
||||||
private boolean supportsInternal(Class<?> clazz, boolean checkForXmlRootElement) {
|
jaxbContext = createJaxbContext();
|
||||||
if (checkForXmlRootElement && clazz.getAnnotation(XmlRootElement.class) == null) {
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
if (clazz.getAnnotation(XmlType.class) == null) {
|
catch (JAXBException ex) {
|
||||||
return false;
|
throw convertJaxbException(ex);
|
||||||
}
|
}
|
||||||
if (StringUtils.hasLength(getContextPath())) {
|
|
||||||
String className = ClassUtils.getQualifiedName(clazz);
|
|
||||||
int lastDotIndex = className.lastIndexOf('.');
|
|
||||||
if (lastDotIndex == -1) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
String packageName = className.substring(0, lastDotIndex);
|
|
||||||
String[] contextPaths = StringUtils.tokenizeToStringArray(getContextPath(), ":");
|
|
||||||
for (String contextPath : contextPaths) {
|
|
||||||
if (contextPath.equals(packageName)) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
|
|
||||||
return Arrays.asList(classesToBeBound).contains(clazz);
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* JAXBContext
|
* JAXBContext
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Override
|
|
||||||
protected JAXBContext createJaxbContext() throws Exception {
|
protected JAXBContext createJaxbContext() throws Exception {
|
||||||
if (JaxbUtils.getJaxbVersion() < JaxbUtils.JAXB_2) {
|
|
||||||
throw new IllegalStateException(
|
|
||||||
"Cannot use Jaxb2Marshaller in combination with JAXB 1.0. Use Jaxb1Marshaller instead.");
|
|
||||||
}
|
|
||||||
if (StringUtils.hasLength(getContextPath()) && !ObjectUtils.isEmpty(classesToBeBound)) {
|
|
||||||
throw new IllegalArgumentException("specify either contextPath or classesToBeBound property; not both");
|
|
||||||
}
|
|
||||||
if (!ObjectUtils.isEmpty(schemaResources)) {
|
if (!ObjectUtils.isEmpty(schemaResources)) {
|
||||||
if (logger.isDebugEnabled()) {
|
if (logger.isDebugEnabled()) {
|
||||||
logger.debug(
|
logger.debug(
|
||||||
|
@ -288,7 +285,7 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
}
|
}
|
||||||
schema = loadSchema(schemaResources, schemaLanguage);
|
schema = loadSchema(schemaResources, schemaLanguage);
|
||||||
}
|
}
|
||||||
if (StringUtils.hasLength(getContextPath())) {
|
if (StringUtils.hasLength(contextPath)) {
|
||||||
return createJaxbContextFromContextPath();
|
return createJaxbContextFromContextPath();
|
||||||
}
|
}
|
||||||
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
|
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
|
||||||
|
@ -299,6 +296,41 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JAXBContext createJaxbContextFromContextPath() throws JAXBException {
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info("Creating JAXBContext with context path [" + contextPath + "]");
|
||||||
|
}
|
||||||
|
if (jaxbContextProperties != null) {
|
||||||
|
if (classLoader != null) {
|
||||||
|
return JAXBContext.newInstance(contextPath, classLoader, jaxbContextProperties);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return JAXBContext.newInstance(contextPath, ClassUtils.getDefaultClassLoader(), jaxbContextProperties);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (classLoader != null) {
|
||||||
|
return JAXBContext.newInstance(contextPath, classLoader);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return JAXBContext.newInstance(contextPath);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private JAXBContext createJaxbContextFromClasses() throws JAXBException {
|
||||||
|
if (logger.isInfoEnabled()) {
|
||||||
|
logger.info("Creating JAXBContext with classes to be bound [" +
|
||||||
|
StringUtils.arrayToCommaDelimitedString(classesToBeBound) + "]");
|
||||||
|
}
|
||||||
|
if (jaxbContextProperties != null) {
|
||||||
|
return JAXBContext.newInstance(classesToBeBound, jaxbContextProperties);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return JAXBContext.newInstance(classesToBeBound);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
|
private Schema loadSchema(Resource[] resources, String schemaLanguage) throws IOException, SAXException {
|
||||||
Assert.notEmpty(resources, "No resources given");
|
Assert.notEmpty(resources, "No resources given");
|
||||||
Assert.hasLength(schemaLanguage, "No schema language provided");
|
Assert.hasLength(schemaLanguage, "No schema language provided");
|
||||||
|
@ -315,44 +347,65 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
return schemaFactory.newSchema(schemaSources);
|
return schemaFactory.newSchema(schemaSources);
|
||||||
}
|
}
|
||||||
|
|
||||||
private JAXBContext createJaxbContextFromContextPath() throws JAXBException {
|
public boolean supports(Class<?> clazz) {
|
||||||
if (logger.isInfoEnabled()) {
|
if (JAXBElement.class.isAssignableFrom(clazz)) {
|
||||||
logger.info("Creating JAXBContext with context path [" + getContextPath() + "]");
|
return true;
|
||||||
}
|
}
|
||||||
if (jaxbContextProperties != null) {
|
else if (clazz.getAnnotation(XmlRootElement.class) != null) {
|
||||||
if (classLoader != null) {
|
return true;
|
||||||
return JAXBContext.newInstance(getContextPath(), classLoader, jaxbContextProperties);
|
|
||||||
}
|
}
|
||||||
else {
|
if (StringUtils.hasLength(contextPath)) {
|
||||||
return JAXBContext
|
String packageName = ClassUtils.getPackageName(clazz);
|
||||||
.newInstance(getContextPath(), ClassUtils.getDefaultClassLoader(), jaxbContextProperties);
|
String[] contextPaths = StringUtils.tokenizeToStringArray(contextPath, ":");
|
||||||
|
for (String contextPath : contextPaths) {
|
||||||
|
if (contextPath.equals(packageName)) {
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
return false;
|
||||||
return classLoader != null ? JAXBContext.newInstance(getContextPath(), classLoader) :
|
|
||||||
JAXBContext.newInstance(getContextPath());
|
|
||||||
}
|
}
|
||||||
|
else if (!ObjectUtils.isEmpty(classesToBeBound)) {
|
||||||
|
return Arrays.asList(classesToBeBound).contains(clazz);
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
private JAXBContext createJaxbContextFromClasses() throws JAXBException {
|
|
||||||
if (logger.isInfoEnabled()) {
|
|
||||||
logger.info("Creating JAXBContext with classes to be bound [" +
|
|
||||||
StringUtils.arrayToCommaDelimitedString(classesToBeBound) + "]");
|
|
||||||
}
|
|
||||||
if (jaxbContextProperties != null) {
|
|
||||||
return JAXBContext.newInstance(classesToBeBound, jaxbContextProperties);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return JAXBContext.newInstance(classesToBeBound);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Marshaller/Unmarshaller
|
* Marshalling
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@Override
|
/**
|
||||||
|
* Returns a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.
|
||||||
|
*/
|
||||||
|
private Marshaller createMarshaller() {
|
||||||
|
try {
|
||||||
|
Marshaller marshaller = jaxbContext.createMarshaller();
|
||||||
|
initJaxbMarshaller(marshaller);
|
||||||
|
return marshaller;
|
||||||
|
}
|
||||||
|
catch (JAXBException ex) {
|
||||||
|
throw convertJaxbException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called
|
||||||
|
* after creation of JAXB <code>Marshaller</code>, and after the respective properties have been set.
|
||||||
|
*
|
||||||
|
* <p>Default implementation sets the {@link #setMarshallerProperties(Map) defined properties}, the {@link
|
||||||
|
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
|
||||||
|
* schemas}, {@link #setMarshallerListener(Marshaller.Listener) listener}, and {@link #setAdapters(XmlAdapter[])
|
||||||
|
* adapters}.
|
||||||
|
*/
|
||||||
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
|
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
|
||||||
|
if (marshallerProperties != null) {
|
||||||
|
for (String name : marshallerProperties.keySet()) {
|
||||||
|
marshaller.setProperty(name, marshallerProperties.get(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (validationEventHandler != null) {
|
||||||
|
marshaller.setEventHandler(validationEventHandler);
|
||||||
|
}
|
||||||
if (schema != null) {
|
if (schema != null) {
|
||||||
marshaller.setSchema(schema);
|
marshaller.setSchema(schema);
|
||||||
}
|
}
|
||||||
|
@ -366,25 +419,6 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
|
|
||||||
if (schema != null) {
|
|
||||||
unmarshaller.setSchema(schema);
|
|
||||||
}
|
|
||||||
if (unmarshallerListener != null) {
|
|
||||||
unmarshaller.setListener(unmarshallerListener);
|
|
||||||
}
|
|
||||||
if (adapters != null) {
|
|
||||||
for (XmlAdapter adapter : adapters) {
|
|
||||||
unmarshaller.setAdapter(adapter);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Marshalling
|
|
||||||
*/
|
|
||||||
|
|
||||||
public void marshal(Object graph, Result result) throws XmlMappingException {
|
public void marshal(Object graph, Result result) throws XmlMappingException {
|
||||||
marshal(graph, result, null);
|
marshal(graph, result, null);
|
||||||
}
|
}
|
||||||
|
@ -427,6 +461,51 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
* Unmarshalling
|
* Unmarshalling
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a newly created JAXB unmarshaller. JAXB unmarshallers are not necessarily thread safe.
|
||||||
|
*/
|
||||||
|
private Unmarshaller createUnmarshaller() {
|
||||||
|
try {
|
||||||
|
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
|
||||||
|
initJaxbUnmarshaller(unmarshaller);
|
||||||
|
return unmarshaller;
|
||||||
|
}
|
||||||
|
catch (JAXBException ex) {
|
||||||
|
throw convertJaxbException(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called
|
||||||
|
* after creation of JAXB <code>Marshaller</code>, and after the respective properties have been set.
|
||||||
|
*
|
||||||
|
* <p>Default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
|
||||||
|
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
|
||||||
|
* schemas}, {@link #setUnmarshallerListener(Unmarshaller.Listener) listener}, and {@link #setAdapters(XmlAdapter[])
|
||||||
|
* adapters}.
|
||||||
|
*/
|
||||||
|
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
|
||||||
|
if (unmarshallerProperties != null) {
|
||||||
|
for (String name : unmarshallerProperties.keySet()) {
|
||||||
|
unmarshaller.setProperty(name, unmarshallerProperties.get(name));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (validationEventHandler != null) {
|
||||||
|
unmarshaller.setEventHandler(validationEventHandler);
|
||||||
|
}
|
||||||
|
if (schema != null) {
|
||||||
|
unmarshaller.setSchema(schema);
|
||||||
|
}
|
||||||
|
if (unmarshallerListener != null) {
|
||||||
|
unmarshaller.setListener(unmarshallerListener);
|
||||||
|
}
|
||||||
|
if (adapters != null) {
|
||||||
|
for (XmlAdapter adapter : adapters) {
|
||||||
|
unmarshaller.setAdapter(adapter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Object unmarshal(Source source) throws XmlMappingException {
|
public Object unmarshal(Source source) throws XmlMappingException {
|
||||||
return unmarshal(source, null);
|
return unmarshal(source, null);
|
||||||
}
|
}
|
||||||
|
@ -465,6 +544,19 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert the given <code>JAXBException</code> to an appropriate exception from the
|
||||||
|
* <code>org.springframework.oxm</code> hierarchy. <p/> The default implementation delegates to <code>JaxbUtils</code>.
|
||||||
|
* Can be overridden in subclasses.
|
||||||
|
*
|
||||||
|
* @param ex <code>JAXBException</code> that occured
|
||||||
|
* @return the corresponding <code>XmlMappingException</code> instance
|
||||||
|
* @see org.springframework.oxm.jaxb.JaxbUtils#convertJaxbException
|
||||||
|
*/
|
||||||
|
protected XmlMappingException convertJaxbException(JAXBException ex) {
|
||||||
|
return JaxbUtils.convertJaxbException(ex);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Inner classes
|
* Inner classes
|
||||||
*/
|
*/
|
||||||
|
@ -499,7 +591,7 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
catch (UnsupportedEncodingException e) {
|
catch (UnsupportedEncodingException e) {
|
||||||
// ignore
|
// ignore
|
||||||
}
|
}
|
||||||
return "cid:" + contentId;
|
return CID + contentId;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getHost(String elementNamespace, DataHandler dataHandler) {
|
private String getHost(String elementNamespace, DataHandler dataHandler) {
|
||||||
|
@ -547,8 +639,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataHandler getAttachmentAsDataHandler(String contentId) {
|
public DataHandler getAttachmentAsDataHandler(String contentId) {
|
||||||
if (contentId.startsWith("cid:")) {
|
if (contentId.startsWith(CID)) {
|
||||||
contentId = contentId.substring("cid:".length());
|
contentId = contentId.substring(CID.length());
|
||||||
try {
|
try {
|
||||||
contentId = URLDecoder.decode(contentId, "UTF-8");
|
contentId = URLDecoder.decode(contentId, "UTF-8");
|
||||||
}
|
}
|
||||||
|
@ -566,8 +658,8 @@ public class Jaxb2Marshaller extends AbstractJaxbMarshaller
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/**
|
||||||
* DataSource that wraps around a byte array
|
* DataSource that wraps around a byte array.
|
||||||
*/
|
*/
|
||||||
private static class ByteArrayDataSource implements DataSource {
|
private static class ByteArrayDataSource implements DataSource {
|
||||||
|
|
||||||
|
|
|
@ -21,7 +21,6 @@ import javax.xml.bind.UnmarshalException;
|
||||||
import javax.xml.bind.ValidationException;
|
import javax.xml.bind.ValidationException;
|
||||||
|
|
||||||
import org.springframework.oxm.XmlMappingException;
|
import org.springframework.oxm.XmlMappingException;
|
||||||
import org.springframework.util.ClassUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generic utility methods for working with JAXB. Mainly for internal use within the framework.
|
* Generic utility methods for working with JAXB. Mainly for internal use within the framework.
|
||||||
|
@ -31,35 +30,6 @@ import org.springframework.util.ClassUtils;
|
||||||
*/
|
*/
|
||||||
public abstract class JaxbUtils {
|
public abstract class JaxbUtils {
|
||||||
|
|
||||||
public static final int JAXB_1 = 0;
|
|
||||||
|
|
||||||
public static final int JAXB_2 = 1;
|
|
||||||
|
|
||||||
private static final String JAXB_2_CLASS_NAME = "javax.xml.bind.Binder";
|
|
||||||
|
|
||||||
private static int jaxbVersion = JAXB_1;
|
|
||||||
|
|
||||||
static {
|
|
||||||
try {
|
|
||||||
ClassUtils.forName(JAXB_2_CLASS_NAME);
|
|
||||||
jaxbVersion = JAXB_2;
|
|
||||||
}
|
|
||||||
catch (ClassNotFoundException ex1) {
|
|
||||||
// leave JAXB 1 as default
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Gets the major JAXB version. This means we can do things like if <code>(getJaxbVersion() <= JAXB_2)</code>.
|
|
||||||
*
|
|
||||||
* @return a code comparable to the JAXP_XX codes in this class
|
|
||||||
* @see #JAXB_1
|
|
||||||
* @see #JAXB_2
|
|
||||||
*/
|
|
||||||
public static int getJaxbVersion() {
|
|
||||||
return jaxbVersion;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts the given <code>JAXBException</code> to an appropriate exception from the
|
* Converts the given <code>JAXBException</code> to an appropriate exception from the
|
||||||
* <code>org.springframework.oxm</code> hierarchy.
|
* <code>org.springframework.oxm</code> hierarchy.
|
||||||
|
|
|
@ -88,27 +88,37 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
||||||
|
|
||||||
private Boolean standalone;
|
private Boolean standalone;
|
||||||
|
|
||||||
/** Sets the optional binding name for this instance. */
|
/**
|
||||||
|
* Sets the optional binding name for this instance.
|
||||||
|
*/
|
||||||
public void setBindingName(String bindingName) {
|
public void setBindingName(String bindingName) {
|
||||||
this.bindingName = bindingName;
|
this.bindingName = bindingName;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the target class for this instance. This property is required. */
|
/**
|
||||||
|
* Sets the target class for this instance. This property is required.
|
||||||
|
*/
|
||||||
public void setTargetClass(Class targetClass) {
|
public void setTargetClass(Class targetClass) {
|
||||||
this.targetClass = targetClass;
|
this.targetClass = targetClass;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the number of nesting indent spaces. Default is <code>-1</code>, i.e. no indentation. */
|
/**
|
||||||
|
* Sets the number of nesting indent spaces. Default is <code>-1</code>, i.e. no indentation.
|
||||||
|
*/
|
||||||
public void setIndent(int indent) {
|
public void setIndent(int indent) {
|
||||||
this.indent = indent;
|
this.indent = indent;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the document encoding using for marshalling. Default is UTF-8. */
|
/**
|
||||||
|
* Sets the document encoding using for marshalling. Default is UTF-8.
|
||||||
|
*/
|
||||||
public void setEncoding(String encoding) {
|
public void setEncoding(String encoding) {
|
||||||
this.encoding = encoding;
|
this.encoding = encoding;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets the document standalone flag for marshalling. By default, this flag is not present. */
|
/**
|
||||||
|
* Sets the document standalone flag for marshalling. By default, this flag is not present.
|
||||||
|
*/
|
||||||
public void setStandalone(Boolean standalone) {
|
public void setStandalone(Boolean standalone) {
|
||||||
this.standalone = standalone;
|
this.standalone = standalone;
|
||||||
}
|
}
|
||||||
|
@ -136,7 +146,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean supports(Class clazz) {
|
public boolean supports(Class<?> clazz) {
|
||||||
Assert.notNull(clazz, "'clazz' must not be null");
|
Assert.notNull(clazz, "'clazz' must not be null");
|
||||||
String[] mappedClasses = bindingFactory.getMappedClasses();
|
String[] mappedClasses = bindingFactory.getMappedClasses();
|
||||||
String className = clazz.getName();
|
String className = clazz.getName();
|
||||||
|
|
|
@ -55,7 +55,9 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
|
||||||
|
|
||||||
private boolean validating = false;
|
private boolean validating = false;
|
||||||
|
|
||||||
/** Returns the <code>XmlOptions</code>. */
|
/**
|
||||||
|
* Returns the <code>XmlOptions</code>.
|
||||||
|
*/
|
||||||
public XmlOptions getXmlOptions() {
|
public XmlOptions getXmlOptions() {
|
||||||
return xmlOptions;
|
return xmlOptions;
|
||||||
}
|
}
|
||||||
|
@ -69,18 +71,24 @@ public class XmlBeansMarshaller extends AbstractMarshaller {
|
||||||
this.xmlOptions = xmlOptions;
|
this.xmlOptions = xmlOptions;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns whether this marshaller should validate in- and outgoing documents. */
|
/**
|
||||||
|
* Returns whether this marshaller should validate in- and outgoing documents.
|
||||||
|
*/
|
||||||
public boolean isValidating() {
|
public boolean isValidating() {
|
||||||
return validating;
|
return validating;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Sets whether this marshaller should validate in- and outgoing documents. Default is <code>false</code>. */
|
/**
|
||||||
|
* Sets whether this marshaller should validate in- and outgoing documents. Default is <code>false</code>.
|
||||||
|
*/
|
||||||
public void setValidating(boolean validating) {
|
public void setValidating(boolean validating) {
|
||||||
this.validating = validating;
|
this.validating = validating;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Returns true if the given class is an implementation of {@link XmlObject}. */
|
/**
|
||||||
public boolean supports(Class clazz) {
|
* Returns true if the given class is an implementation of {@link XmlObject}.
|
||||||
|
*/
|
||||||
|
public boolean supports(Class<?> clazz) {
|
||||||
return XmlObject.class.isAssignableFrom(clazz);
|
return XmlObject.class.isAssignableFrom(clazz);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2006 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
|
|
||||||
package org.springframework.oxm.jaxb;
|
|
||||||
|
|
||||||
import javax.xml.transform.sax.SAXResult;
|
|
||||||
|
|
||||||
import org.easymock.MockControl;
|
|
||||||
import org.xml.sax.ContentHandler;
|
|
||||||
|
|
||||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
|
||||||
|
|
||||||
public abstract class AbstractJaxbMarshallerTestCase extends AbstractMarshallerTestCase {
|
|
||||||
|
|
||||||
public void testMarshalSaxResult() throws Exception {
|
|
||||||
MockControl handlerControl = MockControl.createStrictControl(ContentHandler.class);
|
|
||||||
ContentHandler handlerMock = (ContentHandler) handlerControl.getMock();
|
|
||||||
handlerMock.setDocumentLocator(null);
|
|
||||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
|
||||||
handlerMock.startDocument();
|
|
||||||
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
|
||||||
handlerMock.startElement("http://samples.springframework.org/flight", "flights", "flights", null);
|
|
||||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
|
||||||
handlerMock.startElement("http://samples.springframework.org/flight", "flight", "flight", null);
|
|
||||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
|
||||||
handlerMock.startElement("http://samples.springframework.org/flight", "number", "number", null);
|
|
||||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
|
||||||
handlerMock.characters(new char[]{'4', '2'}, 0, 2);
|
|
||||||
handlerControl.setMatcher(MockControl.ALWAYS_MATCHER);
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
|
||||||
handlerMock.endPrefixMapping("");
|
|
||||||
handlerMock.endDocument();
|
|
||||||
|
|
||||||
handlerControl.replay();
|
|
||||||
SAXResult result = new SAXResult(handlerMock);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
handlerControl.verify();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -16,153 +16,78 @@
|
||||||
|
|
||||||
package org.springframework.oxm.jaxb;
|
package org.springframework.oxm.jaxb;
|
||||||
|
|
||||||
import java.awt.*;
|
|
||||||
import java.io.ByteArrayOutputStream;
|
|
||||||
import java.io.StringWriter;
|
import java.io.StringWriter;
|
||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.math.BigInteger;
|
|
||||||
import java.net.URI;
|
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Date;
|
|
||||||
import java.util.UUID;
|
|
||||||
import javax.activation.DataHandler;
|
import javax.activation.DataHandler;
|
||||||
import javax.activation.FileDataSource;
|
import javax.activation.FileDataSource;
|
||||||
import javax.xml.bind.JAXBElement;
|
|
||||||
import javax.xml.datatype.Duration;
|
|
||||||
import javax.xml.datatype.XMLGregorianCalendar;
|
|
||||||
import javax.xml.namespace.QName;
|
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.stream.XMLEventWriter;
|
|
||||||
import javax.xml.stream.XMLOutputFactory;
|
|
||||||
import javax.xml.stream.XMLStreamWriter;
|
|
||||||
import javax.xml.transform.Result;
|
import javax.xml.transform.Result;
|
||||||
import javax.xml.transform.Source;
|
|
||||||
import javax.xml.transform.dom.DOMResult;
|
|
||||||
import javax.xml.transform.sax.SAXResult;
|
import javax.xml.transform.sax.SAXResult;
|
||||||
import javax.xml.transform.stax.StAXResult;
|
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
import static org.custommonkey.xmlunit.XMLAssert.assertXMLEqual;
|
|
||||||
import static org.easymock.EasyMock.*;
|
import static org.easymock.EasyMock.*;
|
||||||
import static org.junit.Assert.assertFalse;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.Text;
|
|
||||||
import org.xml.sax.Attributes;
|
import org.xml.sax.Attributes;
|
||||||
import org.xml.sax.ContentHandler;
|
import org.xml.sax.ContentHandler;
|
||||||
import org.xml.sax.Locator;
|
import org.xml.sax.Locator;
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||||
|
import org.springframework.oxm.Marshaller;
|
||||||
import org.springframework.oxm.XmlMappingException;
|
import org.springframework.oxm.XmlMappingException;
|
||||||
import org.springframework.oxm.mime.MimeContainer;
|
import org.springframework.oxm.mime.MimeContainer;
|
||||||
import org.springframework.util.FileCopyUtils;
|
import org.springframework.util.FileCopyUtils;
|
||||||
import org.springframework.util.xml.StaxUtils;
|
|
||||||
|
|
||||||
public class Jaxb2MarshallerTest {
|
public class Jaxb2MarshallerTest extends AbstractMarshallerTestCase {
|
||||||
|
|
||||||
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb";
|
private static final String CONTEXT_PATH = "org.springframework.oxm.jaxb";
|
||||||
|
|
||||||
private static final String EXPECTED_STRING =
|
|
||||||
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
|
||||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
|
||||||
|
|
||||||
private Jaxb2Marshaller marshaller;
|
private Jaxb2Marshaller marshaller;
|
||||||
|
|
||||||
private Flights flights;
|
private Flights flights;
|
||||||
|
|
||||||
@Before
|
@Override
|
||||||
public void createMarshaller() throws Exception {
|
public Marshaller createMarshaller() throws Exception {
|
||||||
marshaller = new Jaxb2Marshaller();
|
marshaller = new Jaxb2Marshaller();
|
||||||
marshaller.setContextPath(CONTEXT_PATH);
|
marshaller.setContextPath(CONTEXT_PATH);
|
||||||
marshaller.afterPropertiesSet();
|
marshaller.afterPropertiesSet();
|
||||||
|
return marshaller;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object createFlights() {
|
||||||
FlightType flight = new FlightType();
|
FlightType flight = new FlightType();
|
||||||
flight.setNumber(42L);
|
flight.setNumber(42L);
|
||||||
flights = new Flights();
|
flights = new Flights();
|
||||||
flights.getFlight().add(flight);
|
flights.getFlight().add(flight);
|
||||||
|
return flights;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void marshalDOMResult() throws Exception {
|
public void marshalSAXResult() throws Exception {
|
||||||
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
|
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
|
||||||
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
|
handlerMock.setDocumentLocator(isA(Locator.class));
|
||||||
Document document = builder.newDocument();
|
handlerMock.startDocument();
|
||||||
DOMResult domResult = new DOMResult(document);
|
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
||||||
marshaller.marshal(flights, domResult);
|
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"),
|
||||||
Document expected = builder.newDocument();
|
isA(Attributes.class));
|
||||||
Element flightsElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"),
|
||||||
expected.appendChild(flightsElement);
|
isA(Attributes.class));
|
||||||
Element flightElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"),
|
||||||
flightsElement.appendChild(flightElement);
|
isA(Attributes.class));
|
||||||
Element numberElement = expected.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
handlerMock.characters(isA(char[].class), eq(0), eq(2));
|
||||||
flightElement.appendChild(numberElement);
|
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
||||||
Text text = expected.createTextNode("42");
|
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
||||||
numberElement.appendChild(text);
|
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
||||||
assertXMLEqual("Marshaller writes invalid DOMResult", expected, document);
|
handlerMock.endPrefixMapping("");
|
||||||
}
|
handlerMock.endDocument();
|
||||||
|
replay(handlerMock);
|
||||||
|
|
||||||
@Test
|
SAXResult result = new SAXResult(handlerMock);
|
||||||
public void marshalStreamResultWriter() throws Exception {
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
StreamResult result = new StreamResult(writer);
|
|
||||||
marshaller.marshal(flights, result);
|
marshaller.marshal(flights, result);
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
verify(handlerMock);
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalStreamResultOutputStream() throws Exception {
|
|
||||||
ByteArrayOutputStream os = new ByteArrayOutputStream();
|
|
||||||
StreamResult result = new StreamResult(os);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING,
|
|
||||||
new String(os.toByteArray(), "UTF-8"));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalStaxResultXMLStreamWriter() throws Exception {
|
|
||||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
|
||||||
Result result = StaxUtils.createStaxResult(streamWriter);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalStaxResultXMLEventWriter() throws Exception {
|
|
||||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
|
||||||
Result result = StaxUtils.createStaxResult(eventWriter);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalStaxResultXMLStreamWriterJaxp14() throws Exception {
|
|
||||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
XMLStreamWriter streamWriter = outputFactory.createXMLStreamWriter(writer);
|
|
||||||
StAXResult result = new StAXResult(streamWriter);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalStaxResultXMLEventWriterJaxp14() throws Exception {
|
|
||||||
XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
|
|
||||||
StringWriter writer = new StringWriter();
|
|
||||||
XMLEventWriter eventWriter = outputFactory.createXMLEventWriter(writer);
|
|
||||||
StAXResult result = new StAXResult(eventWriter);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
assertXMLEqual("Marshaller writes invalid StreamResult", EXPECTED_STRING, writer.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -170,7 +95,8 @@ public class Jaxb2MarshallerTest {
|
||||||
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
|
||||||
marshaller.setContextPath(CONTEXT_PATH);
|
marshaller.setContextPath(CONTEXT_PATH);
|
||||||
marshaller.setMarshallerProperties(
|
marshaller.setMarshallerProperties(
|
||||||
Collections.singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE));
|
Collections.<String, Object>singletonMap(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT,
|
||||||
|
Boolean.TRUE));
|
||||||
marshaller.afterPropertiesSet();
|
marshaller.afterPropertiesSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,43 +123,13 @@ public class Jaxb2MarshallerTest {
|
||||||
marshaller.marshal(flights, result);
|
marshaller.marshal(flights, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void marshalSaxResult() throws Exception {
|
|
||||||
ContentHandler handlerMock = createStrictMock(ContentHandler.class);
|
|
||||||
handlerMock.setDocumentLocator(isA(Locator.class));
|
|
||||||
handlerMock.startDocument();
|
|
||||||
handlerMock.startPrefixMapping("", "http://samples.springframework.org/flight");
|
|
||||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flights"), eq("flights"),
|
|
||||||
isA(Attributes.class));
|
|
||||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("flight"), eq("flight"),
|
|
||||||
isA(Attributes.class));
|
|
||||||
handlerMock.startElement(eq("http://samples.springframework.org/flight"), eq("number"), eq("number"),
|
|
||||||
isA(Attributes.class));
|
|
||||||
handlerMock.characters(isA(char[].class), eq(0), eq(2));
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "number", "number");
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "flight", "flight");
|
|
||||||
handlerMock.endElement("http://samples.springframework.org/flight", "flights", "flights");
|
|
||||||
handlerMock.endPrefixMapping("");
|
|
||||||
handlerMock.endDocument();
|
|
||||||
replay(handlerMock);
|
|
||||||
|
|
||||||
SAXResult result = new SAXResult(handlerMock);
|
|
||||||
marshaller.marshal(flights, result);
|
|
||||||
verify(handlerMock);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void supportsContextPath() throws Exception {
|
public void supportsContextPath() throws Exception {
|
||||||
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
||||||
assertTrue("Jaxb2Marshaller does not support Flights",
|
assertTrue("Jaxb2Marshaller does not support Flights", marshaller.supports(createFlights.getReturnType()));
|
||||||
marshaller.supports(createFlights.getGenericReturnType()));
|
|
||||||
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
||||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
||||||
marshaller.supports(createFlight.getGenericReturnType()));
|
marshaller.supports(createFlight.getReturnType()));
|
||||||
assertFalse("Jaxb2Marshaller supports non-parameterized JAXBElement", marshaller.supports(JAXBElement.class));
|
|
||||||
JAXBElement<Jaxb2MarshallerTest> testElement =
|
|
||||||
new JAXBElement<Jaxb2MarshallerTest>(new QName("something"), Jaxb2MarshallerTest.class, null, this);
|
|
||||||
assertFalse("Jaxb2Marshaller supports wrong JAXBElement", marshaller.supports(testElement.getClass()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -242,39 +138,10 @@ public class Jaxb2MarshallerTest {
|
||||||
marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class});
|
marshaller.setClassesToBeBound(new Class[]{Flights.class, FlightType.class});
|
||||||
marshaller.afterPropertiesSet();
|
marshaller.afterPropertiesSet();
|
||||||
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
Method createFlights = ObjectFactory.class.getDeclaredMethod("createFlights");
|
||||||
assertTrue("Jaxb2Marshaller does not support Flights",
|
assertTrue("Jaxb2Marshaller does not support Flights", marshaller.supports(createFlights.getReturnType()));
|
||||||
marshaller.supports(createFlights.getGenericReturnType()));
|
|
||||||
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
Method createFlight = ObjectFactory.class.getDeclaredMethod("createFlight", FlightType.class);
|
||||||
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
assertTrue("Jaxb2Marshaller does not support JAXBElement<FlightsType>",
|
||||||
marshaller.supports(createFlight.getGenericReturnType()));
|
marshaller.supports(createFlight.getReturnType()));
|
||||||
assertFalse("Jaxb2Marshaller supports non-parameterized JAXBElement", marshaller.supports(JAXBElement.class));
|
|
||||||
JAXBElement<Jaxb2MarshallerTest> testElement =
|
|
||||||
new JAXBElement<Jaxb2MarshallerTest>(new QName("something"), Jaxb2MarshallerTest.class, null, this);
|
|
||||||
assertFalse("Jaxb2Marshaller supports wrong JAXBElement", marshaller.supports(testElement.getClass()));
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void supportsPrimitives() throws Exception {
|
|
||||||
Method primitives = getClass()
|
|
||||||
.getDeclaredMethod("primitives", JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
|
||||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class);
|
|
||||||
Type[] types = primitives.getGenericParameterTypes();
|
|
||||||
for (Type type : types) {
|
|
||||||
assertTrue("Jaxb2Marshaller does not support " + type, marshaller.supports(type));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void supportsStandards() throws Exception {
|
|
||||||
Method standards = getClass()
|
|
||||||
.getDeclaredMethod("standards", JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
|
||||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
|
||||||
JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class, JAXBElement.class,
|
|
||||||
JAXBElement.class);
|
|
||||||
Type[] types = standards.getGenericParameterTypes();
|
|
||||||
for (Type type : types) {
|
|
||||||
assertTrue("Jaxb2Marshaller does not support " + type, marshaller.supports(type));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -300,30 +167,4 @@ public class Jaxb2MarshallerTest {
|
||||||
verify(mimeContainer);
|
verify(mimeContainer);
|
||||||
assertTrue("No XML written", writer.toString().length() > 0);
|
assertTrue("No XML written", writer.toString().length() > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void primitives(JAXBElement<Boolean> bool,
|
|
||||||
JAXBElement<Byte> aByte,
|
|
||||||
JAXBElement<Short> aShort,
|
|
||||||
JAXBElement<Integer> anInteger,
|
|
||||||
JAXBElement<Long> aLong,
|
|
||||||
JAXBElement<Float> aFloat,
|
|
||||||
JAXBElement<Double> aDouble,
|
|
||||||
JAXBElement<byte[]> byteArray) {
|
|
||||||
}
|
|
||||||
|
|
||||||
private void standards(JAXBElement<String> string,
|
|
||||||
JAXBElement<BigInteger> integer,
|
|
||||||
JAXBElement<BigDecimal> decimal,
|
|
||||||
JAXBElement<Calendar> calendar,
|
|
||||||
JAXBElement<Date> date,
|
|
||||||
JAXBElement<QName> qName,
|
|
||||||
JAXBElement<URI> uri,
|
|
||||||
JAXBElement<XMLGregorianCalendar> xmlGregorianCalendar,
|
|
||||||
JAXBElement<Duration> duration,
|
|
||||||
JAXBElement<Object> object,
|
|
||||||
JAXBElement<Image> image,
|
|
||||||
JAXBElement<DataHandler> dataHandler,
|
|
||||||
JAXBElement<Source> source,
|
|
||||||
JAXBElement<UUID> uuid) {
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,126 +16,40 @@
|
||||||
|
|
||||||
package org.springframework.oxm.jaxb;
|
package org.springframework.oxm.jaxb;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
|
||||||
import java.io.StringReader;
|
import java.io.StringReader;
|
||||||
import javax.activation.DataHandler;
|
import javax.activation.DataHandler;
|
||||||
import javax.activation.FileDataSource;
|
import javax.activation.FileDataSource;
|
||||||
import javax.xml.bind.JAXBElement;
|
import javax.xml.bind.JAXBElement;
|
||||||
import javax.xml.parsers.DocumentBuilder;
|
|
||||||
import javax.xml.parsers.DocumentBuilderFactory;
|
|
||||||
import javax.xml.stream.XMLEventReader;
|
|
||||||
import javax.xml.stream.XMLInputFactory;
|
import javax.xml.stream.XMLInputFactory;
|
||||||
import javax.xml.stream.XMLStreamReader;
|
import javax.xml.stream.XMLStreamReader;
|
||||||
import javax.xml.transform.Source;
|
import javax.xml.transform.Source;
|
||||||
import javax.xml.transform.dom.DOMSource;
|
|
||||||
import javax.xml.transform.sax.SAXSource;
|
|
||||||
import javax.xml.transform.stax.StAXSource;
|
|
||||||
import javax.xml.transform.stream.StreamSource;
|
import javax.xml.transform.stream.StreamSource;
|
||||||
|
|
||||||
import static org.easymock.EasyMock.*;
|
import static org.easymock.EasyMock.*;
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.w3c.dom.Document;
|
|
||||||
import org.w3c.dom.Element;
|
|
||||||
import org.w3c.dom.Text;
|
|
||||||
import org.xml.sax.InputSource;
|
|
||||||
import org.xml.sax.XMLReader;
|
|
||||||
import org.xml.sax.helpers.XMLReaderFactory;
|
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
|
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||||
|
import org.springframework.oxm.Unmarshaller;
|
||||||
import org.springframework.oxm.mime.MimeContainer;
|
import org.springframework.oxm.mime.MimeContainer;
|
||||||
import org.springframework.util.xml.StaxUtils;
|
import org.springframework.util.xml.StaxUtils;
|
||||||
|
|
||||||
public class Jaxb2UnmarshallerTest {
|
public class Jaxb2UnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||||
|
|
||||||
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
private static final String INPUT_STRING = "<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
|
||||||
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
"<tns:flight><tns:number>42</tns:number></tns:flight></tns:flights>";
|
||||||
|
|
||||||
private Jaxb2Marshaller unmarshaller;
|
private Jaxb2Marshaller unmarshaller;
|
||||||
|
|
||||||
@Before
|
@Override
|
||||||
public void createMarshaller() throws Exception {
|
public Unmarshaller createUnmarshaller() throws Exception {
|
||||||
unmarshaller = new Jaxb2Marshaller();
|
unmarshaller = new Jaxb2Marshaller();
|
||||||
unmarshaller.setContextPath("org.springframework.oxm.jaxb");
|
unmarshaller.setContextPath("org.springframework.oxm.jaxb");
|
||||||
unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
|
unmarshaller.setSchema(new ClassPathResource("org/springframework/oxm/flight.xsd"));
|
||||||
unmarshaller.afterPropertiesSet();
|
unmarshaller.afterPropertiesSet();
|
||||||
}
|
return unmarshaller;
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalDomSource() throws Exception {
|
|
||||||
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
|
||||||
Document document = builder.newDocument();
|
|
||||||
Element flightsElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flights");
|
|
||||||
document.appendChild(flightsElement);
|
|
||||||
Element flightElement = document.createElementNS("http://samples.springframework.org/flight", "tns:flight");
|
|
||||||
flightsElement.appendChild(flightElement);
|
|
||||||
Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
|
|
||||||
flightElement.appendChild(numberElement);
|
|
||||||
Text text = document.createTextNode("42");
|
|
||||||
numberElement.appendChild(text);
|
|
||||||
DOMSource source = new DOMSource(document);
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalStreamSourceReader() throws Exception {
|
|
||||||
StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testUnmarshalStreamSourceInputStream() throws Exception {
|
|
||||||
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalSAXSource() throws Exception {
|
|
||||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
|
||||||
SAXSource source = new SAXSource(reader, new InputSource(new StringReader(INPUT_STRING)));
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalStaxSourceXmlStreamReader() throws Exception {
|
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
|
||||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
|
||||||
Source source = StaxUtils.createStaxSource(streamReader);
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalStaxSourceXmlEventReader() throws Exception {
|
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
|
||||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
|
||||||
Source source = StaxUtils.createStaxSource(eventReader);
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalStaxSourceXmlStreamReaderJaxp14() throws Exception {
|
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
|
||||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
|
||||||
StAXSource source = new StAXSource(streamReader);
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void unmarshalStaxSourceXmlEventReaderJaxp14() throws Exception {
|
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
|
||||||
XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(INPUT_STRING));
|
|
||||||
StAXSource source = new StAXSource(eventReader);
|
|
||||||
Object flights = unmarshaller.unmarshal(source);
|
|
||||||
testFlights(flights);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -175,20 +89,23 @@ public class Jaxb2UnmarshallerTest {
|
||||||
assertNotNull("datahandler property not set", object.getSwaDataHandler());
|
assertNotNull("datahandler property not set", object.getSwaDataHandler());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testFlights(Object o) {
|
@Override
|
||||||
|
protected void testFlights(Object o) {
|
||||||
Flights flights = (Flights) o;
|
Flights flights = (Flights) o;
|
||||||
assertNotNull("Flights is null", flights);
|
assertNotNull("Flights is null", flights);
|
||||||
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
assertEquals("Invalid amount of flight elements", 1, flights.getFlight().size());
|
||||||
testFlight(flights.getFlight().get(0));
|
testFlight(flights.getFlight().get(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
private void testFlight(Object o) {
|
@Override
|
||||||
|
protected void testFlight(Object o) {
|
||||||
FlightType flight = (FlightType) o;
|
FlightType flight = (FlightType) o;
|
||||||
assertNotNull("Flight is null", flight);
|
assertNotNull("Flight is null", flight);
|
||||||
assertEquals("Number is invalid", 42L, flight.getNumber());
|
assertEquals("Number is invalid", 42L, flight.getNumber());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@Override
|
||||||
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
|
||||||
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
|
||||||
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
|
||||||
|
|
|
@ -1,30 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright 2006 the original author or authors.
|
|
||||||
*
|
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
* you may not use this file except in compliance with the License.
|
|
||||||
* You may obtain a copy of the License at
|
|
||||||
*
|
|
||||||
* http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
*
|
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
* See the License for the specific language governing permissions and
|
|
||||||
* limitations under the License.
|
|
||||||
*/
|
|
||||||
package org.springframework.oxm.jaxb;
|
|
||||||
|
|
||||||
import javax.xml.bind.JAXBException;
|
|
||||||
import javax.xml.bind.MarshalException;
|
|
||||||
import javax.xml.bind.UnmarshalException;
|
|
||||||
import javax.xml.bind.ValidationException;
|
|
||||||
|
|
||||||
import junit.framework.TestCase;
|
|
||||||
|
|
||||||
public class JaxbUtilsTest extends TestCase {
|
|
||||||
|
|
||||||
public void testGetJaxbVersion() throws Exception {
|
|
||||||
assertEquals("Invalid JAXB version", JaxbUtils.JAXB_2, JaxbUtils.getJaxbVersion());
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -29,7 +29,7 @@ import org.junit.Test;
|
||||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
|
|
||||||
@Ignore
|
@Ignore("Code generation not yet support by Spring Build")
|
||||||
public class JibxMarshallerTest extends AbstractMarshallerTestCase {
|
public class JibxMarshallerTest extends AbstractMarshallerTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -22,7 +22,7 @@ import org.junit.Ignore;
|
||||||
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||||
import org.springframework.oxm.Unmarshaller;
|
import org.springframework.oxm.Unmarshaller;
|
||||||
|
|
||||||
@Ignore
|
@Ignore("Code generation not yet support by Spring Build")
|
||||||
public class JibxUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
public class JibxUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -27,7 +27,7 @@ import org.junit.Test;
|
||||||
import org.springframework.oxm.AbstractMarshallerTestCase;
|
import org.springframework.oxm.AbstractMarshallerTestCase;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
|
|
||||||
@Ignore
|
@Ignore("Code generation not yet support by Spring Build")
|
||||||
public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase {
|
public class XmlBeansMarshallerTest extends AbstractMarshallerTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -31,7 +31,7 @@ import org.springframework.oxm.AbstractUnmarshallerTestCase;
|
||||||
import org.springframework.oxm.Unmarshaller;
|
import org.springframework.oxm.Unmarshaller;
|
||||||
import org.springframework.util.xml.StaxUtils;
|
import org.springframework.util.xml.StaxUtils;
|
||||||
|
|
||||||
@Ignore
|
@Ignore("Code generation not yet support by Spring Build")
|
||||||
public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
public class XmlBeansUnmarshallerTest extends AbstractUnmarshallerTestCase {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in New Issue