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:org.kemri.wellcome.dhisreport.api.model.HttpDhis2Server.java

@Override
public ImportSummary postReport(DataValueSet report) throws DHIS2ReportingException {
    log.debug("Posting datavalueset report");
    ImportSummary summary = null;//  www . j ava 2 s.c  o  m

    StringWriter xmlReport = new StringWriter();
    try {
        JAXBContext jaxbDataValueSetContext = JAXBContext.newInstance(DataValueSet.class);
        Marshaller dataValueSetMarshaller = jaxbDataValueSetContext.createMarshaller();
        // output pretty printed
        dataValueSetMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        dataValueSetMarshaller.marshal(report, xmlReport);
    } catch (JAXBException ex) {
        log.error(ex.getMessage());
        throw new Dxf2Exception("Problem marshalling dataValueSet", ex);
    }

    String host = getUrl().getHost();
    int port = getUrl().getPort();

    HttpHost targetHost = new HttpHost(host, port, getUrl().getProtocol());
    DefaultHttpClient httpclient = new DefaultHttpClient();
    BasicHttpContext localcontext = new BasicHttpContext();

    try {
        String postUrl = getUrl().toString() + DATAVALUESET_PATH;
        log.error("Post URL: " + postUrl);
        HttpPost httpPost = new HttpPost(postUrl);
        Credentials creds = new UsernamePasswordCredentials(username, password);
        Header bs = new BasicScheme().authenticate(creds, httpPost, localcontext);
        httpPost.addHeader("Authorization", bs.getValue());
        httpPost.addHeader("Content-Type", "application/xml; charset=utf-8");
        httpPost.addHeader("Accept", "application/xml");

        httpPost.setEntity(new StringEntity(xmlReport.toString()));
        HttpResponse response = httpclient.execute(targetHost, httpPost, localcontext);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            JAXBContext jaxbImportSummaryContext = JAXBContext.newInstance(ImportSummary.class);
            Unmarshaller importSummaryUnMarshaller = jaxbImportSummaryContext.createUnmarshaller();
            summary = (ImportSummary) importSummaryUnMarshaller.unmarshal(entity.getContent());
        }
    } catch (Exception ex) {
        log.error(ex.getMessage());
        throw new Dhis2Exception(this, "Problem accessing Dhis2 server", ex);
    } finally {
        httpclient.getConnectionManager().shutdown();
    }
    return summary;
}

From source file:edu.htwm.vsp.phoebook.rest.client.RestClient.java

/**
 * Example code for deserializing a PhoneUser via JAXB.
 *//*from   w ww.j  a  v a  2 s.c  o  m*/
public PhoneUser unmarshalPhoneUserFromXML(Reader in) throws JAXBException {

    JAXBContext jc = JAXBContext.newInstance(PhoneUser.class);
    assertNotNull(jc);
    Unmarshaller unmarshaller = jc.createUnmarshaller();

    assertNotNull(unmarshaller);
    Object unmarshalledObj = unmarshaller.unmarshal(in);

    assertNotNull(unmarshalledObj);

    assertTrue(unmarshalledObj instanceof PhoneUser);
    return (PhoneUser) unmarshalledObj;
}

From source file:org.javelin.sws.ext.bind.SweJaxbUnmarshallerTest.java

@Test
public void unmarshalVeryComplexContentExplicitRI() throws Exception {
    JAXBContext context = ContextFactory.createContext(new Class[] {
            org.javelin.sws.ext.bind.jaxb.context6.ClassWithVeryComplexContent.class, SingleRootElement.class },
            new HashMap<String, Object>());
    Unmarshaller um = context.createUnmarshaller();
    InputStream inputStream = new ClassPathResource("very-complex-content-01.xml", this.getClass())
            .getInputStream();/*from  ww w  .  j a  v a2  s. co  m*/
    // will throw javax.xml.bind.UnmarshalException: unexpected element (uri:"urn:test", local:"root"). Expected elements are <{y}x"
    JAXBElement<org.javelin.sws.ext.bind.jaxb.context6.ClassWithVeryComplexContent> je = um.unmarshal(
            XMLInputFactory.newFactory().createXMLEventReader(inputStream),
            org.javelin.sws.ext.bind.jaxb.context6.ClassWithVeryComplexContent.class);
    org.javelin.sws.ext.bind.jaxb.context6.ClassWithVeryComplexContent ob = je.getValue();
    assertThat(ob.getStr(), equalTo("test-1"));
    assertThat(ob.getInside(), equalTo("str"));
    assertThat(ob.getInside2().getNumber(), equalTo(42));
    assertThat(ob.getInside2().getStr(), equalTo("test-2"));
    assertThat(ob.getInside2().getInside(), equalTo("inside"));
}

From source file:org.javelin.sws.ext.bind.SweJaxbUnmarshallerTest.java

