Example usage for javax.xml.stream XMLOutputFactory newInstance

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

Introduction

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

Prototype

public static XMLOutputFactory newInstance() throws FactoryConfigurationError 

Source Link

Document

Creates a new instance of the factory in exactly the same manner as the #newFactory() method.

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;// www.  j  a  v a  2 s .  c  om
    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 .  ja  va  2s  .  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 w  w  .j av a  2 s. co  m

        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   w ww  .  j av a  2  s  .  c o m

        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.fudgemsg.wire.xml.FudgeXMLStreamWriter.java

/**
 * Efficiently converts a writer to an XML writer.
 * /*from   ww w  .  ja  v  a 2s  .  c  o m*/
 * @param writer  the writer to convert, not null
 * @return the XML writer, not null
 */
private static XMLStreamWriter createXMLStreamWriter(final Writer writer) {
    try {
        return XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
    } catch (XMLStreamException ex) {
        throw wrapException("create", ex);
    }
}

From source file:org.fudgemsg.xml.FudgeXMLStreamWriter.java

private static XMLStreamWriter createXMLStreamWriter(final Writer writer) {
    try {//from  w  ww.j  ava 2 s.  c om
        return XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
    } catch (XMLStreamException e) {
        throw wrapException("create", e);
    }
}

From source file:org.gatein.management.rest.FailureResponse.java

private void writeXml(OutputStream out, boolean pretty) throws IOException {
    XMLStreamWriter writer;//from  w w  w .  j ava  2  s  . com
    try {
        writer = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
    } catch (XMLStreamException e) {
        throw new IOException("Could not create XML streaming writer.", e);
    }

    try {
        writer.writeStartDocument("UTF-8", "1.0");
        // root element <failureResult>
        nl(writer, pretty);
        writer.writeStartElement("failureResult");
        nl(writer, pretty);
        indent(writer, 1, pretty);

        // <failure>
        writer.writeStartElement("failure");
        writer.writeCharacters(outcome.getFailureDescription());
        writer.writeEndElement();
        nl(writer, pretty);
        indent(writer, 1, pretty);

        // <operationName>
        writer.writeStartElement("operationName");
        writer.writeCharacters(operationName);
        writer.writeEndElement();
        nl(writer, pretty);

        // </failureResult>
        writer.writeEndElement();

        // End document
        writer.writeCharacters("\n");
        writer.writeEndDocument();
        writer.flush();
    } catch (XMLStreamException e) {
        throw new IOException("Exception writing failure response to XML. Failure response message was '"
                + outcome.getFailureDescription() + "'", e);
    }
    //      finally
    //      {
    //         try
    //         {
    //            writer.close();
    //         }
    //         catch (XMLStreamException e)
    //         {
    //            // ignore
    //         }
    //      }
}

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. ja  v a2s  .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.gtdfree.model.GTDDataXMLTools.java

