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:de.qucosa.webapi.v1.DocumentResource.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.writeStartElement("Error");
    w.writeAttribute("message", message);
    w.writeEndElement();/* w  ww  .java 2s. c  o  m*/
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return new ResponseEntity<>(sw.toString(), status);
}

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

private String getDocumentUpdatedResponse() throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeStartElement("Opus_Document_Info");
    w.writeCharacters("Update was successful.");
    w.writeEndElement();//from   w w w .j a  v a  2  s  .c  o  m
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return sw.toString();
}

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

@Override
public void saveToXML(Writer out) throws IOException {
    XMLOutputFactory factory = XMLOutputFactory.newInstance();
    try {/*  www.j ava2  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.basistech.bbhmp.RosapiBundleCollectorMojo.java

private void writeMetadata(Map<Integer, List<BundleSpec>> bundlesByLevel) throws MojoExecutionException {
    OutputStream os = null;//from  w  w  w .  j av a 2  s  .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);
    }
}

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

private String getDocumentCreatedResponse(String id) throws XMLStreamException {
    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeStartElement("Opus_Document");
    w.writeNamespace(XLINK_NAMESPACE_PREFIX, XLINK_NAMESPACE);
    w.writeAttribute(XLINK_NAMESPACE, "href", getHrefLink(id));
    w.writeAttribute("id", id);
    w.writeEndElement();/*from  w  ww .ja  va2s  .c om*/
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();
    return sw.toString();
}

From source file:ca.uhn.fhir.parser.XmlParser.java

@Override
public void doEncodeResourceToWriter(IBaseResource theResource, Writer theWriter) throws DataFormatException {
    XMLStreamWriter eventWriter;
    try {//from   ww w  .  j  a v  a  2s  .c  o  m
        eventWriter = createXmlWriter(theWriter);

        encodeResourceToXmlStreamWriter(theResource, eventWriter, false, false);
        eventWriter.flush();
    } catch (XMLStreamException e) {
        throw new ConfigurationException("Failed to initialize STaX event factory", e);
    }
}

From source file:com.fiorano.openesb.application.application.Application.java

/**
 * Writes manageable properties file with the specified label
 * @param applicationFolderName event process folder
 * @param label environment label/*from   ww w  .ja va  2 s.co m*/
 * @throws FioranoException FioranoException
 * @throws XMLStreamException XMLStreamException
 */
public void writeManageableProperties(File applicationFolderName, Label label)
        throws FioranoException, XMLStreamException {
    File manageablePropertiesFile = getManageablePropertiesFile(applicationFolderName, label);
    if (!manageablePropertiesFile.exists())
        manageablePropertiesFile.getParentFile().mkdirs();

    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //        System.out.println(".....:"+outputFactory);
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    XMLStreamWriter writer = null;
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(manageablePropertiesFile);
        writer = outputFactory.createXMLStreamWriter(fos);
        writer.writeStartDocument();
        {

            writer.writeStartElement(ELEM_TARGET, ELEM_TARGET, Namespaces.URI_TARGET);
            writer.writeAttribute(XMLNS_TARGET, Namespaces.URI_TARGET);
            writer.writeAttribute(XMLNS_XSI, Namespaces.URI_XSI);
            writer.writeAttribute(XSI_LOCATION, Namespaces.URI_ENV_XSD);
            {
                for (ServiceInstance instance : getServiceInstances()) {
                    instance.writeManageableProperties(writer);
                }

            }
            writer.writeEndElement();
        }

        writer.writeEndDocument();

        writer.flush();
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    } catch (IOException e) {
        throw new FioranoException(e);
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (XMLStreamException e) {
            // Ignore
        }

        try {
            if (fos != null)
                fos.close();
        } catch (IOException e) {
            // Ignore
        }
    }

}

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

@RequestMapping(value = "/document", method = RequestMethod.GET)
public ResponseEntity<String> listAll() throws IOException, FedoraClientException, XMLStreamException {
    List<String> pids = fedoraRepository.getPIDsByPattern("^qucosa:");

    StringWriter sw = new StringWriter();
    XMLStreamWriter w = xmlOutputFactory.createXMLStreamWriter(sw);
    w.writeStartDocument("UTF-8", "1.0");
    w.writeStartElement("Opus");
    w.writeAttribute("version", "2.0");
    w.writeStartElement("DocumentList");
    w.writeNamespace("xlink", "http://www.w3.org/1999/xlink");
    for (String pid : pids) {
        String nr = pid.substring(pid.lastIndexOf(':') + 1);
        String href = getHrefLink(nr);
        w.writeEmptyElement("Document");
        w.writeAttribute("xlink:href", href);
        w.writeAttribute("xlink:nr", nr);
        w.writeAttribute("xlink:type", "simple");
    }/*from   ww  w.ja v a  2s  .c  om*/
    w.writeEndElement();
    w.writeEndElement();
    w.writeEndDocument();
    w.flush();

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

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");
    {/*from   w w  w. j a v a 2 s  .com*/
        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:com.norconex.collector.http.client.impl.DefaultHttpClientInitializer.java

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