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;
}
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;
}
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);
}
}
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;
}
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);
}
}
Aggregations