static public void store(GTDModel model, OutputStream out, ActionFilter filter)
        throws IOException, XMLStreamException, FactoryConfigurationError {

    if (filter == null) {
        filter = new DummyFilter(true);
    }/*from  w  ww .j  av  a  2  s .  c om*/
    XMLStreamWriter w = XMLOutputFactory.newInstance().createXMLStreamWriter(out, "UTF-8"); //$NON-NLS-1$

    w.writeStartDocument("UTF-8", "1.0"); //$NON-NLS-1$ //$NON-NLS-2$

    w.writeCharacters(EOL);
    w.writeCharacters(EOL);

    w.writeStartElement("gtd-data"); //$NON-NLS-1$
    w.writeAttribute("version", "2.2"); //$NON-NLS-1$ //$NON-NLS-2$
    w.writeAttribute("modified", ApplicationHelper.formatLongISO(new Date())); //$NON-NLS-1$
    w.writeAttribute("lastActionID", Integer.toString(model.getLastActionID())); //$NON-NLS-1$
    w.writeCharacters(EOL);
    w.writeCharacters(EOL);

    // Write folders

    Folder[] fn = model.toFoldersArray();
    w.writeStartElement("lists"); //$NON-NLS-1$
    w.writeCharacters(EOL);

    for (int i = 0; i < fn.length; i++) {
        Folder ff = fn[i];
        if (ff.isMeta() || !filter.isAcceptable(ff, null)) {
            continue;
        }
        w.writeCharacters(SKIP);
        w.writeStartElement("list"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(ff.getId())); //$NON-NLS-1$
        w.writeAttribute("name", ff.getName()); //$NON-NLS-1$
        w.writeAttribute("type", ff.getType().toString()); //$NON-NLS-1$
        w.writeAttribute("closed", Boolean.toString(ff.isClosed())); //$NON-NLS-1$
        if (ff.getCreated() != null)
            w.writeAttribute("created", Long.toString(ff.getCreated().getTime())); //$NON-NLS-1$
        if (ff.getModified() != null)
            w.writeAttribute("modified", Long.toString(ff.getModified().getTime())); //$NON-NLS-1$
        if (ff.getResolved() != null)
            w.writeAttribute("resolved", Long.toString(ff.getResolved().getTime())); //$NON-NLS-1$
        if (!ff.isInBucket() && ff.getDescription() != null) {
            w.writeAttribute("description", ApplicationHelper.escapeControls(ff.getDescription())); //$NON-NLS-1$
        }
        w.writeCharacters(EOL);

        for (Action a : ff) {

            if (!filter.isAcceptable(ff, a)) {
                continue;
            }

            w.writeCharacters(SKIPSKIP);
            w.writeStartElement("action"); //$NON-NLS-1$
            w.writeAttribute("id", Integer.toString(a.getId())); //$NON-NLS-1$
            w.writeAttribute("created", Long.toString(a.getCreated().getTime())); //$NON-NLS-1$
            w.writeAttribute("resolution", a.getResolution().toString()); //$NON-NLS-1$
            if (a.getResolved() != null)
                w.writeAttribute("resolved", Long.toString(a.getResolved().getTime())); //$NON-NLS-1$
            if (a.getModified() != null)
                w.writeAttribute("modified", Long.toString(a.getModified().getTime())); //$NON-NLS-1$
            if (a.getDescription() != null)
                w.writeAttribute("description", ApplicationHelper.escapeControls(a.getDescription())); //$NON-NLS-1$
            if (a.getStart() != null)
                w.writeAttribute("start", Long.toString(a.getStart().getTime())); //$NON-NLS-1$
            if (a.getRemind() != null)
                w.writeAttribute("remind", Long.toString(a.getRemind().getTime())); //$NON-NLS-1$
            if (a.getDue() != null)
                w.writeAttribute("due", Long.toString(a.getDue().getTime())); //$NON-NLS-1$
            if (a.getType() != null)
                w.writeAttribute("type", a.getType().toString()); //$NON-NLS-1$
            if (a.getUrl() != null)
                w.writeAttribute("url", a.getUrl().toString()); //$NON-NLS-1$
            if (a.isQueued())
                w.writeAttribute("queued", Boolean.toString(a.isQueued())); //$NON-NLS-1$
            if (a.getProject() != null)
                w.writeAttribute("project", a.getProject().toString()); //$NON-NLS-1$
            if (a.getPriority() != null)
                w.writeAttribute("priority", a.getPriority().toString()); //$NON-NLS-1$
            w.writeEndElement();
            w.writeCharacters(EOL);
        }
        w.writeCharacters(SKIP);
        w.writeEndElement();
        w.writeCharacters(EOL);
    }
    w.writeEndElement();
    w.writeCharacters(EOL);

    // Write projects
    Project[] pn = model.toProjectsArray();
    w.writeStartElement("projects"); //$NON-NLS-1$
    w.writeCharacters(EOL);

    for (int i = 0; i < pn.length; i++) {
        Project ff = pn[i];

        if (!filter.isAcceptable(ff, null)) {
            continue;
        }

        w.writeCharacters(SKIP);
        w.writeStartElement("project"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(ff.getId())); //$NON-NLS-1$
        w.writeAttribute("name", ff.getName()); //$NON-NLS-1$
        w.writeAttribute("closed", String.valueOf(ff.isClosed())); //$NON-NLS-1$
        if (ff.getCreated() != null)
            w.writeAttribute("created", Long.toString(ff.getCreated().getTime())); //$NON-NLS-1$
        if (ff.getModified() != null)
            w.writeAttribute("modified", Long.toString(ff.getModified().getTime())); //$NON-NLS-1$
        if (ff.getResolved() != null)
            w.writeAttribute("resolved", Long.toString(ff.getResolved().getTime())); //$NON-NLS-1$

        if (ff.getDescription() != null) {
            w.writeAttribute("description", ApplicationHelper.escapeControls(ff.getDescription())); //$NON-NLS-1$
        }

        StringBuilder sb = new StringBuilder();

        for (Action a : ff) {
            if (!filter.isAcceptable(ff, a)) {
                continue;
            }
            if (sb.length() > 0) {
                sb.append(","); //$NON-NLS-1$
            }
            sb.append(a.getId());
        }
        w.writeAttribute("actions", sb.toString()); //$NON-NLS-1$
        w.writeEndElement();
        w.writeCharacters(EOL);
    }
    w.writeEndElement();
    w.writeCharacters(EOL);

    // Write queue
    Folder f = model.getQueue();

    if (filter.isAcceptable(f, null)) {
        w.writeStartElement("queue"); //$NON-NLS-1$
        w.writeAttribute("id", String.valueOf(f.getId())); //$NON-NLS-1$
        w.writeAttribute("name", f.getName()); //$NON-NLS-1$

        StringBuilder sb = new StringBuilder();
        Iterator<Action> i = f.iterator();
        if (i.hasNext()) {
            sb.append(i.next().getId());
        }
        while (i.hasNext()) {
            sb.append(","); //$NON-NLS-1$
            sb.append(i.next().getId());
        }
        w.writeAttribute("actions", sb.toString()); //$NON-NLS-1$
        w.writeEndElement();
        w.writeCharacters(EOL);
    }

    // containers
    w.writeEndElement();
    w.writeEndDocument();

    w.flush();
    w.close();

    //changed=false;

}

From source file:org.icgc.dcc.portal.manifest.writer.GNOSManifestWriter.java

@SneakyThrows
public static void write(OutputStream buffer, ListMultimap<String, ManifestFile> bundles, long timestamp) {
    int rowCount = 0;
    // If this is thread-safe, perhaps we can make this static???
    val factory = XMLOutputFactory.newInstance();
    @Cleanup//  w  ww .  ja va 2  s. c o m
    val writer = new IndentingXMLStreamWriter(factory.createXMLStreamWriter(buffer, FILE_ENCODING));

    startXmlDocument(writer, timestamp);

    for (val url : bundles.keySet()) {
        val bundle = bundles.get(url);

        if (isEmpty(bundle)) {
            continue;
        }

        val bundleId = bundle.get(0).getDataBundleId();

        writeXmlEntry(writer, bundleId, url, bundle, ++rowCount);
    }

    endXmlDocument(writer);
}