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 iaf by ibissource.
the class TransformerPool method getTransformerHandler.
public TransformerHandler getTransformerHandler() throws TransformerConfigurationException {
TransformerHandler handler = ((SAXTransformerFactory) tFactory).newTransformerHandler(templates);
Transformer transformer = handler.getTransformer();
transformer.setErrorListener(new TransformerErrorListener());
// Set URIResolver on transformer for Xalan. Setting it on the factory
// doesn't work for Xalan. See
// https://www.oxygenxml.com/archives/xsl-list/200306/msg00021.html
transformer.setURIResolver(classLoaderURIResolver);
return handler;
}
use of javax.xml.transform.sax.TransformerHandler in project application by collectionspace.
the class AssemblingContentHandler method apply_xslt.
private void apply_xslt(InputSource xslt, String root) throws SAXException {
String errMsg = String.format("Config Generation: '%s' - Could not create inner parser.", getSrcFileName());
try {
ContentHandler inner = new AssemblingContentHandler(parser, up, false, false, this);
TransformerHandler transformer = transfactory.newTransformerHandler(new StreamSource(xslt.getByteStream()));
transformer.setResult(new SAXResult(inner));
delegated = transformer;
delegated.startDocument();
if (root != null) {
delegated.startElement("", root, root, new AttributesImpl());
}
delegated_root = root;
delegated_depth = 1;
} catch (TransformerConfigurationException e) {
throw new SAXException(errMsg, e);
} catch (ConfigException e) {
throw new SAXException(errMsg, e);
} catch (IOException e) {
throw new SAXException(errMsg, e);
}
}
use of javax.xml.transform.sax.TransformerHandler in project application by collectionspace.
the class AssemblingParser method parse.
public void parse(Result out) throws ConfigException {
String errMsg = String.format("Config Generation: '%s' - Exception raised during parsing.", this.getMain().getPublicId());
try {
String rootpath = AssemblingParser.class.getPackage().getName().replaceAll("\\.", "/") + "/" + root_file;
// load the file at org/collectionspace/chain/csp/config/impl/parser/root.xml
InputStream root = Thread.currentThread().getContextClassLoader().getResourceAsStream(rootpath);
SAXParser parser = factory.newSAXParser();
XMLReader reader = parser.getXMLReader();
TransformerHandler xform = transfactory.newTransformerHandler();
xform.setResult(out);
AssemblingContentHandler assemblingContentHandler = new AssemblingContentHandler(this, xform);
logger.info(String.format("Temporary XMLMerge files will be written out to '%s'.", AssemblingContentHandler.getTempDirectory()));
reader.setContentHandler(assemblingContentHandler);
reader.parse(new InputSource(root));
} catch (IOException e) {
throw new ConfigException(errMsg, e);
} catch (ParserConfigurationException e) {
throw new ConfigException(errMsg, e);
} catch (SAXException e) {
throw new ConfigException(errMsg, e);
} catch (TransformerConfigurationException e) {
throw new ConfigException(errMsg, e);
}
}
use of javax.xml.transform.sax.TransformerHandler in project santuario-java by apache.
the class XIncludeHandler method startElement.
@Override
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
if (xIncludeNS.equals(uri) && xIncludeLN.equals(localName)) {
String href = atts.getValue("href");
if (href == null) {
throw new SAXException("XInclude href attribute is missing");
}
String parse = atts.getValue("parse");
if (parse != null && !"xml".equals(parse)) {
throw new UnsupportedOperationException("Only parse=\"xml\" is currently supported");
}
String xpointer = atts.getValue("xpointer");
URL url = ClassLoaderUtils.getResource(href, XIncludeHandler.class);
if (url == null) {
throw new SAXException("XML file not found: " + href);
}
Document document = null;
try {
document = uriDocMap.get(url.toURI());
} catch (URISyntaxException ex) {
throw new SAXException(ex);
}
if (document == null) {
DOMResult domResult = new DOMResult();
try {
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
saxTransformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
transformerHandler.setResult(domResult);
xmlReader.setContentHandler(new XIncludeHandler(transformerHandler, uriDocMap));
xmlReader.parse(url.toExternalForm());
} catch (TransformerConfigurationException e) {
throw new SAXException(e);
} catch (IOException e) {
throw new SAXException(e);
}
document = (Document) domResult.getNode();
document.setDocumentURI(url.toExternalForm());
try {
uriDocMap.put(url.toURI(), document);
} catch (URISyntaxException e) {
throw new SAXException(e);
}
}
SAXResult saxResult = new SAXResult(this);
skipEvents = true;
try {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
transformerFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);
Transformer transformer = transformerFactory.newTransformer();
if (xpointer == null) {
transformer.transform(new DOMSource(document, document.getDocumentURI()), saxResult);
} else {
NodeList nodeList = evaluateXPointer(xpointer, document);
int length = nodeList.getLength();
for (int i = 0; i < length; i++) {
Node node = nodeList.item(i);
transformer.transform(new DOMSource(node, document.getDocumentURI()), saxResult);
}
}
} catch (TransformerConfigurationException e) {
throw new SAXException(e);
} catch (TransformerException e) {
throw new SAXException(e);
} finally {
skipEvents = false;
}
} else {
this.contentHandler.startElement(uri, localName, qName, atts);
}
}
Aggregations