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:com.norconex.collector.http.sitemap.impl.StandardSitemapResolverFactory.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//  w ww.ja  va2  s .  com
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("sitemapResolverFactory");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeAttribute("lenient", Boolean.toString(lenient));
        if (sitemapLocations != null) {
            for (String location : sitemapLocations) {
                writer.writeStartElement("location");
                writer.writeCharacters(location);
                writer.writeEndElement();
            }
        }
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.pocketsoap.salesforce.soap.SoapRequestEntity.java

public final void writeRequest(OutputStream out) throws IOException {
    XMLOutputFactory f = XMLOutputFactory.newInstance();
    try {//  w  w w  .j a  v  a  2  s. c o m
        XMLStreamWriter w = f.createXMLStreamWriter(new BufferedOutputStream(out, 1024), "UTF-8");
        w.writeStartDocument();
        w.writeStartElement("s", "Envelope", SOAP_NS);
        w.writeNamespace("s", SOAP_NS);
        w.writeNamespace("p", PARTNER_NS);
        w.setPrefix("p", PARTNER_NS);
        w.setPrefix("s", SOAP_NS);
        if (hasHeaders()) {
            w.writeStartElement(SOAP_NS, "Header");
            writeHeaders(w);
            w.writeEndElement();
        }
        w.writeStartElement(SOAP_NS, "Body");
        writeBody(w);
        w.writeEndElement();//body
        w.writeEndElement();//envelope
        w.writeEndDocument();
        w.close();
    } catch (XMLStreamException e) {
        throw new IOException("Error generating request xml", e);
    }
}

From source file:com.norconex.committer.core.impl.MultiCommitter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from  w  w  w. jav  a 2s  .c  o  m*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("committer");
        writer.writeAttribute("class", getClass().getCanonicalName());
        for (ICommitter committer : committers) {
            writer.flush();
            if (!(committer instanceof IXMLConfigurable)) {
                LOG.error("Cannot save committer to XML as it does not " + "implement IXMLConfigurable: "
                        + committer);
            }
            ((IXMLConfigurable) committer).saveToXML(out);
        }
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:de.shadowhunt.subversion.internal.httpv1.CheckoutOperation.java

@Override
protected HttpUriRequest createRequest() {
    final Writer body = new StringBuilderWriter();
    try {/*from  www  .ja v a2  s .  co  m*/
        final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body);
        writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0);
        writer.writeStartElement("checkout");
        writer.writeDefaultNamespace(XmlConstants.DAV_NAMESPACE);
        writer.writeStartElement("activity-set");
        writer.writeStartElement("href");
        final URI transactionURI = URIUtils.createURI(repository, transaction);
        writer.writeCData(transactionURI.toString());
        writer.writeEndElement(); // href
        writer.writeEndElement(); // activity-set
        writer.writeEmptyElement("apply-to-version");
        writer.writeEndElement(); //checkout
        writer.writeEndDocument();
        writer.close();
    } catch (final XMLStreamException e) {
        throw new SubversionException("could not create request body", e);
    }

    final URI uri = URIUtils.createURI(repository, resource);
    final DavTemplateRequest request = new DavTemplateRequest("CHECKOUT", uri);
    request.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_XML));
    return request;
}

From source file:de.shadowhunt.subversion.internal.ResolveOperation.java

@Override
protected HttpUriRequest createRequest() {
    final Writer body = new StringBuilderWriter();
    try {//w  ww.  j a  va  2s.com
        final XMLStreamWriter writer = XML_OUTPUT_FACTORY.createXMLStreamWriter(body);
        writer.writeStartDocument(XmlConstants.ENCODING, XmlConstants.VERSION_1_0);
        writer.writeStartElement("get-locations");
        writer.writeDefaultNamespace(XmlConstants.SVN_NAMESPACE);
        writer.writeEmptyElement("path");
        writer.writeStartElement("peg-revision");
        writer.writeCharacters(revision.toString());
        writer.writeEndElement(); // peg-revision
        writer.writeStartElement("location-revision");
        writer.writeCharacters(expected.toString());
        writer.writeEndElement(); // location-revision
        writer.writeEndElement(); //get-locations
        writer.writeEndDocument();
        writer.close();
    } catch (final XMLStreamException e) {
        throw new SubversionException("could not create request body", e);
    }

    final URI uri = URIUtils.createURI(repository, resource);
    final DavTemplateRequest request = new DavTemplateRequest("REPORT", uri);
    request.setEntity(new StringEntity(body.toString(), CONTENT_TYPE_XML));
    return request;
}

