Example usage for javax.xml.bind Marshaller setProperty

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

Introduction

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

Prototype

public void setProperty(String name, Object value) throws PropertyException;

Source Link

Document

Set the particular property in the underlying implementation of Marshaller .

Usage

From source file:org.axiom_tools.codecs.ModelCodec.java

/**
 * Converts a model to XML./*  w ww .  ja  va  2s. com*/
 * @return model XML, or empty
 */
public String toXML() {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Marshaller m = buildJAXBContext().createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(this.entity, stream);
        return stream.toString(XML_ENCODING).trim();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
        return Empty;
    }
}

From source file:jails.http.converter.xml.Jaxb2RootElementHttpMessageConverter.java

private void setCharset(MediaType contentType, Marshaller marshaller) throws PropertyException {
    if (contentType != null && contentType.getCharSet() != null) {
        marshaller.setProperty(Marshaller.JAXB_ENCODING, contentType.getCharSet().name());
    }//from w w  w  .ja va  2  s .  c o m
}

From source file:org.bremersee.comparator.test.ComparatorItemTests.java

@Test
public void testXmlComparatorItem() throws Exception {

    System.out.println("Testing XML write-read operations ...");

    ComparatorItem item = new ComparatorItem("i0", true);
    item.next("i1", false).next("i2", true);

    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();

    marshaller.marshal(item, sw);//from   w w  w. j av  a  2 s .  co m

    String xmlStr = sw.toString();

    System.out.println(xmlStr);

    ComparatorItem readItem = (ComparatorItem) jaxbContext.createUnmarshaller()
            .unmarshal(new StringReader(xmlStr));

    System.out.println(item.toList(true));
    System.out.println(readItem.toList(true));

    TestCase.assertEquals(item.toList(true), readItem.toList(true));

    System.out.println("OK\n");
}

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

public void setXmlMarshalContent(String contextPath, Object obj)
        throws JAXBException, UnsupportedEncodingException {
    JAXBContext jc = JAXBContext.newInstance(contextPath);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, ENCODING);
    StringWriter sw = new StringWriter();
    m.marshal(obj, sw);/*from w  w w  . j a  v a 2 s  .co m*/
    String req = sw.toString();
    LOG.debug("XML request: " + req);
    setRequestBody(sw.toString().getBytes(ENCODING), ENCODING);
}

From source file:be.fedict.eid.dss.ws.DSSJAXBRIContext.java

@Override
public Marshaller createMarshaller() throws JAXBException {
    LOG.debug("createMarshaller");
    Marshaller marshaller = this.jaxbRiContext.createMarshaller();
    marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new DSSNamespacePrefixMapper());
    return marshaller;
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test
public void marshalIllegalName() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // and I thought it's illegal...
    m.marshal(new JAXBElement<String>(new QName("a", "a a"), String.class, "hello"), System.out);
}

From source file:com.tangfan.test.UserServiceTest.java

/**
 * add jaxws-ri???//from   w  w w .  jav  a 2s.  co m
 */
@Test
public void add() {
    try {
        //1.?xml
        JAXBContext jaxb = JAXBContext.newInstance(LicenseInfo.class);
        User lu = new User();
        lu.setId(0);
        lu.setNickname("");
        lu.setUsername("admin");
        lu.setPassword("123");
        LicenseInfo info = new LicenseInfo();
        info.setLoginUser(lu);

        QName qName = new QName(ns, "licenseInfo");
        JAXBElement<LicenseInfo> jele = new JAXBElement<LicenseInfo>(qName, LicenseInfo.class, info);

        Marshaller mars = jaxb.createMarshaller();
        mars.setProperty(Marshaller.JAXB_FRAGMENT, true);//xml
        mars.setProperty(Marshaller.JAXB_ENCODING, "utf-8");//?

        //2.?dom
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
        mars.marshal(jele, doc);

        //3.Headers.create?header
        WSBindingProvider wsb = (WSBindingProvider) port;
        wsb.setOutboundHeaders(Headers.create(doc.getDocumentElement()));

        User u = new User();
        u.setId(2);
        u.setUsername("master");
        u.setNickname("jboss?");
        u.setPassword("123456");
        port.add(u);
    } catch (UserException_Exception e) {
        System.out.println(e.getMessage());
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    }
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test(expected = IllegalAnnotationsException.class)
public void marshalMixed() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4 mc4 = new org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4();
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context4.MyClassJ4.class, mc4), System.out);
}

From source file:edu.isi.misd.scanner.network.worker.webapp.ResultsReleaseDelegate.java

private void writeRejectedServiceResponse(String id, String url, String siteName, String nodeName,
        String comments, File outputFile) throws Exception {
    // 1. Populate the rejected ServiceResponse
    ServiceResponse response = new ServiceResponse();
    ServiceResponseMetadata responseMetadata = new ServiceResponseMetadata();
    responseMetadata.setRequestID(id);/* ww  w.  j av  a2 s.com*/
    responseMetadata.setRequestURL(url);
    responseMetadata.setRequestState(ServiceRequestStateType.REJECTED);
    responseMetadata.setRequestStateDetail("Reason: " + (comments.isEmpty() ? "not specified." : comments));
    responseMetadata.setRequestSiteName(siteName);
    responseMetadata.setRequestNodeName(nodeName);
    response.setServiceResponseMetadata(responseMetadata);

    // 2. Write out the result response using JAXB
    JAXBContext jaxbContext = JAXBContext.newInstance(ServiceResponse.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    if (outputFile.exists()) {
        try {
            FileUtils.forceDelete(outputFile);
        } catch (Exception e) {
            log.warn("Could not delete output file: " + outputFile.getCanonicalPath());
            throw e;
        }
    }
    jaxbMarshaller.marshal(response, outputFile);
}

From source file:org.javelin.sws.ext.bind.jaxb.JaxbTest.java

@Test(expected = IllegalAnnotationsException.class)
public void marshalIllegalProperties() throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class);
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1 mc5 = new org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1();
    mc5.setC(String.class);
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context5.MyClassJ5_1.class, mc5), System.out);
}