@Test
public void unmarshalVeryComplexContentExplicit() throws Exception {
    JAXBContext context = JAXBContext.newInstance("org.javelin.sws.ext.bind.context1");
    // ClassWithVeryComplexContent value = new ClassWithVeryComplexContent("test", "str", new ClassWithComplexContent("test", 42, "inside"));
    Unmarshaller um = context.createUnmarshaller();
    InputStream inputStream = new ClassPathResource("very-complex-content-01.xml", this.getClass())
            .getInputStream();//ww  w  . ja  v a  2  s  . c om
    JAXBElement<ClassWithVeryComplexContent> je = um.unmarshal(
            XMLInputFactory.newFactory().createXMLEventReader(inputStream), ClassWithVeryComplexContent.class);
    assertTrue(je.getDeclaredType() == ClassWithVeryComplexContent.class);
    assertFalse("Should not be nil", je.isNil());
    assertTrue(je.getValue() instanceof ClassWithVeryComplexContent);
    ClassWithVeryComplexContent ob = (ClassWithVeryComplexContent) je.getValue();
    assertThat(ob.getStr(), equalTo("test-1"));
    assertThat(ob.getInside(), equalTo("str"));
    assertThat(ob.getInside2().getNumber(), equalTo(42));
    assertThat(ob.getInside2().getStr(), equalTo("test-2"));
    assertThat(ob.getInside2().getInside(), equalTo("inside"));
}

From source file:fr.fastconnect.factory.tibco.bw.maven.compile.ArchiveBuilder.java

private Repository load(File f) {
    try {/*w w w  .java2 s . c om*/
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        Object o = jaxbUnmarshaller.unmarshal(f);
        return (Repository) o;
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:be.fedict.eid.dss.model.bean.ServicesManagerSingletonBean.java

@SuppressWarnings("unchecked")
private Map<String, String> loadDocumentServiceClassNames() {
    LOG.debug("getDocumentServiceClassNames");
    Map<String, String> documentServiceClassNames = new HashMap<String, String>();
    Enumeration<URL> resources;
    try {/*from ww w  . ja va2 s.co m*/
        resources = getResources("META-INF/eid-dss-document.xml");
    } catch (IOException e) {
        LOG.error("I/O error: " + e.getMessage(), e);
        return documentServiceClassNames;
    }
    Unmarshaller unmarshaller;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(be.fedict.eid.dss.spi.document.ObjectFactory.class);
        unmarshaller = jaxbContext.createUnmarshaller();
    } catch (JAXBException e) {
        LOG.error("JAXB error: " + e.getMessage(), e);
        return documentServiceClassNames;
    }
    while (resources.hasMoreElements()) {
        URL resource = resources.nextElement();
        LOG.debug("resource URL: " + resource.toString());
        JAXBElement<DigitalSignatureServiceDocumentType> jaxbElement;
        try {
            jaxbElement = (JAXBElement<DigitalSignatureServiceDocumentType>) unmarshaller.unmarshal(resource);
        } catch (JAXBException e) {
            LOG.error("JAXB error: " + e.getMessage(), e);
            continue;
        }
        DigitalSignatureServiceDocumentType digitalSignatureServiceDocument = jaxbElement.getValue();
        String documentServiceClassName = digitalSignatureServiceDocument.getDocumentServiceClass();
        List<String> contentTypes = digitalSignatureServiceDocument.getContentType();
        for (String contentType : contentTypes) {
            documentServiceClassNames.put(contentType, documentServiceClassName);
        }
    }
    return documentServiceClassNames;
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void loadVids() throws JAXBException {
    File dir = new File(Configuration.getProperty(Constants.VID_DIR));
    File[] files = dir.listFiles(new FilenameFilter() {
        @Override/*from   ww w. j a  v  a 2 s.  com*/
        public boolean accept(File dir, String name) {
            return Pattern.matches(VID_FILE_NAME_PATTERN, name);
        }
    });
    if (files != null) {
        for (File file : files) {
            JAXBContext jaxbContext = JAXBContext.newInstance(VID.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
            VID vid = (VID) jaxbUnmarshaller.unmarshal(file);
            if (vid != null) {
                staticMapFile.getVids().add(vid);
            }
        }
    }
}

From source file:esg.node.components.registry.AtsWhitelistGleaner.java

public synchronized AtsWhitelistGleaner loadMyAtsWhitelist() {
    log.info("Loading my ATS Whitelist info from " + atsWhitelistPath + atsWhitelistFile);
    try {/*from www .j  a va2s .co  m*/
        JAXBContext jc = JAXBContext.newInstance(AtsWhitelist.class);
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement<AtsWhitelist> root = u
                .unmarshal(new StreamSource(new File(atsWhitelistPath + atsWhitelistFile)), AtsWhitelist.class);
        atss = root.getValue();
    } catch (Exception e) {
        log.error(e);
    }
    return this;
}

From source file:org.jasig.portlet.widget.service.GoogleGadgetService.java

public Module getModule(String url) {

    Module module = null;/*  w w w.  j av  a 2  s .c o  m*/

    try {

        InputStream stream = getStreamFromUrl(url);
        JAXBContext jc = JAXBContext.newInstance(Module.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        module = (Module) unmarshaller.unmarshal(stream);

    } catch (JAXBException e) {
        log.error(e);
    } catch (ClientProtocolException e) {
        log.error(e);
    } catch (IOException e) {
        log.error(e);
    }

    return module;
}

From source file:esg.node.components.registry.AtsWhitelistGleaner.java

public AtsWhitelist createAtsWhitelistFromString(String atsWhitelistContentString) {
    log.info("Loading my ATS Whitelist info from \n" + atsWhitelistContentString + "\n");
    AtsWhitelist fromContentAtsWhitelist = null;
    try {//from   ww w . java  2  s.c  o m
        JAXBContext jc = JAXBContext.newInstance(AtsWhitelist.class);
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement<AtsWhitelist> root = u
                .unmarshal(new StreamSource(new StringReader(atsWhitelistContentString)), AtsWhitelist.class);
        fromContentAtsWhitelist = root.getValue();
    } catch (Exception e) {
        log.error(e);
    }
    return fromContentAtsWhitelist;
}