Skip to content

Commit 4d3aa59

Browse files
committed
Polishing
(cherry picked from commit 6fef8b9)
1 parent c6503eb commit 4d3aa59

File tree

4 files changed

+39
-34
lines changed

4 files changed

+39
-34
lines changed

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

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
import javax.xml.transform.Source;
6565
import javax.xml.transform.dom.DOMSource;
6666
import javax.xml.transform.sax.SAXSource;
67+
import javax.xml.transform.stax.StAXSource;
6768
import javax.xml.transform.stream.StreamSource;
6869
import javax.xml.validation.Schema;
6970
import javax.xml.validation.SchemaFactory;
@@ -82,7 +83,6 @@
8283
import org.springframework.core.JdkVersion;
8384
import org.springframework.core.annotation.AnnotationUtils;
8485
import org.springframework.core.io.Resource;
85-
import org.springframework.core.io.ResourceLoader;
8686
import org.springframework.oxm.GenericMarshaller;
8787
import org.springframework.oxm.GenericUnmarshaller;
8888
import org.springframework.oxm.MarshallingFailureException;
@@ -102,14 +102,15 @@
102102
import org.springframework.util.xml.StaxUtils;
103103

104104
/**
105-
* Implementation of the {@code Marshaller} interface for JAXB 2.0.
105+
* Implementation of the {@code GenericMarshaller} interface for JAXB 2.0+.
106106
*
107107
* <p>The typical usage will be to set either the "contextPath" or the "classesToBeBound"
108108
* property on this bean, possibly customize the marshaller and unmarshaller by setting
109109
* properties, schemas, adapters, and listeners, and to refer to it.
110110
*
111111
* @author Arjen Poutsma
112112
* @author Juergen Hoeller
113+
* @author Rossen Stoyanchev
113114
* @since 3.0
114115
* @see #setContextPath(String)
115116
* @see #setClassesToBeBound(Class[])
@@ -169,8 +170,6 @@ public class Jaxb2Marshaller implements MimeMarshaller, MimeUnmarshaller, Generi
169170

170171
private ClassLoader beanClassLoader;
171172

172-
private ResourceLoader resourceLoader;
173-
174173
private final Object jaxbContextMonitor = new Object();
175174

176175
private volatile JAXBContext jaxbContext;
@@ -299,7 +298,7 @@ public void setValidationEventHandler(ValidationEventHandler validationEventHand
299298
* Specify the {@code XmlAdapter}s to be registered with the JAXB {@code Marshaller}
300299
* and {@code Unmarshaller}
301300
*/
302-
public void setAdapters(XmlAdapter<?, ?>[] adapters) {
301+
public void setAdapters(XmlAdapter<?, ?>... adapters) {
303302
this.adapters = adapters;
304303
}
305304

@@ -313,7 +312,7 @@ public void setSchema(Resource schemaResource) {
313312
/**
314313
* Set the schema resources to use for validation.
315314
*/
316-
public void setSchemas(Resource[] schemaResources) {
315+
public void setSchemas(Resource... schemaResources) {
317316
this.schemaResources = schemaResources;
318317
}
319318

@@ -887,7 +886,7 @@ private static class Jaxb2AttachmentMarshaller extends AttachmentMarshaller {
887886

888887
private final MimeContainer mimeContainer;
889888

890-
private Jaxb2AttachmentMarshaller(MimeContainer mimeContainer) {
889+
public Jaxb2AttachmentMarshaller(MimeContainer mimeContainer) {
891890
this.mimeContainer = mimeContainer;
892891
}
893892

@@ -941,7 +940,7 @@ private static class Jaxb2AttachmentUnmarshaller extends AttachmentUnmarshaller
941940

942941
private final MimeContainer mimeContainer;
943942

944-
private Jaxb2AttachmentUnmarshaller(MimeContainer mimeContainer) {
943+
public Jaxb2AttachmentUnmarshaller(MimeContainer mimeContainer) {
945944
this.mimeContainer = mimeContainer;
946945
}
947946

@@ -991,7 +990,7 @@ private static class ByteArrayDataSource implements DataSource {
991990

992991
private final int length;
993992

994-
private ByteArrayDataSource(String contentType, byte[] data, int offset, int length) {
993+
public ByteArrayDataSource(String contentType, byte[] data, int offset, int length) {
995994
this.contentType = contentType;
996995
this.data = data;
997996
this.offset = offset;

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2CollectionHttpMessageConverter.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -26,7 +26,6 @@
2626
import java.util.List;
2727
import java.util.SortedSet;
2828
import java.util.TreeSet;
29-
3029
import javax.xml.bind.JAXBException;
3130
import javax.xml.bind.UnmarshalException;
3231
import javax.xml.bind.Unmarshaller;
@@ -54,13 +53,15 @@
5453
* does not support writing.
5554
*
5655
* @author Arjen Poutsma
56+
* @author Rossen Stoyanchev
5757
* @since 3.2
5858
*/
5959
public class Jaxb2CollectionHttpMessageConverter<T extends Collection>
6060
extends AbstractJaxb2HttpMessageConverter<T> implements GenericHttpMessageConverter<T> {
6161

6262
private final XMLInputFactory inputFactory = createXmlInputFactory();
6363

64+
6465
/**
6566
* Always returns {@code false} since Jaxb2CollectionHttpMessageConverter
6667
* required generic type information in order to read a Collection.
@@ -121,6 +122,7 @@ protected T readFromSource(Class<? extends T> clazz, HttpHeaders headers, Source
121122
throw new UnsupportedOperationException();
122123
}
123124

125+
@SuppressWarnings("unchecked")
124126
public T read(Type type, Class<?> contextClass, HttpInputMessage inputMessage)
125127
throws IOException, HttpMessageNotReadableException {
126128

@@ -162,7 +164,6 @@ else if (elementClass.isAnnotationPresent(XmlType.class)) {
162164
/**
163165
* Create a Collection of the given type, with the given initial capacity
164166
* (if supported by the Collection type).
165-
*
166167
* @param collectionClass the type of Collection to instantiate
167168
* @return the created Collection instance
168169
*/
@@ -220,14 +221,12 @@ protected void writeToResult(T t, HttpHeaders headers, Result result) throws IOE
220221
/**
221222
* Create a {@code XMLInputFactory} that this converter will use to create {@link
222223
* javax.xml.stream.XMLStreamReader} and {@link javax.xml.stream.XMLEventReader} objects.
223-
* <p/> Can be overridden in subclasses, adding further initialization of the factory.
224+
* <p>Can be overridden in subclasses, adding further initialization of the factory.
224225
* The resulting factory is cached, so this method will only be called once.
225-
*
226-
* @return the created factory
227226
*/
228227
protected XMLInputFactory createXmlInputFactory() {
229228
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
230-
inputFactory.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);
229+
inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
231230
inputFactory.setXMLResolver(NO_OP_XML_RESOLVER);
232231
return inputFactory;
233232
}

spring-web/src/main/java/org/springframework/http/converter/xml/Jaxb2RootElementHttpMessageConverter.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -32,32 +32,35 @@
3232
import javax.xml.transform.sax.SAXSource;
3333
import javax.xml.transform.stream.StreamSource;
3434

35+
import org.xml.sax.EntityResolver;
36+
import org.xml.sax.InputSource;
37+
import org.xml.sax.SAXException;
38+
import org.xml.sax.XMLReader;
39+
import org.xml.sax.helpers.XMLReaderFactory;
40+
3541
import org.springframework.core.annotation.AnnotationUtils;
3642
import org.springframework.http.HttpHeaders;
3743
import org.springframework.http.MediaType;
3844
import org.springframework.http.converter.HttpMessageConversionException;
3945
import org.springframework.http.converter.HttpMessageNotReadableException;
4046
import org.springframework.http.converter.HttpMessageNotWritableException;
4147
import org.springframework.util.ClassUtils;
42-
import org.xml.sax.EntityResolver;
43-
import org.xml.sax.InputSource;
44-
import org.xml.sax.SAXException;
45-
import org.xml.sax.XMLReader;
46-
import org.xml.sax.helpers.XMLReaderFactory;
4748

4849
/**
49-
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter} that can read
50-
* and write XML using JAXB2.
50+
* Implementation of {@link org.springframework.http.converter.HttpMessageConverter HttpMessageConverter}
51+
* that can read and write XML using JAXB2.
5152
*
52-
* <p>This converter can read classes annotated with {@link XmlRootElement} and {@link XmlType}, and write classes
53-
* annotated with with {@link XmlRootElement}, or subclasses thereof.
53+
* <p>This converter can read classes annotated with {@link XmlRootElement} and {@link XmlType},
54+
* and write classes annotated with with {@link XmlRootElement}, or subclasses thereof.
5455
*
5556
* @author Arjen Poutsma
57+
* @author Sebastien Deleuze
58+
* @author Rossen Stoyanchev
5659
* @since 3.0
5760
*/
5861
public class Jaxb2RootElementHttpMessageConverter extends AbstractJaxb2HttpMessageConverter<Object> {
5962

60-
private boolean processExternalEntities = false;
63+
private boolean processExternalEntities = false;
6164

6265

6366
/**
@@ -68,6 +71,9 @@ public void setProcessExternalEntities(boolean processExternalEntities) {
6871
this.processExternalEntities = processExternalEntities;
6972
}
7073

74+
/**
75+
* Returns the configured value for whether XML external entities are allowed.
76+
*/
7177
public boolean isProcessExternalEntities() {
7278
return this.processExternalEntities;
7379
}
@@ -81,7 +87,7 @@ public boolean canRead(Class<?> clazz, MediaType mediaType) {
8187

8288
@Override
8389
public boolean canWrite(Class<?> clazz, MediaType mediaType) {
84-
return AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType);
90+
return (AnnotationUtils.findAnnotation(clazz, XmlRootElement.class) != null && canWrite(mediaType));
8591
}
8692

8793
@Override
@@ -99,7 +105,7 @@ protected Object readFromSource(Class<?> clazz, HttpHeaders headers, Source sour
99105
return unmarshaller.unmarshal(source);
100106
}
101107
else {
102-
JAXBElement jaxbElement = unmarshaller.unmarshal(source, clazz);
108+
JAXBElement<?> jaxbElement = unmarshaller.unmarshal(source, clazz);
103109
return jaxbElement.getValue();
104110
}
105111
}
@@ -138,7 +144,7 @@ protected Source processSource(Source source) {
138144
@Override
139145
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
140146
try {
141-
Class clazz = ClassUtils.getUserClass(o);
147+
Class<?> clazz = ClassUtils.getUserClass(o);
142148
Marshaller marshaller = createMarshaller(clazz);
143149
setCharset(headers.getContentType(), marshaller);
144150
marshaller.marshal(o, result);

spring-web/src/main/java/org/springframework/http/converter/xml/SourceHttpMessageConverter.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -57,6 +57,7 @@
5757
* that can read and write {@link Source} objects.
5858
*
5959
* @author Arjen Poutsma
60+
* @author Rossen Stoyanchev
6061
* @since 3.0
6162
*/
6263
public class SourceHttpMessageConverter<T extends Source> extends AbstractHttpMessageConverter<T> {
@@ -94,12 +95,13 @@ public void setProcessExternalEntities(boolean processExternalEntities) {
9495
}
9596

9697
/**
97-
* @return the configured value for whether XML external entities are allowed.
98+
* Returns the configured value for whether XML external entities are allowed.
9899
*/
99100
public boolean isProcessExternalEntities() {
100101
return this.processExternalEntities;
101102
}
102103

104+
103105
@Override
104106
public boolean supports(Class<?> clazz) {
105107
return SUPPORTED_CLASSES.contains(clazz);
@@ -150,8 +152,7 @@ private DOMSource readDOMSource(InputStream body) throws IOException {
150152
private SAXSource readSAXSource(InputStream body) throws IOException {
151153
try {
152154
XMLReader reader = XMLReaderFactory.createXMLReader();
153-
reader.setFeature(
154-
"http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
155+
reader.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities());
155156
byte[] bytes = StreamUtils.copyToByteArray(body);
156157
if (!isProcessExternalEntities()) {
157158
reader.setEntityResolver(NO_OP_ENTITY_RESOLVER);

0 commit comments

Comments
 (0)