Example usage for javax.xml.bind Unmarshaller unmarshal

List of usage examples for javax.xml.bind Unmarshaller unmarshal

Introduction

In this page you can find the example usage for javax.xml.bind Unmarshaller unmarshal.

Prototype

public <T> JAXBElement<T> unmarshal(javax.xml.stream.XMLEventReader reader, Class<T> declaredType)
        throws JAXBException;

Source Link

Document

Unmarshal root element to JAXB mapped declaredType and return the resulting content tree.

Usage

From source file:Main.java

/**
 * Converting DOM Element object into JAXB OASIS XML Object
 * @param <T>/*from  ww w. j a v  a  2s  .  c  om*/
 * @param cls
 * @param domElement
 * @return
 */
public static <T> T marshal(Class<T> cls, Element domElement) {
    try {
        JAXBContext jc = JAXBContext.newInstance(cls);
        javax.xml.bind.Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<T> jaxbObject = unmarshaller.unmarshal(domElement, cls);

        T object = jaxbObject.getValue();

        return object;
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

From source file:eu.scape_project.tool.toolwrapper.data.tool_spec.utils.Utils.java

/**
 * Unmarshals an input stream of xml data to a Tool.
 *//* w  w  w.j av  a 2  s .com*/
public static Tool fromInputStream(InputStream input) throws JAXBException, IOException, SAXException {
    Unmarshaller unmarshaller = Utils.createUnmarshaller();
    JAXBElement<Tool> unmarshalled = unmarshaller.unmarshal(new StreamSource(input), Tool.class);
    return unmarshalled.getValue();
}

From source file:ch.entwine.weblounge.common.impl.security.AccessControlParser.java

/**
 * Reads an ACL from an xml input stream.
 * /*from  w w  w  .j  av a2 s.  c om*/
 * @param in
 *          the xml input stream
 * @return the access control list
 * @throws IOException
 *           if there is a problem reading the stream
 */
public static AccessControlList parseAcl(InputStream in) throws IOException {
    Unmarshaller unmarshaller;
    try {
        unmarshaller = jaxbContext.createUnmarshaller();
        return unmarshaller.unmarshal(DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(in),
                AccessControlListImpl.class).getValue();
    } catch (Throwable t) {
        throw new IOException(t);
    } finally {
        IOUtils.closeQuietly(in);
    }
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

public static <T> T readObjectFromInputStream(final InputStream inputStream, final Class<T> expectedType) {
    try {//from ww w.  ja  v a2  s. c o m
        JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
        return element.getValue();
    } catch (final JAXBException e) {
        throw new SwidException("Cannot process resource.", e);
    }
}

From source file:net.sf.dynamicreports.report.defaults.Defaults.java

private static XmlDynamicReports load() {
    String resource = "dynamicreports-defaults.xml";
    InputStream is = null;/*from   ww  w. j  av  a2  s  .  com*/

    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    if (classLoader != null) {
        is = classLoader.getResourceAsStream(resource);
    }
    if (is == null) {
        classLoader = Defaults.class.getClassLoader();
        if (classLoader != null) {
            is = classLoader.getResourceAsStream(resource);
        }
        if (is == null) {
            is = Defaults.class.getResourceAsStream("/" + resource);
        }
    }
    if (is == null) {
        return null;
    }

    try {
        Unmarshaller unmarshaller = JAXBContext.newInstance(XmlDynamicReports.class).createUnmarshaller();
        JAXBElement<XmlDynamicReports> root = unmarshaller.unmarshal(new StreamSource(is),
                XmlDynamicReports.class);
        return root.getValue();
    } catch (JAXBException e) {
        log.error("Could not load dynamic reports defaults", e);
        return null;
    }
}

From source file:Main.java

public static <T> T unmarshal(Reader r, Class<T> clazz)
        throws JAXBException, XMLStreamException, FactoryConfigurationError {
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*from   w w w  .  j  a va2  s .c o  m*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    XMLStreamReader xmlsr = XMLInputFactory.newFactory().createXMLStreamReader(r);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null);
    unmarshaller.setEventHandler(valEventHndlr);

    JAXBElement<T> elem = null;
    try {
        elem = unmarshaller.unmarshal(xmlsr, clazz);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new UnmarshalException(e.getMessage(), e);
        }
    }

    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Unmarshall Exception if there is a parsing error
                String msg = MessageFormat.format("Line {0}, Col: {1}: {2}",
                        valEvent.getLocator().getLineNumber(), valEvent.getLocator().getColumnNumber(),
                        valEvent.getLinkedException().getMessage());
                throw new UnmarshalException(msg, valEvent.getLinkedException());
            }
        }
    }
    return elem.getValue();
}

