-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dependency updates & ChainedMarshaller added (#8)
* Dependency updates & ChainedUnmarshaller added
- Loading branch information
1 parent
b38e7e3
commit 9814826
Showing
7 changed files
with
261 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,4 +52,4 @@ pipeline | |
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
src/main/java/com/itelg/spring/xom/unmarshaller/utils/ChainedUnmarshaller.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.itelg.spring.xom.unmarshaller.utils; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.oxm.Unmarshaller; | ||
import org.springframework.oxm.UnmarshallingFailureException; | ||
import org.springframework.oxm.XmlMappingException; | ||
import org.springframework.util.Assert; | ||
|
||
public class ChainedUnmarshaller implements Unmarshaller | ||
{ | ||
private List<Unmarshaller> unmarshallers; | ||
|
||
public ChainedUnmarshaller(List<Unmarshaller> unmarshallers) | ||
{ | ||
Assert.notEmpty(unmarshallers, "'unmarshallers' must not be empty"); | ||
this.unmarshallers = unmarshallers; | ||
} | ||
|
||
@Override | ||
public boolean supports(Class<?> clazz) | ||
{ | ||
for (Unmarshaller unmarshaller : unmarshallers) | ||
{ | ||
if (unmarshaller.supports(clazz)) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
@Override | ||
public Object unmarshal(Source source) throws IOException, XmlMappingException | ||
{ | ||
List<String> errors = new ArrayList<>(); | ||
byte[] byteSource = toByteArray(source); | ||
|
||
for (Unmarshaller unmarshaller : unmarshallers) | ||
{ | ||
try (InputStream inputStream = new ByteArrayInputStream(byteSource)) | ||
{ | ||
return unmarshaller.unmarshal(new StreamSource(inputStream)); | ||
} | ||
catch (RuntimeException e) | ||
{ | ||
errors.add(e.getMessage()); | ||
} | ||
} | ||
|
||
throw new UnmarshallingFailureException("Could not unmarshal (Errors: " + StringUtils.join(errors, ",") + ")"); | ||
} | ||
|
||
private static byte[] toByteArray(Source source) throws IOException | ||
{ | ||
StreamSource streamSource = (StreamSource) source; | ||
|
||
try (InputStream inputStream = streamSource.getInputStream(); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) | ||
{ | ||
IOUtils.copy(inputStream, outputStream); | ||
return outputStream.toByteArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/test/java/com/itelg/spring/xom/unmarshaller/utils/ChainedUnmarshallerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
package com.itelg.spring.xom.unmarshaller.utils; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.junit.Assert.fail; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.stream.StreamSource; | ||
|
||
import org.junit.Test; | ||
import org.springframework.oxm.Unmarshaller; | ||
import org.springframework.oxm.UnmarshallingFailureException; | ||
import org.springframework.oxm.XmlMappingException; | ||
|
||
import com.itelg.spring.xom.unmarshaller.XomUnmarshaller; | ||
import com.itelg.spring.xom.unmarshaller.parser.Parser; | ||
import com.itelg.xpath.helper.XPathHelper; | ||
|
||
import nu.xom.Element; | ||
|
||
public class ChainedUnmarshallerTest | ||
{ | ||
private Unmarshaller stringIntegerUnmarshaller = new ChainedUnmarshaller(Arrays.asList(new StringUnmarshaller(), new IntegerUnmarshaller())); | ||
|
||
private Unmarshaller integerStringUnmarshaller = new ChainedUnmarshaller(Arrays.asList(new IntegerUnmarshaller(), new StringUnmarshaller())); | ||
|
||
private String stringXml = "<String><value>abc</value></String>"; | ||
|
||
private String integerXml = "<Integer><value>123</value></Integer>"; | ||
|
||
@Test | ||
public void testConstructorWithNullList() | ||
{ | ||
try | ||
{ | ||
new ChainedUnmarshaller(null); | ||
fail("exception expected"); | ||
} | ||
catch (IllegalArgumentException e) | ||
{ | ||
assertEquals("'unmarshallers' must not be empty", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testConstructorWithEmptyList() | ||
{ | ||
try | ||
{ | ||
new ChainedUnmarshaller(Collections.emptyList()); | ||
fail("exception expected"); | ||
} | ||
catch (IllegalArgumentException e) | ||
{ | ||
assertEquals("'unmarshallers' must not be empty", e.getMessage()); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSupports() | ||
{ | ||
assertFalse(stringIntegerUnmarshaller.supports(Double.class)); | ||
assertTrue(stringIntegerUnmarshaller.supports(String.class)); | ||
assertTrue(stringIntegerUnmarshaller.supports(Integer.class)); | ||
|
||
assertFalse(integerStringUnmarshaller.supports(Double.class)); | ||
assertTrue(integerStringUnmarshaller.supports(String.class)); | ||
assertTrue(integerStringUnmarshaller.supports(Integer.class)); | ||
} | ||
|
||
@Test | ||
public void testUnmarshal() throws XmlMappingException, IOException | ||
{ | ||
assertEquals("abc", stringIntegerUnmarshaller.unmarshal(createSource(stringXml))); | ||
assertEquals("abc", integerStringUnmarshaller.unmarshal(createSource(stringXml))); | ||
assertEquals(Integer.valueOf(123), stringIntegerUnmarshaller.unmarshal(createSource(integerXml))); | ||
assertEquals(Integer.valueOf(123), integerStringUnmarshaller.unmarshal(createSource(integerXml))); | ||
} | ||
|
||
@Test | ||
public void testUnmarshalWithException() throws XmlMappingException, IOException | ||
{ | ||
try | ||
{ | ||
stringIntegerUnmarshaller.unmarshal(createSource("123")); | ||
fail("exception expected"); | ||
} | ||
catch (UnmarshallingFailureException e) | ||
{ | ||
assertEquals("Could not unmarshal (Errors: Could not unmarshal; nested exception is nu.xom.ParsingException: Content is not allowed in prolog. at line 1, column 1,Could not unmarshal; nested exception is nu.xom.ParsingException: Content is not allowed in prolog. at line 1, column 1)", e.getMessage()); | ||
} | ||
} | ||
|
||
private static Source createSource(String xml) | ||
{ | ||
return new StreamSource(new ByteArrayInputStream(xml.getBytes())); | ||
} | ||
|
||
private static class StringUnmarshaller extends XomUnmarshaller | ||
{ | ||
public StringUnmarshaller() | ||
{ | ||
super(Collections.singletonList(new StringParser())); | ||
} | ||
} | ||
|
||
private static class StringParser implements Parser<String> | ||
{ | ||
@Override | ||
public String parse(Element rootElement) | ||
{ | ||
return XPathHelper.getString("value", rootElement); | ||
} | ||
} | ||
|
||
private static class IntegerUnmarshaller extends XomUnmarshaller | ||
{ | ||
public IntegerUnmarshaller() | ||
{ | ||
super(Collections.singletonList(new IntegerParser())); | ||
} | ||
} | ||
|
||
private static class IntegerParser implements Parser<Integer> | ||
{ | ||
@Override | ||
public Integer parse(Element rootElement) | ||
{ | ||
return XPathHelper.getInteger("value", rootElement); | ||
} | ||
} | ||
} |