Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:org.vertx.java.http.eventbusbridge.unit.EventBusBridgeRequestTest.java

@Test
public void testUnmarshallingFromXml() throws JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(EventBusBridgeRequest.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    InputStream input = getClass().getResourceAsStream("/unit/test-request.xml");
    EventBusBridgeRequest request = (EventBusBridgeRequest) jaxbUnmarshaller.unmarshal(input);
    assertEquals("testaddress", request.getAddress());
    assertEquals(Base64.decodeAsString("SGVsbG8gV29ybGQ="), new String(request.getMessage()));
    assertEquals("http://www.test.com/response", request.getResponseUrl());
    assertEquals("String", request.getEventBusMessageType().toString());
    assertEquals("application/xml", request.getResponseMediaType().toString());
}

From source file:org.esbtools.gateway.GatewayRequest.java

public String toXML() {
    StringWriter thisXML = new StringWriter();

    try {/*w  ww . j  a va  2  s  .  c om*/
        JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.marshal(this, thisXML);

    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return thisXML.toString();
}

From source file:com.github.woozoo73.ht.format.XmlFormat.java

public String formatInternal(Invocation invocation) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(invocation.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(invocation, writer);

    String xml = writer.getBuffer().toString();

    return xml;//from  w  w  w  . j av  a  2 s.c  om
}

From source file:org.wallerlab.yoink.molecular.data.AbstractJaxbReader.java

protected JAXBElement<Cml> init(Object jaxbObject) {
    JAXBElement<Cml> marshalled = null;
    try {/*from ww w.  j  av  a  2s  .co m*/
        jaxbContext = JAXBContext.newInstance(Class.forName(jaxbObject.getClass().getName()));
        jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        marshalled = unmarshal(jaxbObject);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
    return marshalled;
}

From source file:es.caib.sgtsic.xml.XmlManager.java

public XmlManager(Class<T> clazz) throws JAXBException {

    this.clazz = clazz;
    this.jaxbContext = JAXBContext.newInstance(clazz);
}

From source file:org.fcrepo.auth.oauth.integration.api.TestBinding.java

@Test
public void testBinding() throws JAXBException {
    LOGGER.trace("Executing testBinding()");
    final JAXBContext context = JAXBContext.newInstance(WebAppConfig.class);
    final Unmarshaller u = context.createUnmarshaller();
    final WebAppConfig o = (WebAppConfig) u.unmarshal(getClass().getResourceAsStream("/web.xml"));
    assertEquals("Fedora-on-ModeShape", o.displayName());
    assertTrue(o.contextParams()//from w ww.  jav  a  2  s.co  m
            .contains(new ContextParam("contextConfigLocation", "classpath:spring-test/rest.xml; "
                    + "classpath:spring-test/repo.xml; " + "classpath:spring-test/security.xml")));
    assertTrue(o.listeners()
            .contains(new Listener(null, "org.springframework.web.context.ContextLoaderListener")));
    final ServletMapping sm = o.servletMappings("jersey-servlet").iterator().next();
    assertNotNull(sm);
    assertEquals("/*", sm.urlPattern());

    FilterMapping fm = o.filterMappings("TokenFilter").iterator().next();
    assertNotNull(fm);
    assertEquals("/token", fm.urlPattern());

    fm = o.filterMappings("OpFilter").iterator().next();
    assertNotNull(fm);
    assertEquals("/rest/objects/authenticated/*", fm.urlPattern());

}

From source file:com.autofrog.pandabot.client.PandoraBot.java

public PandoraBot(String botId) throws JAXBException {
    this.botId = botId;
    client = new HttpClient();
    jc = JAXBContext.newInstance(PandorabotResult.class);
    u = jc.createUnmarshaller();/*  w w w.  j  a  va 2s .com*/
}

From source file:org.mule.modules.rest.model.LeagueTransformers.java

@Transformer(resultMimeType = "text/xml")
public String toXml(League league) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(League.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(league, boas);//from  w w  w.java 2 s  .c o m

    return new String(boas.toByteArray());
}

From source file:eu.scape_project.tool.toolwrapper.data.components_spec.utils.Utils.java

/**
 * Method that creates a {@link Components} instance from the provided
 * component spec file, validating it against component spec XML Schema
 * //from  w w  w .  j  a  va2s  . c om
 * @param componentSpecFilePath
 *            path to the component spec file
 */
public static Components createComponents(String componentSpecFilePath) {
    Components component = null;
    File schemaFile = null;
    try {
        JAXBContext context = JAXBContext.newInstance(Components.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();

        // copy XML Schema from resources to a temporary location
        schemaFile = File.createTempFile("schema", null);
        FileUtils.copyInputStreamToFile(Utils.class.getResourceAsStream(COMPONENTS_SPEC_FILENAME_IN_RESOURCES),
                schemaFile);
        Schema schema = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI).newSchema(schemaFile);

        // validate provided toolspec against XML Schema
        unmarshaller.setSchema(schema);

        // unmarshal it
        final FileInputStream stream = new FileInputStream(new File(componentSpecFilePath));
        try {
            component = unmarshaller.unmarshal(new StreamSource(stream), Components.class).getValue();
        } finally {
            stream.close();
        }
    } catch (JAXBException e) {
        log.error("The component spec provided doesn't validate against its schema!", e);
    } catch (SAXException e) {
        log.error("The XML Schema is not valid!", e);
    } catch (IOException e) {
        log.error("An error occured while copying the XML Schema from the resources to a temporary location!",
                e);
    } catch (Exception e) {
        log.error("An error occured!", e);
    } finally {
        if (schemaFile != null) {
            schemaFile.deleteOnExit();
        }
    }
    return component;
}

From source file:de.utkast.encoding.EncodingTestClient.java

@Test
public void testEncoding() throws Exception {

    String input = "?? ?";
    String url = "http://localhost:8080/restful-encoding/enc";

    EncodingDTO dto = new EncodingDTO(input);

    JAXBContext ctx = JAXBContext.newInstance(EncodingDTO.class);
    StringWriter writer = new StringWriter();
    ctx.createMarshaller().marshal(dto, writer);

    System.out.println(String.format("Will send: %s", writer.toString()));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    HttpEntity entity = new ByteArrayEntity(writer.toString().getBytes("UTF-8"));
    post.setEntity(entity);/*from w w  w .jav  a  2s  .  c o m*/
    post.setHeader("Content-Type", "application/xml;charset=UTF-8");
    post.setHeader("Accept", "application/xml;charset=UTF-8");

    HttpResponse response = client.execute(post);
    String output = EntityUtils.toString(response.getEntity(), "UTF-8");

    assertEquals(writer.toString(), output);
}