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.u2apple.tool.filter.ExcludeFilter.java

private static void loadRules() {
    URL url = DevicePatternFilter.class.getResource(RULES_CONF);
    File file = new File(url.getFile());
    JAXBContext jaxbContext;
    try {/*  w ww . ja  v  a  2 s.co m*/
        jaxbContext = JAXBContext.newInstance(Rules.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        rules = (Rules) unmarshaller.unmarshal(file);
    } catch (JAXBException ex) {
        logger.error("Fail to parse exclude-filter.xml as {}", ex);
    }
}

From source file:eu.seaclouds.platform.dashboard.util.ObjectMapperHelpers.java

/**
 * Transforms a XML string to an List<Object> using javax.xml.bind.Unmarshaller.
 * If you want to parse a single Object please @see {#link XmlToObject}
 *
 * @param xml  string representing a collection of objects
 * @param type Class of the contained objects within the list
 * @param <T>  target class/*from  w  w  w.  j a va  2 s.c  o m*/
 * @return a List of T
 * @throws IOException if is not possible to parse the object
 **/
public static <T> List<T> XmlToObjectCollection(String xml, Class<T> type) throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(ObjectMapperHelpers.JAXBCollection.class, type);
    Unmarshaller u = ctx.createUnmarshaller();

    Source src = new StreamSource(IOUtils.toInputStream(xml));
    ObjectMapperHelpers.JAXBCollection<T> collection = u
            .unmarshal(src, ObjectMapperHelpers.JAXBCollection.class).getValue();
    return collection.getItems();
}

From source file:Main.java

/**
 * Generic method to Validate XML file while unmarshalling against their schema.
 * /*w  w w  .  j a  v a  2  s .c  o  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:eu.eexcess.partnerdata.evaluation.enrichment.PartnerRecommenderEvaluationTestHelper.java

static public ResultList parseResponse(String responseString) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(ResultList.class);

    StringReader reader = new StringReader(responseString);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    Object resultListObject = unmarshaller.unmarshal(reader);

    if (resultListObject instanceof ResultList) {
        return (ResultList) resultListObject;

    }/*  www  .j  av a 2 s.co  m*/
    return null;
}

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

/**
 * Loads a query from database/*from  ww  w .ja v  a2  s .  c o  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:com.mymita.vaadlets.JAXBUtils.java

private static final Vaadlets unmarshal(final Reader aReader, final Resource theSchemaResource) {
    try {/*from ww  w  . j  a v a2  s. co  m*/
        final JAXBContext jc = JAXBContext.newInstance(CONTEXTPATH);
        final Unmarshaller unmarshaller = jc.createUnmarshaller();
        if (theSchemaResource != null) {
            final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
            schemaFactory.setResourceResolver(new ClasspathResourceResolver());
            final Schema schema = schemaFactory.newSchema(new StreamSource(theSchemaResource.getInputStream()));
            unmarshaller.setSchema(schema);
        }
        return unmarshaller
                .unmarshal(XMLInputFactory.newInstance().createXMLStreamReader(aReader), Vaadlets.class)
                .getValue();
    } catch (final JAXBException | SAXException | XMLStreamException | FactoryConfigurationError
            | IOException e) {
        throw new RuntimeException("Can't unmarschal", e);
    }
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

@SuppressWarnings("unchecked")
public static <T> T asObjectFromXml(String str, Class<T> clazz, Class<?>... otherClazz)
        throws JAXBException, IOException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(otherClazz));
    list.add(clazz);/* w w w  .  java2  s.  c  om*/

    JAXBContext context = JAXBContext.newInstance(list.toArray(new Class[list.size()]));
    Unmarshaller unmarshaller = context.createUnmarshaller();

    return (T) unmarshaller.unmarshal(new StringReader(str));
}

From source file:att.jaxrs.util.Marshal.java

public static <T> T unmarshal(Class<T> xmlType, InputStream inputStream) {
    System.out.println("unmarshalling input stream");
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
    StringBuilder out = new StringBuilder();
    try {//from  ww  w . ja  v a  2s . c  om
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
    String namespace = out.toString();

    namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, "");

    InputStream stream = Util.getInputStreamFromString(namespace);
    JAXBContext jaxbContext;
    XmlRootElement doc = null;
    try {
        jaxbContext = JAXBContext.newInstance(xmlType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        doc = (XmlRootElement) unmarshaller.unmarshal(stream);
        System.out.println("unmarshall successful");
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return (T) doc;
}

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

public static <T> T readJAXB(InputStream is, Class... clazz) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller u = jc.createUnmarshaller();

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

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

@SuppressWarnings("unchecked")
public static <T> T asObjectFromJson(String str, Class<T> clazz, Class<?>... otherClazz) throws JAXBException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(otherClazz));
    list.add(clazz);//w  ww. java  2s .  c  o  m

    Map<String, Object> props = new HashMap<String, Object>();
    props.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);

    JAXBContext context = JAXBContextFactory.createContext(list.toArray(new Class[list.size()]), props);
    Unmarshaller unm = context.createUnmarshaller();

    return (T) unm.unmarshal(new StringReader(str));
}