Example usage for javax.xml.stream XMLStreamWriter flush

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

Introduction

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

Prototype

public void flush() throws XMLStreamException;

Source Link

Document

Write any cached data to the underlying output mechanism.

Usage

From source file:eu.scape_project.planning.xml.ProjectExportAction.java

/**
 * Dumps binary data to provided file. It results in an XML file with a
 * single element: data./*w  w w  .j  a  v a2  s. c  o m*/
 * 
 * @param id
 * @param data
 * @param f
 * @param encoder
 * @throws IOException
 */
private static void writeBinaryData(int id, InputStream data, File f) throws IOException {

    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {
        XMLStreamWriter writer = factory.createXMLStreamWriter(new FileWriter(f));

        writer.writeStartDocument(PlanXMLConstants.ENCODING, "1.0");
        writer.writeStartElement("data");
        writer.writeAttribute("id", "" + id);

        Base64InputStream base64EncodingIn = new Base64InputStream(data, true,
                PlanXMLConstants.BASE64_LINE_LENGTH, PlanXMLConstants.BASE64_LINE_BREAK);

        OutputStream out = new WriterOutputStream(new XMLStreamContentWriter(writer),
                PlanXMLConstants.ENCODING);
        // read the binary data and encode it on the fly
        IOUtils.copy(base64EncodingIn, out);
        out.flush();

        // all data is written - end 
        writer.writeEndElement();
        writer.writeEndDocument();

        writer.flush();
        writer.close();

    } catch (XMLStreamException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

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  2  s. 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:gdv.xport.util.HtmlFormatter.java

private static void writeTo(final XMLStreamWriter xmlStreamWriter, final Satz satz, final int zeile)
        throws XMLStreamException {
    xmlStreamWriter.writeStartElement("div");
    xmlStreamWriter.writeAttribute("class", "Satz");
    if (satz instanceof Datensatz) {
        Datensatz datensatz = (Datensatz) satz;
        xmlStreamWriter.writeAttribute("title", "Satzart " + datensatz.getSatzartFeld().getInhalt() + "."
                + datensatz.getSparteFeld().getInhalt());
    } else {//from   www.  j a  v  a 2  s.co  m
        xmlStreamWriter.writeAttribute("title", "Satzart " + satz.getSatzartFeld().getInhalt());
    }
    int n = zeile;
    for (Iterator<Teildatensatz> iterator = satz.getTeildatensaetze().iterator(); iterator.hasNext();) {
        Teildatensatz teildatensatz = iterator.next();
        writeTo(xmlStreamWriter, teildatensatz, n);
        if (iterator.hasNext()) {
            xmlStreamWriter.writeCharacters("\n");
        }
        n++;
    }
    xmlStreamWriter.writeEndElement();
    xmlStreamWriter.writeCharacters("\n");
    xmlStreamWriter.flush();
}

From source file:net.solarnetwork.web.support.SimpleXmlHttpMessageConverter.java

@Override
protected void writeInternal(Object t, HttpOutputMessage outputMessage)
        throws IOException, HttpMessageNotWritableException {
    OutputStream out = outputMessage.getBody();
    try {/* www .  j  av a  2s .c  o  m*/
        XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(out, "UTF-8");
        writer.writeStartDocument("UTF-8", "1.0");
        outputObject(t, t.getClass().getSimpleName(), writer);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new HttpMessageConversionException("Error creating XMLStreamWriter", e);
    }
}

From source file:com.norconex.collector.http.checksum.impl.DefaultHttpDocumentChecksummer.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from ww w.  j  a v a  2s. c  o  m*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("httpDocumentChecksummer");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeStartElement("field");
        writer.writeCharacters(field);
        writer.writeEndElement();
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:net.solarnetwork.util.JavaBeanXmlSerializer.java

private void endXml(XMLStreamWriter writer) {
    try {/*from w  ww.  j  a  v a  2  s  . c  om*/
        writer.writeEndDocument();
        writer.flush();
    } catch (XMLStreamException e) {
        // ignore this
    } finally {
        SDF.remove();
    }
}

From source file:com.microsoft.windowsazure.storage.table.TableParser.java

/**
 * Reserved for internal use. Writes a single entity to the specified <code>OutputStream</code> as a complete XML
 * document.//from w w w.  j  a v  a2 s.  c o  m
 * 
 * @param entity
 *            The instance implementing {@link TableEntity} to write to the output stream.
 * @param isTableEntry
 *            A flag indicating the entity is a reference to a table at the top level of the storage service when
 *            <code>true<code> and a reference to an entity within a table when <code>false</code>.
 * @param opContext
 *            An {@link OperationContext} object used to track the execution of the operation.
 * @param outStream
 *            The <code>OutputStream</code> to write the entity to.
 * 
 * @throws XMLStreamException
 *             if an error occurs creating or accessing the stream.
 * @throws StorageException
 *             if a Storage service error occurs.
 */
private static void writeSingleAtomEntity(final StringWriter strWriter, final TableEntity entity,
        final boolean isTableEntry, final OperationContext opContext)
        throws XMLStreamException, StorageException {
    final XMLOutputFactory xmlOutFactoryInst = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlw = xmlOutFactoryInst.createXMLStreamWriter(strWriter);

    // default is UTF8
    xmlw.writeStartDocument(Constants.UTF8_CHARSET, "1.0");

    writeAtomEntity(entity, isTableEntry, xmlw, opContext);

    // end doc
    xmlw.writeEndDocument();
    xmlw.flush();
}

From source file:com.norconex.collector.core.filter.impl.ExtensionReferenceFilter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//  www . j a va  2s  .  c o  m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("filter");
        writer.writeAttribute("class", getClass().getCanonicalName());
        saveToXML(writer);
        writer.writeAttribute("caseSensitive", Boolean.toString(caseSensitive));
        writer.writeCharacters(extensions);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

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

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/* w w w. ja v a 2s .c  o m*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("committer");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeStartElement("directory");
        writer.writeCharacters(directory);
        writer.writeEndElement();
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.norconex.collector.core.filter.impl.RegexReferenceFilter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//ww  w . ja va  2 s  .co  m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("filter");
        writer.writeAttribute("class", getClass().getCanonicalName());
        super.saveToXML(writer);
        writer.writeAttribute("caseSensitive", Boolean.toString(caseSensitive));
        writer.writeCharacters(regex == null ? "" : regex);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}