com.itelg.spring.xom.unmarshaller.utils.ChainedUnmarshaller Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of spring-xom-unmarshaller Show documentation
Show all versions of spring-xom-unmarshaller Show documentation
Spring XML Unmarshalling with XOM
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 unmarshallers;
public ChainedUnmarshaller(List 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 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();
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy