Example usage for javax.xml.stream XMLStreamWriter close

List of usage examples for javax.xml.stream XMLStreamWriter close

Introduction

In this page you can find the example usage for javax.xml.stream XMLStreamWriter close.

Prototype

public void close() throws XMLStreamException;

Source Link

Document

Close this writer and free any resources associated with the writer.

Usage

From source file:org.staxcel.internal.SpreadSheetImpl.java

/**
 * Write all shared strings to ./xl/SharedStrings.xml file
 * /*  w w w  . jav a2s  .  c om*/
 * @throws IOException
 */
protected void addSharedStringsFile() throws IOException {
    try {
        BufferedWriter writer = createWriter(OpenXMLFormatConstants.SHARED_STRING_FILE_NAME);
        XMLStreamWriter xmlWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(writer);
        xmlWriter.writeStartDocument("UTF-8", "1.0");
        xmlWriter.writeStartElement("sst");
        xmlWriter.writeAttribute("xmlns", OpenXMLNamespace.NS_OPENXML_SPREADSHEETML_2006_MAIN);
        xmlWriter.writeAttribute("count", String.valueOf(this.sharedStrings.size()));
        xmlWriter.writeAttribute("uniqueCount", String.valueOf(this.sharedStrings.size()));

        for (Entry<String, Integer> entry : this.sharedStrings.entrySet()) {
            xmlWriter.writeStartElement("si");
            xmlWriter.writeStartElement("t");
            xmlWriter.writeCharacters(entry.getKey());
            xmlWriter.writeEndElement();
            xmlWriter.writeEndElement();
        }
        xmlWriter.writeEndElement();
        xmlWriter.writeEndDocument();
        xmlWriter.close();
        writer.flush();
        writer.close();
    } catch (XMLStreamException ex) {
        logger.error("XML error when creating SharedStrings.xml file", ex);
        throw new StaxcelException("XML error when creating SharedStrings.xml file", ex);
    }
}

From source file:org.tolven.app.bean.DataExtractBean.java

public void streamResultsXML(Writer out, DataQueryResults dq) {
    Long totalCount = null;/*ww  w.j  a v a 2  s . co m*/
    if (dq.isReturnTotalCount()) {
        getMDQueryResults(dq, true);
        totalCount = (Long) dq.getIterator().next();
    }

    if (dq.getLimit() != 0) {
        getMDQueryResults(dq, false);
    }
    try {
        XMLStreamWriter xmlStreamWriter = null;
        try {
            XMLOutputFactory factory = XMLOutputFactory.newInstance();
            xmlStreamWriter = factory.createXMLStreamWriter(out);
            xmlStreamWriter.writeStartDocument("UTF-8", "1.0");
            xmlStreamWriter.writeStartElement("results");
            xmlStreamWriter.writeAttribute("path", dq.getPath());
            xmlStreamWriter.writeAttribute("account", String.valueOf(dq.getAccount().getId()));
            xmlStreamWriter.writeAttribute("database",
                    getTolvenPropertiesBean().getProperty("tolven.repository.oid"));
            GregorianCalendar nowCal = new GregorianCalendar();
            nowCal.setTime(dq.getNow());
            DatatypeFactory xmlFactory = DatatypeFactory.newInstance();
            XMLGregorianCalendar ts = xmlFactory.newXMLGregorianCalendar(nowCal);
            xmlStreamWriter.writeAttribute("timestamp", ts.toXMLFormat());
            if (!dq.isItemQuery()) {
                xmlStreamWriter.writeAttribute("offset", Long.toString(dq.getOffset()));
                xmlStreamWriter.writeAttribute("limit", Long.toString(dq.getLimit()));
                xmlStreamWriter.writeAttribute("count", Long.toString(dq.getCount()));
            }
            if (!dq.isItemQuery() && totalCount != null) {
                xmlStreamWriter.writeAttribute("totalCount", totalCount.toString());
            }
            addExtendedFields(dq);
            //addPlaceholderIds(dq);
            if (MenuStructure.PLACEHOLDER.equals(dq.getMenuStructure().getRole())) {
                addParentFields(dq);
            }
            addSimpleXMLColumnHeadings(dq, xmlStreamWriter);
            if (dq.getLimit() != 0) {
                addXMLRows(dq, xmlStreamWriter);
            }
            xmlStreamWriter.writeEndElement();
            xmlStreamWriter.writeEndDocument();
        } finally {
            if (xmlStreamWriter != null) {
                xmlStreamWriter.close();
            }
        }
    } catch (Exception ex) {
        throw new RuntimeException("Could not obtain XML results for menupath: " + dq.getPath()
                + " in account: " + dq.getAccount().getId(), ex);
    }
}

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   www .ja v  a  2s .  com
        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 {//  ww w .j a va2 s .  c  o m
        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 {/* ww  w  .  j  a  va  2 s  . c  o 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("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  w ww.j  a va  2s.  c o 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 ww.j av  a2 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);
        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  ww w  . j av  a 2 s .c o m*/
        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();//from  w  w  w.j  a v a  2  s .  c o  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  w w.j a v a 2  s .c o  m*/
        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();
}