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: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);/* w ww.j a v a2s  .co m*/

    return w.toString();
}

From source file:org.bremersee.sms.test.ModelTests.java

@Test
public void testXmlSmsSendResponseDto() throws Exception {

    System.out.println("Testing XML SmsSendResponseDto ...");

    GoyyaSmsSendResponseDto goyyaResponse = new GoyyaSmsSendResponseDto("OK");

    SmsSendResponseDto response = new SmsSendResponseDto(new SmsSendRequestDto("bremersee", "0123456789",
            "Hello", new Date(System.currentTimeMillis() + 30000L)), goyyaResponse.isOk(), goyyaResponse);

    ByteArrayOutputStream out = new ByteArrayOutputStream();

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

    m.marshal(response, out);/*from w  ww  .  j  av a 2s .  co  m*/

    String xmlStr = new String(out.toByteArray(), "UTF-8");

    System.out.println(xmlStr);

    SmsSendResponseDto readResponse = (SmsSendResponseDto) jaxbContext.createUnmarshaller()
            .unmarshal(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));

    m.marshal(readResponse, System.out);

    GoyyaSmsSendResponseDto tmp = ExtensionUtils.transform(readResponse.getExtension(),
            GoyyaSmsSendResponseDto.class, jaxbContext, new ObjectMapper());
    readResponse.setExtension(tmp);

    TestCase.assertEquals(response, readResponse);

    System.out.println("OK\n");
}

From source file:org.axiom_tools.codecs.ModelCodec.java

/**
 * Converts a model to XML.// w w w . ja  v  a  2 s  . com
 * @return model XML, or empty
 */
public String toXML() {
    try {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        Marshaller m = buildJAXBContext().createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.marshal(this.entity, stream);
        return stream.toString(XML_ENCODING).trim();
    } catch (Exception e) {
        Log.error(e.getMessage(), e);
        return Empty;
    }
}

From source file:fr.fastconnect.factory.tibco.bw.maven.compile.ArchiveBuilder.java

public void save(File f) {
    try {/*from w w w  . ja  v a 2 s . co m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Marshaller m = jaxbContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        m.marshal(this.repository, f);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:com.manydesigns.mail.queue.FileSystemMailQueue.java

public String enqueue(Email email) throws QueueException {
    try {//from   www.ja va2s  .co  m
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        String emailId = RandomUtil.createRandomId(20);
        File destinationFile = getEmailFile(emailId);
        checkDirectory(queuedDirectory);
        if (!email.getAttachments().isEmpty()) {
            File attachDir = getEmailAttachmentsDirectory(emailId);
            checkDirectory(attachDir);
            for (Attachment attachment : email.getAttachments()) {
                String attachmentId = RandomUtil.createRandomId(20);
                File attachmentFile = new File(attachDir, attachmentId + ".bin");
                FileOutputStream fos = new FileOutputStream(attachmentFile);
                IOUtils.copy(attachment.getInputStream(), fos);
                IOUtils.closeQuietly(fos);
                IOUtils.closeQuietly(attachment.getInputStream());
                attachment.setFilePath(attachmentFile.getAbsolutePath());
            }
        }
        marshaller.marshal(email, destinationFile);
        return emailId;
    } catch (Exception e) {
        throw new QueueException("Couldn't enqueue mail", e);
    }
}

From source file:gov.nih.nci.cabig.caaers.web.study.ExportStudyController.java

@Override
protected ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object o,
        BindException e) throws Exception {

    Integer studyID = Integer.valueOf(request.getParameter("id"));
    Study study = studyDao.getById(studyID);
    studyDao.initialize(study);/*from  w w w.jav a  2  s. c  o m*/

    // START study export pre-population
    study.setCoordinatingCenter(new CoordinatingCenter());
    study.getCoordinatingCenter().setStudyCoordinatingCenter(study.getStudyCoordinatingCenter());

    study.setFundingSponsor(new FundingSponsor());
    study.getFundingSponsor().setStudyFundingSponsor(study.getStudyFundingSponsors().get(0));

    for (OrganizationAssignedIdentifier id : study.getOrganizationAssignedIdentifiers()) {
        if (id.getOrganization().equals(study.getFundingSponsor().getStudyFundingSponsor().getOrganization())) {
            study.getFundingSponsor().setOrganizationAssignedIdentifier(id);
            study.getFundingSponsor().getOrganizationAssignedIdentifier().setPrimaryIndicator(true);
            break;
        }
    }
    for (OrganizationAssignedIdentifier id : study.getOrganizationAssignedIdentifiers()) {
        if (id.getOrganization()
                .equals(study.getCoordinatingCenter().getStudyCoordinatingCenter().getOrganization())) {
            study.getCoordinatingCenter().setOrganizationAssignedIdentifier(id);
            study.getCoordinatingCenter().getOrganizationAssignedIdentifier().setPrimaryIndicator(false);
            break;
        }
    }
    // END study export pre-population

    gov.nih.nci.cabig.caaers.integration.schema.study.Studies studies = converter
            .convertStudyDomainToStudyDto(study);

    //Marshall the Data Transfer Object according to Study.xsd schema,
    //and download it to the client machine.
    try {
        String tempDir = System.getProperty("java.io.tmpdir");
        String fileName = "ExportedStudy_" + study.getPrimaryIdentifierValue();
        fileName = RuleUtil.getStringWithoutSpaces(fileName);

        StringWriter sw = new StringWriter();
        JAXBContext jaxbContext = JAXBContext.newInstance("gov.nih.nci.cabig.caaers.integration.schema.study");

        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(studies, sw);
        BufferedWriter out = new BufferedWriter(new FileWriter(tempDir + fileName + ".xml"));
        out.write(sw.toString());
        out.flush();
        out.close();

        response.setContentType("application/xml");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".xml");
        response.setHeader("Content-length", String.valueOf(sw.toString().length()));
        response.setHeader("Pragma", "private");
        response.setHeader("Cache-control", "private, must-revalidate");

        OutputStream outputStream = response.getOutputStream();
        File file = new File(tempDir + fileName + ".xml");
        FileInputStream fileIn = new FileInputStream(file);
        byte[] buffer = new byte[2048];
        int bytesRead = fileIn.read(buffer);
        while (bytesRead >= 0) {
            if (bytesRead > 0)
                outputStream.write(buffer, 0, bytesRead);
            bytesRead = fileIn.read(buffer);
        }
        outputStream.flush();
        outputStream.close();
        fileIn.close();

    } catch (Exception ex) {
        log.error(ex);
        ex.printStackTrace();
    }
    return null;
}

