Example usage for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT

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

Introduction

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

Prototype

String JAXB_FORMATTED_OUTPUT

To view the source code for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT.

Click Source Link

Document

The name of the property used to specify whether or not the marshalled XML data is formatted with linefeeds and indentation.

Usage

From source file:com.sportpm.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null).//from   w w w.ja  v  a2s  .  c o  m
 * ???pooling
 */
@SuppressWarnings("rawtypes")
public static Marshaller createMarshaller(Class clazz, String encoding) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);

        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:fr.fastconnect.factory.tibco.bw.codereview.ConvertRulesToSonarMojo.java

public void save(File f, Rules rules) {
    try {//from  w  w  w.  ja va2 s.c o m
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(rules, f);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:at.ac.tuwien.dsg.comot.m.common.Utils.java

public static String asJsonString(Object obj, Class<?>... clazz) throws JAXBException {

    List<Object> list = new ArrayList<Object>(Arrays.asList(clazz));
    list.add(obj.getClass());/*  w  ww .  ja  va2  s.  c  om*/

    StringWriter w = new StringWriter();

    Map<String, Object> props = new HashMap<String, Object>();
    // props.put(JAXBContextProperties.JSON_INCLUDE_ROOT, false);
    props.put(JAXBContextProperties.MEDIA_TYPE, MediaType.APPLICATION_JSON);

    JAXBContext context = JAXBContextFactory.createContext(list.toArray(new Class[list.size()]), props);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(obj, w);

    return w.toString();
}

From source file:Main.java

/**
 * @param m/*  ww  w  .  j av a 2  s  .  com*/
 * @param encoding
 * @return
 * @throws PropertyException
 */
private static Marshaller setMarshallerProperty(Marshaller m) throws PropertyException {
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    // set Output charset to project's charset;
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return m;
}

From source file:com.healthcit.cacure.web.controller.FormExportController.java

@RequestMapping(method = RequestMethod.GET)
public void exportForm(@RequestParam(value = "id", required = true) Long formId,
        @RequestParam(value = "format", required = true) String format, HttpServletResponse response) {

    try {//from  ww  w .  java2s  .  c  o  m
        OutputStream oStream = response.getOutputStream();
        Cure cureXml = dataExporter.constructFormXML(formId);
        JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");

        if (ExportFormat.XML.name().endsWith(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xml;", formId);
            response.setHeader("Content-Disposition", fileNameHeader);
            response.setContentType("application/xml");

            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.marshal(cureXml, oStream);
            oStream.flush();
        } else if (ExportFormat.EXCEL.name().equals(format)) {
            String fileNameHeader = String.format("attachment; filename=form-%d.xlxml;", formId);
            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));
        }
    } 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:com.faceye.feature.util.JaxbMapper.java

/**
 * Marshallerencoding(?null)./*from  w w  w.ja v  a  2  s . com*/
 * ???pooling
 */
public static Marshaller createMarshaller(Class clazz, String encoding) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);

        Marshaller marshaller = jaxbContext.createMarshaller();

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

        if (StringUtils.isNotBlank(encoding)) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        }
        //         marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "");
        //         marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, "");

        //         marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapper() {
        //                @Override
        //                public String[] getPreDeclaredNamespaceUris() {
        //                    return new String[] { WellKnownNamespace.XML_SCHEMA_INSTANCE };
        //                }
        //
        //                @Override
        //                public String getPreferredPrefix(String namespaceUri, String suggestion, boolean requirePrefix) {
        //                    if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA_INSTANCE))
        //                        return "xsi";
        //                    if (namespaceUri.equals(WellKnownNamespace.XML_SCHEMA))
        //                        return "xs";
        //                    if (namespaceUri.equals(WellKnownNamespace.XML_MIME_URI))
        //                        return "xmime";
        //                    return suggestion;
        //
        //                }
        //            });

        return marshaller;
    } catch (JAXBException e) {
        logger.error(">>FaceYe form xml 2 class exception:", e);
        throw Exceptions.unchecked(e);
    }
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.JaxbMapperHelper.java

/**
 * Marshallerencoding(?null). ???pooling
 *///  ww  w . java  2  s .  co  m
public static Marshaller createMarshaller(Class<?> clazz, String encoding) {
    try {
        JAXBContext jaxbContext = getJaxbContext(clazz);

        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 ExceptionHelper.unchecked(e);
    }
}

From source file:org.osmsurround.ra.export.GpxExport.java

private void marshalData(GpxType gpxType, OutputStream os) {
    try {//from ww w  . j a v a2  s.co  m
        JAXBContext jaxbContext = JAXBContext.newInstance(GpxType.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(of.createGpx(gpxType), os);
    } catch (JAXBException e) {
        throw new AnalyzerException(e);
    }
}

From source file:$.JaxbMapper.java

/**
     * Marshallerencoding(?null)./* w  ww  . ja v a 2 s .c  om*/
     * ???pooling
     */
    public static Marshaller createMarshaller(Class clazz, String encoding) {
        try {
            JAXBContext jaxbContext = getJaxbContext(clazz);

            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:com.castlemock.web.basis.support.FileRepositorySupport.java

public <T> void save(T type, String filename) {
    Writer writer = null;//from   w w w  .  ja va  2 s .co  m
    try {
        JAXBContext context = JAXBContext.newInstance(type.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new FileWriter(filename);
        marshaller.marshal(type, writer);
    } catch (JAXBException e) {
        LOGGER.error("Unable to parse file: " + filename, e);
        throw new IllegalStateException("Unable to parse the following file: " + filename);
    } catch (IOException e) {
        LOGGER.error("Unable to read file: " + filename, e);
        throw new IllegalStateException("Unable to read the following file: " + filename);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                LOGGER.error("Unable to close file writer for type " + type.getClass().getSimpleName(), e);
            }
        }
    }
}