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:org.pmedv.jake.JakeUtil.java

/**
 * Updates the {@link RecentFileList} with a new file
 * /*from w  w w .j a v a 2s  .c  om*/
 * @param filename the name to append to the list
 */
public static void updateRecentFiles(String filename) {

    RecentFileList fileList = null;

    try {

        String inputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String inputFileName = "recentFiles.xml";
        File inputFile = new File(inputDir + inputFileName);

        if (inputFile.exists()) {
            Unmarshaller u = JAXBContext.newInstance(RecentFileList.class).createUnmarshaller();
            fileList = (RecentFileList) u.unmarshal(inputFile);
        }

        if (fileList == null)
            fileList = new RecentFileList();

    } catch (JAXBException e) {
        e.printStackTrace();
    }

    if (fileList.getRecentFiles().size() >= 5) {
        fileList.getRecentFiles().remove(0);
    }

    if (!fileList.getRecentFiles().contains(filename))
        fileList.getRecentFiles().add(filename);

    Marshaller m;

    try {
        String outputDir = System.getProperty("user.home") + "/." + AppContext.getName() + "/";
        String outputFileName = "recentFiles.xml";
        m = JAXBContext.newInstance(RecentFileList.class).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        File output = new File(outputDir + outputFileName);
        m.marshal(fileList, output);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:com.athena.peacock.controller.common.component.RHEVMRestTemplate.java

@SuppressWarnings({ "unchecked", "rawtypes" })
private static String marshal(Object obj, String rootElementName) {
    StringWriter sw = new StringWriter();
    try {/*from   www .  j  a  v a2  s .com*/
        JAXBContext jaxbCtx = JAXBContext.newInstance(obj.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(new JAXBElement(new QName(rootElementName), obj.getClass(), obj), sw);
        sw.close();

    } catch (PropertyException e) {
        e.printStackTrace();
    } catch (JAXBException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return sw.toString();
}

From source file:org.biopax.validator.api.ValidatorUtils.java

/**
 * Gets the results Marshaller (for validation results.)
 * @return//from www .j a  v a2s.c  o  m
 */
public static Marshaller getMarshaller() {
    //return resultsMarshaller;
    try {
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        return marshaller;
    } catch (JAXBException e) {
        throw new RuntimeException("Failed to create Marshaller", e);
    }
}

From source file:Main.java

private static Marshaller getJaxbMarshaller(Class<?> classesToBeBound, Map<String, Object> marshallerProps)
        throws Exception {
    Marshaller marshaller = JAXB_MARSHALLER_CACHE.get(classesToBeBound);
    if (marshaller == null) {
        JAXBContext jaxbContext = JAXBContext.newInstance(classesToBeBound);
        marshaller = jaxbContext.createMarshaller();
        if (marshallerProps != null && marshallerProps.size() > 0) {
            for (Map.Entry<String, Object> prop : marshallerProps.entrySet()) {
                marshaller.setProperty(prop.getKey(), prop.getValue());
            }/*  w  w w.  ja v  a 2 s  . com*/
        }
        JAXB_MARSHALLER_CACHE.put(classesToBeBound, marshaller);
    }
    return marshaller;
}

From source file:com.comcast.cats.service.util.HttpClientUtil.java

public static synchronized byte[] getPayload(Object domain, boolean prettyPrint) {
    byte[] payload = null;

    String xml = null;/*from w  w  w . j  a  v  a  2 s .c o  m*/

    StringWriter writer = new StringWriter();

    try {
        JAXBContext context = JAXBContext.newInstance(domain.getClass());

        Marshaller marshaller = context.createMarshaller();

        if (prettyPrint) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }

        marshaller.marshal(domain, writer);
        xml = writer.toString();
    } catch (JAXBException e) {
        logger.error("[ JAXBException   ] " + e.getMessage());
    }

    logger.trace("[ PAYLOAD  ] " + xml);

    if (null != xml) {
        payload = xml.getBytes();
    }

    return payload;
}

From source file:com.netxforge.oss2.core.xml.JaxbUtils.java

public static Marshaller getMarshallerFor(final Object obj, final JAXBContext jaxbContext) {
    final Class<?> clazz = (Class<?>) (obj instanceof Class<?> ? obj : obj.getClass());

    Map<Class<?>, Marshaller> marshallers = m_marshallers.get();
    if (jaxbContext == null) {
        if (marshallers == null) {
            marshallers = new WeakHashMap<Class<?>, Marshaller>();
            m_marshallers.set(marshallers);
        }/*from www.  j  ava 2 s. c o  m*/
        if (marshallers.containsKey(clazz)) {
            LogUtils.tracef(clazz, "found unmarshaller for %s", clazz);
            return marshallers.get(clazz);
        }
    }
    LogUtils.tracef(clazz, "creating unmarshaller for %s", clazz);

    try {
        final JAXBContext context;
        if (jaxbContext == null) {
            context = getContextFor(clazz);
        } else {
            context = jaxbContext;
        }
        final Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        final Schema schema = getValidatorFor(clazz);
        marshaller.setSchema(schema);
        if (jaxbContext == null)
            marshallers.put(clazz, marshaller);

        return marshaller;
    } catch (JAXBException e) {
        throw EXCEPTION_TRANSLATOR.translate("creating XML marshaller", e);
    }
}

From source file:com.kcs.core.utilities.Utility.java

public static OutputStream generate(Object jaxbElement, OutputStream output) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbElement.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(jaxbElement, output);
    return output;
}

From source file:eu.squadd.reflections.mapper.ServiceModelTranslator.java

public static String marshallToString(Class sourceClass, Object source) {
    try {//from  www. ja  v a2s . c  om
        JAXBContext jaxbContext = JAXBContext.newInstance(sourceClass);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException ex) {
        System.err.println(ex.getMessage());
        return null;
    }
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static Marshaller getMarshaller(boolean isFragment, Class... docClass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(docClass);
    Marshaller m = context.createMarshaller();
    m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, isFragment);
    return m;/*from   ww  w.j a  v  a2 s . co m*/
}

From source file:com.jaspersoft.jasperserver.rest.RESTUtils.java

public static Marshaller getMarshaller(Class... docClass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(docClass);
    Marshaller m = context.createMarshaller();
    m.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(javax.xml.bind.Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    return m;/*from   ww  w . j  a v  a  2  s.c o  m*/
}