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:br.gov.frameworkdemoiselle.behave.integration.alm.objects.util.GenerateXMLString.java

public static Testplan getTestPlanObject(HttpResponse response) throws IOException, JAXBException {

    Testplan plan = null;//w w  w . jav a2s.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(Testplan.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        StringReader reader = new StringReader(xmlString.toString());
        plan = (Testplan) unmarshaller.unmarshal(reader);
    }

    return plan;

}

From source file:com.silverpeas.admin.components.Instanciateur.java

static WAComponent loadComponent(File file) throws IOException, JAXBException, XMLStreamException {
    JAXBContext context = JAXBContext.newInstance("com.silverpeas.admin.components");
    Unmarshaller unmarshaller = context.createUnmarshaller();
    InputStream in = new FileInputStream(file);
    try {/*from  w  w  w .j  a  va 2s  .  c om*/
        return (unmarshaller.unmarshal(factory.createXMLStreamReader(in), WAComponent.class)).getValue();
    } finally {
        IOUtils.closeQuietly(in);
    }

}

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 ww  w  .  j av a2 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:ch.admin.suis.msghandler.common.Message.java

/**
 * Creates a message from this reader.// w ww . j a v  a 2  s.  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:com.aionemu.gameserver.model.TribeRelationCheck.java

@BeforeClass
public static void init() throws Exception {
    File xml = new File("./data/static_data/tribe/tribe_relations.xml");
    Schema schema = null;//from   w  ww  . java2 s.  co  m
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    TribeRelationsData tribeRelations = null;
    NpcData npcTemplates = null;

    try {
        schema = sf.newSchema(new File("./data/static_data/tribe/tribe_relations.xsd"));
        JAXBContext jc = JAXBContext.newInstance(TribeRelationsData.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        tribeRelations = (TribeRelationsData) unmarshaller.unmarshal(xml);

        xml = new File("./data/static_data/npcs/npc_templates.xml");
        schema = sf.newSchema(new File("./data/static_data/npcs/npcs.xsd"));
        jc = JAXBContext.newInstance(NpcData.class);
        unmarshaller = jc.createUnmarshaller();
        unmarshaller.setSchema(schema);
        npcTemplates = (NpcData) unmarshaller.unmarshal(xml);
    } catch (SAXException e1) {
        System.out.println(e1.getMessage());
    } catch (JAXBException e2) {
        System.out.println(e2.getMessage());
    }

    // Not interesting
    DataManager.NPC_SKILL_DATA = new NpcSkillData();
    DataManager.NPC_DATA = npcTemplates;
    DataManager.TRIBE_RELATIONS_DATA = tribeRelations;
    DataManager.ZONE_DATA = new DummyZoneData();
    DataManager.WORLD_MAPS_DATA = new DummyWorldMapData();

    Config.load();
    // AIConfig.ONCREATE_DEBUG = true;
    AIConfig.EVENT_DEBUG = true;
    ThreadConfig.THREAD_POOL_SIZE = 20;
    ThreadPoolManager.getInstance();

    AI2Engine.getInstance().load(null);

    /**
     * Comment out these lines in DAOManager.registerDAO() if not using DB: <tt> 
     * if (!dao.supports(getDatabaseName(),
     *       getDatabaseMajorVersion(), getDatabaseMinorVersion())) { return; } 
     * </tt>
     */
    DAOManager.init();

    world = World.getInstance();
    asmo = DummyPlayer.createAsmodian();
    asmoPosition = World.getInstance().createPosition(DummyWorldMapData.DEFAULT_MAP, 100f, 100f, 0f, (byte) 0,
            0);

    MapRegion asmoRegion = asmoPosition.getWorldMapInstance().getRegion(100f, 100f, 0);
    asmoRegion.getObjects().put(asmo.getObjectId(), asmo);
    asmoRegion.activate();
    asmo.setPosition(asmoPosition);

    ely = DummyPlayer.createElyo();
    elyPosition = World.getInstance().createPosition(DummyWorldMapData.DEFAULT_MAP, 200f, 200f, 0f, (byte) 0,
            0);
    MapRegion elyRegion = elyPosition.getWorldMapInstance().getRegion(200f, 200f, 0);
    elyRegion.getObjects().put(ely.getObjectId(), ely);
    elyRegion.activate();
    ely.setPosition(elyPosition);

    PacketBroadcaster.getInstance();
    DuelService.getInstance();
    PlayerMoveTaskManager.getInstance();
    MoveTaskManager.getInstance();
}

