Example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter

List of usage examples for javax.xml.stream XMLOutputFactory createXMLStreamWriter

Introduction

In this page you can find the example usage for javax.xml.stream XMLOutputFactory createXMLStreamWriter.

Prototype

public abstract XMLStreamWriter createXMLStreamWriter(Result result) throws XMLStreamException;

Source Link

Document

Create a new XMLStreamWriter that writes to a JAXP result.

Usage

From source file:co.turnus.trace.io.XmlTraceStreamWriter.java

public void open() {
    try {//from w w  w  .  j  a va 2  s.  co  m
        XMLOutputFactory factory = XMLOutputFactory.newInstance();
        writer = factory.createXMLStreamWriter(stream);
        if (!compressedXml) {
            writer = new IndentingXMLStreamWriter(writer);
        }
        writeStartDocument();
    } catch (XMLStreamException e) {
        throw new TurnusRuntimeException("Error opening the trace stream writer", e.getCause());
    }
}

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.// ww  w  .j  ava2 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:mediathek.controller.IoXmlSchreiben.java

private void xmlSchreibenStart() throws IOException, XMLStreamException {
    Log.systemMeldung("Start Schreiben nach: " + xmlFilePath.toAbsolutePath());
    final OutputStream outputStream = Files.newOutputStream(xmlFilePath);
    if (xmlFilePath.endsWith(GuiKonstanten.FORMAT_BZ2)) {
        bZip2CompressorOutputStream = new BZip2CompressorOutputStream(outputStream, 2);
        out = new OutputStreamWriter(bZip2CompressorOutputStream, Konstanten.KODIERUNG_UTF);
    } else if (xmlFilePath.endsWith(GuiKonstanten.FORMAT_ZIP)) {
        zipOutputStream = new ZipOutputStream(outputStream);
        ZipEntry entry = new ZipEntry(Konstanten.PROGRAMMNAME);
        zipOutputStream.putNextEntry(entry);
        out = new OutputStreamWriter(zipOutputStream, Konstanten.KODIERUNG_UTF);
    } else {//from  ww w.  j  av  a  2  s .  c om
        out = new OutputStreamWriter(outputStream, Konstanten.KODIERUNG_UTF);
    }

    XMLOutputFactory outFactory = XMLOutputFactory.newInstance();
    writer = outFactory.createXMLStreamWriter(out);
    writer.writeStartDocument(Konstanten.KODIERUNG_UTF, "1.0");
    writer.writeCharacters("\n");//neue Zeile
    writer.writeStartElement(Konstanten.XML_START);
    writer.writeCharacters("\n");//neue Zeile
}

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 w  w w  . j  a  va 2 s . c  om
        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.norconex.collector.http.sitemap.impl.DefaultSitemapResolver.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from w  ww  .java 2 s  .c  o m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("sitemap");
        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.adobe.acs.commons.wcm.impl.SiteMapServlet.java

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType(request.getResponseContentType());
    if (StringUtils.isNotEmpty(this.characterEncoding)) {
        response.setCharacterEncoding(characterEncoding);
    }//  ww w  . java  2  s. c  o  m
    ResourceResolver resourceResolver = request.getResourceResolver();
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
    Page page = pageManager.getContainingPage(request.getResource());

    XMLOutputFactory outputFactory = XMLOutputFactory.newFactory();
    try {
        XMLStreamWriter stream = outputFactory.createXMLStreamWriter(response.getWriter());
        stream.writeStartDocument("1.0");

        stream.writeStartElement("", "urlset", NS);
        stream.writeNamespace("", NS);

        // first do the current page
        write(page, stream, resourceResolver);

        for (Iterator<Page> children = page.listChildren(new PageFilter(false, true), true); children
                .hasNext();) {
            write(children.next(), stream, resourceResolver);
        }

        if (damAssetTypes.size() > 0 && damAssetProperty.length() > 0) {
            for (Resource assetFolder : getAssetFolders(page, resourceResolver)) {
                writeAssets(stream, assetFolder, resourceResolver);
            }
        }

        stream.writeEndElement();

        stream.writeEndDocument();
    } catch (XMLStreamException e) {
        throw new IOException(e);
    }
}

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

/**
 * Generates a XML response that describe the EC2 instances
 *//*from www  . jav  a 2 s  .  com*/
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:AIR.Common.Web.HttpWebHelper.java

