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:edu.harvard.hms.dbmi.bd2k.irct.ws.rs.resultconverter.XMLTabularDataConverter.java

@Override
public StreamingOutput createStream(final Result result) {
    StreamingOutput stream = new StreamingOutput() {
        @Override//from   www .j  a  v a  2 s  .com
        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:com.norconex.collector.http.sitemap.impl.StandardSitemapResolverFactory.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from w w w  .java2 s  .c o m
        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.norconex.collector.http.fetch.impl.GenericMetadataFetcher.java

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

From source file:com.mpobjects.rtcalltree.report.xml.XmlReporter.java

protected XMLStreamWriter createStreamWriter(Writer aWriter) {
    XMLOutputFactory factory = XMLOutputFactory.newFactory();
    XMLStreamWriter writer;//from   w  w  w . j  a  v a  2s  .com
    try {
        writer = factory.createXMLStreamWriter(aWriter);
    } catch (XMLStreamException e) {
        LOG.error("Failed to create XMLStreamWriter. " + e.getMessage(), e);
        return null;
    }
    return writer;
}

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

@RequestMapping(value = "/relation/urn/{URN}", method = RequestMethod.GET)
public ResponseEntity<String> describeRelationships(@PathVariable String URN)
        throws XMLStreamException, IOException, FedoraClientException {
    String pid = fedoraRepository.getPIDByIdentifier(URN);
    List<Tuple<String>> constituentPredecessorPids = fedoraRepository.getPredecessorPIDs(pid,
            FedoraRepository.RELATION_CONSTITUENT);
    List<Tuple<String>> derivativePredecessorPIDs = fedoraRepository.getPredecessorPIDs(pid,
            FedoraRepository.RELATION_DERIVATIVE);
    List<Tuple<String>> constituentSuccessorPids = fedoraRepository.getSuccessorPIDs(pid,
            FedoraRepository.RELATION_CONSTITUENT);
    List<Tuple<String>> derivativeSuccessorPids = fedoraRepository.getSuccessorPIDs(pid,
            FedoraRepository.RELATION_DERIVATIVE);

    XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newFactory();
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeStartElement("Opus_Document");
    w.writeStartElement("DocumentId");
    w.writeCharacters(stripPrefix(pid));
    w.writeEndElement();// w w w .j a  v  a2s  .  c om
    w.writeStartElement("Relations");
    writeRelationElement(constituentPredecessorPids, w, "PredecessorRelation", "journal");
    writeRelationElement(derivativePredecessorPIDs, w, "PredecessorRelation", "predecessor");
    writeRelationElement(constituentSuccessorPids, w, "SuccessorRelation", "issue");
    writeRelationElement(derivativeSuccessorPids, w, "SuccessorRelation", "predecessor");
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();

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

From source file:AIR.Common.Web.HttpWebHelperTest.java

@Test
public void SendXmlTest() throws IOException, InterruptedException, XMLStreamException {

    MockData data = new MockData("This is a test");

    IdiotServer server = new IdiotServer(8888);
    server.start();/*from  ww  w .ja  v a2s. c  om*/

    _httpWebHelper.sendXml("http://localhost:8888/", data);
    server.join(1000);
    if (server.isAlive()) {
        server.interrupt();
        throw new AssertionError("Server did not receive request");
    }
    assertTrue(server.isSucceeded());
    assertNull(server.getError());
    String response = server.getResponse();
    _logger.info(response);
    ByteArrayOutputStream databytes = new ByteArrayOutputStream();
    XMLOutputFactory factory = TdsXmlOutputFactory.newInstance();
    XMLStreamWriter writer = factory.createXMLStreamWriter(databytes);
    data.writeXML(writer);
    assertEquals(response, databytes.toString());

    assertTrue(server.getHeaders().get(0).startsWith("POST"));
    boolean foundContentType = false;
    for (String header : server.getHeaders()) {
        _logger.info(header);
        if (header.equals("Content-Type: text/xml")) {
            foundContentType = true;
        }
    }
    assertTrue(foundContentType);
}

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

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*from   w w  w  .j  ava  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);
    }
}

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

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*  ww w.jav a2  s.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.MultiCommitter.java

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {//from  ww  w .java 2  s.co  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:com.norconex.collector.core.filter.impl.RegexMetadataFilter.java

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