Public createMarshaller/Unmarshaller methods on Jaxb2Marshaller

Closes gh-22708
This commit is contained in:
Juergen Hoeller 2019-04-02 14:55:39 +02:00
parent c1a74de48d
commit 271445b89e
2 changed files with 85 additions and 71 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2019 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -111,16 +111,16 @@ import org.springframework.util.xml.StaxUtils;
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @since 3.0 * @since 3.0
* @see #setContextPath(String) * @see #setContextPath
* @see #setClassesToBeBound(Class[]) * @see #setClassesToBeBound
* @see #setJaxbContextProperties(Map) * @see #setJaxbContextProperties
* @see #setMarshallerProperties(Map) * @see #setMarshallerProperties
* @see #setUnmarshallerProperties(Map) * @see #setUnmarshallerProperties
* @see #setSchema(Resource) * @see #setSchema
* @see #setSchemas(Resource[]) * @see #setSchemas
* @see #setMarshallerListener(javax.xml.bind.Marshaller.Listener) * @see #setMarshallerListener
* @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) * @see #setUnmarshallerListener
* @see #setAdapters(XmlAdapter[]) * @see #setAdapters
*/ */
public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller,
BeanClassLoaderAware, InitializingBean { BeanClassLoaderAware, InitializingBean {
@ -246,8 +246,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
* Set the packages to search for classes with JAXB2 annotations in the classpath. * Set the packages to search for classes with JAXB2 annotations in the classpath.
* This is using a Spring-bases search and therefore analogous to Spring's component-scan * This is using a Spring-bases search and therefore analogous to Spring's component-scan
* feature ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}). * feature ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
* <p>Setting either this property, {@link #setContextPath "contextPath"} * <p>Setting either this property, {@link #setContextPath "contextPath"} or
* or {@link #setClassesToBeBound "classesToBeBound"} is required. * {@link #setClassesToBeBound "classesToBeBound"} is required.
*/ */
public void setPackagesToScan(@Nullable String... packagesToScan) { public void setPackagesToScan(@Nullable String... packagesToScan) {
this.packagesToScan = packagesToScan; this.packagesToScan = packagesToScan;
@ -270,8 +270,9 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Set the JAXB {@code Marshaller} properties. These properties will be set on the * Set the JAXB {@code Marshaller} properties.
* underlying JAXB {@code Marshaller}, and allow for features such as indentation. * <p>These properties will be set on the underlying JAXB {@code Marshaller},
* and allow for features such as indentation.
* @param properties the properties * @param properties the properties
* @see javax.xml.bind.Marshaller#setProperty(String, Object) * @see javax.xml.bind.Marshaller#setProperty(String, Object)
* @see javax.xml.bind.Marshaller#JAXB_ENCODING * @see javax.xml.bind.Marshaller#JAXB_ENCODING
@ -284,8 +285,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Set the JAXB {@code Unmarshaller} properties. These properties will be set on the * Set the JAXB {@code Unmarshaller} properties.
* underlying JAXB {@code Unmarshaller}. * <p>These properties will be set on the underlying JAXB {@code Unmarshaller}.
* @param properties the properties * @param properties the properties
* @see javax.xml.bind.Unmarshaller#setProperty(String, Object) * @see javax.xml.bind.Unmarshaller#setProperty(String, Object)
*/ */
@ -338,7 +339,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Set the schema language. Default is the W3C XML Schema: {@code http://www.w3.org/2001/XMLSchema"}. * Set the schema language.
* Default is the W3C XML Schema: {@code http://www.w3.org/2001/XMLSchema"}.
* @see XMLConstants#W3C_XML_SCHEMA_NS_URI * @see XMLConstants#W3C_XML_SCHEMA_NS_URI
* @see XMLConstants#RELAXNG_NS_URI * @see XMLConstants#RELAXNG_NS_URI
*/ */
@ -349,8 +351,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
/** /**
* Set the resource resolver, as used to load the schema resources. * Set the resource resolver, as used to load the schema resources.
* @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver) * @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver)
* @see #setSchema(Resource) * @see #setSchema
* @see #setSchemas(Resource[]) * @see #setSchemas
*/ */
public void setSchemaResourceResolver(LSResourceResolver schemaResourceResolver) { public void setSchemaResourceResolver(LSResourceResolver schemaResourceResolver) {
this.schemaResourceResolver = schemaResourceResolver; this.schemaResourceResolver = schemaResourceResolver;
@ -374,10 +376,11 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Specify whether the {@link #supports(Class)} returns {@code true} for the {@link JAXBElement} class. * Specify whether the {@link #supports(Class)} returns {@code true} for the
* <p>Default is {@code false}, meaning that {@code supports(Class)} always returns {@code false} for * {@link JAXBElement} class.
* {@code JAXBElement} classes (though {@link #supports(Type)} can return {@code true}, since it can * <p>Default is {@code false}, meaning that {@code supports(Class)} always returns
* obtain the type parameters of {@code JAXBElement}). * {@code false} for {@code JAXBElement} classes (though {@link #supports(Type)} can
* return {@code true}, since it can obtain the type parameters of {@code JAXBElement}).
* <p>This property is typically enabled in combination with usage of classes like * <p>This property is typically enabled in combination with usage of classes like
* {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView}, * {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView},
* since the {@code ModelAndView} does not offer type parameter information at runtime. * since the {@code ModelAndView} does not offer type parameter information at runtime.
@ -433,8 +436,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
* {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or * {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or
* {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource} * {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource}
* instances. * instances.
* <p><strong>Note:</strong> setting this option to {@code true} also * <p><strong>Note:</strong> setting this option to {@code true} also automatically
* automatically sets {@link #setSupportDtd} to {@code true}. * sets {@link #setSupportDtd} to {@code true}.
*/ */
public void setProcessExternalEntities(boolean processExternalEntities) { public void setProcessExternalEntities(boolean processExternalEntities) {
this.processExternalEntities = processExternalEntities; this.processExternalEntities = processExternalEntities;
@ -710,6 +713,24 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
} }
/**
* Return a newly created JAXB marshaller.
* <p>Note: JAXB marshallers are not necessarily thread-safe.
* This method is public as of 5.2.
* @since 5.2
* @see #createUnmarshaller()
*/
public Marshaller createMarshaller() {
try {
Marshaller marshaller = getJaxbContext().createMarshaller();
initJaxbMarshaller(marshaller);
return marshaller;
}
catch (JAXBException ex) {
throw convertJaxbException(ex);
}
}
private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException { private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException {
XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult); XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult);
if (streamWriter != null) { if (streamWriter != null) {
@ -727,26 +748,14 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe. * Template method that can be overridden by concrete JAXB marshallers
*/ * for custom initialization behavior. Gets called after creation of JAXB
protected Marshaller createMarshaller() { * {@code Marshaller}, and after the respective properties have been set.
try { * <p>The default implementation sets the
Marshaller marshaller = getJaxbContext().createMarshaller(); * {@link #setMarshallerProperties defined properties}, the
initJaxbMarshaller(marshaller); * {@link #setValidationEventHandler validation event handler}, the
return marshaller; * {@link #setSchemas schemas}, {@link #setMarshallerListener listener},
} * and {@link #setAdapters adapters}.
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}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setMarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setMarshallerListener(javax.xml.bind.Marshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/ */
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException { protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
if (this.marshallerProperties != null) { if (this.marshallerProperties != null) {
@ -809,6 +818,24 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
} }
/**
* Return a newly created JAXB unmarshaller.
* <p>Note: JAXB unmarshallers are not necessarily thread-safe.
* This method is public as of 5.2.
* @since 5.2
* @see #createMarshaller()
*/
public Unmarshaller createUnmarshaller() {
try {
Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller();
initJaxbUnmarshaller(unmarshaller);
return unmarshaller;
}
catch (JAXBException ex) {
throw convertJaxbException(ex);
}
}
protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException { protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException {
XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource); XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource);
if (streamReader != null) { if (streamReader != null) {
@ -875,27 +902,14 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Return a newly created JAXB unmarshaller. * Template method that can be overridden by concrete JAXB marshallers
* Note: JAXB unmarshallers are not necessarily thread-safe. * for custom initialization behavior. Gets called after creation of JAXB
*/ * {@code Marshaller}, and after the respective properties have been set.
protected Unmarshaller createUnmarshaller() { * <p>The default implementation sets the
try { * {@link #setUnmarshallerProperties defined properties}, the
Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller(); * {@link #setValidationEventHandler validation event handler}, the
initJaxbUnmarshaller(unmarshaller); * {@link #setSchemas schemas}, {@link #setUnmarshallerListener listener},
return unmarshaller; * and {@link #setAdapters adapters}.
}
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}, and after the respective properties have been set.
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
* {@link #setAdapters(XmlAdapter[]) adapters}.
*/ */
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException { protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
if (this.unmarshallerProperties != null) { if (this.unmarshallerProperties != null) {
@ -920,8 +934,8 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
} }
/** /**
* Convert the given {@code JAXBException} to an appropriate exception from the * Convert the given {@code JAXBException} to an appropriate exception
* {@code org.springframework.oxm} hierarchy. * from the {@code org.springframework.oxm} hierarchy.
* @param ex {@code JAXBException} that occurred * @param ex {@code JAXBException} that occurred
* @return the corresponding {@code XmlMappingException} * @return the corresponding {@code XmlMappingException}
*/ */

View File

@ -327,7 +327,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override @Override
protected javax.xml.bind.Unmarshaller createUnmarshaller() { public javax.xml.bind.Unmarshaller createUnmarshaller() {
return unmarshaller; return unmarshaller;
} }
}; };
@ -361,7 +361,7 @@ public class Jaxb2MarshallerTests extends AbstractMarshallerTests<Jaxb2Marshalle
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class); final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
@Override @Override
protected javax.xml.bind.Unmarshaller createUnmarshaller() { public javax.xml.bind.Unmarshaller createUnmarshaller() {
return unmarshaller; return unmarshaller;
} }
}; };