Example usage for javax.xml.stream XMLStreamWriter writeEndElement

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

Introduction

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

Prototype

public void writeEndElement() throws XMLStreamException;

Source Link

Document

Writes an end tag to the output relying on the internal state of the writer to determine the prefix and local name of the event.

Usage

From source file:com.norconex.collector.http.url.impl.GenericURLNormalizer.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from ww  w  .  j a  va2 s . com*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("urlNormalizer");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeStartElement("normalizations");
        writer.writeCharacters(StringUtils.join(normalizations, ","));
        writer.writeEndElement();
        writer.writeStartElement("replacements");
        for (Replace replace : replaces) {
            writer.writeStartElement("replace");
            writer.writeStartElement("match");
            writer.writeCharacters(replace.getMatch());
            writer.writeEndElement();
            writer.writeStartElement("replacement");
            writer.writeCharacters(replace.getReplacement());
            writer.writeEndElement();
            writer.writeEndElement();
        }
        writer.writeEndElement();
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.hazelcast.simulator.probes.probes.impl.HdrLatencyDistributionResult.java

@Override
public void writeTo(XMLStreamWriter writer) {
    Histogram tmp = histogram.copy();/*w  w w .  ja va2s . c om*/
    int size = tmp.getNeededByteBufferCapacity();
    ByteBuffer byteBuffer = ByteBuffer.allocate(size);
    int bytesWritten = tmp.encodeIntoCompressedByteBuffer(byteBuffer);
    byteBuffer.rewind();
    byteBuffer.limit(bytesWritten);
    String encodedData = Base64.encodeBase64String(byteBuffer.array());
    try {
        writer.writeStartElement(ProbesResultXmlElements.HDR_LATENCY_DATA.getName());
        writer.writeCData(encodedData);
        writer.writeEndElement();
    } catch (XMLStreamException e) {
        throw new RuntimeException(e);
    }
}

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

private void writeDict(XMLStreamWriter writer, Map<?, ?> map) throws XMLStreamException {
    StaxUtilities.writeStartElement(writer, null, null, DICT_NODE);

    for (Map.Entry current : map.entrySet()) {
        Object key = current.getKey();
        if (key != null) {
            StaxUtilities.writeSimpleTextNode(writer, null, null, KEY_NODE, key.toString());

            writeValue(writer, current.getValue());
        }//  w  w w . j av  a 2  s  .c  o m
    }

    writer.writeEndElement();
}

From source file:dk.statsbiblioteket.util.xml.XMLStepperTest.java

@SuppressWarnings("CallToPrintStackTrace")
private synchronized boolean writerIsCollapsing() {
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    try {/*from w  ww  .  j  a  v a 2  s.  c  om*/
        XMLStreamWriter out = xmlOutFactory.createXMLStreamWriter(os);
        out.writeStartElement("foo");
        out.writeEndElement();
        out.flush();
        return "<foo />".equals(os.toString());
    } catch (XMLStreamException e) {
        throw new RuntimeException("Unable to determine if XMLStreamWriter collapses empty elements", e);
    }
}

From source file:com.norconex.collector.http.filter.impl.SegmentCountURLFilter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from   ww  w  . j a v  a 2s. co  m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("filter");
        writer.writeAttribute("class", getClass().getCanonicalName());
        super.saveToXML(writer);
        writer.writeAttribute("count", Integer.toString(count));
        writer.writeAttribute("duplicate", Boolean.toString(duplicate));
        writer.writeAttribute("separator", separator);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}

From source file:com.marklogic.client.impl.CombinedQueryBuilderImpl.java

