Example usage for javax.xml.bind Marshaller JAXB_FRAGMENT

List of usage examples for javax.xml.bind Marshaller JAXB_FRAGMENT

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller JAXB_FRAGMENT.

Prototype

String JAXB_FRAGMENT

To view the source code for javax.xml.bind Marshaller JAXB_FRAGMENT.

Click Source Link

Document

The name of the property used to specify whether or not the marshaller will generate document level events (ie calling startDocument or endDocument).

Usage

From source file:de.tudarmstadt.ukp.dkpro.core.io.tiger.TigerXmlWriter.java

@Override
public void process(JCas aJCas) throws AnalysisEngineProcessException {
    OutputStream docOS = null;/*  www.  ja va  2 s .c  o m*/
    try {
        docOS = getOutputStream(aJCas, filenameSuffix);

        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLEventWriter xmlEventWriter = new IndentingXMLEventWriter(
                xmlOutputFactory.createXMLEventWriter(docOS));

        JAXBContext context = JAXBContext.newInstance(TigerSentence.class);
        Marshaller marshaller = context.createMarshaller();
        // We use the marshaller only for individual sentences. That way, we do not have to 
        // build the whole TIGER object graph before seralizing, which should safe us some
        // memory.
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

        XMLEventFactory xmlef = XMLEventFactory.newInstance();
        xmlEventWriter.add(xmlef.createStartDocument());
        xmlEventWriter.add(xmlef.createStartElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createStartElement("", "", "body"));

        int sentenceNumber = 1;
        for (Sentence s : select(aJCas, Sentence.class)) {
            TigerSentence ts = convertSentence(s, sentenceNumber);
            marshaller.marshal(new JAXBElement<TigerSentence>(new QName("s"), TigerSentence.class, ts),
                    xmlEventWriter);
            sentenceNumber++;
        }

        xmlEventWriter.add(xmlef.createEndElement("", "", "body"));
        xmlEventWriter.add(xmlef.createEndElement("", "", "corpus"));
        xmlEventWriter.add(xmlef.createEndDocument());
    } catch (Exception e) {
        throw new AnalysisEngineProcessException(e);
    } finally {
        closeQuietly(docOS);
    }
}

From source file:com.redhat.akashche.wixgen.dir.DirectoryGeneratorTest.java

@Test
public void test() throws Exception {
    // emulate externally provided conf file
    String json = GSON.toJson(ImmutableMap.builder().put("appName", "Test Wix Application")
            .put("versionMajor", "0").put("versionMinor", "1").put("versionPatch", "0")
            .put("vendor", "Test Vendor")
            .put("licenseFilePath", "src/test/resources/com/redhat/akashche/wixgen/dir/LICENSE.rtf")
            .put("iconPath", "src/test/resources/com/redhat/akashche/wixgen/dir/test_icon.ico")
            .put("topBannerBmpPath", "src/test/resources/com/redhat/akashche/wixgen/dir/top_banner.bmp")
            .put("greetingsBannerBmpPath",
                    "src/test/resources/com/redhat/akashche/wixgen/dir/greetings_banner.bmp")
            .put("registryKeys", ImmutableList.builder().add(ImmutableMap.builder().put("root", "HKCU")
                    .put("key", "Software\\Test Wix Application")
                    .put("values", ImmutableList.builder()
                            .add(ImmutableMap.builder().put("type", "string").put("name", "Application Name")
                                    .put("value", "Test Application").build())
                            .add(ImmutableMap.builder().put("type", "integer").put("name", "Version Minor")
                                    .put("value", "1").build())
                            .add(ImmutableMap.builder().put("type", "string").put("name", "Test Path")
                                    .put("value", "[INSTALLDIR]src\\test\\resources\\com").build())
                            .build())/*from w  w w .java  2s  . c o m*/
                    .build()).build())
            .put("environmentVariables", ImmutableList.builder()
                    .add(ImmutableMap.builder().put("name", "TEST_WIX_VAR").put("action", "create")
                            .put("value", "Test Wix Var Contents").build())
                    .add(ImmutableMap.builder().put("name", "PATH").put("action", "set")
                            .put("value", "[INSTALLDIR]src\\test\\resources\\com\\redhat").build())
                    .build())
            .build());
    WixConfig conf = GSON.fromJson(json, WixConfig.class);
    Wix wix = new DirectoryGenerator().createFromDir(new File("src"), conf);
    JAXBContext jaxb = JAXBContext.newInstance(Wix.class.getPackage().getName());
    Marshaller marshaller = jaxb.createMarshaller();
    marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
    Writer writer = null;
    try {
        OutputStream os = new FileOutputStream("target/test.wxs");
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        marshaller.marshal(wix, writer);
    } finally {
        closeQuietly(writer);
    }

    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    try {
        OutputStream os = new FileOutputStream("target/components.xml");
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        Directory dir = findWixDirectory(wix);
        marshaller.marshal(dir, writer);
    } finally {
        closeQuietly(writer);
    }

    try {
        OutputStream os = new FileOutputStream("target/feature.xml");
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        Feature dir = findWixFeature(wix);
        marshaller.marshal(dir, writer);
    } finally {
        closeQuietly(writer);
    }
}

