Search in sources :

Example 71 with StreamResult

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

the class ConfigXmlGenerator method format.

private String format(final String input, int indent) {
    if (!formatted) {
        return input;
    }
    StreamResult xmlOutput = null;
    try {
        final Source xmlInput = new StreamSource(new StringReader(input));
        xmlOutput = new StreamResult(new StringWriter());
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        /* Older versions of Xalan still use this method of setting indent values.
            * Attempt to make this work but don't completely fail if it's a problem.
            */
        try {
            transformerFactory.setAttribute("indent-number", indent);
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-number attribute; cause: " + e.getMessage());
            }
        }
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        /* Newer versions of Xalan will look for a fully-qualified output property in order to specify amount of
            * indentation to use.  Attempt to make this work as well but again don't completely fail if it's a problem.
            */
        try {
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", Integer.toString(indent));
        } catch (IllegalArgumentException e) {
            if (LOGGER.isFinestEnabled()) {
                LOGGER.finest("Failed to set indent-amount property; cause: " + e.getMessage());
            }
        }
        transformer.transform(xmlInput, xmlOutput);
        return xmlOutput.getWriter().toString();
    } catch (Exception e) {
        LOGGER.warning(e);
        return input;
    } finally {
        if (xmlOutput != null) {
            closeResource(xmlOutput.getWriter());
        }
    }
}
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 72 with StreamResult

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

the class LayoutPresetPersistence method savePreset.

public void savePreset(String name, Layout layout) {
    Preset preset = addPreset(new Preset(name, layout));
    FileOutputStream fos = null;
    try {
        //Create file if dont exist
        FileObject folder = FileUtil.getConfigFile("layoutpresets");
        if (folder == null) {
            folder = FileUtil.getConfigRoot().createFolder("layoutpresets");
        }
        File presetFile = new File(FileUtil.toFile(folder), name + ".xml");
        //Create doc
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = factory.newDocumentBuilder();
        final Document document = documentBuilder.newDocument();
        document.setXmlVersion("1.0");
        document.setXmlStandalone(true);
        //Write doc
        preset.writeXML(document);
        //Write XML file
        fos = new FileOutputStream(presetFile);
        Source source = new DOMSource(document);
        Result result = new StreamResult(fos);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.transform(source, result);
    } catch (Exception e) {
        Logger.getLogger("").log(Level.SEVERE, "Error while writing preset file", e);
    } finally {
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException ex) {
            }
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) IOException(java.io.IOException) Document(org.w3c.dom.Document) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) IOException(java.io.IOException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) FileObject(org.openide.filesystems.FileObject) File(java.io.File)

Example 73 with StreamResult

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

the class ExporterGraphML method transform.

private void transform(Document document) throws TransformerConfigurationException, TransformerException {
    Source source = new DOMSource(document);
    Result result = new StreamResult(writer);
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    transformer.transform(source, result);
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 74 with StreamResult

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

the class XsltStylesheet method retryXsltTransformation.

private String retryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException, IOException {
    Templates templates = getTemplatesFromStreamSource();
    Transformer transf = templates.newTransformer();
    transf.setErrorListener(elistener);
    if (args.length > 1) {
        addParametersToTransformer(context, transf, args[1]);
    }
    PipedWriter pwriter = new PipedWriter();
    PipedReader preader = new PipedReader();
    pwriter.connect(preader);
    StreamResult result = new StreamResult(pwriter);
    transf.transform(domSource, result);
    char[] cbuf = new char[1024];
    int len = preader.read(cbuf, 0, 1024);
    StringBuilder builder = new StringBuilder();
    builder.append(CharBuffer.wrap(cbuf, 0, len));
    // judge from the first chunk
    htmlish = isHtml(builder.toString());
    while (len == 1024) {
        len = preader.read(cbuf, 0, 1024);
        if (len > 0) {
            builder.append(CharBuffer.wrap(cbuf, 0, len));
        }
    }
    preader.close();
    pwriter.close();
    return builder.toString();
}
Also used : PipedWriter(java.io.PipedWriter) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) PipedReader(java.io.PipedReader)

Example 75 with StreamResult

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

the class XslTransformer method main.

public static void main(String[] args) throws TransformerException, IOException {
    if (args.length < 3 || args.length > 4) {
        throw new IllegalArgumentException("USAGE: <style-sheet> <source-file> <dest-file> [dest-dir]");
    }
    File stylesheet = new File(args[0]);
    File source = new File(args[1]);
    File dest = new File(args[2]);
    String destDir = "";
    if (args.length > 3) {
        destDir = args[3];
    }
    System.out.format("=> stylesheet %s%n", stylesheet);
    System.out.format("=> source %s%n", source);
    System.out.format("=> dest %s%n", dest);
    System.out.format("=> destDir %s%n", destDir);
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer(new StreamSource(stylesheet));
    System.out.format("=> transformer %s (%s)%n", transformer, transformer.getClass().getName());
    if (destDir.length() > 0) {
        transformer.setParameter("base.dir", destDir + "/");
    }
    FileOutputStream outstr = new FileOutputStream(dest);
    try {
        transformer.transform(new StreamSource(source), new StreamResult(outstr));
    } finally {
        outstr.close();
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) FileOutputStream(java.io.FileOutputStream) File(java.io.File)

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