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:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

static SoapMessageImpl createResponse(SoapMessageImpl requestMessage, Marshaller marshaller,
        JAXBElement<?> jaxbElement) throws Exception {
    return SoapUtils.toResponse(requestMessage, soap -> {
        soap.getSOAPBody().removeContents();
        marshaller.marshal(jaxbElement, soap.getSOAPBody());
    });/*from   w  w  w  . jav  a  2s .c om*/
}

From source file:dk.statsbiblioteket.doms.licensemodule.integrationtest.LicenseModuleRestWSTester.java

@SuppressWarnings("all")
private static void testGetUsersLicenses() throws Exception {
    // GetUserLicensesOutputDTO getUserLicenses

    GetUsersLicensesInputDTO input = new GetUsersLicensesInputDTO();
    input.setAttributes(createUserObjAttributeDTO());
    input.setLocale("da");

    JAXBContext context = JAXBContext.newInstance(GetUsersLicensesInputDTO.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(input, outputStream);

    // serialize to XML
    String inputXML = outputStream.toString();
    System.out.println("input xml:\n" + inputXML);

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client//ww  w  .  j a  v a  2 s  . c  o m
            .resource(UriBuilder.fromUri("http://localhost:8080/licensemodule/services/").build());
    // Call with XML
    //GetUsersLicensesOutputDTO output = service.path("getUserLicenses").type(MediaType.TEXT_XML).accept(MediaType.TEXT_XML).entity(inputXML).post(GetUsersLicensesOutputDTO.class);

    // Call with @XmlRootElement
    GetUsersLicensesOutputDTO output = service.path("getUserLicenses").type(MediaType.TEXT_XML)
            .accept(MediaType.TEXT_XML).entity(input).post(GetUsersLicensesOutputDTO.class);
    context = JAXBContext.newInstance(GetUsersLicensesOutputDTO.class);
    outputStream = new ByteArrayOutputStream();
    m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(output, outputStream);

    // serialize to XML
    String outputXML = outputStream.toString();
    System.out.println(outputXML);

    System.out.println("output, licensenames:" + output.getLicenses());
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;/*  w w w.  ja va2 s.  co  m*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        //         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        //         marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java

public static String serializeObject(Object object) throws JAXBException {
    if (object == null) {
        return null;
    }// w w w  .j  a  v  a  2 s  . c o m

    Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    marshaller.marshal(object, sw);
    return sw.toString();
}

From source file:dk.statsbiblioteket.doms.licensemodule.integrationtest.LicenseModuleRestWSTester.java

@SuppressWarnings("all")
private static void testCheckAccessForIds() throws Exception {
    CheckAccessForIdsInputDTO input = new CheckAccessForIdsInputDTO();
    input.setPresentationType("Search");
    input.setAttributes(createUserObjAttributeDTO());
    ArrayList<String> ids = new ArrayList<String>();
    ids.add("doms_radioTVCollection:uuid:371157ee-b120-4504-bfaf-364c15a4137c");//radio TV        
    ids.add("doms_radioTVCollection:uuid:c3386ed5-9b79-47a2-a648-8de53569e630");//radio TV
    ids.add("doms_reklamefilm:uuid:35a1aa76-97a1-4f1b-b5aa-ad2a246eeeec"); //reklame
    ids.add("doms_newspaperCollection:uuid:18709dea-802c-4bd7-98e6-32ca3b285774-segment_6"); //aviser      
    input.setIds(ids);/*  www . j a va 2  s  . c om*/

    JAXBContext context = JAXBContext.newInstance(CheckAccessForIdsInputDTO.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(input, outputStream);

    // serialize to XML
    String inputXML = outputStream.toString();
    System.out.println(inputXML);
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client
            .resource(UriBuilder.fromUri("http://devel06:9612/licensemodule/services/").build());

    // Call with XML
    CheckAccessForIdsOutputDTO output = service.path("checkAccessForIds").type(MediaType.TEXT_XML)
            .accept(MediaType.TEXT_XML).entity(inputXML).post(CheckAccessForIdsOutputDTO.class);
    context = JAXBContext.newInstance(CheckAccessForIdsOutputDTO.class);
    outputStream = new ByteArrayOutputStream();
    m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(output, outputStream);

    // serialize to XML
    String outputXML = outputStream.toString();
    System.out.println(outputXML);

    System.out.println("query:" + output.getQuery());
    System.out.println("presentationtype:" + output.getPresentationType());
    System.out.println("number of IDs:" + output.getAccessIds().size());
}

From source file:dk.statsbiblioteket.doms.licensemodule.integrationtest.LicenseModuleRestWSTester.java

@SuppressWarnings("all")
private static void testValidateAccess() throws Exception {
    // Test Validate Access
    ValidateAccessInputDTO input = new ValidateAccessInputDTO();

    ArrayList<UserObjAttributeDTO> userObjAttributes = createUserObjAttributeDTO();
    input.setAttributes(userObjAttributes);

    ArrayList<String> groups = new ArrayList<String>();
    groups.add("IndividueltForbud");
    groups.add("Klausuleret");
    input.setGroups(groups);//from   www.j  av a  2 s . c o  m
    input.setPresentationType("images");

    JAXBContext context = JAXBContext.newInstance(ValidateAccessInputDTO.class);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(input, outputStream);

    // serialize to XML
    String inputXML = outputStream.toString();
    System.out.println("input xml:\n" + inputXML);

    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create(config);
    WebResource service = client
            .resource(UriBuilder.fromUri("http://localhost:8080/licensemodule/services/").build());

    // Call with XML
    ValidateAccessOutputDTO output = service.path("validateAccess").type(MediaType.TEXT_XML)
            .accept(MediaType.TEXT_XML).entity(inputXML).post(ValidateAccessOutputDTO.class);

    // Call with @XmlRootElement
    // output = service.path("validateAccess").type(MediaType.TEXT_XML).accept(MediaType.TEXT_XML).entity(input).post(ValidateAccessOutputDTO.class);

    context = JAXBContext.newInstance(ValidateAccessOutputDTO.class);
    outputStream = new ByteArrayOutputStream();
    m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(output, outputStream);

    // serialize to XML
    String outputXML = outputStream.toString();
    System.out.println(outputXML);

    // Access depends on the licenses in the DB.
    System.out.println("access :" + output.isAccess());
}

From source file:com.netxforge.oss2.core.xml.JaxbUtils.java

public static void marshal(final Object obj, final Writer writer) {
    final Marshaller jaxbMarshaller = getMarshallerFor(obj, null);
    try {//  w ww .  ja v  a2 s .  c  o  m
        jaxbMarshaller.marshal(obj, writer);
    } catch (final JAXBException e) {
        throw EXCEPTION_TRANSLATOR.translate("marshalling " + obj.getClass().getSimpleName(), e);
    }
}

From source file:com.redhat.akashche.wixgen.cli.Launcher.java

private static void writeXml(Marshaller marshaller, Object jaxbElement, String path, boolean fragment)
        throws Exception {
    Writer writer = null;/*from w ww  .  j a va  2 s . c o m*/
    try {
        OutputStream os = new FileOutputStream(new File(path));
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
        marshaller.marshal(jaxbElement, writer);
    } finally {
        closeQuietly(writer);
    }
}

From source file:com.evolveum.midpoint.testing.model.client.sample.RunScript.java

private static String marshalObject(Object object) throws JAXBException, FileNotFoundException {
    JAXBContext jc = getJaxbContext();
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    marshaller.marshal(object, sw);
    return sw.toString();
}

From source file:Main.java

public static <T> String write(T content, Class<T> typeParameterClass) {
    ByteArrayOutputStream baos = null;
    try {//  www  .  j a v a2  s  .com
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        //jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        baos = new ByteArrayOutputStream();
        OutputStreamWriter osw = new OutputStreamWriter(baos, "UTF-8");
        jaxbMarshaller.marshal(content, osw);
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return baos != null ? baos.toString() : null;
}