public void sendXml(String url, IXmlSerializable inputData, OutputStream outputData) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    HttpPost post = new HttpPost(url);
    post.setHeader("Content-Type", "text/xml");
    HttpResponse response = null;/*from w  w  w.  j  ava  2s  .co  m*/
    try {
        XMLOutputFactory output = TdsXmlOutputFactory.newInstance();
        XMLStreamWriter writer = output.createXMLStreamWriter(out);
        inputData.writeXML(writer);
        ByteArrayEntity entity = new ByteArrayEntity(out.toByteArray());
        post.setEntity(entity);
        response = _client.execute(post, _contextPool.get());
    } catch (IOException | XMLStreamException e) {
        try {
            outputData.write(String.format("HTTP client through exception: %s", e.getMessage()).getBytes());
        } catch (IOException e1) {
            // Our IOExceptioin through an IOException--bad news!!!
            _logger.error("Output stream encountered an error while attempting to process an error message",
                    e1);
            _logger.error(String.format("Original drror message was \"\"", e.getMessage()));
        }
    }
    if (response != null) {
        HttpEntity responseEntity = response.getEntity();
        if (responseEntity != null) {
            String responseString = "";
            try {
                responseString = EntityUtils.toString(responseEntity);
                outputData.write(responseString.getBytes());
            } catch (IOException e) {
                // Our IOExceptioin through an IOException--bad news!!!
                _logger.error("Output stream encountered an error while attempting to process a reply", e);
                _logger.error(String.format("Original reply content was \"\"", responseString));
            }
            EntityUtils.consumeQuietly(responseEntity);
        }
    }
}

From source file:com.norconex.collector.http.delay.impl.DefaultDelayResolver.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from w  ww . j  av  a 2 s .  c  o m
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("delay");
        writer.writeAttribute("class", getClass().getCanonicalName());
        writer.writeAttribute("default", Long.toString(defaultDelay));
        writer.writeAttribute("scope", scope);
        writer.writeAttribute("ignoreRobotsCrawlDelay", Boolean.toString(ignoreRobotsCrawlDelay));

        for (DelaySchedule schedule : schedules) {
            writer.writeStartElement("schedule");
            if (schedule.getDayOfWeekRange() != null) {
                writer.writeAttribute("dayOfWeek", "from " + schedule.getDayOfWeekRange().getMinimum() + " to "
                        + schedule.getDayOfWeekRange().getMaximum());
            }
            if (schedule.getDayOfMonthRange() != null) {
                writer.writeAttribute("dayOfMonth", "from " + schedule.getDayOfMonthRange().getMinimum()
                        + " to " + schedule.getDayOfMonthRange().getMaximum());
            }
            if (schedule.getTimeRange() != null) {
                writer.writeAttribute("time", "from " + schedule.getTimeRange().getLeft().toString("HH:MM")
                        + " to " + schedule.getTimeRange().getRight().toString("HH:MM"));
            }
            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.http.client.impl.DefaultHttpClientInitializer.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from   ww w.  ja  v  a2  s.  c  o m*/
        XMLStreamWriter writer = factory.createXMLStreamWriter(out);
        writer.writeStartElement("httpClientInitializer");
        writer.writeAttribute("class", getClass().getCanonicalName());

        writeSimpleElement(writer, "cookiesDisabled", Boolean.toString(cookiesDisabled));
        writeSimpleElement(writer, "userAgent", userAgent);
        writeSimpleElement(writer, "authMethod", authMethod);
        writeSimpleElement(writer, "authUsername", authUsername);
        writeSimpleElement(writer, "authPassword", authPassword);
        writeSimpleElement(writer, "authUsernameField", authUsernameField);
        writeSimpleElement(writer, "authPasswordField", authPasswordField);
        writeSimpleElement(writer, "authURL", authURL);
        writeSimpleElement(writer, "authHostname", authHostname);
        writeSimpleElement(writer, "authPort", Integer.toString(authPort));
        writeSimpleElement(writer, "authRealm", authRealm);
        writeSimpleElement(writer, "proxyHost", proxyHost);
        writeSimpleElement(writer, "proxyPort", Integer.toString(proxyPort));
        writeSimpleElement(writer, "proxyUsername", proxyUsername);
        writeSimpleElement(writer, "proxyPassword", proxyPassword);
        writeSimpleElement(writer, "proxyRealm", proxyRealm);
        writer.writeEndElement();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new IOException("Cannot save as XML.", e);
    }
}