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.tolven.assembler.ear.EARAssembler.java

protected String getXSLT(PluginDescriptor pd, File localDestDir) throws XMLStreamException, IOException {
    StringWriter xslt = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;
    try {/*from ww  w . j av a  2s  . c om*/
        writer = factory.createXMLStreamWriter(xslt);
        writer.writeStartDocument("UTF-8", "1.0");
        writer.writeCharacters("\n");
        writer.writeStartElement("xsl:stylesheet");
        writer.writeAttribute("version", "2.0");
        writer.writeNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        writer.writeNamespace("tp", "http://java.sun.com/xml/ns/javaee");
        writer.writeCharacters("\n");
        writer.writeStartElement("xsl:output");
        writer.writeAttribute("method", "xml");
        writer.writeAttribute("indent", "yes");
        writer.writeAttribute("encoding", "UTF-8");
        writer.writeAttribute("omit-xml-declaration", "no");
        writer.writeEndElement();
        writer.writeCharacters("\n");
        addMainTemplate(writer);
        addRootTemplate(writer);
        addJavaLibraryTemplates(pd, localDestDir);
        addConnectorModuleTemplates(pd, localDestDir, writer);
        addEJBModuleTemplates(pd, localDestDir, writer);
        addWARModuleTemplates(pd, localDestDir, writer);
        addSecurityRoleTemplates(pd, writer);
        writer.writeEndDocument();
        writer.writeEndDocument();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
    return xslt.toString();
}

From source file:org.tolven.assembler.ejbmodule.EJBModuleAssembler.java

protected String getEJBJARXSLT() throws XMLStreamException {
    StringWriter xslt = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlStreamWriter = null;
    try {/*from w w  w. j  a va  2s .  com*/
        xmlStreamWriter = factory.createXMLStreamWriter(xslt);
        xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:stylesheet");
        xmlStreamWriter.writeAttribute("version", "2.0");
        xmlStreamWriter.writeNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        xmlStreamWriter.writeNamespace("tp", "http://java.sun.com/xml/ns/javaee");
        xmlStreamWriter.writeAttribute("exclude-result-prefixes", "tp");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:output");
        xmlStreamWriter.writeAttribute("method", "xml");
        xmlStreamWriter.writeAttribute("indent", "yes");
        xmlStreamWriter.writeAttribute("encoding", "UTF-8");
        xmlStreamWriter.writeAttribute("omit-xml-declaration", "no");
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        addMainTemplate(xmlStreamWriter);
        addRootTemplate(xmlStreamWriter);
        addEnterpriseBeansChildren(xmlStreamWriter);
        addAssemblyDescriptorChildren(xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
    } finally {
        if (xmlStreamWriter != null) {
            xmlStreamWriter.close();
        }
    }
    return xslt.toString();
}

From source file:org.tolven.assembler.ejbmodule.EJBModuleAssembler.java

protected String getORM(PluginDescriptor pd) {
    try {/*from  w  w  w.  j  a  v a2s  .  c  om*/
        StringWriter writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = null;
        try {
            xmlStreamWriter = factory.createXMLStreamWriter(writer);
            xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
            xmlStreamWriter.writeCharacters("\n");
            xmlStreamWriter.writeStartElement("entity-mappings");
            xmlStreamWriter.writeAttribute("xmlns", "http://java.sun.com/xml/ns/persistence/orm");
            xmlStreamWriter.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xmlStreamWriter.writeAttribute("xsi:schemaLocation",
                    "http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm/orm_2_0.xsd");
            xmlStreamWriter.writeAttribute("version", "2.0");
            xmlStreamWriter.writeCharacters("\n");
            ExtensionPoint exnPt = getMyExtensionPoint(EXNPT_ORM);
            Extension exn = exnPt.getConnectedExtensions().iterator().next();
            String ormId = exn.getParameter("ormId").valueAsString();
            if (ormId == null || ormId.length() == 0) {
                throw new RuntimeException(exnPt.getUniqueId() + "@ormId must have a value");
            }
            addPersistenceUnitMetadata(pd, ormId, xmlStreamWriter);
            addTableGenerators(pd, ormId, xmlStreamWriter);
            addMappedSuperclasses(pd, ormId, xmlStreamWriter);
            addEntities(pd, ormId, xmlStreamWriter);
            addEmbeddables(pd, ormId, xmlStreamWriter);
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeEndDocument();
        } finally {
            if (xmlStreamWriter != null) {
                xmlStreamWriter.close();
            }
        }
        return writer.toString();
    } catch (XMLStreamException ex) {
        throw new RuntimeException("Could not create orm.xml", ex);
    }
}

From source file:org.tolven.assembler.ejbmodule.EJBModuleAssembler.java

protected String getPersistenceXML(PluginDescriptor pd) {
    try {//from ww  w  .j a  v a  2s . co  m
        StringWriter writer = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = null;
        try {
            xmlStreamWriter = factory.createXMLStreamWriter(writer);
            xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
            xmlStreamWriter.writeCharacters("\n");
            xmlStreamWriter.writeStartElement("persistence");
            xmlStreamWriter.writeAttribute("xmlns", "http://java.sun.com/xml/ns/persistence");
            xmlStreamWriter.writeAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xmlStreamWriter.writeAttribute("xsi:schemaLocation",
                    "http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd");
            xmlStreamWriter.writeAttribute("version", "2.0");
            xmlStreamWriter.writeCharacters("\n");
            addPersistenceUnit(pd, xmlStreamWriter);
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeCharacters("\n");
            xmlStreamWriter.writeEndDocument();
        } finally {
            if (xmlStreamWriter != null) {
                xmlStreamWriter.close();
            }
        }
        return writer.toString();
    } catch (XMLStreamException ex) {
        throw new RuntimeException("Could not create orm.xml", ex);
    }
}

From source file:org.tolven.assembler.facesconfig.FacesConfigAssembler.java

protected String getXSLT(PluginDescriptor pluginDescriptor) throws XMLStreamException {
    StringWriter xslt = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlStreamWriter = null;
    boolean added = false;
    try {/*from  w w  w  . j a v  a 2 s.  com*/
        xmlStreamWriter = factory.createXMLStreamWriter(xslt);
        xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:stylesheet");
        xmlStreamWriter.writeAttribute("version", "2.0");
        xmlStreamWriter.writeNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        xmlStreamWriter.writeNamespace("tp", "http://java.sun.com/xml/ns/javaee");
        xmlStreamWriter.writeAttribute("exclude-result-prefixes", "tp");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:output");
        xmlStreamWriter.writeAttribute("method", "xml");
        xmlStreamWriter.writeAttribute("indent", "yes");
        xmlStreamWriter.writeAttribute("encoding", "UTF-8");
        xmlStreamWriter.writeAttribute("omit-xml-declaration", "no");
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        addMainTemplate(xmlStreamWriter);
        addRootTemplate(xmlStreamWriter);
        added = added | addApplicationChildren(pluginDescriptor, xmlStreamWriter);
        added = added | addGlobalValidatorTemplates(pluginDescriptor, xmlStreamWriter);
        added = added | addGlobalConverterTemplates(pluginDescriptor, xmlStreamWriter);
        added = added | addLifeCycleTemplates(pluginDescriptor, xmlStreamWriter);
        added = added | addNavigationRuleTemplates(pluginDescriptor, xmlStreamWriter);
        added = added | addManagedBeanTemplates(pluginDescriptor, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
    } finally {
        if (xmlStreamWriter != null) {
            xmlStreamWriter.close();
        }
    }
    if (added) {
        return xslt.toString();
    } else {
        return null;
    }
}

From source file:org.tolven.assembler.tomcatserver.TomcatServerXMLAssembler.java

protected String getXSLT(ExtensionPoint connectorExtensionPoint) throws XMLStreamException, IOException {
    StringWriter xslt = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlStreamWriter = null;
    try {/*from w w  w.j a  va  2  s  .c om*/
        xmlStreamWriter = factory.createXMLStreamWriter(xslt);
        xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:stylesheet");
        xmlStreamWriter.writeAttribute("version", "2.0");
        xmlStreamWriter.writeNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:output");
        xmlStreamWriter.writeAttribute("method", "xml");
        xmlStreamWriter.writeAttribute("indent", "yes");
        xmlStreamWriter.writeAttribute("encoding", "UTF-8");
        xmlStreamWriter.writeAttribute("omit-xml-declaration", "no");
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        addMainTemplate(xmlStreamWriter);
        addServiceTemplate(connectorExtensionPoint, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.writeEndDocument();
    } finally {
        if (xmlStreamWriter != null) {
            xmlStreamWriter.close();
        }
    }
    return xslt.toString();
}

From source file:org.tolven.assembler.war.WARAssembler.java

protected void addTagInfo(Extension taglibExn, File myWARPluginDir) throws IOException, XMLStreamException {
    File metaInfTagDir = new File(myWARPluginDir.getPath() + "/META-INF/tags");
    metaInfTagDir.mkdirs();/*  w w w. j av a2  s.  co  m*/
    StringWriter tagLibWriter = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter writer = null;
    try {
        writer = factory.createXMLStreamWriter(tagLibWriter);
        writer.writeStartDocument("UTF-8", "1.0");
        writer.writeCharacters("\n");
        writer.writeDTD(
                "<!DOCTYPE facelet-taglib PUBLIC \"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN\" \"http://java.sun.com/dtd/facelet-taglib_1_0.dtd\">");
        writer.writeCharacters("\n");
        writer.writeStartElement("facelet-taglib");
        writer.writeCharacters("\n");
        String namespace = taglibExn.getParameter("namespace").valueAsString();
        writer.writeStartElement("namespace");
        writer.writeCharacters(namespace);
        writer.writeEndElement();
        writer.writeCharacters("\n");
        PluginDescriptor taglibPD = taglibExn.getDeclaringPluginDescriptor();
        addTagSource(taglibPD, metaInfTagDir, writer);
        addTagValidator(taglibPD, writer);
        addTagConverter(taglibPD, writer);
        writer.writeEndElement();
        writer.writeCharacters("\n");
        writer.writeEndDocument();
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
    String tagFilename = taglibExn.getParameter("tag-filename").valueAsString();
    File metaInfTagFile = new File(metaInfTagDir, tagFilename);
    logger.debug("Write tagLib string to " + metaInfTagFile.getPath());
    FileUtils.writeStringToFile(metaInfTagFile, tagLibWriter.toString());
}

From source file:org.tolven.assembler.webxml.WebXMLAssembler.java

protected String getXSLT(PluginDescriptor pd) throws XMLStreamException {
    StringWriter xslt = new StringWriter();
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlStreamWriter = null;
    try {/*from w ww  .j a  v a  2  s .  c om*/
        xmlStreamWriter = factory.createXMLStreamWriter(xslt);
        xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:stylesheet");
        xmlStreamWriter.writeAttribute("version", "2.0");
        xmlStreamWriter.writeNamespace("xsl", "http://www.w3.org/1999/XSL/Transform");
        xmlStreamWriter.writeNamespace("tp", "http://java.sun.com/xml/ns/javaee");
        xmlStreamWriter.writeAttribute("exclude-result-prefixes", "tp");
        xmlStreamWriter.writeCharacters("\n");
        xmlStreamWriter.writeStartElement("xsl:output");
        xmlStreamWriter.writeAttribute("method", "xml");
        xmlStreamWriter.writeAttribute("indent", "yes");
        xmlStreamWriter.writeAttribute("encoding", "UTF-8");
        xmlStreamWriter.writeAttribute("omit-xml-declaration", "no");
        xmlStreamWriter.writeEndElement();
        xmlStreamWriter.writeCharacters("\n");
        addMainTemplate(xmlStreamWriter);
        addRootTemplate(xmlStreamWriter);
        addContextParameterTemplate(xmlStreamWriter);
        addContextParameterCallTemplates(pd, xmlStreamWriter);
        addFilterTemplates(pd, xmlStreamWriter);
        addListenerTemplates(pd, xmlStreamWriter);
        addServletTemplates(pd, xmlStreamWriter);
        addEJBLocalRefTemplates(pd, xmlStreamWriter);
        addSessionConfigTemplates(pd, xmlStreamWriter);
        addWelcomeFileListTemplates(pd, xmlStreamWriter);
        addWebSecurityConstraintTemplates(pd, xmlStreamWriter);
        addLoginConfigTemplates(pd, xmlStreamWriter);
        addSecurityRoleTemplates(pd, xmlStreamWriter);
        addEnvEntryTemplates(pd, xmlStreamWriter);
        addErrorPageTemplates(pd, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
    } finally {
        if (xmlStreamWriter != null) {
            xmlStreamWriter.close();
        }
    }
    return xslt.toString();
}

From source file:org.wso2.carbon.mediator.datamapper.engine.output.writers.XMLWriter.java

private void init(Schema outputSchema) throws SchemaException, WriterException {
    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
    try {/*from   ww  w .  j  a va  2s .  com*/
        xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(stringWriter);
        //creating root element of the xml message
        namespaceMap = outputSchema.getNamespaceMap();
        writeStartElement(outputSchema.getName(), xmlStreamWriter);
        Iterator<Map.Entry<String, String>> namespaceEntryIterator = namespaceMap.entrySet().iterator();
        while (namespaceEntryIterator.hasNext()) {
            Map.Entry<String, String> entry = namespaceEntryIterator.next();
            xmlStreamWriter.writeNamespace(entry.getValue(), entry.getKey());
        }
    } catch (XMLStreamException e) {
        throw new WriterException("Error while creating xml output factory. " + e.getMessage());
    }
}

From source file:org.wso2.carbon.registry.core.jdbc.Repository.java

/**
 * Method to do a dump.//from ww  w.ja v a  2 s  .  c  o  m
 *
 * @param _path   the path to obtain the dump from.
 * @param writer the writer used.
 *
 * @throws RegistryException if the operation failed.
 */
public void dump(String _path, Writer writer) throws RegistryException {
    String path = _path;
    if (!path.equals("/") && path.endsWith("/")) {
        // remove the / suffix
        path = path.substring(0, path.length() - 1);
    }
    XMLStreamWriter xmlWriter = null;
    try {
        XMLOutputFactory xof = XMLOutputFactory.newInstance();
        xmlWriter = xof.createXMLStreamWriter(writer);

        // we are not using xmlWriter.writeStartDocument and writeEndDocument to get rid of the
        // xml descriptor it put in every child node
        dumpRecursively(path, xmlWriter, writer);
    } catch (XMLStreamException e) {
        String msg = "Failed to serialize the dumped element at " + path + ".";
        log.error(msg);
        throw new RegistryException(msg, e);
    } finally {
        if (xmlWriter != null) {
            try {
                xmlWriter.close();
            } catch (XMLStreamException e) {
            }
        }
    }
}