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:io.onedecision.engine.decisions.impl.DecisionModelFactory.java

@Override
public void write(Definitions dm, Writer out) throws IOException {
    JAXBContext context;//from   ww  w  . j  a v a2 s  . c  o m
    try {
        context = JAXBContext.newInstance(Definitions.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        // Since no @XmlRootElement generated for Definitions need to create
        // element wrapper here. See
        // https://weblogs.java.net/blog/kohsuke/archive/2006/03/why_does_jaxb_p.html
        m.marshal(new JAXBElement<Definitions>(DEFINITIONS, Definitions.class, dm), out);
    } catch (JAXBException e) {
        throw new IOException(e.getMessage(), e);
    }
}

From source file:eu.modaclouds.sla.service.rest.AbstractSLARest.java

protected String printError(int code, String text) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {/*from w ww .ja v a 2  s .co m*/
        ErrorResponse errorResponse = new ErrorResponse();

        errorResponse.setCode(code);
        errorResponse.setMessage(text);

        JAXBContext jaxbContext;
        jaxbContext = JAXBContext.newInstance(eu.atos.sla.parser.data.ErrorResponse.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(errorResponse, out);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return out.toString();

}

From source file:eu.modaclouds.sla.service.rest.AbstractSLARest.java

protected String printMessage(int code, String text) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    try {//ww  w  . j  a v  a 2  s.  c om

        MessageResponse messageResponse = new MessageResponse();

        messageResponse.setCode(code);
        messageResponse.setMessage(text);

        JAXBContext jaxbContext = JAXBContext.newInstance(eu.atos.sla.parser.data.MessageResponse.class);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        marshaller.marshal(messageResponse, out);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return out.toString();

}

From source file:org.bremersee.sms.test.ModelTests.java

@Test
public void testXmlSmsSendRequestDto() throws Exception {

    System.out.println("Testing XML SmsSendRequestDto ...");

    SmsSendRequestDto request = new SmsSendRequestDto("bremersee", "0123456789", "Hello",
            new Date(System.currentTimeMillis() + 30000L));

    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    m.marshal(request, out);/*from   w w w .  j ava 2 s.  com*/

    String xmlStr = new String(out.toByteArray(), "UTF-8");

    System.out.println(xmlStr);

    SmsSendRequestDto readRequest = (SmsSendRequestDto) jaxbContext.createUnmarshaller()
            .unmarshal(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

    m.marshal(readRequest, System.out);

    TestCase.assertEquals(request, readRequest);

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

From source file:org.bremersee.sms.test.ModelTests.java

@Test
public void testXmlSmsSendResponseDto() throws Exception {

    System.out.println("Testing XML SmsSendResponseDto ...");

    GoyyaSmsSendResponseDto goyyaResponse = new GoyyaSmsSendResponseDto("OK");

    SmsSendResponseDto response = new SmsSendResponseDto(new SmsSendRequestDto("bremersee", "0123456789",
            "Hello", new Date(System.currentTimeMillis() + 30000L)), goyyaResponse.isOk(), goyyaResponse);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    m.marshal(response, out);// w  w  w . j  av a  2  s  . c  om

    String xmlStr = new String(out.toByteArray(), "UTF-8");

    System.out.println(xmlStr);

    SmsSendResponseDto readResponse = (SmsSendResponseDto) jaxbContext.createUnmarshaller()
            .unmarshal(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

    m.marshal(readResponse, System.out);

    GoyyaSmsSendResponseDto tmp = ExtensionUtils.transform(readResponse.getExtension(),
            GoyyaSmsSendResponseDto.class, jaxbContext, new ObjectMapper());
    readResponse.setExtension(tmp);

    TestCase.assertEquals(response, readResponse);

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

From source file:eu.openminted.toolkit.elsevier.retriever.JaxbMarshalingTest.java

@Test
public void shouldSerialise() {
    Marshaller marshaller = null;
    try {/*from ww w  .  j av a 2  s  . co  m*/
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        //            marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        Writer writer = new StringWriter();
        marshaller.marshal(testObject, writer);

        String serialised = writer.toString();
        //            serialised.replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF\\-8\" standalone=\"yes\"\\?>", "");
        String testXmlAsString = FileUtils.readFileToString(new File("src/test/resources/S0140673616313228"),
                "UTF-8");

        System.out.println("serialised test object :\n" + serialised);
        System.out.println("test xml :\n" + testXmlAsString);

        //            Assert.assertEquals(asString, testXmlAsString);
    } catch (PropertyException ex) {
        ex.printStackTrace();
    } catch (JAXBException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

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

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

    log.info("context1");
    org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1 mc1 = new org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1();
    mc1.setP(new MyProperty1());
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context1.MyClassJ1.class, mc1), System.out);
    log.info("context2");
    org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2 mc2 = new org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2();
    mc2.setP(new MyProperty2());
    m.marshal(new JAXBElement<org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2>(new QName("a", "a"),
            org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class, mc2), System.out);

    Object object = ctx.createUnmarshaller().unmarshal(
            new StreamSource(
                    new StringReader("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\r\n"
                            + "<ns2:a xmlns=\"urn:x\" xmlns:ns2=\"a\">\r\n" + "    <p/>\r\n" + "</ns2:a>")),
            org.javelin.sws.ext.bind.jaxb.context2.MyClassJ2.class);
    log.info("Class: {}", ((JAXBElement<?>) object).getValue().getClass());
}

From source file:mx.bigdata.cfdi.CFDv3.java

public void marshal(OutputStream out) throws Exception {
    Marshaller m = CONTEXT.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "http://www.sat.gob.mx/cfd/3 cfdv3.xsd");
    m.marshal(document, out);//from  w  w  w  . ja  v  a  2  s .c  o  m
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedTypeAllAvailablityHAVE() {
    String hospitalstatushave;/*from w  w w.  j a v  a2s .co m*/
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService.getHospitalAllAvailableBedTypes();
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return hospitalstatushave;

}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedTypeAvailablityHAVE(String hospitalname) {
    String hospitalstatushave;// w w  w .  j a  va  2  s  .co m
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService
            .getHospitalAvailableBedTypes(hospitalname);
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return hospitalstatushave;

}