Search in sources :

Example 41 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project tika by apache.

the class HtmlParserTest method makeHtmlTransformer.

/**
     * Create ContentHandler that transforms SAX events into textual HTML output,
     * and writes it out to <writer> - typically this is a StringWriter.
     *
     * @param writer Where to write resulting HTML text.
     * @return ContentHandler suitable for passing to parse() methods.
     * @throws Exception
     */
private ContentHandler makeHtmlTransformer(Writer writer) throws Exception {
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
    handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "no");
    handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
    handler.setResult(new StreamResult(writer));
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 42 with TransformerHandler

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

the class PMMLDiscretizePreprocPortObjectSpec method createTransformerHandlerForSave.

static TransformerHandler createTransformerHandlerForSave(final OutputStream out) throws TransformerConfigurationException, SAXException {
    SAXTransformerFactory fac = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler handler = fac.newTransformerHandler();
    Transformer t = handler.getTransformer();
    // t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(out));
    handler.startDocument();
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 43 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler 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 44 with TransformerHandler

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

the class PMMLPreprocPortObject method createTransformerHandlerForSave.

// /**
// * {@inheritDoc}
// */
// @Override
// public String toString()  {
// if (m_operations.size() == 0) {
// return "";
// }
// try {
// int size = m_operations.size();
// final double subProgress = 1.0 / size;
// int n = 0;
// 
// for (PMMLPreprocOperation op : m_operations) {
// out.putNextEntry(new ZipEntry(op.getClass().getName()));
// 
// PortObjectZipOutputStreamAndString sout
// = new PortObjectZipOutputStreamAndString(out);
// TransformerHandler handler =
// createTransformerHandlerForSave(sout);
// op.save(handler,
// exec.createSubProgress(subProgress));
// handler.endElement(null, null, LOCAL_TRANS);
// handler.endDocument();
// System.out.println(sout.getString());
// out.closeEntry();
// exec.setProgress(subProgress * ++n);
// exec.checkCanceled();
// }
// out.close();
// } catch (Exception e) {
// throw new IOException(e);
// }
// }
static TransformerHandler createTransformerHandlerForSave(final OutputStream out) throws TransformerConfigurationException, SAXException {
    SAXTransformerFactory fac = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler handler = fac.newTransformerHandler();
    Transformer t = handler.getTransformer();
    t.setOutputProperty(OutputKeys.METHOD, "xml");
    t.setOutputProperty(OutputKeys.INDENT, "yes");
    handler.setResult(new StreamResult(out));
    handler.startDocument();
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 45 with TransformerHandler

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

the class PMMLPreprocPortObject method save.

/**
 * {@inheritDoc}
 */
@Override
protected void save(final PortObjectZipOutputStream out, final ExecutionMonitor exec) throws IOException, CanceledExecutionException {
    if (m_operations.size() == 0) {
        return;
    }
    try {
        int size = m_operations.size();
        final double subProgress = 1.0 / size;
        int n = 0;
        for (PMMLPreprocOperation op : m_operations) {
            out.putNextEntry(new ZipEntry(op.getClass().getName()));
            PortObjectZipOutputStreamAndString sout = new PortObjectZipOutputStreamAndString(out);
            TransformerHandler handler = createTransformerHandlerForSave(sout);
            String writeElement = op.getTransformElement().toString();
            handler.startElement("", "", writeElement, null);
            op.save(handler, exec.createSubProgress(subProgress));
            handler.endElement("", "", writeElement);
            handler.endDocument();
            out.closeEntry();
            exec.setProgress(subProgress * ++n);
            exec.checkCanceled();
        }
        out.close();
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) CanceledExecutionException(org.knime.core.node.CanceledExecutionException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)46 StreamResult (javax.xml.transform.stream.StreamResult)33 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)29 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 Transformer (javax.xml.transform.Transformer)16 SAXException (org.xml.sax.SAXException)13 StringWriter (java.io.StringWriter)11 AttributesImpl (org.xml.sax.helpers.AttributesImpl)11 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 Metadata (org.apache.tika.metadata.Metadata)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 OutputStream (java.io.OutputStream)5 FileOutputStream (java.io.FileOutputStream)4 TransformerException (javax.xml.transform.TransformerException)4 ParseContext (org.apache.tika.parser.ParseContext)4 Test (org.junit.Test)4 OutputStreamWriter (java.io.OutputStreamWriter)3