From source file:eu.interedition.collatex.cli.Engine.java

void write() throws IOException {
    final SimpleVariantGraphSerializer serializer = new SimpleVariantGraphSerializer(variantGraph);
    if ("csv".equals(outputFormat)) {
        serializer.toCsv(out);/*from  w  ww. j a v  a  2 s.  c  o  m*/
    } else if ("dot".equals(outputFormat)) {
        serializer.toDot(out);
    } else if ("graphml".equals(outputFormat) || "tei".equals(outputFormat)) {
        XMLStreamWriter xml = null;
        try {
            xml = XMLOutputFactory.newInstance().createXMLStreamWriter(out);
            xml.writeStartDocument(outputCharset.name(), "1.0");
            if ("graphml".equals(outputFormat)) {
                serializer.toGraphML(xml);
            } else {
                serializer.toTEI(xml);
            }
            xml.writeEndDocument();
        } catch (XMLStreamException e) {
            throw new IOException(e);
        } finally {
            if (xml != null) {
                try {
                    xml.close();
                } catch (XMLStreamException e) {
                    throw new IOException(e);
                }
            }
        }
    } else {
        JsonProcessor.write(variantGraph, out);
    }
}

From source file:com.concursive.connect.web.modules.api.services.BackupService.java

public boolean process(TransactionItem transactionItem, Connection db) throws Exception {

    LOG.debug("Backup requested...");

    // TODO: Fix cyclical/endless backups; need to keep an array of ids already archived
    // TODO: Fix if lookup value is -1, then a lookup is not needed

    DataRecordFactory factory = DataRecordFactory.INSTANCE;

    // Override the default response packet so that the returned data records
    // can be replayed using the RestoreService; stream the output
    PacketContext packetContext = transactionItem.getPacketContext();
    packetContext.setReturnType(PacketContext.RETURN_DATARECORDS);

    // Start with the original record(s) being backed up
    LOG.debug("Performing buildList query");
    Object object = transactionItem.getObject();
    if (object instanceof java.util.AbstractList || object instanceof java.util.AbstractMap) {
        Object result = TransactionItem.doExecute(transactionItem.getObject(), db, TransactionItem.SELECT,
                packetContext, "buildList");
        // TODO: check result
    } else {//ww  w  .j a  v a 2 s .  c  o  m
        Object newObject = ObjectUtils.constructObject(object.getClass(), db, ObjectUtils.getParamAsInt(object,
                factory.retrieveUniqueField(transactionItem.getName() + "List")));
        transactionItem.setObject(newObject);
    }

    // Start backup recursion...
    //   Consider lower-memory options to stream records without holding in memory
    //   option 1: use pagedList to get x record(s) at a time
    //   option 2: consider queue for limiting backup requests and asynchronous backups

    XMLOutputFactory outputFactory = XMLOutputFactory.newInstance();
    OutputStream outputStream = null;
    if (packetContext.getOutputStream() == null) {
        outputStream = packetContext.getResponse().getOutputStream();
    } else {
        outputStream = packetContext.getOutputStream();
    }
    XMLStreamWriter writer = outputFactory.createXMLStreamWriter(outputStream, "utf-8");
    writer.writeStartDocument();
    writer.writeCharacters(System.getProperty("line.separator"));
    writer.writeStartElement("concursive");
    writer.writeCharacters(System.getProperty("line.separator"));

    ArrayList<String> addedRecords = new ArrayList<String>();
    addRecords(writer, transactionItem.getObject(), transactionItem, packetContext, db, addedRecords);
    writer.writeEndElement();
    writer.writeCharacters(System.getProperty("line.separator"));
    writer.flush();
    writer.close();

    return true;
}

From source file:com.norconex.committer.idol.IdolCommitter.java

