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.unitedinternet.cosmo.dav.impl.StandardDavResponse.java

public void sendDavError(CosmoDavException e) throws IOException {
    setStatus(e.getErrorCode());/*  w ww .  j a v a  2  s .  c  om*/
    if (!e.hasContent()) {
        return;
    }

    XMLStreamWriter writer = null;

    try {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writer.writeStartDocument();
        e.writeTo(writer);
        writer.writeEndDocument();

        setContentType("text/xml; charset=UTF-8");
        byte[] bytes = out.toByteArray();
        setContentLength(bytes.length);
        getOutputStream().write(bytes);
    } catch (Exception e2) {
        LOG.error("Error writing XML", e2);
        LOG.error("Original exception", e);
        setStatus(500);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                LOG.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlCollectionFormat.java

public String format(CollectionItem collection) {
    try {//from ww  w.j a va 2s  .com
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "collection");

        writer.writeCharacters("Collection: ");

        if (collection.getDisplayName() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(collection.getDisplayName());
            writer.writeEndElement();
        }

        if (collection.getUid() != null) {
            writer.writeCharacters(" (uuid ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(collection.getUid());
            writer.writeEndElement();
            writer.writeCharacters(")");
        }

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

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlPreferenceFormat.java

public String format(Preference pref) {
    try {/*from  ww w .j av a 2s  .c  o  m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "preference");

        writer.writeCharacters("Preference: ");

        if (pref.getKey() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(pref.getKey());
            writer.writeEndElement();
        }

        writer.writeCharacters(" = ");

        if (pref.getValue() != null) {
            writer.writeStartElement("span");
            writer.writeAttribute("class", "value");
            writer.writeCharacters(pref.getValue());
            writer.writeEndElement();
        }

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

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlSubscriptionFormat.java

private String format(CollectionSubscription sub, boolean isCollectionProvided, CollectionItem collection,
        boolean isTicketProvided, Ticket ticket) {
    try {//from   ww  w . j  av  a2  s .  co m
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "local-subscription");

        if (sub.getDisplayName() != null) {
            writer.writeCharacters("Subscription: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "name");
            writer.writeCharacters(sub.getDisplayName());
            writer.writeEndElement();
        }

        if (sub.getCollectionUid() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "collection");
            writer.writeCharacters("Collection: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "uuid");
            writer.writeCharacters(sub.getCollectionUid());
            writer.writeEndElement();
            if (isCollectionProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(collection != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

        if (sub.getTicketKey() != null) {
            writer.writeStartElement("div");
            writer.writeAttribute("class", "ticket");
            writer.writeCharacters("Ticket: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(sub.getTicketKey());
            writer.writeEndElement();
            if (isTicketProvided) {
                writer.writeCharacters(" Exists? ");
                writer.writeStartElement("span");
                writer.writeAttribute("class", "exists");
                writer.writeCharacters(Boolean.valueOf(ticket != null).toString());
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }

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

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.unitedinternet.cosmo.model.text.XhtmlTicketFormat.java

public String format(Ticket ticket) {
    try {/*from  ww  w .j  av a 2  s  .  c o  m*/
        StringWriter sw = new StringWriter();
        XMLStreamWriter writer = createXmlWriter(sw);

        writer.writeStartElement("div");
        writer.writeAttribute("class", "ticket");

        if (ticket.getKey() != null) {
            writer.writeCharacters("Key: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "key");
            writer.writeCharacters(ticket.getKey());
            writer.writeEndElement();
        }

        if (ticket.getType() != null) {
            writer.writeCharacters("Type: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "type");
            writer.writeAttribute("title", ticket.getType().toString());
            writer.writeCharacters(ticket.getType().toString());
            writer.writeEndElement();
        }

        if (ticket.getTimeout() != null) {
            writer.writeCharacters("Timeout: ");
            writer.writeStartElement("span");
            writer.writeAttribute("class", "timeout");
            writer.writeAttribute("title", ticket.getTimeout());
            writer.writeCharacters(ticket.getTimeout());
            writer.writeEndElement();
        }

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

        return sw.toString();
    } catch (XMLStreamException e) {
        throw new CosmoXMLStreamException("Error formatting XML", e);
    }
}

From source file:org.unitedinternet.cosmo.util.DomWriter.java

public static String write(Node n) throws XMLStreamException, IOException {
    XMLStreamWriter writer = null;
    try {/*from  w  w w . j  a  v a  2 s . c om*/
        if ((n.getNamespaceURI() != null && n.getAttributes().getLength() > 0)
                && (n.getNamespaceURI().equals(n.getAttributes().item(0).getNodeValue()))) {
            n.getAttributes().removeNamedItem(n.getAttributes().item(0).getNodeName());
        }

        StringWriter out = new StringWriter();
        writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(out);
        writeNode(n, writer);
        writer.close();
        return out.toString();
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (XMLStreamException e2) {
                LOG.warn("Unable to close XML writer", e2);
            }
        }
    }
}

From source file:org.vanbest.xmltv.Main.java

public void fetchData() throws FactoryConfigurationError, Exception {
    if (!config.quiet) {
        showHeader();/*from   w w w . j  ava2s  .  c  om*/
        logger.info("Fetching programme data for " + this.days + " days starting from day " + this.offset);
        int enabledCount = 0;
        for (Channel c : config.channels) {
            if (c.enabled)
                enabledCount++;
        }
        logger.info("... from " + enabledCount + " channels");
        logger.info("... using cache at " + config.cacheDbHandle);
    }
    if (clearCache) {
        ProgrammeCache cache = new ProgrammeCache(config);
        cache.clear();
        cache.close();
    }
    Map<String, EPGSource> guides = new HashMap<String, EPGSource>();
    EPGSourceFactory factory = EPGSourceFactory.newInstance();
    // EPGSource gids = new TvGids(config);
    // if (clearCache) gids.clearCache();

    XMLStreamWriter writer = XMLOutputFactory.newInstance().createXMLStreamWriter(outputWriter);
    writer.writeStartDocument();
    writer.writeCharacters("\n");
    writer.writeDTD("<!DOCTYPE tv SYSTEM \"xmltv.dtd\">");
    writer.writeCharacters("\n");
    writer.writeStartElement("tv");
    writer.writeAttribute("generator-info-url", "http://github.com/janpascal/tv_grab_nl_java");
    writer.writeAttribute("source-info-url", "http://tvgids.nl/");
    writer.writeAttribute("source-info-name", "TvGids.nl");
    writer.writeAttribute("generator-info-name",
            "tv_grab_nl_java release " + config.project_version + ", built " + config.build_time);
    writer.writeCharacters(System.getProperty("line.separator"));

    for (Channel c : config.channels)
        if (c.enabled)
            c.serialize(writer, config.fetchLogos);

    for (int day = offset; day < offset + days; day++) {
        if (!config.quiet)
            System.out.print("Fetching information for day " + day);
        for (Channel c : config.channels) {
            if (!c.enabled)
                continue;
            if (!config.quiet)
                System.out.print(".");
            if (!guides.containsKey(c.source)) {
                guides.put(c.source, factory.createEPGSource(c.source, config));
            }
            List<Programme> programmes = guides.get(c.source).getProgrammes(c, day);
            for (Programme p : programmes)
                p.serialize(writer);
            writer.flush();
        }
        if (!config.quiet)
            System.out.println();
    }

    writer.writeEndElement();
    writer.writeEndDocument();
    writer.flush();
    writer.close();

    for (String source : guides.keySet()) {
        guides.get(source).close();
    }

    if (!config.quiet) {
        EPGSource.Stats stats = new EPGSource.Stats();
        for (String source : guides.keySet()) {
            EPGSource.Stats part = guides.get(source).getStats();
            stats.cacheHits += part.cacheHits;
            stats.cacheMisses += part.cacheMisses;
            stats.fetchErrors += part.fetchErrors;
        }
        logger.info("Number of programmes from cache: " + stats.cacheHits);
        logger.info("Number of programmes fetched: " + stats.cacheMisses);
        logger.warn("Number of fetch errors: " + stats.fetchErrors);
    }
}

From source file:org.webguitoolkit.ui.util.export.XMLTableExport.java

public void writeTo(Table table, OutputStream out) {
    TableExportOptions exportOptions = table.getExportOptions();
    boolean showOnlyDisplayed = exportOptions.isShowOnlyDisplayedColumns();

    try {//from  ww  w . ja va  2 s. co  m
        XMLStreamWriter sw = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
        logger.debug("export table " + ((Table) table).getTitle() + " to xml");
        sw.writeStartElement("export");

        List<ITableColumn> columns = getTableColumns(showOnlyDisplayed, table);
        List<?> tabledata = table.getDefaultModel().getFilteredList();
        int rownr = 1;
        for (Iterator<?> it = tabledata.iterator(); it.hasNext();) {
            sw.writeStartElement("row");
            sw.writeAttribute("rownumber", String.valueOf(rownr));
            DataBag dbag = (DataBag) it.next();
            for (ITableColumn column : columns) {
                Object obj = dbag.get(column.getProperty());
                if (obj != null) {
                    writeObjectTag(sw, column, obj);
                }
            }
            sw.writeEndElement();
            rownr++;
        }
        sw.writeEndElement();
        sw.flush();
        sw.close();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    } catch (FactoryConfigurationError e) {
        throw new RuntimeException(e);
    }
}

From source file:org.wso2.carbon.dataservices.core.engine.DSOMDataSource.java

public XMLStreamReader getReader() throws XMLStreamException {
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream(XMLREADER_DEFAULT_MESSAGE_SIZE);
    XMLStreamWriter xmlWriter = DBUtils.getXMLOutputFactory().createXMLStreamWriter(byteOut);
    this.serialize(xmlWriter);
    xmlWriter.close();
    ByteArrayInputStream byteIn = new ByteArrayInputStream(byteOut.toByteArray());
    return DBUtils.getXMLInputFactory().createXMLStreamReader(byteIn);
}

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

/**
 * Method to do a dump./* w  w  w.  j a  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) {
            }
        }
    }
}