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

@Override
public void write(Definitions dm, Writer out) throws IOException {
    JAXBContext context;/*w  w w.  j  a  v  a2 s  .  co  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:io.mapzone.arena.csw.CswRequest.java

protected void writeObject(JAXBElement jaxb) throws Exception {
    Marshaller marshaller = jaxbContext.get().createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    marshaller.marshal(jaxb, out());
}

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 source file:it.cnr.icar.eric.server.interfaces.rest.QueryManagerURLHandler.java

/** 
 * Process the QueryManager POST request and sends the response back to the client
 *
 *///  w w w .j av a2 s. c o m
void processPostRequest() throws IOException, RegistryException, InvalidRequestException,
        UnimplementedException, ObjectNotFoundException {
    try {
        PrintWriter out = response.getWriter();
        String xmlAdhocQueryRequest = request.getParameter("xmldoc");

        String message = xmlAdhocQueryRequest;
        //TODO: consider using unmarshall directly from BindingUtility
        StreamSource messageSS = new StreamSource(new StringReader(message));
        Unmarshaller unmarshaller = bu.getJAXBContext().createUnmarshaller();
        AdhocQueryRequest req = (AdhocQueryRequest) unmarshaller.unmarshal(messageSS);

        ServerRequestContext context = new ServerRequestContext("QueryManagerURLHandler.processPostRequest",
                req);
        context.setUser(currentUser);
        RegistryResponseType regResponse = QueryManagerFactory.getInstance().getQueryManager()
                .submitAdhocQuery(context);
        //            Marshaller marshaller = bu.rsFac.createMarshaller();
        Marshaller marshaller = bu.getJAXBContext().createMarshaller();
        marshaller.marshal(regResponse, out);
        out.close();

    } catch (JAXBException ex) {
        log.error(ex.toString(), ex);
        throw new RegistryException(ex);
    }
}

From source file:de.uniwue.info6.database.jaxb.ScenarioExporter.java

/**
 *
 *
 * @param scenario/*w  w w. j  a  v a  2  s. co m*/
 */
public File generateScenarioXml(Scenario scenario) {
    scenario = populateScenario(scenario);
    File base = new File(scriptSystemPath + File.separator + Cfg.RESOURCE_PATH);
    File saveDir = new File(base, String.valueOf(scenario.getId()));

    try {
        if (!saveDir.exists() && saveDir.getParentFile().exists()) {
            saveDir.mkdir();
        }
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy_MM_dd-HH_mm_ss");
        String baseName = "scenario_" + scenario.getId() + "_export_" + fmt.format(new Date());

        String conflict = "VERSION_CONFLICT";
        File scenarioXml = new File(saveDir, baseName + ".xml");
        File scenarioXmlConflict = new File(saveDir, baseName + "_" + conflict + ".xml");
        File scenarioXsd = new File(saveDir, baseName + ".xsd");
        File scenarioXsdConflict = new File(saveDir, baseName + "_" + conflict + ".xsd");
        File scenarioMain = new File(scriptSystemPath, "reference_schema.xsd");
        File scenarioMainConflict = new File(saveDir, baseName + "_" + "REFERENCE_SCHEMA.xsd");

        JAXBContext jaxbContext = JAXBContext.newInstance(Scenario.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(scenario, scenarioXml);

        SchemaOutputResolver sor = new CustomOutputResolver(saveDir, baseName + ".xsd");
        jaxbContext.generateSchema(sor);

        if (!scenarioMain.exists()) {
            LOGGER.error("REFERENCE SCHEMA IS MISSING: " + scenarioMain);
        }

        if (!scenarioXsd.exists()) {
            LOGGER.error("GENERATED XSD IS MISSING: " + scenarioXsd);
        }

        if (!scenarioXml.exists()) {
            LOGGER.error("GENERATED XML IS MISSING: " + scenarioXml);
        }

        if (scenarioMain.exists() && scenarioXsd.exists() && scenarioXml.exists()) {
            String referenceMD5 = calculateMD5FromFile(scenarioMain);
            String newMD5 = calculateMD5FromFile(scenarioXsd);

            File export = null;
            boolean falseMD5 = !referenceMD5.equals(newMD5);
            File conflictReadme = new File(saveDir, conflict + "_README.txt");

            if (falseMD5) {
                FileUtils.moveFile(scenarioXml, scenarioXmlConflict);
                FileUtils.moveFile(scenarioXsd, scenarioXsdConflict);
                FileUtils.copyFile(scenarioMain, scenarioMainConflict);
                scenarioXml = scenarioXmlConflict;
                scenarioXsd = scenarioXsdConflict;
                export = new File(saveDir, "scenario_" + scenario.getId() + "_export_" + conflict + ".zip");
                String readme = Cfg.inst().getProp(DEF_LANGUAGE, "MISC.XSD_XML_CONFLICT") + "\n"
                        + scenarioMainConflict.getName();

                if (conflictReadme.exists()) {
                    conflictReadme.delete();
                }
                if (!conflictReadme.exists()) {
                    conflictReadme.createNewFile();
                }

                PrintWriter out = new PrintWriter(conflictReadme);
                out.println(readme);
                out.flush();
                out.close();
            } else {
                export = new File(saveDir, "scenario_" + scenario.getId() + "_export.zip");
            }

            ArrayList<File> exportFiles = new ArrayList<File>();
            if (scenarioXml.exists()) {
                exportFiles.add(scenarioXml);
            }
            if (scenarioXsd.exists()) {
                exportFiles.add(scenarioXsd);
            }
            if (falseMD5 && conflictReadme.exists() && scenarioMainConflict.exists()) {
                exportFiles.add(conflictReadme);
                exportFiles.add(scenarioMainConflict);
            }
            if (export.exists()) {
                export.delete();
            }
            return zip(exportFiles, export);
        }

    } catch (Exception e) {
        LOGGER.error("EXPORT XSD/XML FAILED!", e);
    }
    return null;
}

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

@Test
public void shouldSerialise() {
    Marshaller marshaller = null;
    try {/*from   www . j a  v  a  2s  .  com*/
        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:Main.java

public final String getMessage() throws Exception {

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
            (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
    xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
    jaxbMarshaller.marshal(this, xmlStreamWriter);
    xmlStreamWriter.writeEndDocument();/*from   w  w w.j  ava  2  s . c  o  m*/
    xmlStreamWriter.close();
    return new String(baos.toByteArray());
}

From source file:edu.harvard.hul.ois.drs.pdfaconvert.service.servlets.PdfaConverterServlet.java

private String errorMessageToString(ErrorMessage errorMessage) {
    String errorMessageStr = null;
    try {//from  w w w . j  av  a  2  s. c o  m
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        JAXBContext jaxbContext = JAXBContext.newInstance(ErrorMessage.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.marshal(errorMessage, outStream);
        errorMessageStr = outStream.toString();
    } catch (JAXBException jbe) {
        errorMessageStr = errorMessage.toString();
    }
    return errorMessageStr;
}

From source file:edu.harvard.lib.lcloud.ItemDAO.java

protected String marshallObject(Object obj) throws JAXBException {
    StringWriter sw = new StringWriter();
    String jsonString = null;//from  ww  w .  j a  v  a2 s  .  co m
    Marshaller jaxbMarshaller = JAXBHelper.context.createMarshaller();
    jaxbMarshaller.marshal(obj, sw);
    return sw.toString();
}

From source file:com.provenance.cloudprovenance.sconverter.PolicyResponseConverter.java

public String marhsallObject(PolicyResponse pResponse) {

    JAXBContext jaxbContext;// w w  w  . j av  a2s .  c  o m
    StringWriter sw = new StringWriter();

    try {
        jaxbContext = JAXBContext.newInstance(instanceDir);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", cProvPrefixMapper);

        JAXBElement<PolicyResponse> el = pFactory.createPolicyResponse(pResponse);

        marshaller.marshal(el, sw);

        logger.info("Sucessfully marshalled the policy objects: " + el.getName());

    } catch (JAXBException e) {
        e.printStackTrace();
    }

    return sw.toString();
}