Search in sources :

Example 51 with Transformer

use of javax.xml.transform.Transformer in project tinker by Tencent.

the class JavaXmlUtil method saveDocument.

/**
     * save document
     *
     * @param document
     * @param outputFullFilename
     */
public static void saveDocument(final Document document, final String outputFullFilename) {
    OutputStream outputStream = null;
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        DOMSource domSource = new DOMSource(document);
        transformer.setOutputProperty(OutputKeys.ENCODING, Constant.Encoding.UTF8);
        outputStream = new FileOutputStream(outputFullFilename);
        StreamResult result = new StreamResult(outputStream);
        transformer.transform(domSource, result);
    } catch (Exception e) {
        throw new JavaXmlUtilException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.close();
            } catch (Exception e) {
                throw new JavaXmlUtilException(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) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream)

Example 52 with Transformer

use of javax.xml.transform.Transformer in project sonarqube by SonarSource.

the class DebtModelXMLExporter method prettyFormatXml.

private static String prettyFormatXml(String xml) {
    try {
        Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", DEFAULT_INDENT);
        Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8))));
        StreamResult res = new StreamResult(new ByteArrayOutputStream());
        serializer.transform(xmlSource, res);
        return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray(), StandardCharsets.UTF_8);
    } catch (TransformerException ignored) {
    // Ignore, raw XML will be returned
    }
    return xml;
}
Also used : InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InputSource(org.xml.sax.InputSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) TransformerException(javax.xml.transform.TransformerException)

Example 53 with Transformer

use of javax.xml.transform.Transformer in project che by eclipse.

the class Launching method serializeDocumentInt.

/**
     * Serializes a XML document into a string - encoded in UTF8 format,
     * with platform line separators.
     *
     * @param doc document to serialize
     * @return the document as a string
     * @throws TransformerException if an unrecoverable error occurs during the serialization
     * @throws IOException if the encoding attempted to be used is not supported
     */
private static String serializeDocumentInt(Document doc) throws TransformerException, IOException {
    ByteArrayOutputStream s = new ByteArrayOutputStream();
    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    //$NON-NLS-1$
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    DOMSource source = new DOMSource(doc);
    StreamResult outputTarget = new StreamResult(s);
    transformer.transform(source, outputTarget);
    //$NON-NLS-1$
    return s.toString("UTF8");
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 54 with Transformer

use of javax.xml.transform.Transformer in project KeepScore by nolanlawson.

the class XmlHelper method prettyPrint.

public static String prettyPrint(String unformattedXml) {
    try {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        // initialize StreamResult with File object to save to file
        StreamResult result = new StreamResult(new StringWriter());
        StreamSource source = new StreamSource(new StringReader(unformattedXml));
        transformer.transform(source, result);
        return result.getWriter().toString();
    } catch (Throwable t) {
        // should never happen
        throw new RuntimeException(t);
    }
}
Also used : 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)

Example 55 with Transformer

use of javax.xml.transform.Transformer in project openhab1-addons by openhab.

the class Helper method nodeToString.

/***
     * Converts a xml Node into String
     *
     * @param node to convert
     * @return converted string
     */
public static String nodeToString(Node node) {
    StringWriter sw = new StringWriter();
    try {
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), new StreamResult(sw));
    } catch (TransformerException te) {
        logger.warn("nodeToString Transformer Exception", te);
    }
    return sw.toString();
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerException(javax.xml.transform.TransformerException)

Aggregations

Transformer (javax.xml.transform.Transformer)449 StreamResult (javax.xml.transform.stream.StreamResult)354 DOMSource (javax.xml.transform.dom.DOMSource)272 TransformerFactory (javax.xml.transform.TransformerFactory)222 TransformerException (javax.xml.transform.TransformerException)175 StringWriter (java.io.StringWriter)153 Document (org.w3c.dom.Document)114 IOException (java.io.IOException)105 StreamSource (javax.xml.transform.stream.StreamSource)98 Source (javax.xml.transform.Source)97 DocumentBuilder (javax.xml.parsers.DocumentBuilder)70 File (java.io.File)66 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)64 SAXException (org.xml.sax.SAXException)62 Element (org.w3c.dom.Element)59 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)57 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)57 ByteArrayOutputStream (java.io.ByteArrayOutputStream)53 StringReader (java.io.StringReader)52 Result (javax.xml.transform.Result)52