From source file:it.cnr.icar.eric.client.xml.registry.infomodel.IdentifiableImpl.java

public String toXML() throws JAXRException {
    try {/*w  ww  . jav  a 2  s  . c  o m*/
        StringWriter sw = new StringWriter();
        Marshaller marshaller = BindingUtility.getInstance().getJAXBContext().createMarshaller();
        marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        JAXBElement<IdentifiableType> ebIdentifiable = bu.rimFac
                .createIdentifiable((IdentifiableType) toBindingObject());
        marshaller.marshal(ebIdentifiable, sw);

        return sw.toString();
    } catch (javax.xml.bind.JAXBException e) {
        throw new JAXRException(e);
    }
}

From source file:com.rapid.server.ActionCache.java

public ActionCache(ServletContext servletContext) throws JAXBException {

    // retain servletContext
    _servletContext = servletContext;/*from   www . j  av a  2  s  .  c  om*/

    // create the JAXB context and marshalers for this
    JAXBContext jaxb = JAXBContext.newInstance(Cache.class);
    _marshaller = jaxb.createMarshaller();
    _marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    _unmarshaller = jaxb.createUnmarshaller();

    // initialise the map of all application caches
    _applicationCaches = new HashMap<String, Cache>();

}

From source file:ee.ria.xroad.proxy.clientproxy.MetadataClientRequestProcessor.java

private static void marshal(Object object, OutputStream out) throws Exception {
    Marshaller marshaller = JAXB_CTX.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(object, out);/*w  w w.  j a  v  a 2 s  . co m*/
}

From source file:com.u2apple.tool.dao.DeviceXmlDaoJaxbImpl.java

private void flushDevices() throws JAXBException, PropertyException {
    if (isDeviceChanged) {
        File file = new File(Configuration.getProperty(Constants.DEVICES_XML));
        JAXBContext jaxbContext = JAXBContext.newInstance(StaticMapFile.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(getStaticMapFile(), file);
        isDeviceChanged = false;/* w  ww . ja  v  a 2 s .  c  o m*/
    }
}