Skip to content

Commit 271445b

Browse files
committed
Public createMarshaller/Unmarshaller methods on Jaxb2Marshaller
Closes gh-22708
1 parent c1a74de commit 271445b

File tree

2 files changed

+85
-71
lines changed

2 files changed

+85
-71
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jaxb/Jaxb2Marshaller.java

Lines changed: 83 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -111,16 +111,16 @@
111111
* @author Juergen Hoeller
112112
* @author Rossen Stoyanchev
113113
* @since 3.0
114-
* @see #setContextPath(String)
115-
* @see #setClassesToBeBound(Class[])
116-
* @see #setJaxbContextProperties(Map)
117-
* @see #setMarshallerProperties(Map)
118-
* @see #setUnmarshallerProperties(Map)
119-
* @see #setSchema(Resource)
120-
* @see #setSchemas(Resource[])
121-
* @see #setMarshallerListener(javax.xml.bind.Marshaller.Listener)
122-
* @see #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener)
123-
* @see #setAdapters(XmlAdapter[])
114+
* @see #setContextPath
115+
* @see #setClassesToBeBound
116+
* @see #setJaxbContextProperties
117+
* @see #setMarshallerProperties
118+
* @see #setUnmarshallerProperties
119+
* @see #setSchema
120+
* @see #setSchemas
121+
* @see #setMarshallerListener
122+
* @see #setUnmarshallerListener
123+
* @see #setAdapters
124124
*/
125125
public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller,
126126
BeanClassLoaderAware, InitializingBean {
@@ -246,8 +246,8 @@ public Class<?>[] getClassesToBeBound() {
246246
* Set the packages to search for classes with JAXB2 annotations in the classpath.
247247
* This is using a Spring-bases search and therefore analogous to Spring's component-scan
248248
* feature ({@link org.springframework.context.annotation.ClassPathBeanDefinitionScanner}).
249-
* <p>Setting either this property, {@link #setContextPath "contextPath"}
250-
* or {@link #setClassesToBeBound "classesToBeBound"} is required.
249+
* <p>Setting either this property, {@link #setContextPath "contextPath"} or
250+
* {@link #setClassesToBeBound "classesToBeBound"} is required.
251251
*/
252252
public void setPackagesToScan(@Nullable String... packagesToScan) {
253253
this.packagesToScan = packagesToScan;
@@ -270,8 +270,9 @@ public void setJaxbContextProperties(Map<String, ?> jaxbContextProperties) {
270270
}
271271

272272
/**
273-
* Set the JAXB {@code Marshaller} properties. These properties will be set on the
274-
* underlying JAXB {@code Marshaller}, and allow for features such as indentation.
273+
* Set the JAXB {@code Marshaller} properties.
274+
* <p>These properties will be set on the underlying JAXB {@code Marshaller},
275+
* and allow for features such as indentation.
275276
* @param properties the properties
276277
* @see javax.xml.bind.Marshaller#setProperty(String, Object)
277278
* @see javax.xml.bind.Marshaller#JAXB_ENCODING
@@ -284,8 +285,8 @@ public void setMarshallerProperties(Map<String, ?> properties) {
284285
}
285286

286287
/**
287-
* Set the JAXB {@code Unmarshaller} properties. These properties will be set on the
288-
* underlying JAXB {@code Unmarshaller}.
288+
* Set the JAXB {@code Unmarshaller} properties.
289+
* <p>These properties will be set on the underlying JAXB {@code Unmarshaller}.
289290
* @param properties the properties
290291
* @see javax.xml.bind.Unmarshaller#setProperty(String, Object)
291292
*/
@@ -338,7 +339,8 @@ public void setSchemas(Resource... schemaResources) {
338339
}
339340

340341
/**
341-
* Set the schema language. Default is the W3C XML Schema: {@code http://www.w3.org/2001/XMLSchema"}.
342+
* Set the schema language.
343+
* Default is the W3C XML Schema: {@code http://www.w3.org/2001/XMLSchema"}.
342344
* @see XMLConstants#W3C_XML_SCHEMA_NS_URI
343345
* @see XMLConstants#RELAXNG_NS_URI
344346
*/
@@ -349,8 +351,8 @@ public void setSchemaLanguage(String schemaLanguage) {
349351
/**
350352
* Set the resource resolver, as used to load the schema resources.
351353
* @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver)
352-
* @see #setSchema(Resource)
353-
* @see #setSchemas(Resource[])
354+
* @see #setSchema
355+
* @see #setSchemas
354356
*/
355357
public void setSchemaResourceResolver(LSResourceResolver schemaResourceResolver) {
356358
this.schemaResourceResolver = schemaResourceResolver;
@@ -374,10 +376,11 @@ public void setMtomEnabled(boolean mtomEnabled) {
374376
}
375377

376378
/**
377-
* Specify whether the {@link #supports(Class)} returns {@code true} for the {@link JAXBElement} class.
378-
* <p>Default is {@code false}, meaning that {@code supports(Class)} always returns {@code false} for
379-
* {@code JAXBElement} classes (though {@link #supports(Type)} can return {@code true}, since it can
380-
* obtain the type parameters of {@code JAXBElement}).
379+
* Specify whether the {@link #supports(Class)} returns {@code true} for the
380+
* {@link JAXBElement} class.
381+
* <p>Default is {@code false}, meaning that {@code supports(Class)} always returns
382+
* {@code false} for {@code JAXBElement} classes (though {@link #supports(Type)} can
383+
* return {@code true}, since it can obtain the type parameters of {@code JAXBElement}).
381384
* <p>This property is typically enabled in combination with usage of classes like
382385
* {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView},
383386
* since the {@code ModelAndView} does not offer type parameter information at runtime.
@@ -433,8 +436,8 @@ public boolean isSupportDtd() {
433436
* {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or
434437
* {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource}
435438
* instances.
436-
* <p><strong>Note:</strong> setting this option to {@code true} also
437-
* automatically sets {@link #setSupportDtd} to {@code true}.
439+
* <p><strong>Note:</strong> setting this option to {@code true} also automatically
440+
* sets {@link #setSupportDtd} to {@code true}.
438441
*/
439442
public void setProcessExternalEntities(boolean processExternalEntities) {
440443
this.processExternalEntities = processExternalEntities;
@@ -710,6 +713,24 @@ public void marshal(Object graph, Result result, @Nullable MimeContainer mimeCon
710713
}
711714
}
712715

716+
/**
717+
* Return a newly created JAXB marshaller.
718+
* <p>Note: JAXB marshallers are not necessarily thread-safe.
719+
* This method is public as of 5.2.
720+
* @since 5.2
721+
* @see #createUnmarshaller()
722+
*/
723+
public Marshaller createMarshaller() {
724+
try {
725+
Marshaller marshaller = getJaxbContext().createMarshaller();
726+
initJaxbMarshaller(marshaller);
727+
return marshaller;
728+
}
729+
catch (JAXBException ex) {
730+
throw convertJaxbException(ex);
731+
}
732+
}
733+
713734
private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result staxResult) throws JAXBException {
714735
XMLStreamWriter streamWriter = StaxUtils.getXMLStreamWriter(staxResult);
715736
if (streamWriter != null) {
@@ -727,26 +748,14 @@ private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result s
727748
}
728749

729750
/**
730-
* Return a newly created JAXB marshaller. JAXB marshallers are not necessarily thread safe.
731-
*/
732-
protected Marshaller createMarshaller() {
733-
try {
734-
Marshaller marshaller = getJaxbContext().createMarshaller();
735-
initJaxbMarshaller(marshaller);
736-
return marshaller;
737-
}
738-
catch (JAXBException ex) {
739-
throw convertJaxbException(ex);
740-
}
741-
}
742-
743-
/**
744-
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
745-
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
746-
* <p>The default implementation sets the {@link #setMarshallerProperties(Map) defined properties}, the {@link
747-
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
748-
* schemas}, {@link #setMarshallerListener(javax.xml.bind.Marshaller.Listener) listener}, and
749-
* {@link #setAdapters(XmlAdapter[]) adapters}.
751+
* Template method that can be overridden by concrete JAXB marshallers
752+
* for custom initialization behavior. Gets called after creation of JAXB
753+
* {@code Marshaller}, and after the respective properties have been set.
754+
* <p>The default implementation sets the
755+
* {@link #setMarshallerProperties defined properties}, the
756+
* {@link #setValidationEventHandler validation event handler}, the
757+
* {@link #setSchemas schemas}, {@link #setMarshallerListener listener},
758+
* and {@link #setAdapters adapters}.
750759
*/
751760
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException {
752761
if (this.marshallerProperties != null) {
@@ -809,6 +818,24 @@ else if (this.mappedClass != null) {
809818
}
810819
}
811820

821+
/**
822+
* Return a newly created JAXB unmarshaller.
823+
* <p>Note: JAXB unmarshallers are not necessarily thread-safe.
824+
* This method is public as of 5.2.
825+
* @since 5.2
826+
* @see #createMarshaller()
827+
*/
828+
public Unmarshaller createUnmarshaller() {
829+
try {
830+
Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller();
831+
initJaxbUnmarshaller(unmarshaller);
832+
return unmarshaller;
833+
}
834+
catch (JAXBException ex) {
835+
throw convertJaxbException(ex);
836+
}
837+
}
838+
812839
protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException {
813840
XMLStreamReader streamReader = StaxUtils.getXMLStreamReader(staxSource);
814841
if (streamReader != null) {
@@ -875,27 +902,14 @@ else if (streamSource.getReader() != null) {
875902
}
876903

877904
/**
878-
* Return a newly created JAXB unmarshaller.
879-
* Note: JAXB unmarshallers are not necessarily thread-safe.
880-
*/
881-
protected Unmarshaller createUnmarshaller() {
882-
try {
883-
Unmarshaller unmarshaller = getJaxbContext().createUnmarshaller();
884-
initJaxbUnmarshaller(unmarshaller);
885-
return unmarshaller;
886-
}
887-
catch (JAXBException ex) {
888-
throw convertJaxbException(ex);
889-
}
890-
}
891-
892-
/**
893-
* Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
894-
* Gets called after creation of JAXB {@code Marshaller}, and after the respective properties have been set.
895-
* <p>The default implementation sets the {@link #setUnmarshallerProperties(Map) defined properties}, the {@link
896-
* #setValidationEventHandler(ValidationEventHandler) validation event handler}, the {@link #setSchemas(Resource[])
897-
* schemas}, {@link #setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener) listener}, and
898-
* {@link #setAdapters(XmlAdapter[]) adapters}.
905+
* Template method that can be overridden by concrete JAXB marshallers
906+
* for custom initialization behavior. Gets called after creation of JAXB
907+
* {@code Marshaller}, and after the respective properties have been set.
908+
* <p>The default implementation sets the
909+
* {@link #setUnmarshallerProperties defined properties}, the
910+
* {@link #setValidationEventHandler validation event handler}, the
911+
* {@link #setSchemas schemas}, {@link #setUnmarshallerListener listener},
912+
* and {@link #setAdapters adapters}.
899913
*/
900914
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException {
901915
if (this.unmarshallerProperties != null) {
@@ -920,8 +934,8 @@ protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBExcept
920934
}
921935

922936
/**
923-
* Convert the given {@code JAXBException} to an appropriate exception from the
924-
* {@code org.springframework.oxm} hierarchy.
937+
* Convert the given {@code JAXBException} to an appropriate exception
938+
* from the {@code org.springframework.oxm} hierarchy.
925939
* @param ex {@code JAXBException} that occurred
926940
* @return the corresponding {@code XmlMappingException}
927941
*/

spring-oxm/src/test/java/org/springframework/oxm/jaxb/Jaxb2MarshallerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -327,7 +327,7 @@ public void unmarshalStreamSourceWithXmlOptions() throws Exception {
327327
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
328328
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
329329
@Override
330-
protected javax.xml.bind.Unmarshaller createUnmarshaller() {
330+
public javax.xml.bind.Unmarshaller createUnmarshaller() {
331331
return unmarshaller;
332332
}
333333
};
@@ -361,7 +361,7 @@ public void unmarshalSaxSourceWithXmlOptions() throws Exception {
361361
final javax.xml.bind.Unmarshaller unmarshaller = mock(javax.xml.bind.Unmarshaller.class);
362362
Jaxb2Marshaller marshaller = new Jaxb2Marshaller() {
363363
@Override
364-
protected javax.xml.bind.Unmarshaller createUnmarshaller() {
364+
public javax.xml.bind.Unmarshaller createUnmarshaller() {
365365
return unmarshaller;
366366
}
367367
};

0 commit comments

Comments
 (0)