From e25183eab1b5fbce42750e92e95248c0c24445b0 Mon Sep 17 00:00:00 2001 From: Stevo Slavic Date: Thu, 15 Mar 2012 00:22:51 +0100 Subject: [PATCH] Use configured encoding during JiBX unmarshalling 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 --- .../oxm/jibx/JibxMarshaller.java | 4 +-- .../springframework/oxm/jibx/FlightType.java | 26 ++++++++++++----- .../oxm/jibx/JibxUnmarshallerTests.java | 29 ++++++++++++++++--- .../org/springframework/oxm/jibx/binding.xml | 1 + 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java index f4e3336b4a..a988fbd2c1 100644 --- a/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java +++ b/spring-oxm/src/main/java/org/springframework/oxm/jibx/JibxMarshaller.java @@ -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. @@ -349,7 +349,7 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe 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); diff --git a/spring-oxm/src/test/java/org/springframework/oxm/jibx/FlightType.java b/spring-oxm/src/test/java/org/springframework/oxm/jibx/FlightType.java index c34003aa92..c779e9382c 100644 --- a/spring-oxm/src/test/java/org/springframework/oxm/jibx/FlightType.java +++ b/spring-oxm/src/test/java/org/springframework/oxm/jibx/FlightType.java @@ -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. @@ -18,13 +18,23 @@ package org.springframework.oxm.jibx; 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; + } } diff --git a/spring-oxm/src/test/java/org/springframework/oxm/jibx/JibxUnmarshallerTests.java b/spring-oxm/src/test/java/org/springframework/oxm/jibx/JibxUnmarshallerTests.java index 3b19ec620c..871af7388f 100644 --- a/spring-oxm/src/test/java/org/springframework/oxm/jibx/JibxUnmarshallerTests.java +++ b/spring-oxm/src/test/java/org/springframework/oxm/jibx/JibxUnmarshallerTests.java @@ -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. @@ -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 * @@ -31,6 +35,10 @@ import org.springframework.oxm.Unmarshaller; */ public class JibxUnmarshallerTests extends AbstractUnmarshallerTests { + protected static final String INPUT_STRING_WITH_SPECIAL_CHARACTERS = + "" + + "Air Liberté42"; + @Override protected Unmarshaller createUnmarshaller() throws Exception { JibxMarshaller unmarshaller = new JibxMarshaller(); @@ -60,4 +68,17 @@ public class JibxUnmarshallerTests extends AbstractUnmarshallerTests { // 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()); + } + } diff --git a/spring-oxm/src/test/resources/org/springframework/oxm/jibx/binding.xml b/spring-oxm/src/test/resources/org/springframework/oxm/jibx/binding.xml index 415021ce06..2f8cc0ad92 100644 --- a/spring-oxm/src/test/resources/org/springframework/oxm/jibx/binding.xml +++ b/spring-oxm/src/test/resources/org/springframework/oxm/jibx/binding.xml @@ -8,6 +8,7 @@ +