Example usage for javax.xml.bind JAXBContext createUnmarshaller

List of usage examples for javax.xml.bind JAXBContext createUnmarshaller

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext createUnmarshaller.

Prototype

public abstract Unmarshaller createUnmarshaller() throws JAXBException;

Source Link

Document

Create an Unmarshaller object that can be used to convert XML data into a java content tree.

Usage

From source file:com.netsteadfast.greenstep.util.DynamicHqlUtils.java

public static DynamicHql loadResource(String resource) throws Exception {
    DynamicHql dynamicHql = resourceDataMap.get(resource);
    if (dynamicHql == null) {
        InputStream in = null;// ww w.j  ava 2s.c om
        try {
            in = DynamicHqlUtils.class.getResourceAsStream("/dynamichql/" + resource);
            byte[] xmlBytes = IOUtils.toByteArray(in);
            JAXBContext jaxbContext = JAXBContext.newInstance(DynamicHql.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            dynamicHql = (DynamicHql) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlBytes));
            resourceDataMap.put(resource, dynamicHql);
        } catch (IOException e) {
            logger.error(e.getMessage().toString());
            throw e;
        } finally {
            if (in != null) {
                IOUtils.closeQuietly(in);
            }
            in = null;
        }
    }
    return dynamicHql;
}

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

/**
 * Method that creates a {@link Components} instance from the provided
 * component spec file, validating it against component spec XML Schema
 * /* w  w  w .jav  a  2s. c  o  m*/
 * @param componentSpecFilePath
 *            path to the component spec file
 */
public static Components createComponents(String componentSpecFilePath) {
    Components component = null;
    File schemaFile = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Components.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // copy XML Schema from resources to a temporary location
        schemaFile = File.createTempFile("schema", null);
        FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES),
                schemaFile);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

        // validate provided toolspec against XML Schema
        unmarshaller.setSchema(schema);

        // unmarshal it
        final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath));
        try {
            component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue();
        } finally {
            stream.close();
        }
    } catch (JAXBException e) {
        log.error("The component spec provided doesn't validate against its schema!", e);
    } catch (SAXException e) {
        log.error("The XML Schema is not valid!", e);
    } catch (IOException e) {
        log.error("An error occured while copying the XML Schema from the resources to a temporary location!",
                e);
    } catch (Exception e) {
        log.error("An error occured!", e);
    } finally {
        if (schemaFile != null) {
            schemaFile.deleteOnExit();
        }
    }
    return component;
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.JaxbMapperHelper.java

/**
 * UnMarshaller. ???pooling//from  w  ww  .  ja v  a 2 s. co m
 */
public static Unmarshaller createUnmarshaller(Class<?> clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw ExceptionHelper.unchecked(e);
    }
}

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);//  w  w w. j  a v  a  2  s  . c  o m
        }
    }
    return definitions;
}

From source file:com.technofovea.hl2parse.vdf.MaterialReader.java

public static final MaterialRefList loadFromXml(InputStream stream) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(MaterialReference.class, MaterialRefList.class);
    Unmarshaller um = jc.createUnmarshaller();
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Object o = um.unmarshal(stream);
    return (MaterialRefList) o;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(String str, Class<T> clszz) {
    JAXBContext context = null;
    T obj = null;/* ww  w . j av a  2  s  .  c o m*/

    try {
        context = JAXBContext.newInstance(clszz);
        StringReader reader = new StringReader(str);
        Unmarshaller unmar = context.createUnmarshaller();
        obj = (T) unmar.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:com.sportpm.mapper.JaxbMapper.java

/**
 * UnMarshaller.//from www.j  ava2  s .  c  o  m
 * ???pooling
 */
@SuppressWarnings("rawtypes")
public static Unmarshaller createUnmarshaller(Class clazz) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);
        return jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:$.JaxbMapper.java

/**
     * UnMarshaller./*from  ww  w. j a v  a  2  s .  c om*/
     * ???pooling
     */
    public static Unmarshaller createUnmarshaller(Class clazz) {
        try {
            JAXBContext jaxbContext = getJaxbContext(clazz);
            return jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {
            throw Exceptions.unchecked(e);
        }
    }

From source file:Main.java

/**
 * Unmarshal XML data from XML string using XSD string and return the resulting JAXB content tree
 *
 * @param dummyCtxObject/* ww  w  . j  a  va 2 s  .c o m*/
 *            Dummy contect object for creating related JAXB context
 * @param strXML
 *            XML
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshalling(Object dummyCtxObject, String strXML, String strXSD) throws Exception {
    if (dummyCtxObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (strXML == null) {
        throw new RuntimeException("No XML document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader readerXSD = null;
    StringReader readerXML = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyCtxObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        readerXSD = new StringReader(strXSD);
        readerXML = new StringReader(strXML);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new StreamSource(readerXSD));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(new StreamSource(readerXML));
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (readerXSD != null) {
            readerXSD.close();
            readerXSD = null;
        }
        if (readerXML != null) {
            readerXML.close();
            readerXML = null;
        }
    }

    return unmarshalledObject;
}

From source file:Main.java

/**
 * Parse XML using JAXB and a model class.
 * //from   w w  w. jav  a2  s .  co  m
 * @param in an input stream
 * @return the requested object
 * @param cls the root class
 * @throws JAXBException thrown on an error
 */
@SuppressWarnings("unchecked")
public static <T> T parseJaxb(Class<T> cls, InputStream in) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(cls);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Unmarshaller um = context.createUnmarshaller();
    return (T) um.unmarshal(in);
}