/**
 * Commits the addition operations./*from   w w  w . ja v  a  2  s.c  o  m*/
 * @param addOperations additions
 */
public void dreAddData(List<IAddOperation> addOperations) {
    if (addOperations.isEmpty()) {
        return;
    }
    if (LOG.isInfoEnabled()) {
        LOG.info("Sending " + addOperations.size() + " documents for addition to " + createURL());
    }

    StringBuilder b = new StringBuilder();
    b.append(createURL());

    if (isCFS()) {
        b.append("action=ingest&adds=");
        StringWriter xml = new StringWriter();
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        try {
            XMLStreamWriter writer = factory.createXMLStreamWriter(xml);
            writer.writeStartElement("adds");
            buildCfsXmlBatchContent(writer, addOperations);
            writer.writeEndElement();
            writer.flush();
            writer.close();
            b.append(URLEncoder.encode(xml.toString(), CharEncoding.UTF_8));
        } catch (Exception e) {
            throw new CommitterException("Cannot create XML.", e);
        }
        postToIDOL(b.toString(), StringUtils.EMPTY);
    } else {
        b.append("DREADDDATA?");
        QueryString qs = new QueryString();
        for (String key : dreAddDataParams.keySet()) {
            qs.addString(key, dreAddDataParams.get(key));
        }
        String addURL = qs.applyOnURL(b.toString());
        String idxBatch = buildIdxBatchContent(addOperations);
        postToIDOL(addURL, idxBatch);
    }
    if (LOG.isInfoEnabled()) {
        LOG.debug("Done sending additions to " + createURL());
    }
}

From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java

public static void write(List<Scale> modeList, String fileName) {
    XMLOutputFactory xof = XMLOutputFactory.newInstance();
    XMLStreamWriter xtw = null;

    // <mode>
    // <name>Peruvian tritonic 2</name>
    // <interval>3</interval>
    // <interval>4</interval>
    // <interval>5</interval>
    // </mode>

    String ns = "http://rockhoppertech.com/modes-1.0";

    // StringWriter sw = new StringWriter();
    try {/*w ww  .  j a  v  a 2s. co m*/
        xtw = xof.createXMLStreamWriter(new FileWriter(fileName));
        // xtw = xof.createXMLStreamWriter(sw);
        xtw.writeComment("all elements here are explicitly in the mode namespace");
        xtw.writeStartDocument("utf-8", "1.0");
        xtw.setPrefix("modes", ns);
        xtw.writeStartElement(ns, "modes");
        xtw.writeNamespace("modes", ns);

        for (Scale scale : modeList) {
            // xtw.writeStartElement(ns, "mode");
            xtw.writeStartElement("mode");

            // xtw.writeStartElement(ns, "name");
            xtw.writeStartElement("name");
            xtw.writeCharacters(scale.getName());
            xtw.writeEndElement();

            // xtw.writeStartElement(ns, "intervals");
            // xtw.writeStartElement(ns, "interval");
            xtw.writeStartElement("intervals");
            int[] intervals = scale.getIntervals();
            for (int i = 0; i < intervals.length; i++) {
                xtw.writeStartElement("interval");
                xtw.writeCharacters("" + intervals[i]);
                xtw.writeEndElement();
            }
            xtw.writeEndElement(); // intervals

            xtw.writeEndElement(); // mode
        }

        xtw.writeEndElement(); // modes
        xtw.writeEndDocument();
        xtw.flush();
        xtw.close();
        // System.err.println(sw.toString());

    } catch (XMLStreamException | IOException e) {
        logger.error(e.getLocalizedMessage(), e);
        e.printStackTrace();
    }

}

From source file:com.norconex.collector.http.fetch.impl.DefaultDocumentFetcher.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {// ww w.  j a  v a  2 s.c  om
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("httpDocumentFetcher");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeStartElement("validStatusCodes");
        if (validStatusCodes != null) {
            writer.writeCharacters(StringUtils.join(validStatusCodes));
        }
        writer.writeEndElement();
        writer.writeStartElement("headersPrefix");
        if (headersPrefix != null) {
            writer.writeCharacters(headersPrefix);
        }
        writer.writeEndElement();
        writer.writeEndElement();
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}