Example usage for javax.xml.stream XMLStreamWriter writeStartDocument

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

Introduction

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

Prototype

public void writeStartDocument(String encoding, String version) throws XMLStreamException;

Source Link

Document

Write the XML Declaration.

Usage

From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java

private String pullNextXmlChunkFromTopElementOnStack(XMLChunkerState data) throws KettleException {
    Stack<String> elementStack = data.getElementStack();
    XMLStreamReader xmlStreamReader = data.getXmlStreamReader();

    int elementStackDepthOnEntry = elementStack.size();
    StringWriter stringWriter = new StringWriter();

    try {/* w w w . ja v  a 2s  . c  o  m*/
        XMLStreamWriter xmlStreamWriter = data.getXmlOutputFactory().createXMLStreamWriter(stringWriter);

        xmlStreamWriter.writeStartDocument(CharEncoding.UTF_8, "1.0");

        // put the current element on because presumably it's the open element for the one
        // that is being looked for.

        XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);

        while (xmlStreamReader.hasNext() & elementStack.size() >= elementStackDepthOnEntry) {

            switch (xmlStreamReader.next()) {

            case XMLStreamConstants.END_DOCUMENT:
                break; // handled below explicitly.

            case XMLStreamConstants.END_ELEMENT:
                elementStack.pop();
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            case XMLStreamConstants.START_ELEMENT:
                elementStack.push(xmlStreamReader.getLocalName());
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            default:
                XmlReaderToWriter.write(xmlStreamReader, xmlStreamWriter);
                break;

            }

        }

        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
    } catch (Exception e) {
        throw new KettleException("unable to process a chunk of the xero xml stream", e);
    }

    return stringWriter.toString();
}

From source file:de.huxhorn.sulky.plist.impl.PropertyListWriter.java

public void write(XMLStreamWriter writer, PropertyList object, boolean isRoot) throws XMLStreamException {
    if (isRoot) {
        writer.writeStartDocument("utf-8", "1.0");
        writer.writeCharacters("\n");
        writer.writeDTD(/* w  w w  .  ja  va  2 s  .  c  om*/
                "<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
        writer.writeCharacters("\n");
    }
    StaxUtilities.writeStartElement(writer, null, null, PLIST_NODE);
    StaxUtilities.writeAttribute(writer, false, null, null, PLIST_VERSION_ATTRIBUTE, PLIST_VERSION);

    if (object != null) {
        writeValue(writer, object.getRoot());
    }

    writer.writeEndElement();
    if (isRoot) {
        writer.writeEndDocument();
    }
}

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  ww.  j  a v a  2  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: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  w w  . j a  v a2  s . com*/
    } 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:de.qucosa.webapi.v1.SearchResource.java

private ResponseEntity<String> errorResponse(String message, HttpStatus status) throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeEmptyElement("SearchResult");
    w.writeStartElement("Error");
    w.writeAttribute("message", message);
    w.writeEndElement();/* w  w  w . jav a  2 s .  co m*/
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return new ResponseEntity<>(sw.toString(), status);
}

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.ja va2s.  com*/
 * 
 * @param outStream
 *            The <code>OutputStream</code> to write the entity to.
 * @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.
 * @throws XMLStreamException
 *             if an error occurs creating or accessing the stream.
 * @throws StorageException
 *             if a Storage service error occurs.
 */
