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:ymanv.forex.provider.impl.EuropeanCentralBank.java

private List<RateEntity> getRates(String response, boolean usdBase) throws IOException {
    SimpleDateFormat sf = new SimpleDateFormat(DATE_FORMAT_STRNG);
    Calendar sfCalendar = sf.getCalendar();
    sfCalendar.setTimeZone(TZ);// ww  w .  j  av a  2s .c o  m

    List<RateEntity> rates = new ArrayList<>();

    String cleanResponse = response.substring(response.indexOf("<Cube>"), response.lastIndexOf("</Cube>") + 7);
    try {
        JAXBContext context = JAXBContext.newInstance(Result.class);
        Unmarshaller un = context.createUnmarshaller();
        Result res = (Result) un.unmarshal(new StringReader(cleanResponse));

        for (Day d : res.getDays()) {
            Date date = d.getDate();
            sf.parse(sf.format(date));
            sfCalendar.set(Calendar.HOUR, 15);
            date = sf.getCalendar().getTime();

            if (!usdBase) {
                for (Rate r : d.getRates()) {
                    rates.add(new RateEntity(BASE_CURRENCY, r.getTocur(), new BigDecimal(r.getValue()), date));
                }
            } else {
                BigDecimal usdToEurValue = null;

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        usdToEurValue = invert(new BigDecimal(r.getValue()));
                        break;
                    }
                }

                for (Rate r : d.getRates()) {
                    if (USD.equals(r.getTocur())) {
                        rates.add(new RateEntity(USD, BASE_CURRENCY, usdToEurValue, date));
                    } else {
                        rates.add(new RateEntity(USD, r.getTocur(),
                                new BigDecimal(r.getValue()).multiply(usdToEurValue), date));
                    }
                }
            }
        }

        return rates;
    } catch (ParseException | JAXBException e) {
        throw new IOException(e);
    }
}

From source file:dk.dma.epd.common.prototype.communication.webservice.ShoreHttp.java

public Object getXmlUnmarshalledContent(String contextPath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(contextPath);
    Unmarshaller u = jc.createUnmarshaller();
    return u.unmarshal(new ByteArrayInputStream(responseBody));
}

From source file:org.apache.cxf.systest.jaxrs.JAXRSClientServerUserResourceAsteriskTest.java

private Chapter readChapter(InputStream is) throws Exception {
    JAXBContext c = JAXBContext.newInstance(new Class[] { Chapter.class });
    Unmarshaller u = c.createUnmarshaller();
    return (Chapter) u.unmarshal(is);
}

From source file:dk.dma.msinm.common.time.TimeParser.java

/**
 * Parses the time into a {@code TimeModel} model.
 * @param time the time description to parse
 * @return the time model/*from   ww w. java 2s.c om*/
 */
public TimeModel parseModel(String time) throws TimeException {
    String timeXml = null;
    try {
        // Transform the time description into xml
        timeXml = parse(time);

        // Attempt to parse the XML
        JAXBContext jc = JAXBContext.newInstance(TimeModel.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        return (TimeModel) unmarshaller.unmarshal(new StringReader(timeXml));

    } catch (Exception e) {
        throw new TimeException("Failed parsing time description: " + time + "\n" + timeXml, e);
    }
}

From source file:com.netsteadfast.greenstep.util.SystemFtpClientUtils.java

private static void processXml(SystemFtpClientResultObj resultObj) throws Exception {
    SysFtpTranVO tran = resultObj.getSysFtpTran();
    List<SystemFtpClientData> datas = new LinkedList<SystemFtpClientData>();
    JAXBContext jaxbContext = null;
    Unmarshaller jaxbUnmarshaller = null;
    if (!StringUtils.isBlank(tran.getXmlClassName())) {
        Class<?> xmlBeanClazz = Class.forName(tran.getXmlClassName());
        jaxbContext = JAXBContext.newInstance(xmlBeanClazz);
        jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    }/*from   ww  w  .  ja  v a2  s  . c  o m*/
    for (File file : resultObj.getFiles()) {
        SystemFtpClientData ftpData = new SystemFtpClientData();
        logWarnFileSize(file);
        String content = FileUtils.readFileToString(file, Constants.BASE_ENCODING); // xml utf-8
        ftpData.setContent(content);
        ftpData.setDatas(null);
        ftpData.setFile(file);
        if (jaxbUnmarshaller != null) {
            Object obj = jaxbUnmarshaller
                    .unmarshal(new ByteArrayInputStream(content.getBytes(Constants.BASE_ENCODING))); // xml utf-8
            ftpData.setXmlBean(obj);
        }
        datas.add(ftpData);
    }
    resultObj.setDatas(datas);
}

From source file:it.geosolutions.unredd.onlinestats.ppio.JAXBStatisticConfigurationPPIO.java

public StatisticConfiguration decode(InputStream arg0) throws Exception {
    Unmarshaller unmarshaller = buildUnmarshaller();
    StatisticConfiguration cfg = (StatisticConfiguration) unmarshaller.unmarshal(arg0);
    return cfg;/* w  w  w .  java 2  s. c  om*/
}

From source file:com.jkoolcloud.tnt4j.streams.utils.StreamsCache.java

private static void loadPersisted() {
    try {//  w  w  w.  j av a 2 s .  c  o m
        JAXBContext jc = JAXBContext.newInstance(CacheRoot.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File persistedFile = new File(DEFAULT_FILE_NAME);
        LOGGER.log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "StreamsCache.loading.file", persistedFile.getAbsolutePath());
        if (!persistedFile.exists()) {
            LOGGER.log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                    "StreamsCache.loading.file.not.found");
            return;
        }

        CacheRoot root = (CacheRoot) unmarshaller.unmarshal(persistedFile);

        Map<String, CacheValue> mapProperty = root.getEntriesMap();
        if (MapUtils.isNotEmpty(mapProperty)) {
            for (Map.Entry<String, CacheValue> entry : mapProperty.entrySet()) {
                valuesCache.put(entry.getKey(), entry.getValue());
            }
        }
        LOGGER.log(OpLevel.DEBUG, StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "StreamsCache.loading.done", mapProperty == null ? 0 : mapProperty.size(),
                persistedFile.getAbsolutePath());
    } catch (JAXBException exc) {
        Utils.logThrowable(LOGGER, OpLevel.ERROR,
                StreamsResources.getBundle(StreamsResources.RESOURCE_BUNDLE_NAME),
                "StreamsCache.loading.failed", exc);
    }
}

From source file:org.camelcookbook.rest.binding.BindingModeSpringTest.java

@Test
public void testGetOneXml() throws Exception {
    final Item origItem = getItemService().getItem(0);

    String out = fluentTemplate().to("undertow:http://localhost:" + port1 + "/items/0/xml")
            .withHeader(Exchange.HTTP_METHOD, "GET").request(String.class);

    JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    Item itemOut = (Item) jaxbUnmarshaller.unmarshal(new StringReader(out));

    assertEquals(origItem, itemOut);/*from w w  w.j ava 2s .co m*/
}

From source file:org.openmrs.module.dhisreport.api.dxf2.DataValueSetTest.java

@Test
public void unMarshallDataValueSet() throws Exception {
    ClassPathResource resource = new ClassPathResource("dvset.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(DataValueSet.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    DataValueSet dvset = (DataValueSet) jaxbUnmarshaller.unmarshal(resource.getInputStream());
    assertEquals(5, dvset.getDataValues().size());
}