Search in sources :

Example 6 with Transformer

use of javax.xml.transform.Transformer 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 Transformer

use of javax.xml.transform.Transformer 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 Transformer

use of javax.xml.transform.Transformer 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 Transformer

use of javax.xml.transform.Transformer 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 Transformer

use of javax.xml.transform.Transformer 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

Transformer (javax.xml.transform.Transformer)1034 StreamResult (javax.xml.transform.stream.StreamResult)799 DOMSource (javax.xml.transform.dom.DOMSource)644 TransformerFactory (javax.xml.transform.TransformerFactory)511 TransformerException (javax.xml.transform.TransformerException)398 StringWriter (java.io.StringWriter)322 IOException (java.io.IOException)264 Document (org.w3c.dom.Document)260 StreamSource (javax.xml.transform.stream.StreamSource)204 Source (javax.xml.transform.Source)188 File (java.io.File)162 DocumentBuilder (javax.xml.parsers.DocumentBuilder)149 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)145 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)142 Element (org.w3c.dom.Element)136 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)124 SAXException (org.xml.sax.SAXException)112 StringReader (java.io.StringReader)108 Result (javax.xml.transform.Result)106 ByteArrayOutputStream (java.io.ByteArrayOutputStream)104