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.cbio.portal.pipelines.foundation.FoundationXmlGeneratorTasklet.java

/**
 * Converts instance of ClientCaseInfoType to XML document.
 * //from   w w w.j a v  a  2 s.  co  m
 * @param clientCaseInfo
 */
private void writeFoundationXmlDocument(ClientCaseInfoType clientCaseInfo) {
    String xmlData = null;
    StringWriter sw = new StringWriter();
    String outputFilename = outputDirectory + File.separator + cancerStudyIdentifier + "_merged.xml";
    try {
        JAXBContext context = JAXBContext.newInstance(clientCaseInfo.getClass());
        Marshaller jaxbMarshaller = context.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(clientCaseInfo, new File(outputFilename));
        xmlData = sw.toString();
        LOG.info("Merged Foundation XML document written to: " + outputFilename);
    } catch (JAXBException ex) {
        LOG.error("Error marshalling XML document from client case info object");
    }
}

From source file:controller.JaxbUtil.java

public Marshaller createMarshaller(String encoding) {
    try {//from   w ww.j av a2 s . c o m
        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 ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.px100systems.util.XmlParser.java

/**
 * Serialize the bean to XML/*from  w w w .  j  ava  2 s  . 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:org.osmsurround.ra.export.GpxExport.java

private void marshalData(GpxType gpxType, OutputStream os) {
    try {/*from ww w .  j av  a2  s  . c o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(GpxType.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(of.createGpx(gpxType), os);
    } catch (JAXBException e) {
        throw new AnalyzerException(e);
    }
}

From source file:com.savoirtech.jaxb.engine.MarshallerPool.java

@Override
public Object makeObject() throws Exception {
    LOG.debug("Creating a new marshaller");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setEventHandler(new ValidationChecker());
    marshaller.setProperty("jaxb.encoding", "UTF-8");
    return marshaller;
}

From source file:com.mijao.poc.jaxb.MarshallerPool.java

@Override
public Object makeObject() throws Exception {
    logger.debug("Creating a new marshaller");
    Marshaller marshaller = context.createMarshaller();
    marshaller.setEventHandler(new ValidationChecker());
    marshaller.setProperty("jaxb.encoding", "UTF-8");
    return marshaller;
}

From source file:com.zaubersoftware.gnip4j.http.JSONDeserializationTest.java

/** regression test for a NPE exception */
@Test/*  w w w  .j  av  a2  s . com*/
public void testNPE() throws Exception {
    final InputStream is = getClass().getClassLoader()
            .getResourceAsStream("com/zaubersoftware/gnip4j/payload/payload-twitter-entities.js");
    final InputStream expectedIs = getClass().getClassLoader()
            .getResourceAsStream("com/zaubersoftware/gnip4j/payload/payload-twitter-entities.xml");

    try {
        final String json = IOUtils.toString(is);
        final JsonParser parser = mapper.getJsonFactory().createJsonParser(json);
        final Activity activity = parser.readValueAs(Activity.class);
        final StringWriter w = new StringWriter();
        mapper.getJsonFactory().createJsonGenerator(w).writeObject(activity);

        final Marshaller o = ctx.createMarshaller();
        o.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter ww = new StringWriter();
        o.marshal(activity, ww);
        assertEquals(IOUtils.toString(expectedIs), ww.toString());
    } finally {
        is.close();
    }
}

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 w  w  w  . j av a2s  .c o  m*/
        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:com.castlemock.web.basis.support.FileRepositorySupport.java

public <T> void save(T type, String filename) {
    Writer writer = null;/*from   w  w w  .j  a  v a2s .  c o m*/
    try {
        JAXBContext context = JAXBContext.newInstance(type.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        writer = new FileWriter(filename);
        marshaller.marshal(type, writer);
    } catch (JAXBException e) {
        LOGGER.error("Unable to parse file: " + filename, e);
        throw new IllegalStateException("Unable to parse the following file: " + filename);
    } catch (IOException e) {
        LOGGER.error("Unable to read file: " + filename, e);
        throw new IllegalStateException("Unable to read the following file: " + filename);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                LOGGER.error("Unable to close file writer for type " + type.getClass().getSimpleName(), e);
            }
        }
    }
}

From source file:com.quangphuong.crawler.util.XMLUtil.java

public <T> void marshallUtil(String xmlPath, T entityClass) {
    try {//from w w w. ja  va 2 s. c  o m
        JAXBContext cxt = JAXBContext.newInstance(entityClass.getClass());
        Marshaller marshaller = cxt.createMarshaller();
        marshaller.setProperty(marshaller.JAXB_FORMATTED_OUTPUT, true);
        File file = new File(rootPath, xmlPath);
        System.out.println("Path: " + file.getAbsolutePath());
        marshaller.marshal(entityClass, file);
    } catch (Exception e) {
        e.printStackTrace();
    }
}