From source file:eu.openminted.toolkit.elsevier.retriever.JaxbMarshalingTest.java

@Test
public void shouldSerialise() {
    Marshaller marshaller = null;
    try {//from   www  .  j  ava 2 s  .c o m
        marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        //            marshaller.setProperty("com.sun.xml.bind.xmlDeclaration", Boolean.FALSE);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

        Writer writer = new StringWriter();
        marshaller.marshal(testObject, writer);

        String serialised = writer.toString();
        //            serialised.replaceAll("<\\?xml version=\"1.0\" encoding=\"UTF\\-8\" standalone=\"yes\"\\?>", "");
        String testXmlAsString = FileUtils.readFileToString(new File("src/test/resources/S0140673616313228"),
                "UTF-8");

        System.out.println("serialised test object :\n" + serialised);
        System.out.println("test xml :\n" + testXmlAsString);

        //            Assert.assertEquals(asString, testXmlAsString);
    } catch (PropertyException ex) {
        ex.printStackTrace();
    } catch (JAXBException ex) {
        ex.printStackTrace();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedTypeAvailablityHAVE(String hospitalname) {
    String hospitalstatushave;/*ww  w  .j a v  a 2  s  .co m*/
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService
            .getHospitalAvailableBedTypes(hospitalname);
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return hospitalstatushave;

}

From source file:com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlistParser.java

private void marshallPlist(Plist plist, Writer projectFile) throws JAXBException {
    JAXBContext ctx = JAXBContext.newInstance(com.sap.prd.mobile.ios.mios.xcodeprojreader.jaxb.JAXBPlist.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    try {/*from www .j a v a 2  s.  c  o  m*/
        marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", xmlHeaders);
    } catch (PropertyException ex) {
        marshaller.setProperty("com.sun.xml.bind.xmlHeaders", xmlHeaders);
    }
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.marshal(plist, projectFile);
}

From source file:com.cognifide.aet.rest.XUnitServlet.java

private Marshaller prepareJaxbMarshaller() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Testsuites.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    return jaxbMarshaller;
}

From source file:eu.impress.repository.service.BedAvailabilityServiceImpl.java

@Override
public String getBedTypeAllAvailablityHAVE() {
    String hospitalstatushave;/*from  w ww . j  av  a2 s. c o  m*/
    List<eu.impress.repository.model.BedStats> bedStatsList = bedService.getHospitalAllAvailableBedTypes();
    HospitalStatus hospitalStatus = beansTransformation.BedTypesStatstoHAVE(bedStatsList);

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(HospitalStatus.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter sw = new StringWriter();
        jaxbMarshaller.marshal(hospitalStatus, sw);
        hospitalstatushave = sw.toString();

    } catch (JAXBException e) {
        e.printStackTrace();
        return "Error Marshalling XML Object";
    }

    return hospitalstatushave;

}

From source file:ee.ria.xroad.opmonitordaemon.QueryRequestHandler.java

static Marshaller createMarshaller(AttachmentMarshaller attachmentMarshaller) throws Exception {
    Marshaller marshaller = JAXB_CTX.createMarshaller();

    marshaller.setAttachmentMarshaller(attachmentMarshaller);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    return marshaller;
}

From source file:mx.bigdata.sat.cfdi.CFDv3.java

public void guardar(OutputStream out) throws Exception {
    Marshaller m = context.createMarshaller();
    m.setProperty("com.sun.xml.bind.namespacePrefixMapper", new NamespacePrefixMapperImpl(localPrefixes));
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
            "http://www.sat.gob.mx/cfd/3  " + "http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd");
    byte[] xmlHeaderBytes = XML_HEADER.getBytes("UTF8");
    out.write(xmlHeaderBytes);//from w  ww.j  a  v a2 s  .co  m
    m.marshal(document, out);
}

From source file:edu.isi.misd.scanner.network.worker.webapp.ResultsReleaseDelegate.java

private void writeRejectedServiceResponse(String id, String url, String siteName, String nodeName,
        String comments, File outputFile) throws Exception {
    // 1. Populate the rejected ServiceResponse
    ServiceResponse response = new ServiceResponse();
    ServiceResponseMetadata responseMetadata = new ServiceResponseMetadata();
    responseMetadata.setRequestID(id);/*from w  ww  .j  a  v a  2  s. c  o m*/
    responseMetadata.setRequestURL(url);
    responseMetadata.setRequestState(ServiceRequestStateType.REJECTED);
    responseMetadata.setRequestStateDetail("Reason: " + (comments.isEmpty() ? "not specified." : comments));
    responseMetadata.setRequestSiteName(siteName);
    responseMetadata.setRequestNodeName(nodeName);
    response.setServiceResponseMetadata(responseMetadata);

    // 2. Write out the result response using JAXB
    JAXBContext jaxbContext = JAXBContext.newInstance(ServiceResponse.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    if (outputFile.exists()) {
        try {
            FileUtils.forceDelete(outputFile);
        } catch (Exception e) {
            log.warn("Could not delete output file: " + outputFile.getCanonicalPath());
            throw e;
        }
    }
    jaxbMarshaller.marshal(response, outputFile);
}