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.px100systems.util.XmlParser.java

/**
 * Serialize the bean to XML// w  w w .  j ava  2s .  c o m
 * @param bean teh bean to serialize
 * @return XML
 */
public String write(T bean) {
    StringBuilder result = new StringBuilder();
    Writer writer = new StringBuilderWriter(result);
    try {
        Marshaller m = jaxb.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(bean, writer);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(writer);
    }
    return result.toString();
}

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

/**
 * Write XML entity to the given destination.
 * /*from   www.  j  a  va  2 s . c om*/
 * @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:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java

/**
 * Write the {@link AForm} to xml file.//from ww w  . j  a v  a 2  s. co  m
 * 
 * @param form the {@link AForm} object.
 * @param file the file to which to dump to. 
 * @throws Exception
 */
public static void writeXML(AForm form, File file) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(AForm.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(form, file);
}

From source file:net.sourceforge.subsonic.controller.JAXBWriter.java

private Marshaller createXmlMarshaller() throws JAXBException {
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StringUtil.ENCODING_UTF8);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    return marshaller;
}

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

/**
 * Marshallerencoding(?null)./*from w ww. ja  va 2s  .  co  m*/
 * ???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:com.androidwhy.modules.mapper.JaxbMapper.java

/**
 * Marshallerencoding(?null).//from ww  w .  j ava  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);
        }

        return marshaller;
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

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

/**
 * Marshallerencoding(?null).//from  w  w  w . j a v a  2s.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 ExceptionUtils.unchecked(e);
    }
}

From source file:com.biomeris.i2b2.export.ws.messages.MessageBuilder.java

public static String buildPMGetServiceRequest(Network network) throws JAXBException {
    com.biomeris.i2b2.export.datavo.pm.ObjectFactory pmObjectFactory = new com.biomeris.i2b2.export.datavo.pm.ObjectFactory();
    com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory extObjectFactory = new com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory();

    ProxyedRequestMessageType proxyedRequestMessageType = new ProxyedRequestMessageType();

    Proxy proxy = new Proxy();
    proxy.setRedirectUrl(network.getPmServiceAddress() + PMSERVICE_NAME);
    proxyedRequestMessageType.setProxy(proxy);

    MessageHeaderType messageHeaderType = new MessageHeaderType();
    SecurityType securityType = new SecurityType();
    securityType.setDomain(network.getDomain());
    securityType.setUsername(network.getUsername());
    PasswordType passwordType = JAXB.unmarshal(new StringReader(network.getPassword()), PasswordType.class);
    securityType.setPassword(passwordType);
    messageHeaderType.setSecurity(securityType);
    messageHeaderType.setProjectId(network.getProject());
    ApplicationType applicationType = new ApplicationType();
    applicationType.setApplicationName("Export Cell");
    applicationType.setApplicationVersion("1.0");
    messageHeaderType.setSendingApplication(applicationType);
    proxyedRequestMessageType.setMessageHeader(messageHeaderType);

    RequestHeaderType requestHeaderType = new RequestHeaderType();
    requestHeaderType.setResultWaittimeMs(1800000);
    proxyedRequestMessageType.setRequestHeader(requestHeaderType);

    BodyType bodyType = new BodyType();

    GetUserConfigurationType g = pmObjectFactory.createGetUserConfigurationType();
    g.getProject().add("undefined");
    JAXBElement<GetUserConfigurationType> any1 = pmObjectFactory.createGetUserConfiguration(g);

    bodyType.getAny().add(any1);// w w w .  j a v  a  2 s . c o m
    proxyedRequestMessageType.setMessageBody(bodyType);

    JAXBContext jc = JAXBContext.newInstance(ProxyedRequestMessageType.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter sw = new StringWriter();
    JAXBElement<ProxyedRequestMessageType> xxx = extObjectFactory.createRequestPM(proxyedRequestMessageType);
    m.marshal(xxx, sw);

    return sw.toString();
}

From source file:actions.AddStudent.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    ActionForward findForward = mapping.findForward("main");

    StudentForm formStudent = (StudentForm) request.getAttribute("studentForm");

    Student newStudent = new Student();
    BeanUtils.copyProperties(newStudent, formStudent);

    System.out.println("newStudent.firstName=" + newStudent.getFirstName());
    System.out.println("newStudent.lastName=" + newStudent.getLastName());
    System.out.println("newStudent.studentId=" + newStudent.getStudentId());
    System.out.println("newStudent.dob=" + newStudent.getDob());

    //Student.fileWrite(null, newStudent.fileOutputString());
    boolean successAdd = newStudent.saveStudent();
    Student.getStudents().put(newStudent.getStudentId(), newStudent);

    ActionMessages messages = new ActionMessages();
    if (successAdd) {
        messages.add("message1", (new ActionMessage("label.student.added.successfully")));
    } else {/*from ww  w  .j  av a 2  s  .  c om*/
        messages.add("error", (new ActionMessage("label.student.added.error")));
    }
    saveMessages(request, messages);

    //**********************************************************************
    //**********************************************************************
    //**********************************************************************
    //http://www.vogella.com/tutorials/JAXB/article.html
    // create JAXB context and instantiate marshaller
    JAXBContext context = JAXBContext.newInstance(Student.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    // Write to System.out
    StringWriter sw = new StringWriter();
    m.marshal(newStudent, sw);
    String xmlStudent = sw.toString();
    System.out.println("xmlEncodedStudent=" + xmlStudent);
    //**********************************************************************
    //**********************************************************************

    //Unmarshall back for testing
    Unmarshaller um = context.createUnmarshaller();
    Student backStudent = (Student) um.unmarshal(new StringReader(xmlStudent));
    System.out.println("Student back from xml:" + backStudent.toString());

    //**********************************************************************
    //**********************************************************************
    //**********************************************************************
    return findForward;

}

From source file:de.xirp.profile.ProfileGenerator.java

/**
 * Generates a PRO file with the given path from the given 
 * {@link de.xirp.profile.Profile profile} bean.
 * /*from www.  j a  v a  2s.  c o  m*/
 * @param profile
 *          The profile to generate the XML for.        
 * @param proFile
 *          The file to write the result to. Must be the full path.
 *            
 * @throws JAXBException if the given profile was null, or something 
 *                    went wrong generating the xml.
 * @throws FileNotFoundException if something went wrong generating the xml.
 * 
 * @see de.xirp.profile.Profile
 */
public static void generatePRO(Profile profile, File proFile) throws JAXBException, FileNotFoundException {

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

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

    JAXBContext jc = JAXBContext.newInstance(Profile.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(profile, new FileOutputStream(
            Constants.CONF_PROFILES_DIR + File.separator + fileName + Constants.PROFILE_POSTFIX));
}