Example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter

List of usage examples for javax.xml.stream XMLOutputFactory createXMLStreamWriter

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter.

Prototype

public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamWriter that writes to a JAXP result.

Usage

From source file:org.exoplatform.services.jcr.ext.artifact.ArtifactManagingServiceImpl.java

protected File createSingleMetadata(String groupId, String artifactId, String version)
        throws FileNotFoundException {
    File temp = null;//from   w w w.j  av a  2 s  .c o m
    try {
        String filename = getUniqueFilename("maven-metadata.xml");
        temp = File.createTempFile(filename, null);

        OutputStream os = new FileOutputStream(temp);
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = factory.createXMLStreamWriter(os);
        try {
            writer.writeStartDocument("UTF-8", "1.0");
            writer.writeStartElement("metadata");

            writer.writeStartElement("groupId");
            writer.writeCharacters(groupId);
            writer.writeEndElement();

            writer.writeStartElement("artifactId");
            writer.writeCharacters(artifactId);
            writer.writeEndElement();

            writer.writeStartElement("version");
            writer.writeCharacters(version);
            writer.writeEndElement();

            writer.writeEndElement();
            writer.writeEndDocument();
        } finally {
            writer.flush();
            writer.close();
            os.close();
        }
    } catch (XMLStreamException e) {
        LOG.error("Error on creating metadata - XML", e);
    } catch (IOException e) {
        LOG.error("Error on creating metadata - FILE", e);
    }
    return (temp != null && temp.exists()) ? temp : null;
}

From source file:org.exoplatform.services.jcr.ext.artifact.ArtifactManagingServiceImpl.java

protected File createMultiMetadata(String groupId, String artifactId, String current_version,
        List<String> v_list) throws FileNotFoundException {
    File temp = null;//  w ww. j a v a2s . co  m
    try {
        String filename = getUniqueFilename("maven-metadata.xml");
        temp = File.createTempFile(filename, null);

        OutputStream os = new FileOutputStream(temp);
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = factory.createXMLStreamWriter(os);
        try {
            writer.writeStartDocument("UTF-8", "1.0");
            writer.writeStartElement("metadata");

            writer.writeStartElement("groupId");
            writer.writeCharacters(groupId);
            writer.writeEndElement();

            writer.writeStartElement("artifactId");
            writer.writeCharacters(artifactId);
            writer.writeEndElement();

            String elderVersion;
            if (v_list.size() > 0) {
                Collections.sort(v_list); // sort list
                elderVersion = v_list.get(0); // get first element
            } else
                elderVersion = current_version;
            v_list.add(current_version);

            writer.writeStartElement("version");
            writer.writeCharacters(elderVersion);
            writer.writeEndElement();

            writer.writeStartElement("versions");
            writer.writeStartElement("versioning");

            for (Iterator<String> iterator = v_list.iterator(); iterator.hasNext();) {
                writer.writeStartElement("version");
                writer.writeCharacters(iterator.next());
                writer.writeEndElement();
            }

            writer.writeEndElement();
            writer.writeEndElement();

            writer.writeEndElement();
            writer.writeEndDocument();
        } finally {
            writer.flush();
            writer.close();
            os.close();
        }
    } catch (XMLStreamException e) {
        LOG.error("Error on creating metadata - XML", e);
    } catch (IOException e) {
        LOG.error("Error on creating metadata - FILE", e);
    }
    return (temp != null && temp.exists()) ? temp : null;
}

From source file:org.flowable.bpmn.converter.BpmnXMLConverter.java

public byte[] convertToXML(BpmnModel model, String encoding) {
    try {/*w ww. j  a va 2s.  c om*/

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);

        XMLStreamWriter writer = xof.createXMLStreamWriter(out);
        XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer);

        DefinitionsRootExport.writeRootElement(model, xtw, encoding);
        CollaborationExport.writePools(model, xtw);
        DataStoreExport.writeDataStores(model, xtw);
        SignalAndMessageDefinitionExport.writeSignalsAndMessages(model, xtw);

        for (Process process : model.getProcesses()) {

            if (process.getFlowElements().isEmpty() && process.getLanes().isEmpty()) {
                // empty process, ignore it
                continue;
            }

            ProcessExport.writeProcess(process, xtw);

            for (FlowElement flowElement : process.getFlowElements()) {
                createXML(flowElement, model, xtw);
            }

            for (Artifact artifact : process.getArtifacts()) {
                createXML(artifact, model, xtw);
            }

            // end process element
            xtw.writeEndElement();
        }

        BPMNDIExport.writeBPMNDI(model, xtw);

        // end definitions root element
        xtw.writeEndElement();
        xtw.writeEndDocument();

        xtw.flush();

        outputStream.close();

        xtw.close();

        return outputStream.toByteArray();

    } catch (Exception e) {
        LOGGER.error("Error writing BPMN XML", e);
        throw new XMLException("Error writing BPMN XML", e);
    }
}

From source file:org.flowable.cmmn.converter.CmmnXmlConverter.java

public byte[] convertToXML(CmmnModel model, String encoding) {
    try {/*from ww  w . j a  v  a2 s  .  c om*/

        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        OutputStreamWriter out = new OutputStreamWriter(outputStream, encoding);

        XMLStreamWriter writer = xof.createXMLStreamWriter(out);
        XMLStreamWriter xtw = new IndentingXMLStreamWriter(writer);

        DefinitionsRootExport.writeRootElement(model, xtw, encoding);

        for (Case caseModel : model.getCases()) {

            if (caseModel.getPlanModel().getPlanItems().isEmpty()) {
                // empty case, ignore it
                continue;
            }

            CaseExport.writeCase(caseModel, xtw);

            Stage planModel = caseModel.getPlanModel();
            StageExport.writeStage(planModel, xtw);

            // end case element
            xtw.writeEndElement();
        }

        CmmnDIExport.writeCmmnDI(model, xtw);

        // end definitions root element
        xtw.writeEndElement();
        xtw.writeEndDocument();

        xtw.flush();

        outputStream.close();

        xtw.close();

        return outputStream.toByteArray();

    } catch (Exception e) {
        LOGGER.error("Error writing CMMN XML", e);
        throw new XMLException("Error writing CMMN XML", e);
    }
}

