Java Code Examples for javax.xml.transform.stream.StreamResult#setOutputStream()

The following examples show how to use javax.xml.transform.stream.StreamResult#setOutputStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MeetingPlannerTest.java    From tomee with Apache License 2.0 6 votes vote down vote up
private String sourceToXMLString(Source result) {
    String xmlResult = null;
    try {
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        OutputStream out = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(out);
        transformer.transform(result, streamResult);
        xmlResult = streamResult.getOutputStream().toString();
    } catch (TransformerException e) {
        e.printStackTrace();
    }
    return xmlResult;
}
 
Example 2
Source File: HandlerChainInvokerTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private String getSourceAsString(Source s) {
    String result = "";

    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        OutputStream out = new ByteArrayOutputStream();
        StreamResult streamResult = new StreamResult();
        streamResult.setOutputStream(out);
        transformer.transform(s, streamResult);
        return streamResult.getOutputStream().toString();
    } catch (Exception e) {
        //do nothing
    }
    return result;
}
 
Example 3
Source File: MetsUtils.java    From proarc with GNU General Public License v3.0 6 votes vote down vote up
/**
 *
 * Validates given document agains an XSD schema
 *
 * @param document
 * @param xsd
 * @return
 */
public static List<String> validateAgainstXSD(Document document, InputStream xsd) throws Exception {
    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    factory.setResourceResolver(MetsLSResolver.getInstance());
    Schema schema = factory.newSchema(new StreamSource(xsd));
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    DOMSource domSource = new DOMSource(document);
    StreamResult sResult = new StreamResult();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    sResult.setOutputStream(bos);
    transformer.transform(domSource, sResult);
    InputStream is = new ByteArrayInputStream(bos.toByteArray());
    DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
    dbfactory.setValidating(false);
    dbfactory.setNamespaceAware(true);
    dbfactory.setSchema(schema);
    DocumentBuilder documentBuilder = dbfactory.newDocumentBuilder();
    ValidationErrorHandler errorHandler = new ValidationErrorHandler();
    documentBuilder.setErrorHandler(errorHandler);
    documentBuilder.parse(is);
    return errorHandler.getValidationErrors();
}
 
Example 4
Source File: SailfishDictionaryToQuckfixjConverter.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public void convertToQuickFixJ(InputStream sailfishXML, String outputDir)
        throws IOException, DOMException, MessageNotFoundException, TransformerException {
    Map<String, Document> outputXMLs = new HashMap<>();
    sailfishDictionary = loader.load(sailfishXML);
    String[] versionFix = sailfishDictionary.getNamespace().split("_");
    minor = Integer.parseInt(versionFix[versionFix.length - 1]);
    major = Integer.parseInt(versionFix[versionFix.length - 2]);

    if (major < 5) {
        outputXMLs.put("FIX" + major + minor + ".xml",
                createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.ALL));
    } else {
        outputXMLs.put("FIXT11.xml", createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.ADMIN));
        outputXMLs.put("FIX" + major + minor + ".xml",
                createQuickFixJDictionaryStructure(documentBuilder.newDocument(), Mode.APP));
    }
    StreamResult outputResult = new StreamResult();
    for (String fileName : outputXMLs.keySet()) {
        try (OutputStream out = FileUtils.openOutputStream(
                new File(outputDir, fileName))) {
            outputResult.setOutputStream(out);
            transformer.transform(new DOMSource(outputXMLs.get(fileName)), outputResult);
        }
    }
}
 
Example 5
Source File: WsdlHandleTask.java    From development with Apache License 2.0 6 votes vote down vote up
private void save(String fileName, Document doc)
        throws TransformerException, IOException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(
            "{http://xml.apache.org/xslt}indent-amount", "4");
    DOMSource source = new DOMSource();
    source.setNode(doc);
    StreamResult result = new StreamResult();
    OutputStream out = new FileOutputStream(fileName);
    result.setOutputStream(out);
    transformer.transform(source, result);
    out.close();
}
 
Example 6
Source File: XMLOutputFactoryImpl.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId) {
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 7
Source File: XMLOutputFactoryImpl.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 8
Source File: XMLOutputFactoryImpl.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 9
Source File: XMLOutputFactoryImpl.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 10
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 11
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static void output(Node node, String encoding,
                          OutputStream outputStream) throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);

    StreamResult result = new StreamResult();
    result.setOutputStream(outputStream);

    transformer.transform(source, result);
}
 
Example 12
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 13
Source File: XMLOutputFactoryImpl.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 14
Source File: SailfishDictionaryToQuckfixjConverter.java    From sailfish-core with Apache License 2.0 5 votes vote down vote up
/**
 * @param converter
 * @param args
 * @param index TODO
 * @throws SAXException
 * @throws IOException
 * @throws TransformerException
 */
private static void resave(SailfishDictionaryToQuckfixjConverter converter, Path quickFIXJDictionary)
        throws SAXException, IOException, TransformerException {
    Document document = null;
    try (InputStream sailfishXML = Files.newInputStream(quickFIXJDictionary)) {
        document = converter.documentBuilder.parse(sailfishXML);
    }
    
    Node fixNode = document.getDocumentElement();
    NodeList list = fixNode.getChildNodes();
    for (int i = 0; i < list.getLength(); i++) {
        Node node = list.item(i);
        switch (node.getNodeName()) {
        case "messages":
        case "components":
            reorder(node, Comparators.NAME, null);
            break;
        case "fields":
            reorder(node, Comparators.NUMBER, Comparators.ENUM);
            break;
        }
    }
    
    StreamResult outputResult = new StreamResult();
    try (OutputStream out = Files.newOutputStream(quickFIXJDictionary)) {
        outputResult.setOutputStream(out);
        converter.transformer.transform(new DOMSource(document), outputResult);
    }
}
 
Example 15
Source File: XmlUtil.java    From mq-http-java-sdk with MIT License 5 votes vote down vote up
public static void output(Node node, String encoding,
                          OutputStream outputStream) throws TransformerException {
    Transformer transformer = transFactory.newTransformer();
    transformer.setOutputProperty("encoding", encoding);

    DOMSource source = new DOMSource();
    source.setNode(node);

    StreamResult result = new StreamResult();
    result.setOutputStream(outputStream);

    transformer.transform(source, result);
}
 
Example 16
Source File: XMLOutputFactoryImpl.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}
 
Example 17
Source File: XMLOutputFactoryImpl.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
/** StreamResult object is re-used and the values are set appropriately.
 */
StreamResult toStreamResult(OutputStream os, Writer writer, String systemId){
    StreamResult sr = new StreamResult();
    sr.setOutputStream(os);
    sr.setWriter(writer);
    sr.setSystemId(systemId);
    return sr;
}