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 Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;

Source Link

Document

Unmarshal XML data from the specified pull parser and return the resulting content tree.

Usage

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static Object unmarshal(InputStream inputStream, Class... docClass) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(docClass);
    Unmarshaller u = jc.createUnmarshaller();
    return u.unmarshal(inputStream);
}

From source file:ee.ria.xroad.common.hashchain.HashChainVerifier.java

@SuppressWarnings("unchecked")
private static HashChainResultType parseHashChainResult(InputStream xml) throws Exception {
    Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
    JAXBElement<HashChainResultType> element = (JAXBElement<HashChainResultType>) unmarshaller.unmarshal(xml);

    HashChainValidator.validate(new JAXBSource(jaxbCtx, element));

    return element.getValue();
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static <T> T unmarshal(Class<T> docClass, InputStream inputStream) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(docClass);
    Unmarshaller u = jc.createUnmarshaller();
    return (T) u.unmarshal(inputStream);
}

From source file:com.ibm.watson.developer_cloud.professor_languo.model.stack_exchange.CorpusBuilder.java

/**
 * Unmarshalls (deserializes) a single XML file using JAXB
 *
 * @param xmlFile - The XML file to be unmarshalled
 * @param clazz - The class of object that <code>xmlFile</code> should be unmarshalled to
 * @return An object of class <code>clazz</code>, populated from <code>xmlFile</code>
 * @throws JAXBException/*from  w w w. j  av  a  2  s .c om*/
 */
@SuppressWarnings("unchecked")
public static <T> T unmarshallFile(File xmlFile, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    return (T) jaxbUnmarshaller.unmarshal(xmlFile);
}

From source file:com.ponysdk.spring.ProxyBuilderGenerator.java

public static List<Root> getDefinitions(final String directories) throws JAXBException {
    final JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
    final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    final List<Root> definitions = new ArrayList<Root>();

    final String[] split = directories.split(",");
    for (final String d : split) {

        final List<File> files = read(new File(d));
        for (final File file : files) {
            System.out.println("file = " + file);
            final Root root = (Root) unmarshaller.unmarshal(file);
            definitions.add(root);//from ww  w .ja  va2s  . c o  m
        }
    }
    return definitions;
}

From source file:com.evolveum.midpoint.testing.model.client.sample.RunScript.java

private static <T> T unmarshalFile(File file) throws JAXBException, FileNotFoundException {
    JAXBContext jc = getJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    InputStream is = null;//ww  w  . j  a v a  2  s. c  om
    JAXBElement<T> element = null;
    try {
        is = new FileInputStream(file);
        element = (JAXBElement<T>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}

From source file:com.manydesigns.portofino.dispatcher.DispatcherLogic.java

public static Page loadPage(InputStream inputStream) throws JAXBException {
    Unmarshaller unmarshaller = pagesJaxbContext.createUnmarshaller();
    return (Page) unmarshaller.unmarshal(inputStream);
}

From source file:com.manydesigns.portofino.dispatcher.DispatcherLogic.java

public static <T> T loadConfiguration(InputStream inputStream, Class<? extends T> configurationClass)
        throws Exception {
    if (configurationClass == null) {
        return null;
    }/*from ww w  .j  ava  2 s .  c o m*/
    Object configuration;
    String configurationPackage = configurationClass.getPackage().getName();
    JAXBContext jaxbContext = JAXBContext.newInstance(configurationPackage);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    configuration = unmarshaller.unmarshal(inputStream);
    if (!configurationClass.isInstance(configuration)) {
        logger.error("Invalid configuration: expected " + configurationClass + ", got " + configuration);
        return null;
    }
    Injections.inject(configuration, ElementsThreadLocals.getServletContext(),
            ElementsThreadLocals.getHttpServletRequest());
    if (configuration instanceof PageActionConfiguration) {
        ((PageActionConfiguration) configuration).init();
    }
    return (T) configuration;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readXML(Class<?> class1, File file)
        throws JAXBException, IOException, SAXException, ParserConfigurationException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Unmarshaller um = context.createUnmarshaller();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setFeature("http://xml.org/sax/features/validation", false);

    XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
    try (FileReader reader = new FileReader(file)) {
        SAXSource source = new SAXSource(xr, new InputSource(reader));

        T obj = (T) um.unmarshal(source);
        return obj;
    }//  ww  w .  ja v a  2s . c  o m
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T readXMLFromString(Class<?> class1, String content)
        throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Unmarshaller um = context.createUnmarshaller();

    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    spf.setFeature("http://xml.org/sax/features/validation", false);

    XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader();
    try (StringReader reader = new StringReader(content)) {
        SAXSource source = new SAXSource(xr, new InputSource(reader));

        T obj = (T) um.unmarshal(source);
        return obj;
    }// www  . j  ava2 s  .c o  m
}