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:com.fiorano.openesb.application.DmiObject.java

/**
 * Writes this object to the specified output stream
 * @param _out output stream/*from   w  ww .  java  2  s.  c  om*/
 * @throws FioranoException FioranoException
 */
public void toXMLString(OutputStream _out) throws FioranoException {
    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    try {
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(_out);
        toJXMLString(writer);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    }

}

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

public void toXMLString(Writer _writer) throws FioranoException {
    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    try {/*ww w .  j  a  v  a  2s  .  c o  m*/
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(_writer);
        toJXMLString(writer);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    }

}

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

public void toXMLString(OutputStream _out, boolean writeCDataSections) throws FioranoException {
    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    try {/*w  w  w .j  a v a2 s.c o m*/
        XMLStreamWriter writer = outputFactory.createXMLStreamWriter(_out);
        toJXMLString(writer, writeCDataSections);
        writer.flush();
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    }
}

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

private String makeXMLCombinedQuery(CombinedQueryDefinitionImpl qdef) {
    try {//from  w  w w. j  a va2 s. co m
        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:com.cloud.bridge.service.controller.s3.S3ObjectAction.java

private void executeGetObjectAcl(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String bucketName = (String) request.getAttribute(S3Constants.BUCKET_ATTR_KEY);
    String key = (String) request.getAttribute(S3Constants.OBJECT_ATTR_KEY);

    S3GetObjectAccessControlPolicyRequest engineRequest = new S3GetObjectAccessControlPolicyRequest();
    engineRequest.setBucketName(bucketName);
    engineRequest.setKey(key);/*w ww.ja  v  a2s .c  om*/

    S3AccessControlPolicy engineResponse = ServiceProvider.getInstance().getS3Engine()
            .handleRequest(engineRequest);

    // -> serialize using the apache's Axiom classes
    GetObjectAccessControlPolicyResponse onePolicy = S3SoapServiceImpl
            .toGetObjectAccessControlPolicyResponse(engineResponse);

    try {
        OutputStream os = response.getOutputStream();
        response.setStatus(200);
        response.setContentType("text/xml; charset=UTF-8");
        XMLStreamWriter xmlWriter = xmlOutFactory.createXMLStreamWriter(os);
        String documentStart = new String("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        os.write(documentStart.getBytes());
        MTOMAwareXMLSerializer MTOMWriter = new MTOMAwareXMLSerializer(xmlWriter);
        onePolicy.serialize(new QName("http://s3.amazonaws.com/doc/2006-03-01/",
                "GetObjectAccessControlPolicyResponse", "ns1"), factory, MTOMWriter);
        xmlWriter.flush();
        xmlWriter.close();
        os.close();
    } catch (XMLStreamException e) {
        throw new IOException(e.toString());
    }
}

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

/**
 * Writes this object to specified filename
 * @param fileName file name//  ww  w. ja  v  a 2  s .  com
 * @throws FioranoException FioranoException
 */
public void toXMLString(String fileName) throws FioranoException {
    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    FileOutputStream fos = null;
    XMLStreamWriter writer = null;
    try {
        try {
            fos = new FileOutputStream(fileName);
            writer = outputFactory.createXMLStreamWriter(fos);
            toJXMLString(writer);
            writer.flush();
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (XMLStreamException e) {
                // Ignore
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    } catch (IOException e) {
        throw new FioranoException(e);
    }

}

From source file:com.flexive.shared.media.FxMetadata.java

/**
 * Get this metadata object as XML document
 *
 * @return XML document/* ww w . j  a  v  a2  s .co m*/
 * @throws FxApplicationException on errors
 */
public String toXML() throws FxApplicationException {
    StringWriter sw = new StringWriter(2000);
    try {
        XMLStreamWriter writer = getXmlOutputFactory().createXMLStreamWriter(sw);
        writer.writeStartDocument();
        writer.writeStartElement("metadata");
        writer.writeAttribute("mediatype", getMediaType().name());
        writer.writeAttribute("mimetype", getMimeType());
        writer.writeAttribute("filename", getFilename());
        writeXMLTags(writer);
        for (FxMetadataItem mdi : getMetadata()) {
            final String value = mdi.getValue().replaceAll("[\\x00-\\x1F]", ""); //filter out control characters
            if (StringUtils.isEmpty(value))
                continue;
            writer.writeStartElement("meta");
            writer.writeAttribute("key", mdi.getKey());
            writer.writeCData(value);
            writer.writeEndElement();
        }
        writer.writeEndElement();
        writer.writeEndDocument();
        writer.flush();
        writer.close();
    } catch (XMLStreamException e) {
        throw new FxApplicationException(e, "ex.general.xml", e.getMessage());
    }
    return sw.getBuffer().toString();
}

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

public void toXMLString(String fileName, boolean writeCDataSections) throws FioranoException {
    XMLOutputFactory outputFactory = XMLUtils.getStaxOutputFactory();
    //outputFactory.setProperty(XMLOutputFactory.INDENTATION, "/t");
    FileOutputStream fos = null;/*w  ww  .j a  va  2s  . c o m*/
    XMLStreamWriter writer = null;
    try {
        try {
            fos = new FileOutputStream(fileName);
            writer = outputFactory.createXMLStreamWriter(fos);
            toJXMLString(writer, writeCDataSections);
            writer.flush();
        } finally {
            try {
                if (writer != null)
                    writer.close();
            } catch (XMLStreamException e) {
                // Ignore
            }
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException e) {
                // Ignore
            }
        }
    } catch (XMLStreamException e) {
        throw new FioranoException(e);
    } catch (IOException e) {
        throw new FioranoException(e);
    }
}

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//  ww  w. java 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:jodtemplate.pptx.io.xml.SlideXmlRelsWriter.java

@Override
public void write(final Resources resources, final Slide slide, final XMLOutputFactory xmlOutputFactory)
        throws XMLStreamException, IOException {
    final String slideXmlPath = FilenameUtils
            .normalize(slide.getPresentation().getFullPath() + slide.getRelationship().getTarget(), true);
    final String slideXmlRelsPath = Utils.getRelsPathNoPrefixSeparator(slideXmlPath);
    final Resource slideXmlRelsRes = resources.getResource(slideXmlRelsPath);

    try (final OutputStream os = slideXmlRelsRes.getOutputStream()) {
        os.write(/* ww  w  . j a  va 2s . co  m*/
                "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n".getBytes(CharEncoding.UTF_8));
        final XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(os);
        writer.writeStartElement(OOXMLDocument.RELATIONSHIPS_ELEMENT);
        writer.writeNamespace("", OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE);
        for (Relationship rel : slide.getOtherRelationships()) {
            writer.writeEmptyElement(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE,
                    OOXMLDocument.RELATIONSHIP_ELEMENT);
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.ID_ATTRIBUTE,
                    rel.getId());
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.TYPE_ATTRIBUTE,
                    rel.getType());
            writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE, OOXMLDocument.TARGET_ATTRIBUTE,
                    rel.getTarget());
            if (StringUtils.isNotBlank(rel.getTargetMode())) {
                writer.writeAttribute(OOXMLDocument.RELATIONSHIPS_RELS_NAMESPACE,
                        OOXMLDocument.TARGET_MODE_ATTRIBUTE, rel.getTargetMode());
            }
            writer.flush();
        }
        writer.writeEndElement();
        writer.writeEndDocument();

        writer.flush();
        writer.close();
    }
}