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:net.firejack.platform.core.utils.FileUtils.java

/**
 *
 *
 *
 * @param clazz/*  w w w  . ja va 2 s .  com*/
 * @param is
 *
 * @return
 *
 * @throws javax.xml.bind.JAXBException
 */
public static <T> T readJAXB(Class<T> clazz, InputStream is) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();

    return (T) u.unmarshal(is);
}

From source file:net.firejack.platform.core.utils.FileUtils.java

/**
 * @param clazz//from  w ww .  j ava  2  s.  com
 * @param url
 *
 * @return
 *
 * @throws javax.xml.bind.JAXBException
 */
public static <T> T readJAXB(Class<T> clazz, URL url) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();

    return (T) u.unmarshal(url);
}

From source file:net.firejack.platform.core.utils.FileUtils.java

/**
 * @param clazz//from   w ww  . j av a2  s  . co m
 * @param file
 *
 * @return
 *
 * @throws javax.xml.bind.JAXBException
 */
public static <T> T readJAXB(Class<T> clazz, File file) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();

    return (T) u.unmarshal(file);
}

From source file:br.ufpb.dicomflow.integrationAPI.tools.SendService.java

private static void configureService(ServiceIF service, CommandLine cl)
        throws ParseException, JAXBException, ClassNotFoundException {

    Logger.v(rb.getString("start-service-config"));
    if (!cl.hasOption(SERVICE_OPTION))
        throw new MissingArgumentException(rb.getString("missing-content-opt"));

    JAXBContext jaxbContext;//from w w w . j  a va 2s. co  m
    if (cl.hasOption(SERVICE_CLASS_OPTION)) {

        String serviceClass = cl.getOptionValue(SERVICE_CLASS_OPTION);
        jaxbContext = JAXBContext.newInstance(Class.forName(serviceClass));
        Logger.v(rb.getString("jaxb-context") + Class.forName(serviceClass));

    } else {

        jaxbContext = JAXBContext.newInstance(CONTEXT_PATH);
        Logger.v(rb.getString("jaxb-context") + CONTEXT_PATH);

    }

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    service = (ServiceIF) jaxbUnmarshaller.unmarshal(new File(cl.getOptionValue(SERVICE_OPTION)));

    Logger.v(rb.getString("loaded-service") + service.getName() + " - " + service.getAction() + " - "
            + service.getMessageID());

    Logger.v(rb.getString("finish-service-config"));

}

From source file:org.apache.vxquery.app.util.RestUtils.java

/**
 * Maps the object in the string representation to a java object. To map json
 * entities, this method use {@link ObjectMapper}. For XML this method use
 * {@link Unmarshaller}.//w ww .  ja va 2s  . co m
 * 
 * @param entity
 *            string representation of the object
 * @param type
 *            the class to which the string needs to be mapped to
 * @param contentType
 *            json or XML
 * @param <T>
 *            content's class type
 * @return mapped object
 * @throws IOException
 * @throws JAXBException
 */
public static <T> T mapEntity(String entity, Class<T> type, String contentType)
        throws IOException, JAXBException {
    if (contentType == null) {
        contentType = CONTENT_TYPE_JSON;
    }

    switch (contentType) {
    case CONTENT_TYPE_XML:
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return type.cast(unmarshaller.unmarshal(new StringReader(entity)));
    case CONTENT_TYPE_JSON:
    default:
        ObjectMapper jsonMapper = new ObjectMapper();
        return jsonMapper.readValue(entity, type);
    }
}

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 read(String input, Class<T> typeParameterClass) {
    T content = null;//from   w w  w.j  ava2s  .co m
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        InputStream is = new ByteArrayInputStream(input.getBytes());
        content = (T) jaxbUnmarshaller.unmarshal(is);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return content;
}

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

private static ExportDataConfig getConfig(String configXmlFile) throws Exception {
    InputStream is = ExportData2CsvUtils.class.getClassLoader().getResource(META_CONF_DIR + "/" + configXmlFile)
            .openStream();/*from   w  w w.j a va2  s .  co m*/
    byte[] xmlContent = IOUtils.toString(is, Constants.BASE_ENCODING).getBytes();
    JAXBContext jaxbContext = JAXBContext.newInstance(ExportDataConfig.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ByteArrayInputStream bais = new ByteArrayInputStream(xmlContent);
    ExportDataConfig config = (ExportDataConfig) jaxbUnmarshaller.unmarshal(bais);
    return config;
}

From source file:io.apiman.test.common.util.TestUtil.java

/**
 * Loads a test plan from a file resource.
 * @param planFile/*  www  .  ja  v a 2s .  co  m*/
 */
public static final TestPlan loadTestPlan(File planFile) {
    try {
        if (!planFile.isFile())
            throw new RuntimeException("Test Plan not found: " + planFile.getCanonicalPath()); //$NON-NLS-1$
        JAXBContext jaxbContext = JAXBContext.newInstance(TestPlan.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TestPlan plan = (TestPlan) jaxbUnmarshaller.unmarshal(planFile);
        return plan;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

From source file:io.apiman.test.common.util.TestUtil.java

/**
 * Loads a test plan from a classpath resource.
 * @param resourcePath/*  www.j  a va 2  s . c o  m*/
 * @param cl
 */
public static final TestPlan loadTestPlan(String resourcePath, ClassLoader cl) {
    try {
        URL url = cl.getResource(resourcePath);
        if (url == null)
            throw new RuntimeException("Test Plan not found: " + resourcePath); //$NON-NLS-1$
        JAXBContext jaxbContext = JAXBContext.newInstance(TestPlan.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        TestPlan plan = (TestPlan) jaxbUnmarshaller.unmarshal(url.openStream());
        return plan;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}