use of javax.xml.transform.TransformerConfigurationException in project knime-core by knime.
the class PMMLContentHandler method getPMMLModelFragment.
/**
* @param spec the pmml port object spec
* @return an input stream containing the model fragment
* @throws SAXException if the model cannot be added
*/
public final ByteArrayInputStream getPMMLModelFragment(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();
return new ByteArrayInputStream(out.toByteArray());
}
use of javax.xml.transform.TransformerConfigurationException in project knime-core by knime.
the class PMMLContentHandler method parse.
/**
* Parses the given node.
*
* @param node the node to be parsed
* @throws SAXException if something with the parsing goes wrong
*/
public void parse(final Node node) throws SAXException {
SAXParserFactory fac = SAXParserFactory.newInstance();
SAXParser parser;
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayInputStream in = null;
try {
parser = fac.newSAXParser();
Transformer t = TransformerFactory.newInstance().newTransformer();
Source source = new DOMSource(node);
t.transform(source, new StreamResult(out));
in = new ByteArrayInputStream(out.toByteArray());
parser.parse(new InputSource(in), this);
} catch (ParserConfigurationException e) {
throw new SAXException(e);
} catch (TransformerConfigurationException e) {
throw new SAXException(e);
} catch (TransformerException e) {
throw new SAXException(e);
} catch (IOException e) {
throw new SAXException(e);
} finally {
try {
out.close();
if (in != null) {
in.close();
}
} catch (IOException e) {
// ignore if closing the streams fail
}
}
}
use of javax.xml.transform.TransformerConfigurationException in project cxf by apache.
the class XSLTJaxbProvider method unmarshalFromInputStream.
@Override
protected Object unmarshalFromInputStream(Unmarshaller unmarshaller, InputStream is, Annotation[] anns, MediaType mt) throws JAXBException {
try {
Templates t = createTemplates(getInTemplates(anns, mt), inParamsMap, inProperties);
if (t == null && supportJaxbOnly) {
return super.unmarshalFromInputStream(unmarshaller, is, anns, mt);
}
if (unmarshaller.getClass().getName().contains("eclipse")) {
// eclipse MOXy doesn't work properly with the XMLFilter/Reader thing
// so we need to bounce through a DOM
Source reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
DOMResult dom = new DOMResult();
t.newTransformer().transform(reader, dom);
return unmarshaller.unmarshal(dom.getNode());
}
XMLFilter filter = null;
try {
filter = factory.newXMLFilter(t);
} catch (TransformerConfigurationException ex) {
TemplatesImpl ti = (TemplatesImpl) t;
filter = factory.newXMLFilter(ti.getTemplates());
trySettingProperties(filter, ti);
}
XMLReader reader = new StaxSource(StaxUtils.createXMLStreamReader(is));
filter.setParent(reader);
SAXSource source = new SAXSource();
source.setXMLReader(filter);
if (systemId != null) {
source.setSystemId(systemId);
}
return unmarshaller.unmarshal(source);
} catch (TransformerException ex) {
LOG.warning("Transformation exception : " + ex.getMessage());
throw ExceptionUtils.toInternalServerErrorException(ex, null);
}
}
use of javax.xml.transform.TransformerConfigurationException in project cxf by apache.
the class XSLTJaxbProvider method marshalToOutputStream.
@Override
protected void marshalToOutputStream(Marshaller ms, Object obj, OutputStream os, Annotation[] anns, MediaType mt) throws Exception {
Templates t = createTemplates(getOutTemplates(anns, mt), outParamsMap, outProperties);
if (t == null && supportJaxbOnly) {
super.marshalToOutputStream(ms, obj, os, anns, mt);
return;
}
org.apache.cxf.common.jaxb.JAXBUtils.setMinimumEscapeHandler(ms);
TransformerHandler th = null;
try {
th = factory.newTransformerHandler(t);
} catch (TransformerConfigurationException ex) {
TemplatesImpl ti = (TemplatesImpl) t;
th = factory.newTransformerHandler(ti.getTemplates());
this.trySettingProperties(th, ti);
}
Result result = new StreamResult(os);
if (systemId != null) {
result.setSystemId(systemId);
}
th.setResult(result);
if (getContext() == null) {
th.startDocument();
}
ms.marshal(obj, th);
if (getContext() == null) {
th.endDocument();
}
}
use of javax.xml.transform.TransformerConfigurationException in project uPortal by Jasig.
the class AbstractDom4jExporter method marshal.
/* (non-Javadoc)
* @see org.springframework.oxm.Marshaller#marshal(java.lang.Object, javax.xml.transform.Result)
*/
@Override
public void marshal(Object graph, Result result) throws IOException, XmlMappingException {
@SuppressWarnings("unchecked") final Tuple<String, Element> data = (Tuple<String, Element>) graph;
final Transformer transformer;
try {
transformer = this.xmlUtilities.getIdentityTransformer();
} catch (TransformerConfigurationException e) {
throw new RuntimeException("Failed to load identity Transformer", e);
}
// Setup the transformer to pretty-print the output
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
try {
transformer.transform(new DocumentSource(data.second), result);
} catch (TransformerException e) {
throw new RuntimeException("Failed to write Element to Result for: " + data.first, e);
}
}
Aggregations