com.github.rmannibucau.featuredmock.mock.unmarshaller.jaxb.JaxbUnmarshaller Maven / Gradle / Ivy
package com.github.rmannibucau.featuredmock.mock.unmarshaller.jaxb;
import com.github.rmannibucau.featuredmock.mock.unmarshaller.Unmarshaller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
public class JaxbUnmarshaller implements Unmarshaller {
private static final ConcurrentMap, JAXBContext> CACHE = new ConcurrentHashMap, JAXBContext>();
@Override
public T unmarshall(final Class clazz, final InputStream is) throws IOException {
try {
JAXBContext ctx = CACHE.get(clazz);
if (ctx == null) {
ctx = JAXBContext.newInstance(clazz);
CACHE.putIfAbsent(clazz, ctx);
}
final Object obj = ctx.createUnmarshaller().unmarshal(is);
if (JAXBElement.class.isInstance(obj)) {
return ((JAXBElement) obj).getValue();
}
return clazz.cast(obj);
} catch (final JAXBException e) {
e.printStackTrace();
return null;
}
}
@Override
public boolean accept(final String extension) {
return ".xml".equals(extension);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy