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.wandrell.example.swss.test.util.SoapMessageUtils.java

/**
 * Creates an {@code Entity} from the received {@code SOAPMessage}.
 * <p>//from  ww  w .j av a  2  s  .c om
 * The message should be valid and contain a {@code GetEntityResponse}.
 *
 * @param message
 *            the SOAP message
 * @return an {@code Entity} parsed from the {@code SOAPMessage}
 * @throws JAXBException
 *             if there is any problem when unmarshalling the data
 * @throws SOAPException
 *             if there is any problem when reading the SOAP message
 */
public static final Entity getEntity(final SOAPMessage message) throws JAXBException, SOAPException {
    final JAXBContext jc; // Context for unmarshalling
    final Unmarshaller um; // Unmarshaller for the SOAP message
    final GetEntityResponse response; // Unmarshalled response

    jc = JAXBContext.newInstance(GetEntityResponse.class);
    um = jc.createUnmarshaller();
    response = (GetEntityResponse) um.unmarshal(message.getSOAPBody().extractContentAsDocument());

    return response.getEntity();
}

From source file:mx.bigdata.sat.cfdi.CFDv3.java

private static Comprobante load(InputStream source, String... contexts) throws Exception {
    JAXBContext context = getContext(contexts);
    try {/*www.j a v a 2 s  . c o  m*/
        Unmarshaller u = context.createUnmarshaller();
        return (Comprobante) u.unmarshal(source);
    } finally {
        source.close();
    }
}

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

private static Object unmarshalFile(File file) throws JAXBException, FileNotFoundException {
    JAXBContext jc = ModelClientUtil.instantiateJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    Object o = unmarshaller.unmarshal(new FileInputStream(file));
    if (o instanceof JAXBElement) {
        return ((JAXBElement) o).getValue();
    } else {//from ww w. java 2  s .co m
        return o;
    }
}

From source file:com.intuit.tank.tools.debugger.PanelBuilder.java

private static Headers getHeaders(String serviceUrl) {
    Headers ret = null;/*from  ww  w.j  a v a 2s.c  o m*/
    if (StringUtils.isNotBlank(serviceUrl)) {
        InputStream settingsStream = null;
        try {
            URL url = new URL(serviceUrl + HEADERS_PATH);
            LOG.info(
                    "Starting up: making call to tank service url to get settings.xml " + url.toExternalForm());
            settingsStream = url.openStream();
            JAXBContext ctx = JAXBContext.newInstance(Headers.class.getPackage().getName());
            ret = (Headers) ctx.createUnmarshaller().unmarshal(settingsStream);
        } catch (Exception e) {
            LOG.error("Error gettting headers: " + e, e);
        } finally {
            IOUtils.closeQuietly(settingsStream);
        }
    }
    return ret;
}

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.evolveum.midpoint.testing.model.client.sample.RunScript.java

private static <T> T unmarshallResouce(String path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = getJaxbContext();
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    InputStream is = null;//  www. j  a va2 s .  c  o m
    JAXBElement<T> element = null;
    try {
        is = RunScript.class.getClassLoader().getResourceAsStream(path);
        if (is == null) {
            throw new FileNotFoundException("System resource " + path + " was not found");
        }
        element = (JAXBElement<T>) unmarshaller.unmarshal(is);
    } finally {
        if (is != null) {
            IOUtils.closeQuietly(is);
        }
    }
    if (element == null) {
        return null;
    }
    return element.getValue();
}

From source file:edu.lternet.pasta.common.EmlUtility.java

/**
 * Parses the provided EML 2.1.0 {@code <access>} element string and
 * returns a corresponding JAXB object./*from w w w. ja v a 2 s  . c om*/
 *
 * @param accessString
 *            the {@code <access>} element string.
 * @return a JAXB object corresponding to the provided string.
 *
 * @throws IllegalArgumentException
 *             with a {@linkplain JAXBException} as the cause.
 */
@SuppressWarnings("unchecked")
public static AccessType getAccessType2_1_0(String accessString) {

    try {
        String packageName = AccessType.class.getPackage().getName();
        JAXBContext jc = JAXBContext.newInstance(packageName);
        Unmarshaller u = jc.createUnmarshaller();
        StringReader reader = new StringReader(accessString);

        JAXBElement<AccessType> jaxb = (JAXBElement<AccessType>) u.unmarshal(reader);

        return jaxb.getValue();
    } catch (JAXBException e) {
        throw new IllegalArgumentException(e);
    }

}

