Search in sources :

Example 86 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project CFLint by cflint.

the class HTMLOutput method output.

/**
 * Output bug list in HTML format.
 */
public void output(final BugList bugList, final Writer writer, final CFLintStats stats) throws IOException, TransformerException {
    // 1. Instantiate a TransformerFactory.
    final javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance();
    // 2. Use the TransformerFactory to process the stylesheet Source and
    // generate a Transformer.
    javax.xml.transform.Transformer transformer;
    try {
        final InputStream is = getClass().getResourceAsStream("/findbugs/" + htmlStyle);
        transformer = tFactory.newTransformer(new StreamSource(is));
    } catch (final TransformerConfigurationException e) {
        final InputStream is = getClass().getResourceAsStream("/" + htmlStyle);
        transformer = tFactory.newTransformer(new StreamSource(is));
    }
    final StringWriter sw = new StringWriter();
    new XMLOutput().outputFindBugs(bugList, sw, stats);
    // 3. Use the Transformer to transform an XML Source and send the
    // output to a Result object.
    transformer.transform(new StreamSource(new StringReader(sw.toString())), new StreamResult(writer));
    writer.close();
}
Also used : TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader)

Example 87 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project gocd by gocd.

the class XmlRelaxng method getSchema.

private Schema getSchema(Source source, ThreadContext context) {
    InputStream is = null;
    VerifierFactory factory = new com.thaiopensource.relaxng.jarv.VerifierFactoryImpl();
    if (source instanceof StreamSource) {
        StreamSource ss = (StreamSource) source;
        is = ss.getInputStream();
    } else {
        // if (this.source instanceof DOMSource)
        DOMSource ds = (DOMSource) source;
        StringWriter xmlAsWriter = new StringWriter();
        StreamResult result = new StreamResult(xmlAsWriter);
        try {
            TransformerFactory.newInstance().newTransformer().transform(ds, result);
        } catch (TransformerConfigurationException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        } catch (TransformerException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
        try {
            is = new ByteArrayInputStream(xmlAsWriter.toString().getBytes("UTF-8"));
        } catch (UnsupportedEncodingException ex) {
            throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
        }
    }
    try {
        return factory.compileSchema(is);
    } catch (VerifierConfigurationException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (SAXException ex) {
        throw context.getRuntime().newRuntimeError("Could not parse document: " + ex.getMessage());
    } catch (IOException ex) {
        throw context.getRuntime().newIOError(ex.getMessage());
    }
}
Also used : VerifierFactory(org.iso_relax.verifier.VerifierFactory) DOMSource(javax.xml.transform.dom.DOMSource) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) VerifierConfigurationException(org.iso_relax.verifier.VerifierConfigurationException) SAXException(org.xml.sax.SAXException) StringWriter(java.io.StringWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) TransformerException(javax.xml.transform.TransformerException)

Example 88 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project knime-core by knime.

the class PMMLContentHandler method addPMMLModel.

/**
 * @param fragment the document fragment to add the model to
 * @param spec the pmml port object spec
 * @throws SAXException if the model cannot be added
 */
public final void addPMMLModel(final DocumentFragment fragment, final PMMLPortObjectSpec spec) throws SAXException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    SAXTransformerFactory fac = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler handler;
    try {
        handler = fac.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
        throw new SAXException(e);
    }
    Transformer t = handler.getTransformer();
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(out));
    handler.startDocument();
    /* Here the subclasses can insert the content by overriding the
         * addModelContent method.*/
    addPMMLModelContent(handler, spec);
    handler.endDocument();
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    SAXSource s = new SAXSource(new InputSource(in));
    DOMResult r = new DOMResult(fragment);
    try {
        t.transform(s, r);
        in.close();
        out.close();
    } catch (Exception e) {
        throw new SAXException(e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) SAXSource(javax.xml.transform.sax.SAXSource) DOMResult(javax.xml.transform.dom.DOMResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Example 89 with TransformerConfigurationException

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

the class JsXMLHttpRequest method domToUtf8.

private byte[] domToUtf8(JsSimpleDomNode xml) {
    Node node = xml.getWrappedNode();
    // entire document.
    // if that's an issue, we could code something more complex.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    StreamResult result = new StreamResult(baos);
    DOMSource source = new DOMSource(node);
    try {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
        transformerFactory.newTransformer().transform(source, result);
    } catch (TransformerConfigurationException e) {
        throw new RuntimeException(e);
    } catch (TransformerException e) {
        throw new RuntimeException(e);
    } catch (TransformerFactoryConfigurationError e) {
        throw new RuntimeException(e);
    }
    return baos.toByteArray();
}
Also used : TransformerFactoryConfigurationError(javax.xml.transform.TransformerFactoryConfigurationError) DOMSource(javax.xml.transform.dom.DOMSource) TransformerFactory(javax.xml.transform.TransformerFactory) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Node(org.w3c.dom.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) TransformerException(javax.xml.transform.TransformerException)

Example 90 with TransformerConfigurationException

use of javax.xml.transform.TransformerConfigurationException in project HongsCORE by ihongs.

the class Form method saveDocument.

private void saveDocument(String path, Document docm) throws HongsException {
    File file = new File(path);
    File fold = file.getParentFile();
    if (!fold.exists()) {
        fold.mkdirs();
    }
    TransformerFactory tf = TransformerFactory.newInstance();
    try {
        Transformer tr = tf.newTransformer();
        DOMSource ds = new DOMSource(docm);
        StreamResult sr = new StreamResult(new OutputStreamWriter(new FileOutputStream(file), "utf-8"));
        tr.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        tr.setOutputProperty(OutputKeys.METHOD, "xml");
        tr.setOutputProperty(OutputKeys.INDENT, "yes");
        tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
        tr.transform(ds, sr);
    } catch (TransformerConfigurationException e) {
        throw new HongsException.Common(e);
    } catch (IllegalArgumentException e) {
        throw new HongsException.Common(e);
    } catch (TransformerException e) {
        throw new HongsException.Common(e);
    } catch (FileNotFoundException e) {
        throw new HongsException.Common(e);
    } catch (UnsupportedEncodingException e) {
        throw new HongsException.Common(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) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) HongsException(app.hongs.HongsException) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) 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