Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:it.cnr.icar.eric.common.cms.AbstractService.java

protected SOAPElement getSOAPElementFromBindingObject(Object obj) throws Exception {
    SOAPElement soapElem = null;//from  w  w w  .  j  a  va 2  s .com

    SOAPElement parent = SOAPFactory.newInstance().createElement("dummy");

    Marshaller marshaller = getJAXBContext().createMarshaller();
    marshaller.marshal(obj, System.err);
    marshaller.marshal(obj, new DOMResult(parent));
    soapElem = (SOAPElement) parent.getChildElements().next();

    return soapElem;
}

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

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

    // change name to something different
    item.setName(item.getName() + "Foo");

    JAXBContext jaxbContext = JAXBContext.newInstance(Item.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    StringWriter sw = new StringWriter();

    jaxbMarshaller.marshal(item, sw);

    String xmlItem = sw.toString();

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

    assertEquals(item, getItemService().getItem(0));
}

From source file:gov.nih.nci.cacis.sa.mirthconnect.SemanticAdapterChannelIntegrationTest.java

/**
 * This test calls out acceptSource(..) operation on SematicAdapter WS.
 * The SemanticAdapter WS is the source connector to SemanticAdapterChannel in Mirth.
 * @throws Exception exception//from   w  ww.  j a  v a 2  s  .  c o m
 */
@Test
public void invokeSemanticAdapterWS() throws Exception { //NOPMD
    final JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(AcceptSourcePortType.class);
    factory.setAddress(ADDRESS);
    final AcceptSourcePortType client = (AcceptSourcePortType) factory.create();

    final InputStream sampleMessageIS = FileUtils
            .openInputStream(new File(getClass().getClassLoader().getResource("SARequestSample.xml").toURI()));

    final JAXBContext jc = JAXBContext.newInstance(CaCISRequest.class);
    final Unmarshaller unm = jc.createUnmarshaller();

    final CaCISRequest request = (CaCISRequest) unm.unmarshal(sampleMessageIS);

    final Marshaller m = jc.createMarshaller();
    final StringWriter sw = new StringWriter();
    final PrintWriter pw = new PrintWriter(sw);
    m.marshal(request, pw);
    final String reqStr = sw.toString();

    final CaCISResponse response = client.acceptSource(request);
    assertTrue(response.getStatus() == ResponseStatusType.SUCCESS);

}

From source file:com.moss.veracity.core.config.ConfigManager.java

private void write(Configuration config) throws Exception {
    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(config, new FileWriter(file));

    if (monitor != null) {
        monitor.reset();//  w ww.java 2s  .  c o  m
    }
}

From source file:dk.dbc.rawrepo.oai.ResumptionTokenTest.java

@Test
public void testXmlExpiration() throws Exception {
    ObjectNode jsonOriginal = (ObjectNode) new ObjectMapper().readTree("{\"foo\":\"bar\"}");

    long now = Instant.now().getEpochSecond();
    ResumptionTokenType token = ResumptionToken.toToken(jsonOriginal, 0);

    OAIPMH oaipmh = OBJECT_FACTORY.createOAIPMH();
    ListRecordsType getRecord = OBJECT_FACTORY.createListRecordsType();
    oaipmh.setListRecords(getRecord);/*w w  w .  j a v  a  2  s . c  o  m*/
    getRecord.setResumptionToken(token);
    JAXBContext context = JAXBContext.newInstance(OAIPMH.class);
    StringWriter writer = new StringWriter();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(oaipmh, writer);
    String xml = writer.getBuffer().toString();
    System.out.println("XML is:\n" + xml);
    int start = xml.indexOf("expirationDate=\"") + "expirationDate=\"".length();
    int end = xml.indexOf("\"", start);
    String timestamp = xml.substring(start, end);
    System.out.println("timestamp = " + timestamp);

    assertTrue("Timestamp should be in ISO_INSTANT ending with Z", timestamp.endsWith("Z"));
    TemporalAccessor parse = DateTimeFormatter.ISO_INSTANT.withZone(ZoneId.systemDefault()).parse(timestamp);
    long epochSecond = Instant.from(parse).getEpochSecond();
    long diff = Math.abs(now - epochSecond);
    System.out.println("diff = " + diff);
    assertTrue("Difference between expirationdate and now should be 10 sec or less", diff <= 10);
}

