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: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   w w w.j av a 2  s .  co  m
    }

    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());
            }
        }
    }
}

From source file:Main.java

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

        IOException {//ww w  .j  ava 2s . c  o m

    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:net.firejack.aws.license.LicenseHelper.java

public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException {
    signature(license);// w  ww .  ja  v  a  2s .  c o m

    File tmp = new File("/tmp/", license.getName() + ".xml");

    tmp.getParentFile().mkdirs();
    tmp.createNewFile();
    FileOutputStream stream = new FileOutputStream(tmp);

    JAXBContext jaxbContext = JAXBContext.newInstance(License.class);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(license, stream);
    stream.close();

    return tmp;
}

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);//  w  w w.  ja va2s  . com
}

From source file:Main.java

public static String jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) {

    // Make sure we are given the correct input.
    if (xmlClass == null || jaxbElement == null) {
        return null;
    }//from w w w.  j  a  va2  s. c o  m

    // We will write the XML encoding into a string.
    StringWriter writer = new StringWriter();
    String result;
    try {
        // We will use JAXB to marshal the java objects.
        final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);

        // Marshal the object.
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(jaxbElement, writer);
        result = writer.toString();
    } catch (Exception e) {
        // Something went wrong so get out of here.
        return null;
    } finally {
        try {
            writer.close();
        } catch (IOException ex) {
        }
    }

    // Return the XML string.
    return result;
}

From source file:Main.java

/**
 * Writes the content of the specified object into the specified XML file.
 * @param filename            Path to the XML file.
 * @param content            Content as an object of the specified class.
 * @param typeParameterClass   Class of the object with the content.
 * @return                  File.//from w w w. jav a2 s .  co  m
 */
public static <T> File write(File file, T content, Class<T> typeParameterClass) {
    try {
        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);

        jaxbMarshaller.marshal(content, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

/**
 * Return the xml translation of an object
 * // w  w  w  .j  a va 2 s. c o  m
 * @param cls
 * @param entity
 * @return
 */
public static String objectToXML(Class<?> cls, Object entity) {

    try {
        Marshaller m = JAXBContext.newInstance(cls).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        m.marshal(entity, sw);
        return sw.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Method to write an XML object in the given filePath.
 * //from ww w  . j  a va 2s. c om
 * @param <S>
 *            The type of object that needs to written in XML file.
 * @param filePath
 *            Path of file where xml object gets written
 * @param object
 *            The Object that needs to be written in XML file.
 * @throws Exception
 */
public static <S> void writeXMLObject(String filePath, S object) throws Exception {
    // java XML context object.
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    // file.
    File file = new File(filePath);
    // Creating marshaller
    Marshaller marshaller = jc.createMarshaller();
    // Setting output format
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // Marshalling object.
    marshaller.marshal(object, file);
}

From source file:org.vincibean.salestaxes.jaxb.JaxbFactory.java

/**
 * Factory method, creates a {@link Marshaller} from the context given in the constructor; moreover, ensure that
 * the marshalled XML data is formatted with linefeeds and indentation.  
 * @return an {@link Optional} object which may or may not contain a {@link Marshaller}
 *//*from  w  ww  .  j  a  v a 2 s. c  o  m*/
public static Optional<Marshaller> createMarshaller(final Class<?> context) {
    try {
        Marshaller marshaller = JAXBContext.newInstance(context).createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setSchema(SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
                .newSchema(new ClassPathResource("receipt/Poiuyt.xsd").getFile()));
        marshaller.setEventHandler(new FoobarValidationEventHandler());
        return Optional.fromNullable(marshaller);
    } catch (JAXBException | SAXException | IOException e) {
        logger.warn("Exception on jaxb factory creation: ", e);
        return Optional.absent();
    }
}

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

public static void writeAudioMapToFile(File outputFile, AudioMapType audioMap) {
    JAXBContext jaxbContext;//  ww  w  .  j  a  v a  2  s  .  co 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);
    }
}