From source file:de.thorstenberger.taskmodel.util.JAXBUtils.java

/**
 * Get the unmarshaller.  You must call releaseUnmarshaller to put it back into the pool
 *
 * @param context JAXBContext/* w  w  w  .  jav a 2  s. c om*/
 * @return Unmarshaller
 * @throws JAXBException
 */
public static Unmarshaller getJAXBUnmarshaller(JAXBContext context) throws JAXBException {
    if (!ENABLE_UNMARSHALL_POOLING) {
        if (log.isDebugEnabled()) {
            log.debug("Unmarshaller created [no pooling]");
        }
        return context.createUnmarshaller();
    }
    Unmarshaller unm = upool.get(context);
    if (unm == null) {
        if (log.isDebugEnabled()) {
            log.debug("Unmarshaller created [not in pool]");
        }
        unm = context.createUnmarshaller();
    } else {
        if (log.isDebugEnabled()) {
            log.debug("Unmarshaller obtained [from  pool]");
        }
    }
    return unm;
}

From source file:org.eclipse.winery.topologymodeler.addons.topologycompleter.helper.JAXBHelper.java

/**
 * This method creates an JAXB Unmarshaller used by the methods contained in this class.
 *
 * @return the JAXB unmarshaller object/*from   w  ww .j a va  2 s  . c om*/
 *
 * @throws JAXBException
 *             this exception can occur when the JAXBContext is created
 */
private static Unmarshaller createUnmarshaller() throws JAXBException {
    // initiate JaxB context
    JAXBContext context;
    context = JAXBContext.newInstance(Definitions.class);

    return context.createUnmarshaller();
}

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

public static <T> T unmarshal(Reader r, Class<T> clazz)
        throws JAXBException, XMLStreamException, FactoryConfigurationError {
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);/*from   w ww  .j  a v a2s . c  om*/
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    XMLStreamReader xmlsr = XMLInputFactory.newFactory().createXMLStreamReader(r);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    unmarshaller.setSchema(null);
    unmarshaller.setEventHandler(valEventHndlr);

    JAXBElement<T> elem = null;
    try {
        elem = unmarshaller.unmarshal(xmlsr, clazz);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new UnmarshalException(e.getMessage(), e);
        }
    }

    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Unmarshall Exception if there is a parsing error
                String msg = MessageFormat.format("Line {0}, Col: {1}: {2}",
                        valEvent.getLocator().getLineNumber(), valEvent.getLocator().getColumnNumber(),
                        valEvent.getLinkedException().getMessage());
                throw new UnmarshalException(msg, valEvent.getLinkedException());
            }
        }
    }
    return elem.getValue();
}

From source file:com.netflix.subtitles.ttml.TtmlParagraphResolver.java

private static <T> T deepCopy(T object, Class<T> clazz, String packages) {
    try {// ww w . j a  v a  2 s . c o m
        JAXBContext jaxbContext = JAXBContext.newInstance(packages);

        //  create marshaller which disable validation step
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setEventHandler(event -> true);

        //  create unmarshaller which disable validation step
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        unmarshaller.setEventHandler(event -> true);

        JAXBElement<T> contentObject = new JAXBElement<>(new QName(clazz.getName()), clazz, object);
        return unmarshaller.unmarshal(new JAXBSource(marshaller, contentObject), clazz).getValue();
    } catch (JAXBException e) {
        throw new ParseException("Time overlaps in <p> cannot be resolved.", e);
    }
}