From source file:eu.seaclouds.platform.dashboard.util.ObjectMapperHelpers.java

/**
 * Transforms a XML string to an List<Object> using javax.xml.bind.Unmarshaller.
 * If you want to parse a single Object please @see {#link XmlToObject}
 *
 * @param xml  string representing a collection of objects
 * @param type Class of the contained objects within the list
 * @param <T>  target class/*from w w w.  j  a v  a2  s  .  c  o  m*/
 * @return a List of T
 * @throws IOException if is not possible to parse the object
 **/
public static <T> List<T> XmlToObjectCollection(String xml, Class<T> type) throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(ObjectMapperHelpers.JAXBCollection.class, type);
    Unmarshaller u = ctx.createUnmarshaller();

    Source src = new StreamSource(IOUtils.toInputStream(xml));
    ObjectMapperHelpers.JAXBCollection<T> collection = u
            .unmarshal(src, ObjectMapperHelpers.JAXBCollection.class).getValue();
    return collection.getItems();
}

From source file:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

@SuppressWarnings("unchecked")
static <T> T getRequestData(SoapMessageImpl requestSoap, Class<?> clazz) throws Exception {
    Unmarshaller unmarshaller = createUnmarshaller(clazz);

    try {/*from  ww w .  jav a2s.co m*/
        return (T) unmarshaller.unmarshal(SoapUtils.getFirstChild(requestSoap.getSoap().getSOAPBody()), clazz)
                .getValue();
    } catch (UnmarshalException e) {
        throw new CodedException(X_INVALID_REQUEST, e.getLinkedException()).withPrefix(CLIENT_X);
    }
}

From source file:se.inera.intyg.intygstjanst.web.service.impl.CertificateSenderServiceImplTest.java

@BeforeClass
public static void setupSoapMessages() throws Exception {

    Unmarshaller unmarshaller = JAXBContext.newInstance(RegisterMedicalCertificateResponseType.class)
            .createUnmarshaller();/*from  www.j  a v  a 2  s .c om*/
    okWsMessage = unmarshaller
            .unmarshal(
                    new StreamSource(new ClassPathResource(
                            "CertificateSenderServiceImplTest/soap-message-register-ok.xml").getInputStream()),
                    RegisterMedicalCertificateResponseType.class)
            .getValue();
    errorWsMessage = unmarshaller.unmarshal(new StreamSource(
            new ClassPathResource("CertificateSenderServiceImplTest/soap-message-register-error.xml")
                    .getInputStream()),
            RegisterMedicalCertificateResponseType.class).getValue();
}

From source file:org.biopax.paxtools.client.BiopaxValidatorClient.java

/**
 * Converts a biopax-validator XML response to the java object.
 *
 * @param xml input XML data - validation report - to import
 * @return validation report object/*from   ww  w.  java2 s.c om*/
 * @throws JAXBException when there is an JAXB unmarshalling error
 */
public static ValidatorResponse unmarshal(String xml) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance("org.biopax.validator.jaxb");
    Unmarshaller un = jaxbContext.createUnmarshaller();
    Source src = new StreamSource(new StringReader(xml));
    ValidatorResponse resp = un.unmarshal(src, ValidatorResponse.class).getValue();
    return resp;
}