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:Main.java

/**
 * Generic method to Validate XML file while unmarshalling against their schema.
 * //from   w  w w  . j a  v  a2s  .  co  m
 * @param context
 * @param schemaFile
 * @param object
 * @return
 * @throws SAXException
 * @throws JAXBException
 */
public static Object validateAndUnmarshallXML(JAXBContext context, String schemaFile, InputStream fio)
        throws SAXException, JAXBException {

    if (context != null && (schemaFile != null && schemaFile.trim().length() > 0)) {
        Unmarshaller unMarshaller = context.createUnmarshaller();

        SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); // thread- safe 
        unMarshaller.setSchema(sf.newSchema(new File(schemaFile))); // validate jaxb context against schema 

        return unMarshaller.unmarshal(fio);

    }
    return null;
}

From source file:com.tomtom.speedtools.services.lbs.geocode.implementation.TomTomLbsGeoCodeEngine.java

/**
 * Do the heavy lifting of binding the input stream to the JAX-B annotated objects.
 *
 * @param stream Stream to read from.//from  www  .  j  a v a2s .co  m
 * @return Valid GeoCoderResponse object or null on error.
 * @throws JAXBException If an error occurs unmarshalling.
 */
@Nullable
private static GeoCodeEngineResponse unmarshalGeoCoderResponseBody(@Nonnull final InputStream stream)
        throws JAXBException {
    assert stream != null;
    final JAXBContext context = JAXBContext.newInstance(GeoCodeEngineResponse.class);
    final Unmarshaller unmarshaller = context.createUnmarshaller();
    final GeoCodeEngineResponse response = (GeoCodeEngineResponse) unmarshaller.unmarshal(stream);
    return response;
}

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  .ja v a 2  s  .c  o m*/
        return o;
    }
}

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

private static Comprobante load(InputStream source, String... contexts) throws Exception {
    JAXBContext context = getContext(contexts);
    try {/* ww w.jav  a 2s .  co  m*/
        Unmarshaller u = context.createUnmarshaller();
        return (Comprobante) u.unmarshal(source);
    } finally {
        source.close();
    }
}

From source file:edu.duke.cabig.c3pr.rules.common.XMLUtil.java

public static Object unmarshal(String xml) throws RuleException {
    try {/* w  w w . jav  a2s.c o  m*/
        Unmarshaller unmarshaller = JAXBContext.newInstance("edu.duke.cabig.c3pr.rules.brxml")
                .createUnmarshaller();
        log.debug("reading the rule:" + xml);
        return unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        throw new RuleException(e.getMessage(), e);
    }
}

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 a va  2s .c o  m
 * 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:com.tomtom.speedtools.services.lbs.route.implementation.TomTomLbsRouteEngineRouteEngineActorImpl.java

/**
 * Do the heavy lifting of binding the input stream to the JAX-B annotated objects.
 *
 * @param stream Stream to read from.//ww w . j  a v  a2 s .c  o m
 * @return Valid route response object or null on error.
 * @throws JAXBException Thrown if unmarshalling fails.
 */
@Nonnull
private static TomTomLbsRouteEngineResponse unmarshalRouterResponseBody(@Nonnull final InputStream stream)
        throws JAXBException {
    assert stream != null;
    final JAXBContext context = JAXBContext.newInstance(TomTomLbsRouteEngineResponse.class);
    final Unmarshaller unmarshaller = context.createUnmarshaller();
    final TomTomLbsRouteEngineResponse response = (TomTomLbsRouteEngineResponse) unmarshaller.unmarshal(stream);
    if (response == null) {
        throw new JAXBException("Response cannot be unmarshalled");
    }
    return response;
}

From source file:de.iteratec.iteraplan.businesslogic.service.SavedQueryXmlHelper.java

/**
 * Loads a query from database//from   w ww .  j ava2s. co m
 * 
 * @param <T> The XML dto class the XML content reflects
 * @param clazz The XML dto class the XML content reflects
 * @param schemaName The URI of the schema that will be used to validate the XML query
 * @param savedQuery the query object
 * @return The object tree reflecting the XML query. Instance of a JAXB-marshallable class
 */
@SuppressWarnings("unchecked")
public static <T extends SerializedQuery<? extends ReportMemBean>> T loadQuery(Class<T> clazz,
        String schemaName, SavedQuery savedQuery) {
    if (!ReportType.LANDSCAPE.equals(savedQuery.getType())
            && clazz.isAssignableFrom(LandscapeDiagramXML.class)) {
        LOGGER.error("requested QueryType ('{0}') does not fit the required QueryType ('{1}')",
                ReportType.LANDSCAPE, savedQuery.getType());
        throw new IteraplanBusinessException(IteraplanErrorMessages.INVALID_REQUEST_PARAMETER,
                "savedQueryType");
    }

    try {
        String content = savedQuery.getContent();
        if (content == null || content.length() <= 0) {
            throw new IteraplanTechnicalException(IteraplanErrorMessages.LOAD_QUERY_EXCEPTION);
        }

        Reader queryDefinitionReader = null;
        try {
            Schema schema = getSchema(schemaName);
            JAXBContext context = JAXBContext.newInstance(clazz);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            unmarshaller.setSchema(schema);

            queryDefinitionReader = new StringReader(content);
            return (T) unmarshaller.unmarshal(queryDefinitionReader);
        } finally {
            IOUtils.closeQuietly(queryDefinitionReader);
        }
    } catch (SAXException e) {
        LOGGER.error("SAXException in SavedQueryServiceImpl#loadQuery " + e.getLocalizedMessage());
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    } catch (JAXBException e) {
        LOGGER.error("JAXBException in SavedQueryServiceImpl#loadQuery " + e.getLocalizedMessage());
        throw new IteraplanTechnicalException(IteraplanErrorMessages.INTERNAL_ERROR, e);
    }
}

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

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

    JAXBContext jaxbContext;//from  w ww  .j a  va 2 s  . c  om
    Unmarshaller jaxbUnmarshaller;

    jaxbContext = JAXBContext.newInstance(clazz);

    jaxbUnmarshaller = jaxbContext.createUnmarshaller();

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

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);
}