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: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  ww.  j a v a 2  s.co 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.floreantpos.bo.actions.DataImportAction.java

public static void importMenuItems(InputStream inputStream) throws Exception {

    Map<String, Object> objectMap = new HashMap<String, Object>();

    try {/*from  w  w w.java 2 s . c  om*/

        JAXBContext jaxbContext = JAXBContext.newInstance(Elements.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Elements elements = (Elements) unmarshaller.unmarshal(inputStream);

        List<Tax> taxes = elements.getTaxes();
        if (taxes != null) {
            for (Tax tax : taxes) {
                objectMap.put(tax.getUniqueId(), tax);
                tax.setId(1);

                //TaxDAO.getInstance().saveOrUpdate(tax);
            }
        }

        List<MenuCategory> menuCategories = elements.getMenuCategories();
        if (menuCategories != null) {
            for (MenuCategory menuCategory : menuCategories) {

                String uniqueId = menuCategory.getUniqueId();
                objectMap.put(uniqueId, menuCategory);
                menuCategory.setId(null);

                MenuCategoryDAO.getInstance().save(menuCategory);
            }
        }

        List<MenuGroup> menuGroups = elements.getMenuGroups();
        if (menuGroups != null) {
            for (MenuGroup menuGroup : menuGroups) {

                MenuCategory menuCategory = menuGroup.getParent();
                if (menuCategory != null) {
                    menuCategory = (MenuCategory) objectMap.get(menuCategory.getUniqueId());
                    menuGroup.setParent(menuCategory);
                }

                objectMap.put(menuGroup.getUniqueId(), menuGroup);
                menuGroup.setId(null);

                MenuGroupDAO.getInstance().saveOrUpdate(menuGroup);
            }
        }

        List<MenuModifierGroup> menuModifierGroups = elements.getMenuModifierGroups();
        if (menuModifierGroups != null) {
            for (MenuModifierGroup menuModifierGroup : menuModifierGroups) {
                objectMap.put(menuModifierGroup.getUniqueId(), menuModifierGroup);
                menuModifierGroup.setId(null);

                MenuModifierGroupDAO.getInstance().saveOrUpdate(menuModifierGroup);
            }
        }

        List<MenuModifier> menuModifiers = elements.getMenuModifiers();
        if (menuModifiers != null) {
            for (MenuModifier menuModifier : menuModifiers) {

                objectMap.put(menuModifier.getUniqueId(), menuModifier);
                menuModifier.setId(null);

                MenuModifierGroup menuModifierGroup = menuModifier.getModifierGroup();
                if (menuModifierGroup != null) {
                    menuModifierGroup = (MenuModifierGroup) objectMap.get(menuModifierGroup.getUniqueId());
                    menuModifier.setModifierGroup(menuModifierGroup);
                }

                Tax tax = menuModifier.getTax();
                if (tax != null) {
                    tax = (Tax) objectMap.get(tax.getUniqueId());
                    menuModifier.setTax(tax);
                }

                MenuModifierDAO.getInstance().saveOrUpdate(menuModifier);
            }
        }

        List<MenuItemModifierGroup> menuItemModifierGroups = elements.getMenuItemModifierGroups();
        if (menuItemModifierGroups != null) {
            for (MenuItemModifierGroup mimg : menuItemModifierGroups) {
                objectMap.put(mimg.getUniqueId(), mimg);
                mimg.setId(null);

                MenuModifierGroup menuModifierGroup = mimg.getModifierGroup();
                if (menuModifierGroup != null) {
                    menuModifierGroup = (MenuModifierGroup) objectMap.get(menuModifierGroup.getUniqueId());
                    mimg.setModifierGroup(menuModifierGroup);
                }

                MenuItemModifierGroupDAO.getInstance().save(mimg);
            }
        }

        List<MenuItem> menuItems = elements.getMenuItems();
        if (menuItems != null) {
            for (MenuItem menuItem : menuItems) {

                objectMap.put(menuItem.getUniqueId(), menuItem);
                menuItem.setId(null);

                MenuGroup menuGroup = menuItem.getParent();
                if (menuGroup != null) {
                    menuGroup = (MenuGroup) objectMap.get(menuGroup.getUniqueId());
                    menuItem.setParent(menuGroup);
                }

                Tax tax = menuItem.getTax();
                if (tax != null) {
                    tax = (Tax) objectMap.get(tax.getUniqueId());
                    menuItem.setTax(tax);
                }

                List<MenuItemModifierGroup> menuItemModiferGroups = menuItem.getMenuItemModiferGroups();
                if (menuItemModiferGroups != null) {
                    for (MenuItemModifierGroup menuItemModifierGroup : menuItemModiferGroups) {
                        MenuItemModifierGroup menuItemModifierGroup2 = (MenuItemModifierGroup) objectMap
                                .get(menuItemModifierGroup.getUniqueId());
                        menuItemModifierGroup.setId(menuItemModifierGroup2.getId());
                        menuItemModifierGroup.setModifierGroup(menuItemModifierGroup2.getModifierGroup());
                    }
                }

                MenuItemDAO.getInstance().saveOrUpdate(menuItem);
            }
        }

    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:com.googlecode.sardine.util.SardineUtil.java

/**
 * Helper method for getting the Multistatus response processor.
 *//*from   ww w.  ja v  a2  s.c o  m*/
public static Multistatus getMulitstatus(Unmarshaller unmarshaller, HttpResponse response, String url)
        throws SardineException {
    try {
        return (Multistatus) unmarshaller.unmarshal(response.getEntity().getContent());
    } catch (JAXBException ex) {
        throw new SardineException("Problem unmarshalling the data", url, ex);
    } catch (IOException ex) {
        throw new SardineException(ex);
    }
}

From source file:com.hmsinc.epicenter.classifier.ClassifierFactory.java

/**
 * Creates the actual classifier./*  w  ww  . j  a va 2s . c om*/
 * 
 * @param resource
 * @return classificationEngine
 * @throws Exception
 */
private static ClassificationEngine instantiateClassifier(final Resource resource) throws Exception {

    final Unmarshaller u = JAXBContext.newInstance("com.hmsinc.epicenter.classifier.config")
            .createUnmarshaller();

    // Enable validation
    u.setSchema(schema);

    final InputStream is = resource.getInputStream();
    final ClassifierConfig config = (ClassifierConfig) u.unmarshal(is);
    is.close();

    Validate.notNull(config, "Configuration was null!");
    Validate.notNull(config.getImplementation(), "No implementation was specified.");

    final Class<?> implementation = Class.forName(config.getImplementation());
    Validate.isTrue(ClassificationEngine.class.isAssignableFrom(implementation),
            "Implementation must be an instance of ClassificationEngine (was: " + config.getImplementation()
                    + ")");

    final ClassificationEngine engine = (ClassificationEngine) implementation.newInstance();
    engine.init(config);

    return engine;
}

From source file:ch.admin.suis.msghandler.common.Message.java

/**
 * Creates a message from this reader.//from   ww  w .  j  a  va  2s .  c  o m
 *
 * @param inputStream Flow of data representing a Message.
 * @return Message a built message.
 * @throws JAXBException if an error occures while parsing XML
 */
public static Message createFrom(InputStream inputStream) throws JAXBException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    org.apache.commons.io.IOUtils.copy(inputStream, baos);
    byte[] bytes = baos.toByteArray();

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(V2Envelope.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        V2Envelope envelope = (V2Envelope) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(bytes));
        return EnvelopeTypeParent.toMessage(envelope);
    } catch (UnmarshalException e) {
        JAXBContext jaxbContext = JAXBContext.newInstance(V1Envelope.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        V1Envelope envelope = (V1Envelope) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(bytes));
        return EnvelopeTypeParent.toMessage(envelope);
    }
}

From source file:it.polimi.modaclouds.qos_models.util.XMLHelper.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(URL xmlUrl, Class<T> targetClass) throws JAXBException, SAXException {
    // SchemaFactory schemaFactory =
    // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // Schema schema = schemaFactory.newSchema();
    Unmarshaller unmarshaller = JAXBContext.newInstance(targetClass).createUnmarshaller();
    // unmarshaller.setSchema(schema);
    T object = (T) unmarshaller.unmarshal(xmlUrl);
    return object;
}

From source file:it.polimi.modaclouds.qos_models.util.XMLHelper.java

@SuppressWarnings("unchecked")
public static <T> T deserialize(FileInputStream xmlPath, Class<T> targetClass)
        throws JAXBException, SAXException {
    // SchemaFactory schemaFactory =
    // SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    // Schema schema = schemaFactory.newSchema();
    Unmarshaller unmarshaller = JAXBContext.newInstance(targetClass).createUnmarshaller();
    // unmarshaller.setSchema(schema);
    T object = (T) unmarshaller.unmarshal(xmlPath);
    return object;
}

From source file:Main.java

/**
 * Unmarshal XML data from XML DOM document using XSD string and return the resulting JAXB content tree
 *
 * @param dummyJAXBObject/*  ww w. j  a v  a2 s  .c o m*/
 *            Dummy contect object for creating related JAXB context
 * @param doc
 *            XML DOM document
 * @param strXSD
 *            XSD
 * @return resulting JAXB content tree
 * @throws Exception
 *             in error case
 */
public static Object doUnmarshallingFromDOMDocument(Object dummyJAXBObject, Document doc, String strXSD)
        throws Exception {
    if (dummyJAXBObject == null) {
        throw new RuntimeException("No dummy context objekt (null)!");
    }
    if (doc == null) {
        throw new RuntimeException("No XML DOM document (null)!");
    }
    if (strXSD == null) {
        throw new RuntimeException("No XSD document (null)!");
    }

    Object unmarshalledObject = null;
    StringReader reader = null;

    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(dummyJAXBObject.getClass().getPackage().getName());
        Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();
        reader = new StringReader(strXSD);

        javax.xml.validation.Schema schema = javax.xml.validation.SchemaFactory
                .newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(new StreamSource(reader));
        unmarshaller.setSchema(schema);

        unmarshalledObject = unmarshaller.unmarshal(doc);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Unmarshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (reader != null) {
            reader.close();
            reader = null;
        }
    }

    return unmarshalledObject;
}

From source file:com.sap.prd.mobile.ios.mios.VersionInfoManager.java

private static Dependency parseDependency_1_2_2(File f) throws JAXBException {
    final JAXBContext context = JAXBContext.newInstance(Versions.class);
    final Unmarshaller unmarshaller = context.createUnmarshaller();

    final Versions versions = (Versions) unmarshaller.unmarshal(f);

    final Dependency dependency = new Dependency();
    dependency.setCoordinates(versions.getCoordinates());
    dependency.setScm(versions.getScm());

    if (versions.getDependencies() != null)
        for (Dependency d : versions.getDependencies())
            dependency.addDependency(d);

    return dependency;
}

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 ww  . j  av a2  s  . c o m
 *
 * @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);
    }

}