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:com.healthcit.cacure.web.controller.ModuleImportExportController.java

@RequestMapping(method = RequestMethod.GET)
public void exportModule(@RequestParam(value = "moduleId", required = true) long id,
        @RequestParam(value = "format", required = true) String format, HttpServletResponse response) {

    try {/*from  ww  w. j ava 2s. c  o  m*/
        response.setContentType("text/xml");

        OutputStream oStream = response.getOutputStream();
        Cure cureXml = dataExporter.constructModuleXML(id);
        JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");
        if (ExportFormat.XML.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xml;", id);

            response.setHeader("Content-Disposition", fileNameHeader);
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(cureXml, oStream);
        } else if (ExportFormat.EXCEL.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xlxml;", id);

            response.setHeader("Content-Disposition", fileNameHeader);
            response.setContentType("application/xml");
            StreamSource xslSource = new StreamSource(this.getClass().getClassLoader()
                    .getResourceAsStream(AppConfig.getString(Constants.EXPORT_EXCEL_XSLT_FILE)));
            JAXBSource xmlSource = new JAXBSource(jc, cureXml);
            Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
            transformer.transform(xmlSource, new StreamResult(oStream));
        }
        oStream.flush();
    } catch (IOException e) {
        log.error("Unable to obtain output stream from the response");
        log.error(e.getMessage(), e);
    } catch (JAXBException e) {
        log.error("Unable to marshal the object");
        log.error(e.getMessage(), e);
    } catch (TransformerException e) {
        log.error("XSLT transformation failed");
        log.error(e.getMessage(), e);
    }
}

From source file:cn.hxh.springside.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null)./*from w ww.ja v  a  2s .c  om*/
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

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

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.govsoft.framework.common.util.encode.JaxbBinder.java

/**
 * Marshallerencoding(?Null)./*from   w  w  w .ja va  2 s.c om*/
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

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

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.openthinks.webscheduler.service.WebSecurityService.java

/**
 * save changes to disk file/*  w w  w . j  av  a 2s. c om*/
 */
public void saveToDisk() {
    Checker.require(this.securityConfig).notNull();
    Checker.require(this.webSecurity).notNull();
    File file = this.securityConfig.getConfigFile();
    try {
        FileOutputStream fos = new FileOutputStream(file);
        JAXBContext jaxbContext = JAXBContext.newInstance(WebSecurity.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(this.webSecurity, fos);
        fos.close();
        this.webSecurity.moveToSaved();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.framework.infrastructure.mapper.JaxbMapper.java

/**
 * Marshallerencoding(null)./*w  w  w .j  a  va 2  s  .c  o m*/
 * pooling
 */
public Marshaller createMarshaller(String encoding) {
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();

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

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:es.caib.sgtsic.xml.XmlManager.java

private ByteArrayOutputStream marshal(T item, boolean formattedOutput) throws JAXBException {

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formattedOutput);

    jaxbMarshaller.marshal(item, baos);/*from  ww w.  j a  v  a  2s  . c om*/

    return baos;
}

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

public String marhsallObject(PolicyResponse pResponse) {

    JAXBContext jaxbContext;//from   w  w w  .  ja v  a2  s  .  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();
}

From source file:net.sourceforge.subsonic.controller.JAXBWriter.java

private Marshaller createXmlMarshaller() throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StringUtil.ENCODING_UTF8);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
}

From source file:com.olp.jpa.domain.docu.ut.DepartmentServiceTest.java

public void test_marshalling() throws JAXBException {

    List<DepartmentBean> list = __service.findAll();

    DepartmentBean bean = __service.findByDeptCode("OPRTN");

    JAXBContext ctx = JAXBContext.newInstance(DepartmentBean.class);
    StringWriter writer = new StringWriter();
    Marshaller m = ctx.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(bean, writer);// w w  w .  j  a  v a2 s  .  co m

    final String xml = writer.toString();
    System.out.println(xml);

}

From source file:net.sourceforge.subsonic.controller.JAXBWriter.java

private Marshaller createJsonMarshaller() throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StringUtil.ENCODING_UTF8);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
    marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT, true);
    return marshaller;
}