Search in sources :

Example 76 with StreamResult

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

the class GraphHopperServlet method errorsToXML.

protected String errorsToXML(Collection<Throwable> list) {
    if (list.isEmpty())
        throw new RuntimeException("errorsToXML should not be called with an empty list");
    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.newDocument();
        Element gpxElement = doc.createElement("gpx");
        gpxElement.setAttribute("creator", "GraphHopper");
        gpxElement.setAttribute("version", "1.1");
        doc.appendChild(gpxElement);
        Element mdElement = doc.createElement("metadata");
        gpxElement.appendChild(mdElement);
        Element extensionsElement = doc.createElement("extensions");
        mdElement.appendChild(extensionsElement);
        Element messageElement = doc.createElement("message");
        extensionsElement.appendChild(messageElement);
        messageElement.setTextContent(list.iterator().next().getMessage());
        Element hintsElement = doc.createElement("hints");
        extensionsElement.appendChild(hintsElement);
        for (Throwable t : list) {
            Element error = doc.createElement("error");
            hintsElement.appendChild(error);
            error.setAttribute("message", t.getMessage());
            error.setAttribute("details", t.getClass().getName());
        }
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(writer));
        return writer.toString();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 77 with StreamResult

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

the class ReportGenerator method XMLToHtml.

public static File XMLToHtml(Document xmlDocument, String infilexsl, File outFile) {
    File stylesheet = null;
    outFile = new File(outFile.getAbsolutePath());
    try {
        stylesheet = new File(infilexsl);
        DOMSource source = new DOMSource(xmlDocument);
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = tFactory.newTransformer(stylesource);
        // Make the transformation and write to the output file
        StreamResult result = new StreamResult(outFile);
        transformer.transform(source, result);
    } catch (TransformerException e) {
        logger.error(e.getMessage(), e);
    }
    return outFile;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 78 with StreamResult

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

the class ReportGenerator method getDebugXMLString.

public static String getDebugXMLString(Document doc) throws TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    StringWriter writer = new StringWriter();
    transformer.transform(new DOMSource(doc), new StreamResult(writer));
    return writer.getBuffer().toString().replaceAll("\n|\r", "");
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult)

Example 79 with StreamResult

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

the class FileXML method saveFile.

public void saveFile(String fileName) {
    //		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    //factory.setNamespaceAware(true);
    //factory.setValidating(true);
    File file = null;
    FileOutputStream outFile = null;
    try {
        file = new File(fileName);
        outFile = new FileOutputStream(file);
        // Use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();
        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(outFile);
        //StreamResult result = new StreamResult(System.out);
        transformer.transform(source, result);
    } catch (TransformerException | IOException e) {
        logger.error(e.getMessage(), e);
    } finally {
        if (outFile != null) {
            try {
                outFile.close();
            } catch (IOException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 80 with StreamResult

use of javax.xml.transform.stream.StreamResult in project crawler4j by yasserg.

the class BinaryParseData method getTransformerHandler.

/**
     * Returns a transformer handler that serializes incoming SAX events to
     * XHTML or HTML (depending the given method) using the given output encoding.
     *
     * @param encoding output encoding, or <code>null</code> for the platform default
     */
private static TransformerHandler getTransformerHandler(OutputStream out, String method, String encoding) throws TransformerConfigurationException {
    TransformerHandler transformerHandler = SAX_TRANSFORMER_FACTORY.newTransformerHandler();
    Transformer transformer = transformerHandler.getTransformer();
    transformer.setOutputProperty(OutputKeys.METHOD, method);
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    if (encoding != null) {
        transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
    }
    transformerHandler.setResult(new StreamResult(new PrintStream(out)));
    return transformerHandler;
}
Also used : PrintStream(java.io.PrintStream) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult)

Aggregations

StreamResult (javax.xml.transform.stream.StreamResult)448 Transformer (javax.xml.transform.Transformer)267 DOMSource (javax.xml.transform.dom.DOMSource)234 StringWriter (java.io.StringWriter)206 TransformerFactory (javax.xml.transform.TransformerFactory)138 TransformerException (javax.xml.transform.TransformerException)125 Document (org.w3c.dom.Document)103 IOException (java.io.IOException)94 StreamSource (javax.xml.transform.stream.StreamSource)88 Source (javax.xml.transform.Source)74 Test (org.junit.Test)73 DocumentBuilder (javax.xml.parsers.DocumentBuilder)65 ByteArrayOutputStream (java.io.ByteArrayOutputStream)64 Element (org.w3c.dom.Element)59 File (java.io.File)58 Result (javax.xml.transform.Result)57 StringReader (java.io.StringReader)56 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)53 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)50 ByteArrayInputStream (java.io.ByteArrayInputStream)44