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:Main.java

public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    String pkg = o.getClass().getPackage().getName();
    JAXBContext jc = getCachedContext(pkg);
    Marshaller m = jc.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(o, os);//from   www  . j  av  a 2s. c  om
}

From source file:Main.java

public static void saveInstance(OutputStream outputStream, URL schemaURL, String schemaName, Object instance)
        throws JAXBException,

        IOException {/*  w  w  w.ja  v a  2  s. c om*/

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaURL + " " + schemaName);

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

    marshaller.marshal(instance, outputStream);

    outputStream.flush();

}

From source file:eu.planets_project.tb.impl.serialization.ExperimentRecords.java

/**
 * @param exp/*from   w w w .  j  a  v a  2  s. c o m*/
 * @param out
 */
private static void writeToOutputStream(ExperimentRecords exp, OutputStream out) {
    try {
        JAXBContext jc = JAXBContext.newInstance(ExperimentRecords.class);
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(exp, out);
    } catch (JAXBException e) {
        log.fatal("Writing Experiments to XML failed: " + e);
    }
}

From source file:com.netflix.imfutility.dpp.audio.AudioMapHelper.java

public static void writeAudioMapToFile(File outputFile, AudioMapType audioMap) {
    JAXBContext jaxbContext;//from   w  w  w .  ja va 2 s .  c  o  m
    try {
        jaxbContext = JAXBContext.newInstance(AudioMapType.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        JAXBElement<AudioMapType> audioMapJaxb = new ObjectFactory().createAudioMap(audioMap);
        jaxbMarshaller.marshal(audioMapJaxb, outputFile);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * genXml/* w  w w . j ava2s.c  o m*/
 * 
 * @param o Object Class.
 * @param path : example c:\path\006DS_AMS20130630.xml
 */
public static void genXml(Object o, String path) {
    try {

        // create JAXB context and initializing Marshaller
        JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // for getting nice formatted output
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        //specify the location and name of xml file to be created
        File XMLfile = new File(path);

        // Writing to XML file
        jaxbMarshaller.marshal(o, XMLfile);
        // Writing to console
        jaxbMarshaller.marshal(o, System.out);

    } catch (JAXBException e) {
        // some exception occured
        e.printStackTrace();
    }
}

From source file:com.htmlhifive.tools.jslint.engine.option.xml.JaxbUtil.java

/**
 * JsCheckOptionsxml????.// w  ww.j a  v a2 s. c o  m
 * 
 * 
 * @param checkOptions .
 * @param output .
 */
public static void saveJsCheckOption(JsCheckOption checkOptions, IFile output) {
    try {
        Marshaller marshall = jc.createMarshaller();
        marshall.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.valueOf(true));
        StringWriter writer = new StringWriter();
        marshall.marshal(checkOptions, writer);
        if (output.exists()) {
            output.setContents(IOUtils.toInputStream(writer.toString(), "UTF-8"), IResource.FORCE, null);
        } else {
            output.create(IOUtils.toInputStream(writer.toString(), "UTF-8"), true, null);
        }
        output.refreshLocal(IResource.DEPTH_ONE, null);
    } catch (JAXBException e) {
        logger.put(Messages.EM0006, e, output.getName());
    } catch (CoreException e) {
        logger.put(Messages.EM0100, e);
    } catch (IOException e) {
        logger.put(Messages.EM0006, e, output.getName());
    }
}

From source file:Main.java

public static void marshal(Object o, OutputStream os, Map<String, Object> properties) {
    try {/*from   ww w  .j  a  v a  2  s. c  om*/
        JAXBContext ctx = JAXBContext.newInstance(o.getClass());
        Marshaller marshaller = ctx.createMarshaller();

        for (Entry<String, Object> p : properties.entrySet()) {
            marshaller.setProperty(p.getKey(), p.getValue());
        }

        marshaller.marshal(o, os);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:eu.seaclouds.platform.planner.core.application.agreements.AgreementGenerator.java

private static String serializeToXml(MonitoringInfo monitoringInfo) {
    StringWriter sw = new StringWriter();
    JAXBContext jaxbContext;/*w w  w.ja v  a2  s.co m*/
    String marshalledMonitoringRules = null;
    try {
        jaxbContext = JAXBContext.newInstance(MonitoringRules.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        jaxbMarshaller.marshal(monitoringInfo.getApplicationMonitoringRules(), sw);
        marshalledMonitoringRules = sw.toString();
    } catch (JAXBException e) {
        log.error("Monitoring rules {} can not be marshalled by addSeaCloudsPolicy in " + "DamGenerator",
                monitoringInfo.getApplicationMonitoringRules());
    }

    return marshalledMonitoringRules;
}

From source file:Main.java

public static <T> String marshal(T object) throws JAXBException {
    final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller();
    final StringWriter stringWriter = new StringWriter();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, stringWriter);

    return stringWriter.toString();
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static void marshal(Object object, Writer w) throws JAXBException {
    Class clazz = object.getClass();
    JAXBContext context = s_contexts.get(clazz);
    if (context == null) {
        context = JAXBContext.newInstance(clazz);
        s_contexts.put(clazz, context);//from   ww w .  j  a va2 s .  com
    }

    ValidationEventCollector valEventHndlr = new ValidationEventCollector();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setSchema(null);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setEventHandler(valEventHndlr);

    try {
        marshaller.marshal(object, w);
    } catch (Exception e) {
        if (e instanceof JAXBException) {
            throw (JAXBException) e;
        } else {
            throw new MarshalException(e.getMessage(), e);
        }
    }
    if (valEventHndlr.hasEvents()) {
        for (ValidationEvent valEvent : valEventHndlr.getEvents()) {
            if (valEvent.getSeverity() != ValidationEvent.WARNING) {
                // throw a new Marshall Exception if there is a parsing error
                throw new MarshalException(valEvent.getMessage(), valEvent.getLinkedException());
            }
        }
    }
}