Search in sources :

Example 6 with StreamResult

use of javax.xml.transform.stream.StreamResult in project buck by facebook.

the class BuckXmlTestRunListener method addMainTestResult.

/**
   * Adds one more XML element to the test_result.xml tracking the
   * result of the whole process.
   */
private void addMainTestResult() {
    try {
        File resultFile = getResultFile(mReportDir);
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
        Document doc = docBuilder.parse(resultFile);
        Node testsuite = doc.getElementsByTagName("testsuite").item(0);
        if (mRunFailureMessage != null) {
            Element failureNode = doc.createElement("failure");
            failureNode.setTextContent(mRunFailureMessage);
            testsuite.appendChild(failureNode);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(resultFile);
            transformer.transform(source, result);
        }
    } catch (IOException | ParserConfigurationException | SAXException | TransformerException e) {
        throw new RuntimeException(e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) IOException(java.io.IOException) Document(org.w3c.dom.Document) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 7 with StreamResult

use of javax.xml.transform.stream.StreamResult in project OpenAttestation by OpenAttestation.

the class ConverterUtil method formateXMLString.

public static String formateXMLString(String inputXML) {
    StreamResult xmlOutput = null;
    try {
        Source xmlInput = new StreamSource(new StringReader(inputXML));
        StringWriter stringWriter = new StringWriter();
        xmlOutput = new StreamResult(stringWriter);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        transformer.transform(xmlInput, xmlOutput);
    } catch (Exception e) {
        // simple exception handling, please review it
        throw new RuntimeException(e);
    }
    return xmlOutput.getWriter().toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StringWriter(java.io.StringWriter) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Example 8 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jersey by jersey.

the class ResourceExtendedFlagTest method printSource.

public static void printSource(Source source) {
    try {
        System.out.println("---------------------");
        Transformer trans = TransformerFactory.newInstance().newTransformer();
        Properties oprops = new Properties();
        oprops.put(OutputKeys.OMIT_XML_DECLARATION, "yes");
        oprops.put(OutputKeys.INDENT, "yes");
        oprops.put(OutputKeys.METHOD, "xml");
        trans.setOutputProperties(oprops);
        trans.transform(source, new StreamResult(System.out));
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Properties(java.util.Properties) XPathExpressionException(javax.xml.xpath.XPathExpressionException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

Example 9 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jersey by jersey.

the class WadlBeanParamTest method nodeAsString.

private String nodeAsString(final Object resourceNode) throws TransformerException {
    StringWriter writer = new StringWriter();
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.transform(new DOMSource((Node) resourceNode), new StreamResult(writer));
    return writer.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node)

Example 10 with StreamResult

use of javax.xml.transform.stream.StreamResult in project jersey by jersey.

the class SourceEntityProviderTest method extractContent.

private static String extractContent(Source source) throws TransformerFactoryConfigurationError, TransformerConfigurationException, TransformerException {
    TransformerFactory transFactory = TransformerFactory.newInstance();
    // identity transformation
    Transformer transformer = transFactory.newTransformer();
    StringWriter writer = new StringWriter();
    Result result = new StreamResult(writer);
    transformer.transform(source, result);
    return writer.toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)1328 DOMSource (javax.xml.transform.dom.DOMSource)881 Transformer (javax.xml.transform.Transformer)775 StringWriter (java.io.StringWriter)447 TransformerFactory (javax.xml.transform.TransformerFactory)445 Document (org.w3c.dom.Document)415 TransformerException (javax.xml.transform.TransformerException)366 ByteArrayOutputStream (java.io.ByteArrayOutputStream)337 DocumentBuilder (javax.xml.parsers.DocumentBuilder)332 ByteArrayInputStream (java.io.ByteArrayInputStream)288 IOException (java.io.IOException)266 InputStream (java.io.InputStream)249 Test (org.junit.Test)246 StreamSource (javax.xml.transform.stream.StreamSource)213 XMLStreamReader (javax.xml.stream.XMLStreamReader)185 Source (javax.xml.transform.Source)183 File (java.io.File)175 Element (org.w3c.dom.Element)157 InboundXMLSec (org.apache.xml.security.stax.ext.InboundXMLSec)149 XMLSecurityProperties (org.apache.xml.security.stax.ext.XMLSecurityProperties)149