private String makeXMLCombinedQuery(CombinedQueryDefinitionImpl qdef) {
    try {/*from   www .  ja  v a  2s .c om*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        String qtext = qdef.qtext;
        StructuredQueryDefinition structuredQuery = qdef.structuredQuery;
        RawQueryDefinition rawQuery = qdef.rawQuery;
        QueryOptionsWriteHandle options = qdef.options;
        String sparql = qdef.sparql;
        if (rawQuery != null && rawQuery instanceof RawCombinedQueryDefinition) {
            CombinedQueryDefinitionImpl combinedQdef = parseCombinedQuery(
                    (RawCombinedQueryDefinition) rawQuery);
            rawQuery = combinedQdef.rawQuery;
            if (qtext == null)
                qtext = combinedQdef.qtext;
            if (options == null)
                options = combinedQdef.options;
            if (sparql == null)
                sparql = combinedQdef.sparql;
        }

        XMLStreamWriter serializer = makeXMLSerializer(out);

        serializer.writeStartDocument();
        serializer.writeStartElement("search");
        if (qtext != null) {
            serializer.writeStartElement("qtext");
            serializer.writeCharacters(qtext);
            serializer.writeEndElement();
        } else {
            serializer.writeCharacters("");
        }
        if (sparql != null) {
            serializer.writeStartElement("sparql");
            serializer.writeCharacters(sparql);
            serializer.writeEndElement();
        }
        serializer.flush();
        String structure = "";
        if (structuredQuery != null)
            structure = structuredQuery.serialize();
        if (rawQuery != null)
            structure = HandleAccessor.contentAsString(rawQuery.getHandle());
        out.write(structure.getBytes("UTF-8"));
        out.flush();
        if (options != null) {
            HandleImplementation handleBase = HandleAccessor.as(options);
            Object value = handleBase.sendContent();
            if (value instanceof OutputStreamSender) {
                ((OutputStreamSender) value).write(out);
            } else {
                out.write(HandleAccessor.contentAsString(options).getBytes("UTF-8"));
            }
            out.flush();
        }

        serializer.writeEndElement();
        serializer.writeEndDocument();
        serializer.flush();
        serializer.close();
        return out.toString("UTF-8");
    } catch (Exception e) {
        throw new MarkLogicIOException(e);
    }
}

From source file:org.xmlsh.internal.commands.http.java

private void writeHeaders(String outv, StatusLine statusLine, Header[] allHeaders)
        throws XMLStreamException, SaxonApiException, CoreException, IOException {
    OutputPort out = mShell.getEnv().getOutputPort(outv);

    XMLStreamWriter sw = out.asXMLStreamWriter(getSerializeOpts());
    sw.writeStartDocument();/*from w w w  .  j av  a 2 s  . c  om*/
    sw.writeStartElement("status");
    sw.writeAttribute("status-code", String.valueOf(statusLine.getStatusCode()));
    sw.writeAttribute("reason", statusLine.getReasonPhrase());
    sw.writeAttribute("protocol-version", statusLine.getProtocolVersion().toString());

    sw.writeEndElement();

    sw.writeStartElement("headers");
    for (Header header : allHeaders) {
        sw.writeStartElement("header");
        sw.writeAttribute("name", header.getName());
        sw.writeAttribute("value", header.getValue());
        sw.writeEndElement();
    }
    sw.writeEndElement();
    sw.writeEndDocument();
    sw.close();

}

From source file:org.elasticsearch.discovery.ec2.AmazonEC2Fixture.java

/**
 * Generates a XML response that describe the EC2 instances
 *//*w  w w  . ja v  a  2s .co  m*/
private byte[] generateDescribeInstancesResponse() {
    final XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
    xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, true);

    final StringWriter out = new StringWriter();
    XMLStreamWriter sw;
    try {
        sw = xmlOutputFactory.createXMLStreamWriter(out);
        sw.writeStartDocument();

        String namespace = "http://ec2.amazonaws.com/doc/2013-02-01/";
        sw.setDefaultNamespace(namespace);
        sw.writeStartElement(XMLConstants.DEFAULT_NS_PREFIX, "DescribeInstancesResponse", namespace);
        {
            sw.writeStartElement("requestId");
            sw.writeCharacters(UUID.randomUUID().toString());
            sw.writeEndElement();

            sw.writeStartElement("reservationSet");
            {
                if (Files.exists(nodes)) {
                    for (String address : Files.readAllLines(nodes)) {

                        sw.writeStartElement("item");
                        {
                            sw.writeStartElement("reservationId");
                            sw.writeCharacters(UUID.randomUUID().toString());
                            sw.writeEndElement();

                            sw.writeStartElement("instancesSet");
                            {
                                sw.writeStartElement("item");
                                {
                                    sw.writeStartElement("instanceId");
                                    sw.writeCharacters(UUID.randomUUID().toString());
                                    sw.writeEndElement();

                                    sw.writeStartElement("imageId");
                                    sw.writeCharacters(UUID.randomUUID().toString());
                                    sw.writeEndElement();

                                    sw.writeStartElement("instanceState");
                                    {
                                        sw.writeStartElement("code");
                                        sw.writeCharacters("16");
                                        sw.writeEndElement();

                                        sw.writeStartElement("name");
                                        sw.writeCharacters("running");
                                        sw.writeEndElement();
                                    }
                                    sw.writeEndElement();

                                    sw.writeStartElement("privateDnsName");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("dnsName");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("instanceType");
                                    sw.writeCharacters("m1.medium");
                                    sw.writeEndElement();

                                    sw.writeStartElement("placement");
                                    {
                                        sw.writeStartElement("availabilityZone");
                                        sw.writeCharacters("use-east-1e");
                                        sw.writeEndElement();

                                        sw.writeEmptyElement("groupName");

                                        sw.writeStartElement("tenancy");
                                        sw.writeCharacters("default");
                                        sw.writeEndElement();
                                    }
                                    sw.writeEndElement();

                                    sw.writeStartElement("privateIpAddress");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();

                                    sw.writeStartElement("ipAddress");
                                    sw.writeCharacters(address);
                                    sw.writeEndElement();
                                }
                                sw.writeEndElement();
                            }
                            sw.writeEndElement();
                        }
                        sw.writeEndElement();
                    }
                }
                sw.writeEndElement();
            }
            sw.writeEndElement();

            sw.writeEndDocument();
            sw.flush();
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return out.toString().getBytes(UTF_8);
}

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

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {// w  ww. j  av  a  2 s.  com
        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:com.fiorano.openesb.application.application.RemoteServiceInstance.java

protected void toJXMLString(XMLStreamWriter writer) throws XMLStreamException, FioranoException {
    writer.writeStartElement(ELEM_REMOTE_SERVICE_INSTANCE);
    {//from  w  w w  .  ja v a  2 s  .  c o  m
        writer.writeAttribute(ATTR_NAME, name);
        writer.writeAttribute(ATTR_APPLICATION_GUID, applicationGUID);
        writer.writeAttribute(ATTR_APPLICATION_VERSION, String.valueOf(applicationVersion));
        writer.writeAttribute(ATTR_APPLICATION_SCHEMA_VERSION, applicationSchemaVersion);
        writer.writeAttribute(ATTR_REMOTE_NAME, remoteName);
    }
    writer.writeEndElement();
}