1
1
/*
2
- * Copyright 2002-2018 the original author or authors.
2
+ * Copyright 2002-2019 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
111
111
* @author Juergen Hoeller
112
112
* @author Rossen Stoyanchev
113
113
* @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
124
124
*/
125
125
public class Jaxb2Marshaller implements MimeMarshaller , MimeUnmarshaller , GenericMarshaller , GenericUnmarshaller ,
126
126
BeanClassLoaderAware , InitializingBean {
@@ -246,8 +246,8 @@ public Class<?>[] getClassesToBeBound() {
246
246
* Set the packages to search for classes with JAXB2 annotations in the classpath.
247
247
* This is using a Spring-bases search and therefore analogous to Spring's component-scan
248
248
* 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.
251
251
*/
252
252
public void setPackagesToScan (@ Nullable String ... packagesToScan ) {
253
253
this .packagesToScan = packagesToScan ;
@@ -270,8 +270,9 @@ public void setJaxbContextProperties(Map<String, ?> jaxbContextProperties) {
270
270
}
271
271
272
272
/**
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.
275
276
* @param properties the properties
276
277
* @see javax.xml.bind.Marshaller#setProperty(String, Object)
277
278
* @see javax.xml.bind.Marshaller#JAXB_ENCODING
@@ -284,8 +285,8 @@ public void setMarshallerProperties(Map<String, ?> properties) {
284
285
}
285
286
286
287
/**
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}.
289
290
* @param properties the properties
290
291
* @see javax.xml.bind.Unmarshaller#setProperty(String, Object)
291
292
*/
@@ -338,7 +339,8 @@ public void setSchemas(Resource... schemaResources) {
338
339
}
339
340
340
341
/**
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"}.
342
344
* @see XMLConstants#W3C_XML_SCHEMA_NS_URI
343
345
* @see XMLConstants#RELAXNG_NS_URI
344
346
*/
@@ -349,8 +351,8 @@ public void setSchemaLanguage(String schemaLanguage) {
349
351
/**
350
352
* Set the resource resolver, as used to load the schema resources.
351
353
* @see SchemaFactory#setResourceResolver(org.w3c.dom.ls.LSResourceResolver)
352
- * @see #setSchema(Resource)
353
- * @see #setSchemas(Resource[])
354
+ * @see #setSchema
355
+ * @see #setSchemas
354
356
*/
355
357
public void setSchemaResourceResolver (LSResourceResolver schemaResourceResolver ) {
356
358
this .schemaResourceResolver = schemaResourceResolver ;
@@ -374,10 +376,11 @@ public void setMtomEnabled(boolean mtomEnabled) {
374
376
}
375
377
376
378
/**
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}).
381
384
* <p>This property is typically enabled in combination with usage of classes like
382
385
* {@link org.springframework.web.servlet.view.xml.MarshallingView MarshallingView},
383
386
* since the {@code ModelAndView} does not offer type parameter information at runtime.
@@ -433,8 +436,8 @@ public boolean isSupportDtd() {
433
436
* {@code Source} passed to {@link #unmarshal(Source)} is a {@link SAXSource} or
434
437
* {@link StreamSource}. It has no effect for {@link DOMSource} or {@link StAXSource}
435
438
* 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}.
438
441
*/
439
442
public void setProcessExternalEntities (boolean processExternalEntities ) {
440
443
this .processExternalEntities = processExternalEntities ;
@@ -710,6 +713,24 @@ public void marshal(Object graph, Result result, @Nullable MimeContainer mimeCon
710
713
}
711
714
}
712
715
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
+
713
734
private void marshalStaxResult (Marshaller jaxbMarshaller , Object graph , Result staxResult ) throws JAXBException {
714
735
XMLStreamWriter streamWriter = StaxUtils .getXMLStreamWriter (staxResult );
715
736
if (streamWriter != null ) {
@@ -727,26 +748,14 @@ private void marshalStaxResult(Marshaller jaxbMarshaller, Object graph, Result s
727
748
}
728
749
729
750
/**
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}.
750
759
*/
751
760
protected void initJaxbMarshaller (Marshaller marshaller ) throws JAXBException {
752
761
if (this .marshallerProperties != null ) {
@@ -809,6 +818,24 @@ else if (this.mappedClass != null) {
809
818
}
810
819
}
811
820
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
+
812
839
protected Object unmarshalStaxSource (Unmarshaller jaxbUnmarshaller , Source staxSource ) throws JAXBException {
813
840
XMLStreamReader streamReader = StaxUtils .getXMLStreamReader (staxSource );
814
841
if (streamReader != null ) {
@@ -875,27 +902,14 @@ else if (streamSource.getReader() != null) {
875
902
}
876
903
877
904
/**
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}.
899
913
*/
900
914
protected void initJaxbUnmarshaller (Unmarshaller unmarshaller ) throws JAXBException {
901
915
if (this .unmarshallerProperties != null ) {
@@ -920,8 +934,8 @@ protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBExcept
920
934
}
921
935
922
936
/**
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.
925
939
* @param ex {@code JAXBException} that occurred
926
940
* @return the corresponding {@code XmlMappingException}
927
941
*/
0 commit comments