From source file:org.gluu.saml.AuthRequest.java

public String getStreamedRequest(boolean useBase64) throws XMLStreamException, IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(baos);

    writer.writeStartElement("samlp", "AuthnRequest", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("ID", id);
    writer.writeAttribute("Version", "2.0");
    writer.writeAttribute("IssueInstant", this.issueInstant);
    writer.writeAttribute("ProtocolBinding", "urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST");
    writer.writeAttribute("AssertionConsumerServiceURL", this.samlSettings.getAssertionConsumerServiceUrl());

    writer.writeStartElement("saml", "Issuer", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters(this.samlSettings.getIssuer());
    writer.writeEndElement();/*from   w  ww.  j  a v  a2  s . c o m*/

    writer.writeStartElement("samlp", "NameIDPolicy", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("Format", this.samlSettings.getNameIdentifierFormat());
    writer.writeAttribute("AllowCreate", "true");
    writer.writeEndElement();

    writer.writeStartElement("samlp", "RequestedAuthnContext", "urn:oasis:names:tc:SAML:2.0:protocol");
    writer.writeNamespace("samlp", "urn:oasis:names:tc:SAML:2.0:protocol");

    writer.writeAttribute("Comparison", "exact");

    writer.writeStartElement("saml", "AuthnContextClassRef", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeNamespace("saml", "urn:oasis:names:tc:SAML:2.0:assertion");
    writer.writeCharacters("urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport");
    writer.writeEndElement();

    writer.writeEndElement();

    writer.writeEndElement();
    writer.flush();

    if (log.isDebugEnabled()) {
        log.debug("Genereated Saml Request " + new String(baos.toByteArray(), "UTF-8"));
    }

    if (useBase64) {
        byte[] deflated = CompressionHelper.deflate(baos.toByteArray(), true);
        String base64 = Base64.encodeBase64String(deflated);
        String encoded = URLEncoder.encode(base64, "UTF-8");

        return encoded;
    }

    return new String(baos.toByteArray(), "UTF-8");
}

From source file:org.intermine.api.profile.ProfileManagerTest.java

public void testXMLWrite() throws Exception {
    setUpUserProfiles();/*from w ww  . j av  a  2s . c  o  m*/
    XMLUnit.setIgnoreWhitespace(true);
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    TagManager tagManager = im.getTagManager();

    tagManager.addTag("test-tag", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.name", "attribute", "bob");
    tagManager.addTag("test-tag2", "Department.company", "reference", "bob");
    tagManager.addTag("test-tag2", "Department.employees", "collection", "bob");

    tagManager.addTag("test-tag", "Department.company", "reference", "sally");

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("userprofiles");
        ProfileBinding.marshal(bobProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        ProfileBinding.marshal(sallyProfile, os, writer, PathQuery.USERPROFILE_VERSION, classKeys);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("ProfileManagerBindingTest.xml");
    String expectedXml = IOUtils.toString(is);

    String actualXml = sw.toString().trim();

    assertEquals(normalise(expectedXml), normalise(actualXml));
}

From source file:org.intermine.api.xml.TagBindingTest.java

public void testMarshal() throws Exception {
    XMLUnit.setIgnoreWhitespace(true);/*w ww  .  j  a v  a 2s.  c  o m*/
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        writer.writeStartElement("tags");
        List<Tag> tags = getTags();
        for (Tag tag : tags) {
            TagBinding.marshal(tag, writer);
        }
        writer.writeEndElement();
        writer.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    InputStream is = getClass().getClassLoader().getResourceAsStream("TagBindingTest.xml");
    String expectedXml = IOUtils.toString(is);

    String actualXml = sw.toString().trim();
    System.out.println(normalise(actualXml));
    assertEquals("actual and expected XML should be the same", normalise(expectedXml), normalise(actualXml));
}

From source file:org.intermine.pathquery.PathQuery.java

/**
 * Convert a PathQuery to XML.//from ww  w . jav a 2s.  c  om
 *
 * @param version the version number of the XML format
 * @return this template query as XML.
 */
public synchronized String toXml(int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        PathQueryBinding.marshal(this, "query", model.getName(), writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.pathquery.PathQueryBinding.java

/**
 * Convert a PathQuery to XML.//from   ww w.j  a v a  2  s. c o  m
 *
 * @param query the PathQuery
 * @param queryName the name of the query
 * @param modelName the model name
 * @param version the version number of the xml format, an attribute of the ProfileManager
 * @return the corresponding XML String
 */
public static String marshal(PathQuery query, String queryName, String modelName, int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        marshal(query, queryName, modelName, writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}

From source file:org.intermine.template.TemplateQuery.java

/**
 * Convert a template query to XML./*from   ww w .  j a  v a  2s . co  m*/
 *
 * @param version the version number of the XML format
 * @return this template query as XML.
 */
@Override
public synchronized String toXml(int version) {
    StringWriter sw = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();

    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(sw);
        TemplateQueryBinding.marshal(this, writer, version);
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }

    return sw.toString();
}