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:de.xirp.profile.ProfileGenerator.java

/**
 * Generates a BOT file with the given path from the given 
 * {@link de.xirp.profile.CommunicationSpecification comm-spec} 
 * bean.//from   w w  w. j  a v a  2s .c  o  m
 * 
 * @param commSpec
 *          The comm-spec to generate the XML for. 
 * @param cmsFile
 *          The file to write the result to. Must be the full path.
 * 
 * @throws JAXBException if the given comm-spec was null, or something 
 *                    went wrong generating the xml.
 * @throws FileNotFoundException if something went wrong generating the xml.
 */
public static void generateCMS(CommunicationSpecification commSpec, File cmsFile)
        throws JAXBException, FileNotFoundException {

    if (commSpec == null) {
        throw new JAXBException(I18n.getString("ProfileGenerator.exception.comSpecNull")); //$NON-NLS-1$
    }

    String fileName = FilenameUtils.getBaseName(cmsFile.getName());

    JAXBContext jc = JAXBContext.newInstance(CommunicationSpecification.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(commSpec, new FileOutputStream(
            Constants.CONF_COMMSPECS_DIR + File.separator + fileName + Constants.COMM_SPEC_POSTFIX));
}

From source file:gov.nih.nci.coppa.services.client.ClientUtils.java

private static void printXml(Object obj) throws JAXBException {
    JAXBContext jaxbContext = MAP.get(obj.getClass().getPackage().getName());
    if (jaxbContext == null) {
        jaxbContext = JAXBContext.newInstance(obj.getClass().getPackage().getName());
        MAP.put(obj.getClass().getPackage().getName(), jaxbContext);
    }//  w w w  .j ava2  s .  com
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(obj, System.out);
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//from  www.  j  a v a 2  s  . c  o  m
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        //         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        //         marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

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

public static String asXmlString(Object obj, String contextPath) throws JAXBException {

    StringWriter w = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(contextPath);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(obj, w);//  www  . ja  v a 2s .  c o m

    return w.toString();
}

From source file:com.common.util.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null).//from   w w w .  ja  v  a2 s. c  om
 * ???pooling
 */
public static Marshaller createMarshaller(Class clazz, String encoding) throws JAXBException {
    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;

}

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

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

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

    StringWriter w = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(list.toArray(new Class[list.size()]));

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

    return w.toString();
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

/**
 * Write XML entity to the given destination.
 * /*from w  w  w  .  j a v  a2 s  .  com*/
 * @param entity
 *            XML entity
 * @param destination
 *            destination to write to. Supported destinations: {@link java.io.OutputStream}, {@link java.io.File},
 *            {@link java.io.Writer}
 * @param comment
 *            optional comment which will be added at the begining of the generated XML
 * @throws IllegalArgumentException
 * @throws SwidException
 * @param <T>
 *            JAXB entity
 */
public static <T> void writeObject(final T entity, final Object destination, final String comment) {
    try {
        JAXBContext jaxbContext;
        if (entity instanceof JAXBElement) {
            jaxbContext = JAXBContext.newInstance(((JAXBElement) entity).getValue().getClass());
        } else {
            jaxbContext = JAXBContext.newInstance(entity.getClass());
        }

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        if (StringUtils.isNotBlank(comment)) {
            marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", comment);
        }

        if (destination instanceof java.io.OutputStream) {
            marshaller.marshal(entity, (OutputStream) destination);
        } else if (destination instanceof java.io.File) {
            marshaller.marshal(entity, (java.io.File) destination);
        } else if (destination instanceof java.io.Writer) {
            marshaller.marshal(entity, (java.io.Writer) destination);
        } else {
            throw new IllegalArgumentException("Unsupported destination.");
        }
    } catch (final JAXBException e) {
        throw new SwidException("Cannot write object.", e);
    }
}

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  w w  .  ja va 2s  .  c  o m

    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:com.topsem.common.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null)./* ww  w .  jav a 2  s.c  o m*/
 * ???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 Throwables.propagate(e);
    }
}

From source file:com.wsun.seap.common.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null).//from www.  java2  s . c  o m
 * ???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 ExceptionUtil.unchecked(e);
    }
}