Skip to content

Commit bb5c81e

Browse files
committed
Merge pull request #58 from sslavic/SPR-7865
* SPR-7865: Use configured encoding during JiBX unmarshalling
2 parents e3f5449 + e25183e commit bb5c81e

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -349,7 +349,7 @@ protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
349349
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
350350
try {
351351
IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
352-
return unmarshallingContext.unmarshalDocument(inputStream, null);
352+
return unmarshallingContext.unmarshalDocument(inputStream, encoding);
353353
}
354354
catch (JiBXException ex) {
355355
throw convertJibxException(ex, false);

spring-oxm/src/test/java/org/springframework/oxm/jibx/FlightType.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2006 the original author or authors.
2+
* Copyright 2006-2012 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.
@@ -18,13 +18,23 @@
1818

1919
public class FlightType {
2020

21-
protected long number;
21+
protected String airline;
2222

23-
public long getNumber() {
24-
return this.number;
25-
}
23+
protected long number;
2624

27-
public void setNumber(long number) {
28-
this.number = number;
29-
}
25+
public String getAirline() {
26+
return this.airline;
27+
}
28+
29+
public void setAirline(String airline) {
30+
this.airline = airline;
31+
}
32+
33+
public long getNumber() {
34+
return this.number;
35+
}
36+
37+
public void setNumber(long number) {
38+
this.number = number;
39+
}
3040
}

spring-oxm/src/test/java/org/springframework/oxm/jibx/JibxUnmarshallerTests.java

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2009 the original author or authors.
2+
* Copyright 2002-2012 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.
@@ -16,13 +16,17 @@
1616

1717
package org.springframework.oxm.jibx;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
21-
import org.junit.Ignore;
19+
import java.io.ByteArrayInputStream;
20+
21+
import javax.xml.transform.stream.StreamSource;
2222

23+
import org.junit.Ignore;
24+
import org.junit.Test;
2325
import org.springframework.oxm.AbstractUnmarshallerTests;
2426
import org.springframework.oxm.Unmarshaller;
2527

28+
import static org.junit.Assert.*;
29+
2630
/**
2731
* @author Arjen Poutsma
2832
*
@@ -31,6 +35,10 @@
3135
*/
3236
public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {
3337

38+
protected static final String INPUT_STRING_WITH_SPECIAL_CHARACTERS =
39+
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
40+
"<tns:flight><tns:airline>Air Liberté</tns:airline><tns:number>42</tns:number></tns:flight></tns:flights>";
41+
3442
@Override
3543
protected Unmarshaller createUnmarshaller() throws Exception {
3644
JibxMarshaller unmarshaller = new JibxMarshaller();
@@ -60,4 +68,17 @@ public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
6068
// JiBX does not support reading XML fragments, hence the override here
6169
}
6270

71+
@Test
72+
public void unmarshalStreamSourceInputStreamUsingNonDefaultEncoding() throws Exception {
73+
String encoding = "ISO-8859-1";
74+
((JibxMarshaller)unmarshaller).setEncoding(encoding);
75+
76+
StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING_WITH_SPECIAL_CHARACTERS.getBytes(encoding)));
77+
Object flights = unmarshaller.unmarshal(source);
78+
testFlights(flights);
79+
80+
FlightType flight = ((Flights)flights).getFlight(0);
81+
assertEquals("Airline is invalid", "Air Liberté", flight.getAirline());
82+
}
83+
6384
}

spring-oxm/src/test/resources/org/springframework/oxm/jibx/binding.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
</mapping>
99
<mapping name="flight" class="org.springframework.oxm.jibx.FlightType">
1010
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
11+
<value name="airline" field="airline" usage="optional"/>
1112
<value name="number" field="number" usage="required"/>
1213
</mapping>
1314
</binding>

0 commit comments

Comments
 (0)