From source file:fr.fastconnect.factory.tibco.bw.codereview.ConvertRulesToSonarMojo.java

public void save(File f, Rules rules) {
    try {/*from  ww w.ja v a  2 s.co  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(rules, f);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:org.remus.marketplace.servlets.ContentListing.java

@Override
public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    int contentId = Integer.parseInt(arg0.getParameter("nodeId"));
    arg1.setContentType("text/xml");
    try {/* ww  w .j  a v  a2s .  c o m*/
        JAXBContext newInstance = JAXBContext.newInstance(Marketplace.class,
                org.remus.marketplace.xml.Node.class, Platforms.class, Platform.class, IUs.class, IU.class);

        Marketplace marketplace = new Marketplace();
        Node findById = nodeDao.findById(contentId);
        if (findById != null) {
            org.remus.marketplace.xml.Node node = XMLBuilder.buildNode(serverPrefix, findById);
            marketplace.setNode(node);
            Marshaller createMarshaller = newInstance.createMarshaller();
            XMLSerializer xmlSerializer = XMLBuilder.getXMLSerializer(arg1.getOutputStream());
            createMarshaller.marshal(marketplace, xmlSerializer.asContentHandler());
        } else {
            throw new ServletException("Node not found");
        }
    } catch (JAXBException e) {
        throw new ServletException(e);
    }

}

From source file:org.remus.marketplace.servlets.RecentListing.java

@Override
public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    arg1.setContentType("text/xml");
    try {/*from w  w  w. jav a 2s . c  o m*/
        JAXBContext newInstance = JAXBContext.newInstance(Marketplace.class,
                org.remus.marketplace.xml.Market.class);

        Marketplace marketplace = new Marketplace();

        List<Node> findByCategoriesId = nodeDao
                .find(new AdvancedCriteria().setMaxResults(10).addOrder(Order.desc(Node.CHANGED)));

        Recent recent = new Recent();
        recent.setCount(findByCategoriesId.size());
        for (Node findById : findByCategoriesId) {
            org.remus.marketplace.xml.Node node = XMLBuilder.buildNode(serverPrefix, findById);
            recent.getNode().add(node);
        }
        marketplace.setRecent(recent);
        Marshaller createMarshaller = newInstance.createMarshaller();
        XMLSerializer xmlSerializer = XMLBuilder.getXMLSerializer(arg1.getOutputStream());
        createMarshaller.marshal(marketplace, xmlSerializer.asContentHandler());
    } catch (JAXBException e) {
        throw new ServletException(e);
    }

}

From source file:android.databinding.tool.LayoutXmlProcessor.java

private String toXML(ResourceBundle.LayoutFileBundle layout, Marshaller marshaller) throws JAXBException {
    StringWriter writer = new StringWriter();
    marshaller.marshal(layout, writer);
    return writer.getBuffer().toString();
}

From source file:com.zaubersoftware.gnip4j.http.JSONDeserializationTest.java

/** regression test for a NPE exception */
@Test/*www.java 2  s . c o m*/
public void testNPE() throws Exception {
    final InputStream is = getClass().getClassLoader()
            .getResourceAsStream("com/zaubersoftware/gnip4j/payload/payload-twitter-entities.js");
    final InputStream expectedIs = getClass().getClassLoader()
            .getResourceAsStream("com/zaubersoftware/gnip4j/payload/payload-twitter-entities.xml");

    try {
        final String json = IOUtils.toString(is);
        final JsonParser parser = mapper.getJsonFactory().createJsonParser(json);
        final Activity activity = parser.readValueAs(Activity.class);
        final StringWriter w = new StringWriter();
        mapper.getJsonFactory().createJsonGenerator(w).writeObject(activity);

        final Marshaller o = ctx.createMarshaller();
        o.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter ww = new StringWriter();
        o.marshal(activity, ww);
        assertEquals(IOUtils.toString(expectedIs), ww.toString());
    } finally {
        is.close();
    }
}