private static void writeSingleAtomEntity(final OutputStream outStream, final TableEntity entity,
        final boolean isTableEntry, final OperationContext opContext)
        throws XMLStreamException, StorageException {
    final XMLOutputFactory xmlOutFactoryInst = XMLOutputFactory.newInstance();
    XMLStreamWriter xmlw = xmlOutFactoryInst.createXMLStreamWriter(outStream, Constants.UTF8_CHARSET);

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

    writeAtomEntity(entity, isTableEntry, xmlw, opContext);

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

From source file:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.XMLTabularDataConverter.java

@Override
public StreamingOutput createStream(final Result result) {
    StreamingOutput stream = new StreamingOutput() {
        @Override//w ww .j  a va 2s  . c  o  m
        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            ResultSet rs = null;
            XMLStreamWriter xtw = null;
            try {
                rs = (ResultSet) result.getData();
                rs.load(result.getResultSetLocation());
                XMLOutputFactory xof = XMLOutputFactory.newInstance();
                xtw = xof.createXMLStreamWriter(new OutputStreamWriter(outputStream));
                xtw.writeStartDocument("utf-8", "1.0");
                xtw.writeStartElement("results");
                rs.beforeFirst();
                while (rs.next()) {
                    xtw.writeStartElement("result");
                    for (int i = 0; i < rs.getColumnSize(); i++) {
                        xtw.writeStartElement(rs.getColumn(i).getName().replace(" ", "_"));
                        xtw.writeCharacters(rs.getString(i));
                        xtw.writeEndElement();
                    }
                    xtw.writeEndElement();
                }
                xtw.writeEndElement();
                xtw.writeEndDocument();

                xtw.flush();

            } catch (ResultSetException | PersistableException | XMLStreamException e) {
                log.info("Error creating XML Stream: " + e.getMessage());
            } finally {
                if (xtw != null) {
                    try {
                        xtw.close();
                    } catch (XMLStreamException e) {
                        e.printStackTrace();
                    }
                }
                if (rs != null && !rs.isClosed()) {
                    try {
                        rs.close();
                    } catch (ResultSetException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            }

        }
    };
    return stream;
}

From source file:de.qucosa.webapi.v1.SearchResource.java

private ResponseEntity<String> scrollAndBuildResultList(SearchResponse searchResponse)
        throws XMLStreamException, ParseException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);

    SearchHits searchHits = searchResponse.getHits();

    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    {/*w w  w.j  av a 2 s  .c  o  m*/
        w.writeStartElement("SearchResult");
        {
            w.writeStartElement("Search");
            w.writeAttribute("hits", String.valueOf(searchHits.getTotalHits()));
            w.writeEndElement();
            w.writeStartElement("ResultList");
            w.writeNamespace(XLINK_NAMESPACE_PREFIX, XLINK_NAMESPACE);

            SearchResponse scrollResponse = searchResponse;
            int hitcount = 0;
            while (true) {
                hitcount = writeResultElements(w, searchHits, hitcount);
                scrollResponse = elasticSearchClient.prepareSearchScroll(scrollResponse.getScrollId())
                        .setScroll(new TimeValue(60, TimeUnit.SECONDS)).execute().actionGet();
                searchHits = scrollResponse.getHits();
                if (searchHits.getHits().length == 0) {
                    log.debug("Stop scrolling at hitcount: {}", hitcount);
                    break;
                }
            }

            w.writeEndElement();
        }
        w.writeEndElement();
    }
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();

    log.debug(sw.toString());

    return new ResponseEntity<>(sw.toString(), HttpStatus.OK);
}

From source file:de.codesourcery.eve.skills.util.XMLMapper.java

public void write(Collection<?> beans, IFieldConverters converters, OutputStream outstream)
        throws XMLStreamException, IntrospectionException, IllegalArgumentException, IllegalAccessException {

    final XMLOutputFactory factory = XMLOutputFactory.newInstance();
    final XMLStreamWriter writer = factory.createXMLStreamWriter(outstream, OUTPUT_ENCODING);
    try {/*from w ww  .j  av a2  s  . c om*/
        writer.writeStartDocument(OUTPUT_ENCODING, "1.0");
        writer.writeStartElement("rows");

        if (beans.isEmpty()) {
            writer.writeEndDocument();
            return;
        }

        final Class<?> beanClass = beans.iterator().next().getClass();

        final BeanDescription desc = createBeanDescription(beanClass);

        final Collection<Field> fields = desc.getFields();
        if (fields.isEmpty()) {
            writer.writeEndDocument();
            return;
        }

        for (Object bean : beans) {
            writer.writeStartElement("row");
            for (Field f : fields) {
                final Object fieldValue = f.get(bean);
                final String[] values = converters.getConverter(f).toString(fieldValue);

                final String attrName = this.propertyNameMappings.get(f.getName());
                if (values == null) {
                    writer.writeAttribute(f.getName(), NIL);
                } else if (attrName == null) {
                    writer.writeAttribute(f.getName(), toAttributeValue(values));
                } else {
                    writer.writeAttribute(attrName, toAttributeValue(values));
                }
            }
            writer.writeEndElement();
        }
        writer.writeEndDocument();
    } finally {
        writer.close();
    }
}

From source file:com.basistech.bbhmp.RosapiBundleCollectorMojo.java

private void writeMetadata(Map<Integer, List<BundleSpec>> bundlesByLevel) throws MojoExecutionException {
    OutputStream os = null;/*from ww w  .ja  v  a  2s. c  o m*/
    File md = new File(outputDirectory, "bundles.xml");

    try {
        os = new FileOutputStream(md);
        XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(os);
        writer = new IndentingXMLStreamWriter(writer);
        writer.writeStartDocument("utf-8", "1.0");
        writer.writeStartElement("bundles");
        for (Integer level : bundlesByLevel.keySet()) {
            writer.writeStartElement("level");
            writer.writeAttribute("level", Integer.toString(level));
            for (BundleSpec spec : bundlesByLevel.get(level)) {
                writer.writeStartElement("bundle");
                writer.writeAttribute("start", Boolean.toString(spec.start));
                writer.writeCharacters(spec.filename);
                writer.writeEndElement();
            }
            writer.writeEndElement();
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();

    } catch (IOException | XMLStreamException e) {
        throw new MojoExecutionException("Failed to write metadata file " + md.toString(), e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}