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

/**
 *
 *
 *
 * @param clazz/*from   w  w  w  .  ja  va2s  . c om*/
 * @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/* w ww. j av a2 s .c o  m*/
 * @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  w w . j  a  v a 2  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: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}.//from   ww w  . j  av  a 2  s.c  o  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.testdroid.api.APIEntity.java

@JsonIgnore
public static <T extends APIEntity> T fromXML(String xml, Class<T> type) {
    try {//  w  w  w .  j  a  v a  2s .  c  o  m
        JAXBContext context = getJAXBContext(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException ex) {
        Logger.getLogger(APIEntity.class.getName()).log(Level.SEVERE, null, ex);
    }
    return null;
}

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

/**
 * Loads a test plan from a file resource.
 * @param planFile/*w  w w . jav  a2 s  . c  om*/
 */
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//from  ww  w. j  av a 2  s  .com
 * @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);
    }
}

From source file:com.hp.octane.integrations.uft.items.UftTestDiscoveryResult.java

public static UftTestDiscoveryResult readFromFile(File file) throws JAXBException, FileNotFoundException {
    JAXBContext context = JAXBContext.newInstance(UftTestDiscoveryResult.class);
    Unmarshaller m = context.createUnmarshaller();
    Reader reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8);
    return (UftTestDiscoveryResult) m.unmarshal(reader);
}

From source file:cz.lbenda.dataman.rc.DbConfigFactory.java

public static void load(final String document) {
    if (StringUtils.isBlank(document)) {
        return;/*from   ww  w  . j  av  a  2  s. c  o  m*/
    }
    try {
        JAXBContext jc = JAXBContext.newInstance(cz.lbenda.dataman.schema.dataman.ObjectFactory.class);
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement o = (JAXBElement) u.unmarshal(new StringReader(document));
        if (o.getValue() instanceof DatamanType) {
            DatamanType dc = (DatamanType) o.getValue();
            if (dc.getSessions() == null || dc.getSessions().getSession().isEmpty()) {
                LOG.info("No configuration for loading");
            } else {
                configurations.clear();
                for (SessionType session : dc.getSessions().getSession()) {
                    DbConfig sc = new DbConfig();
                    configurations.add(sc);
                    sc.fromSessionType(session, true);
                }
            }
        } else {
            LOG.error("The string didn't contains dataman configuration: " + o.getClass().getName());
        }
    } catch (JAXBException e) {
        LOG.error("Problem with reading dataman configuration\nXML:" + document + "\n" + e.toString(), e);
    }
}

From source file:eu.modaclouds.sla.mediator.Utils.java

public static <E> E load(Class<E> clazz, InputStream is) throws JAXBException {

    JAXBContext jaxbContext;
    Unmarshaller jaxbUnmarshaller;

    jaxbContext = JAXBContext.newInstance(clazz);

    jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    @SuppressWarnings("unchecked")
    E result = (E) jaxbUnmarshaller.unmarshal(is);
    return result;
}