Search in sources :

Example 81 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class DOMUtils method writeXml.

/**
     * This method writes a DOM document to the given output stream.
     *
     * @param doc
     *            a DOM <code>Document</code>
     * @param encoding
     *            a <code>String</code> with the encoding to use
     * @param docTypeDef
     *            a <code>String</code> with the DTD name; use <code>null</code>
     *            for no DTD
     * @param out
     *            an <code>OutputStream</code> where to write the DOM document
     */
public static void writeXml(Document doc, String encoding, String docTypeDef, OutputStream out) {
    try {
        // prepare the DOM document
        Source source = new DOMSource(doc);
        // prepare the output
        Result result = new StreamResult(out);
        // write the DOM document to the file
        // get Transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (null != docTypeDef) {
            xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeDef);
        }
        // write to a file
        xformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // error generated during transformer configuration
        System.err.println(tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the transformer
        System.err.println(te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 82 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class XsltExtractor method initialize.

public void initialize(TransformerFactory factory) throws InitializationException {
    if (source == null || id == null) {
        throw new InitializationException("Missing source or id");
    }
    if (factory == null) {
        factory = TransformerFactory.newInstance();
        factory.setURIResolver(new BundleURIResolver());
    }
    StreamSource xsltSource = new StreamSource(source.toString());
    xsltSource.setSystemId(source.toString());
    try {
        transformer = factory.newTransformer(xsltSource);
    } catch (TransformerConfigurationException e) {
        throw new InitializationException(e.getMessage(), e);
    }
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource)

Example 83 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class DOMUtils method printXML.

/**
     * This prints the given DOM document to System.out with indentation and
     * utf-8 encoding.
     *
     * @param doc
     *            a DOM <code>Document</code>
     */
public static void printXML(Document doc) {
    try {
        // prepare the DOM document for writing
        Source source = new DOMSource(doc);
        // prepare the output
        Result result = new StreamResult(System.out);
        // write the DOM document to the file
        // get Transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        // write to System.out
        xformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // error generated during transformer configuration
        System.err.println(tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the transformer
        System.err.println(te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 84 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class DOMUtils method writeXml.

/**
     * This method writes a DOM document to the given output stream.
     *
     * @param doc
     *            a DOM <code>Document</code>
     * @param encoding
     *            a <code>String</code> with the encoding to use
     * @param docTypeDef
     *            a <code>String</code> with the DTD name; use <code>null</code>
     *            for no DTD
     * @param out
     *            an <code>OutputStream</code> where to write the DOM document
     */
public static void writeXml(Document doc, String encoding, String docTypeDef, OutputStream out) {
    try {
        // prepare the DOM document
        Source source = new DOMSource(doc);
        // prepare the output
        Result result = new StreamResult(out);
        // write the DOM document to the file
        // get Transformer
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (null != docTypeDef) {
            xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeDef);
        }
        // write to a file
        xformer.transform(source, result);
    } catch (TransformerConfigurationException tce) {
        // error generated during transformer configuration
        System.err.println(tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the transformer
        System.err.println(te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) TransformerException(javax.xml.transform.TransformerException) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 85 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project stanbol by apache.

the class DOMUtils method getStringFromDoc.

/**
     * This returns a string representation of the given document.
     *
     * @param doc
     *            an XML <code>Document</code>
     * @param encoding
     *            a <code>String</code> with the encoding to use
     * @param docTypeDef
     *            a <code>String</code> with the DTD name; use <code>null</code>
     *            for no DTD
     * @return a <code>String</code> with the XML string
     */
public static String getStringFromDoc(Document doc, String encoding, String docTypeDef) {
    try {
        // use a Transformer for output
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer xformer = tFactory.newTransformer();
        xformer.setOutputProperty(OutputKeys.INDENT, "yes");
        xformer.setOutputProperty(OutputKeys.ENCODING, encoding);
        xformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        xformer.setOutputProperty(OutputKeys.METHOD, "xml");
        if (null != docTypeDef) {
            xformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, docTypeDef);
        }
        DOMSource source = new DOMSource(doc);
        StringWriter sw = new StringWriter();
        StreamResult result = new StreamResult(sw);
        xformer.transform(source, result);
        return sw.toString();
    } catch (TransformerConfigurationException tce) {
        // error generated by the parser
        System.err.println("** Transformer Factory error");
        System.err.println("   " + tce.getMessage());
        // use the contained exception, if any
        Throwable x = tce;
        if (tce.getException() != null) {
            x = tce.getException();
        }
        x.printStackTrace();
    } catch (TransformerException te) {
        // error generated by the parser
        System.err.println("** Transformation error");
        System.err.println("   " + te.getMessage());
        // use the contained exception, if any
        Throwable x = te;
        if (te.getException() != null) {
            x = te.getException();
        }
        x.printStackTrace();
    }
    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) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)93 TransformerException (javax.xml.transform.TransformerException)62 Transformer (javax.xml.transform.Transformer)52 StreamResult (javax.xml.transform.stream.StreamResult)49 DOMSource (javax.xml.transform.dom.DOMSource)42 IOException (java.io.IOException)35 TransformerFactory (javax.xml.transform.TransformerFactory)33 StreamSource (javax.xml.transform.stream.StreamSource)23 SAXException (org.xml.sax.SAXException)21 StringWriter (java.io.StringWriter)17 Source (javax.xml.transform.Source)16 TransformerHandler (javax.xml.transform.sax.TransformerHandler)13 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)12 Document (org.w3c.dom.Document)12 ByteArrayInputStream (java.io.ByteArrayInputStream)11 ByteArrayOutputStream (java.io.ByteArrayOutputStream)11 InputStream (java.io.InputStream)10 Node (org.w3c.dom.Node)10 File (java.io.File)9 Result (javax.xml.transform.Result)9