Skip to content

Commit

Permalink
Use configured encoding during JiBX unmarshalling
Browse files Browse the repository at this point in the history
Before this change JibxMarshaller did not use the configured encoding
when unmarshalling XML. This caused issues when content being
unmarshalled was not encoded using the default encoding.

This commit fixes the issue by passing configured encoding to JiBX so
it gets used when unmarshalling instead of the default encoding.

Issue: SPR-7865
  • Loading branch information
sslavic authored and cbeams committed Mar 15, 2012
1 parent e3f5449 commit e25183e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -349,7 +349,7 @@ protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
protected Object unmarshalInputStream(InputStream inputStream) throws XmlMappingException, IOException {
try {
IUnmarshallingContext unmarshallingContext = createUnmarshallingContext();
return unmarshallingContext.unmarshalDocument(inputStream, null);
return unmarshallingContext.unmarshalDocument(inputStream, encoding);
}
catch (JiBXException ex) {
throw convertJibxException(ex, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006 the original author or authors.
* Copyright 2006-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,13 +18,23 @@

public class FlightType {

protected long number;
protected String airline;

public long getNumber() {
return this.number;
}
protected long number;

public void setNumber(long number) {
this.number = number;
}
public String getAirline() {
return this.airline;
}

public void setAirline(String airline) {
this.airline = airline;
}

public long getNumber() {
return this.number;
}

public void setNumber(long number) {
this.number = number;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2009 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,13 +16,17 @@

package org.springframework.oxm.jibx;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import org.junit.Ignore;
import java.io.ByteArrayInputStream;

import javax.xml.transform.stream.StreamSource;

import org.junit.Ignore;
import org.junit.Test;
import org.springframework.oxm.AbstractUnmarshallerTests;
import org.springframework.oxm.Unmarshaller;

import static org.junit.Assert.*;

/**
* @author Arjen Poutsma
*
Expand All @@ -31,6 +35,10 @@
*/
public class JibxUnmarshallerTests extends AbstractUnmarshallerTests {

protected static final String INPUT_STRING_WITH_SPECIAL_CHARACTERS =
"<tns:flights xmlns:tns=\"http://samples.springframework.org/flight\">" +
"<tns:flight><tns:airline>Air Liberté</tns:airline><tns:number>42</tns:number></tns:flight></tns:flights>";

@Override
protected Unmarshaller createUnmarshaller() throws Exception {
JibxMarshaller unmarshaller = new JibxMarshaller();
Expand Down Expand Up @@ -60,4 +68,17 @@ public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception {
// JiBX does not support reading XML fragments, hence the override here
}

@Test
public void unmarshalStreamSourceInputStreamUsingNonDefaultEncoding() throws Exception {
String encoding = "ISO-8859-1";
((JibxMarshaller)unmarshaller).setEncoding(encoding);

StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING_WITH_SPECIAL_CHARACTERS.getBytes(encoding)));
Object flights = unmarshaller.unmarshal(source);
testFlights(flights);

FlightType flight = ((Flights)flights).getFlight(0);
assertEquals("Airline is invalid", "Air Liberté", flight.getAirline());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
</mapping>
<mapping name="flight" class="org.springframework.oxm.jibx.FlightType">
<namespace uri="http://samples.springframework.org/flight" default="elements"/>
<value name="airline" field="airline" usage="optional"/>
<value name="number" field="number" usage="required"/>
</mapping>
</binding>

0 comments on commit e25183e

Please sign in to comment.