From source file:JALPTest.java

/**
 * Unmarshals an xml file into an ApplicationMetadataType.
 *
 * @param path   the path to the xml file to be unmarshalled
 * @return   the created ApplicationMetadataType
 * @throws Exception//  w  w w. j a va 2s.  c  o m
 */
private static ApplicationMetadataType readXML(String path) throws Exception {
    JAXBContext jc = JAXBContext
            .newInstance("com.tresys.jalop.schemas.mil.dod.jalop_1_0.applicationmetadatatypes");
    Unmarshaller unmarshaller = jc.createUnmarshaller();
    JAXBElement<ApplicationMetadataType> jaxAmt = (JAXBElement<ApplicationMetadataType>) unmarshaller
            .unmarshal(new File(path));
    return jaxAmt.getValue();
}

From source file:com.threeglav.sh.bauk.main.StreamHorizonEngine.java

private static final BaukConfiguration findConfiguration() {
    LOG.info(/*ww w  .j a  v a2s .  co  m*/
            "Trying to find configuration file. First if specified as {} system property and then as {} in classpath",
            CONFIG_FILE_PROP_NAME, DEFAULT_CONFIG_FILE_NAME);
    final String configFile = System.getProperty(CONFIG_FILE_PROP_NAME);
    InputStream is = null;
    if (StringUtil.isEmpty(configFile)) {
        LOG.info(
                "Was not able to find system property {}. Trying to find default configuration {} in classpath",
                CONFIG_FILE_PROP_NAME, DEFAULT_CONFIG_FILE_NAME);
        is = StreamHorizonEngine.class.getClassLoader().getResourceAsStream(DEFAULT_CONFIG_FILE_NAME);
        if (is == null) {
            LOG.error("Was not able to find file {} in the classpath. Unable to start application",
                    DEFAULT_CONFIG_FILE_NAME);
            return null;
        }
        LOG.info("Found configuration file {} in classpath", DEFAULT_CONFIG_FILE_NAME);
    } else {
        LOG.info("Found system property {}={}. Will try to load it as configuration...", CONFIG_FILE_PROP_NAME,
                configFile);
        final File cFile = new File(configFile);
        try {
            if (cFile.exists() && cFile.isFile()) {
                LOG.debug("Loading config file [{}] from file system", configFile);
                is = new FileInputStream(configFile);
            } else {
                LOG.debug("Loading config file [{}] from classpath", configFile);
                is = StreamHorizonEngine.class.getClass().getResourceAsStream(configFile);
            }
            LOG.info("Successfully found configuration [{}] on file system", configFile);
        } catch (final FileNotFoundException fnfe) {
            LOG.error("Was not able to find file {}. Use full, absolute path.", configFile);
            return null;
        }
    }
    if (is == null) {
        return null;
    }
    try {
        LOG.debug("Trying to load configuration from xml file");
        final JAXBContext jaxbContext = JAXBContext.newInstance(BaukConfiguration.class);
        final Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        final Schema schema = schemaFactory
                .newSchema(StreamHorizonEngine.class.getResource("/bauk_config.xsd"));
        jaxbUnmarshaller.setSchema(schema);
        final BaukConfiguration config = (BaukConfiguration) jaxbUnmarshaller.unmarshal(is);
        LOG.info("Successfully loaded configuration");
        return config;
    } catch (final Exception exc) {
        LOG.error("Exception while loading configuration", exc);
        exc.printStackTrace();
        return null;
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:br.gov.frameworkdemoiselle.behave.integration.alm.objects.util.GenerateXMLString.java

public static Testcase getTestcaseObject(HttpResponse response) throws IOException, JAXBException {

    Testcase testcase = null;//  w  w  w  .  j  av a2 s .co m
    StringBuffer xmlString = new StringBuffer();
    HttpEntity entity = response.getEntity();
    if (entity != null) {
        InputStream instream = entity.getContent();
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
            String line = "";
            while ((line = reader.readLine()) != null) {
                xmlString.append(line);
            }
        } finally {
            instream.close();
        }
    }

    if (!xmlString.equals("")) {
        JAXBContext jaxbContext = JAXBContext.newInstance(Testcase.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(xmlString.toString());
        testcase = (Testcase) unmarshaller.unmarshal(reader);
    }

    return testcase;

}