Search in sources :

Example 71 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 72 with TransformerHandler

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

Example 73 with TransformerHandler

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);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) AttributesImpl(org.xml.sax.helpers.AttributesImpl) SAXResult(javax.xml.transform.sax.SAXResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) ConfigException(org.collectionspace.chain.csp.config.ConfigException) IOException(java.io.IOException) ContentHandler(org.xml.sax.ContentHandler) SAXException(org.xml.sax.SAXException)

Example 74 with TransformerHandler

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);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) InputStream(java.io.InputStream) SAXParser(javax.xml.parsers.SAXParser) ConfigException(org.collectionspace.chain.csp.config.ConfigException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXException(org.xml.sax.SAXException)

Example 75 with TransformerHandler

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);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) DOMSource(javax.xml.transform.dom.DOMSource) DOMResult(javax.xml.transform.dom.DOMResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) Document(org.w3c.dom.Document) URL(java.net.URL) SAXResult(javax.xml.transform.sax.SAXResult) TransformerException(javax.xml.transform.TransformerException)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)84 StreamResult (javax.xml.transform.stream.StreamResult)57 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)51 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)33 Transformer (javax.xml.transform.Transformer)29 IOException (java.io.IOException)23 SAXException (org.xml.sax.SAXException)22 AttributesImpl (org.xml.sax.helpers.AttributesImpl)17 StringWriter (java.io.StringWriter)13 SAXResult (javax.xml.transform.sax.SAXResult)13 File (java.io.File)11 XMLReader (org.xml.sax.XMLReader)11 FileOutputStream (java.io.FileOutputStream)10 Test (org.junit.Test)10 InputStream (java.io.InputStream)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 OutputStream (java.io.OutputStream)8 ContentHandler (org.xml.sax.ContentHandler)8 InputSource (org.xml.sax.InputSource)8 Metadata (org.apache.tika.metadata.Metadata)7