Skip to content

Commit 8efac21

Browse files
Arjen Poutsmarstoyanchev
authored andcommitted
Support 'empty' StreamSource in Jaxb2Marshaller
Added support for StreamSources that do not have a InputStream or Reader, but do have a System ID. Issue: 10828
1 parent 40c7303 commit 8efac21

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,9 @@ else if (source instanceof StreamSource) {
784784
else if (streamSource.getReader() != null) {
785785
inputSource = new InputSource(streamSource.getReader());
786786
}
787+
else {
788+
inputSource = new InputSource(streamSource.getSystemId());
789+
}
787790
}
788791

789792
try {

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

1717
package org.springframework.oxm.jaxb;
1818

19+
import java.io.File;
20+
import java.io.IOException;
1921
import java.io.StringReader;
20-
2122
import javax.activation.DataHandler;
2223
import javax.activation.FileDataSource;
2324
import javax.xml.bind.JAXBElement;
@@ -26,7 +27,11 @@
2627
import javax.xml.transform.Source;
2728
import javax.xml.transform.stream.StreamSource;
2829

30+
import static org.junit.Assert.*;
2931
import org.junit.Test;
32+
import static org.mockito.BDDMockito.given;
33+
import static org.mockito.BDDMockito.mock;
34+
3035
import org.springframework.core.io.ClassPathResource;
3136
import org.springframework.core.io.Resource;
3237
import org.springframework.oxm.AbstractUnmarshallerTests;
@@ -36,9 +41,6 @@
3641
import org.springframework.oxm.mime.MimeContainer;
3742
import org.springframework.util.xml.StaxUtils;
3843

39-
import static org.junit.Assert.*;
40-
import static org.mockito.BDDMockito.*;
41-
4244
/**
4345
* @author Arjen Poutsma
4446
* @author Biju Kunjummen
@@ -134,4 +136,13 @@ public void unmarshalAnXmlReferingToAWrappedXmlElementDecl() throws Exception {
134136
"test", airplane.getValue().getName());
135137
}
136138

139+
@Test
140+
public void unmarshalFile() throws IOException {
141+
Resource resource = new ClassPathResource("jaxb2.xml", getClass());
142+
File file = resource.getFile();
143+
144+
Flights f = (Flights) unmarshaller.unmarshal(new StreamSource(file));
145+
testFlights(f);
146+
}
147+
137148
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<tns:flights xmlns:tns="http://samples.springframework.org/flight">
2+
<tns:flight>
3+
<tns:number>42</tns:number>
4+
</tns:flight>
5+
</tns:flights>

0 commit comments

Comments
 (0)