Search in sources :

Example 36 with TransformerConfigurationException

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());
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) 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) SAXException(org.xml.sax.SAXException)

Example 37 with TransformerConfigurationException

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
        }
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DOMSource(javax.xml.transform.dom.DOMSource) Source(javax.xml.transform.Source) InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) SAXException(org.xml.sax.SAXException) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) TransformerException(javax.xml.transform.TransformerException) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 38 with TransformerConfigurationException

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);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) SAXSource(javax.xml.transform.sax.SAXSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StaxSource(org.apache.cxf.staxutils.StaxSource) XMLFilter(org.xml.sax.XMLFilter) Templates(javax.xml.transform.Templates) StaxSource(org.apache.cxf.staxutils.StaxSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) SAXSource(javax.xml.transform.sax.SAXSource) XMLReader(org.xml.sax.XMLReader) TransformerException(javax.xml.transform.TransformerException)

Example 39 with TransformerConfigurationException

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();
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) DOMResult(javax.xml.transform.dom.DOMResult)

Example 40 with TransformerConfigurationException

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);
    }
}
Also used : Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) DocumentSource(org.dom4j.io.DocumentSource) Element(org.dom4j.Element) Tuple(org.apereo.portal.utils.Tuple) 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