Search in sources :

Example 96 with Transformer

use of javax.xml.transform.Transformer in project voltdb by VoltDB.

the class JDBCSQLXML method createDOMSource.

/**
     * Retrieves a new DOMSource for reading the XML value designated by this
     * SQLXML instance. <p>
     *
     * @param sourceClass The class of the source
     * @throws java.sql.SQLException if there is an error processing the XML
     *      value or if the given <tt>sourceClass</tt> is not supported.
     * @return a new DOMSource for reading the XML value designated by this
     *      SQLXML instance
     */
@SuppressWarnings("unchecked")
protected <T extends Source> T createDOMSource(Class<T> sourceClass) throws SQLException {
    DOMSource source = null;
    try {
        source = (sourceClass == null) ? new DOMSource() : (DOMSource) sourceClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.sourceInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.sourceInstantiation(ex);
    }
    Transformer transformer = JDBCSQLXML.getIdentityTransformer();
    InputStream inputStream = this.getBinaryStreamImpl();
    StreamSource streamSource = new StreamSource();
    DOMResult domResult = new DOMResult();
    streamSource.setInputStream(inputStream);
    try {
        transformer.transform(streamSource, domResult);
    } catch (TransformerException ex) {
        throw Exceptions.transformFailed(ex);
    }
    source.setNode(domResult.getNode());
    return (T) source;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) DOMResult(javax.xml.transform.dom.DOMResult) GZIPInputStream(java.util.zip.GZIPInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) TransformerException(javax.xml.transform.TransformerException)

Example 97 with Transformer

use of javax.xml.transform.Transformer in project zm-mailbox by Zimbra.

the class QuotedTextUtil method getHtml.

/**
     * Convert the DOM document back to HTML string
     *
     * @param document
     * @return String the String representation of the DOM document
     */
private String getHtml(Document document) {
    try {
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(new DOMSource(document), result);
        return writer.toString();
    } catch (TransformerException e) {
        ZimbraLog.soap.warn("Exception in converting DOM to html", e);
    }
    return null;
}
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) TransformerException(javax.xml.transform.TransformerException)

Example 98 with Transformer

use of javax.xml.transform.Transformer in project zm-mailbox by Zimbra.

the class XsdCleaner method getTranformer.

public static Transformer getTranformer() {
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        return transformer;
    } catch (TransformerFactoryConfigurationError | TransformerException e) {
        LOG.error("XsdCleaner:Problem getting XML Transformer", e);
    }
    return null;
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerException(javax.xml.transform.TransformerException)

Example 99 with Transformer

use of javax.xml.transform.Transformer in project intellij-community by JetBrains.

the class XsltDocumentationProvider method getDocumentation.

@Nullable
private String getDocumentation(String name, String type) {
    try {
        final Transformer transformer = getTemplate().newTransformer();
        transformer.setParameter("element", name);
        transformer.setParameter("type", type);
        final StringWriter writer = new StringWriter();
        transformer.transform(new JDOMSource(getDocumentationDocument()), new StreamResult(writer));
        final String s = writer.toString();
        final Matcher matcher = check.matcher(s);
        if (matcher.find()) {
            if (matcher.group(1).equals("true")) {
                return s.replaceFirst("<META.+?>", "");
            }
        }
    } catch (Exception e) {
        Logger.getInstance(getClass().getName()).error(e);
    }
    return null;
}
Also used : Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) Matcher(java.util.regex.Matcher) JDOMSource(org.jdom.transform.JDOMSource) JDOMException(org.jdom.JDOMException) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) Nullable(org.jetbrains.annotations.Nullable)

Example 100 with Transformer

use of javax.xml.transform.Transformer in project jangaroo-tools by CoreMedia.

the class PomConverter method writePom.

/**
   * Serializes the given DOM document into a POM file within the given directory.
   */
private static void writePom(Document document, File projectBaseDir) throws MojoExecutionException {
    try {
        File pomFile = new File(projectBaseDir, "pom.xml");
        // keep trailing whitespace because it's not reproduced by the transformer and we want to keep the diff small
        String pom = readFileToString(pomFile);
        String trailingWhitespace = pom.substring(pom.lastIndexOf('>') + 1);
        PrintWriter pomWriter = new PrintWriter(pomFile);
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            // the transformer does not reproduce the new line after the XML declaration, so we do it on our own
            // see https://bugs.openjdk.java.net/browse/JDK-7150637
            transformer.setOutputProperty(OMIT_XML_DECLARATION, "yes");
            if (document.getXmlEncoding() != null) {
                pomWriter.print("<?xml version=\"");
                pomWriter.print(document.getXmlVersion());
                pomWriter.print("\" encoding=\"");
                pomWriter.print(document.getXmlEncoding());
                pomWriter.println("\"?>");
            }
            transformer.transform(new DOMSource(document), new StreamResult(pomWriter));
            pomWriter.write(trailingWhitespace);
        } finally {
            pomWriter.close();
        }
    } catch (IOException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    } catch (TransformerException e) {
        throw new MojoExecutionException("error while generating modified POM", e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileUtils.readFileToString(org.apache.commons.io.FileUtils.readFileToString) IOException(java.io.IOException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException) PrintWriter(java.io.PrintWriter)

Aggregations

Transformer (javax.xml.transform.Transformer)348 StreamResult (javax.xml.transform.stream.StreamResult)267 DOMSource (javax.xml.transform.dom.DOMSource)211 TransformerFactory (javax.xml.transform.TransformerFactory)159 TransformerException (javax.xml.transform.TransformerException)137 StringWriter (java.io.StringWriter)125 Document (org.w3c.dom.Document)95 IOException (java.io.IOException)80 StreamSource (javax.xml.transform.stream.StreamSource)78 Source (javax.xml.transform.Source)76 DocumentBuilder (javax.xml.parsers.DocumentBuilder)59 Element (org.w3c.dom.Element)53 File (java.io.File)50 SAXException (org.xml.sax.SAXException)50 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)48 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)46 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)44 StringReader (java.io.StringReader)43 Result (javax.xml.transform.Result)42 InputSource (org.xml.sax.InputSource)38