| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.jaxp; |
| 9 | |
| 10 | import net.sf.saxon.Version; |
| 11 | import net.sf.saxon.s9api.SAXDestination; |
| 12 | import net.sf.saxon.s9api.SaxonApiException; |
| 13 | import net.sf.saxon.s9api.Xslt30Transformer; |
| 14 | import org.xml.sax.InputSource; |
| 15 | import org.xml.sax.SAXException; |
| 16 | |
| 17 | import javax.xml.transform.sax.SAXSource; |
| 18 | import java.io.IOException; |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | * <B>StreamingFilterImpl</B> is an XMLFilter (a SAX2 filter) that performs a transformation |
| 23 | * taking a SAX stream as input and producing a SAX stream as output, using XSLT 3.0 streaming |
| 24 | * to process the source |
| 25 | * |
| 26 | * @since 9.8.0.4 |
| 27 | */ |
| 28 | |
| 29 | public class StreamingFilterImpl extends AbstractXMLFilter { |
| 30 | |
| 31 | private Xslt30Transformer transformer; |
| 32 | |
| 33 | StreamingFilterImpl(Xslt30Transformer transformer) { |
| 34 | this.transformer = transformer; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Parse an XML document - In the context of a Transformer, this means |
| 39 | * perform a transformation. The method is equivalent to transform(). |
| 40 | * |
| 41 | * @param input The input source (the XML document to be transformed) |
| 42 | * @throws SAXException Any SAX exception, possibly |
| 43 | * wrapping another exception. |
| 44 | * @throws IOException An IO exception from the parser, |
| 45 | * possibly from a byte stream or character stream |
| 46 | * supplied by the application. |
| 47 | * @see InputSource |
| 48 | * @see #parse(String) |
| 49 | * @see #setEntityResolver |
| 50 | * @see #setDTDHandler |
| 51 | * @see #setContentHandler |
| 52 | * @see #setErrorHandler |
| 53 | */ |
| 54 | |
| 55 | public void parse(InputSource input) throws IOException, SAXException { |
| 56 | if (parser == null) { |
| 57 | try { |
| 58 | parser = Version.platform.loadParser(); |
| 59 | } catch (Exception err) { |
| 60 | throw new SAXException(err); |
| 61 | } |
| 62 | } |
| 63 | if (lexicalHandler != null && lexicalHandler != contentHandler) { |
| 64 | throw new IllegalStateException("ContentHandler and LexicalHandler must be the same object"); |
| 65 | } |
| 66 | SAXSource source = new SAXSource(); |
| 67 | source.setInputSource(input); |
| 68 | source.setXMLReader(parser); |
| 69 | SAXDestination result = new SAXDestination(contentHandler); |
| 70 | |
| 71 | try { |
| 72 | transformer.applyTemplates(source, result); |
| 73 | } catch (SaxonApiException err) { |
| 74 | throw new SAXException(err); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Get the underlying Transformer. This is a Saxon-specific method that allows the |
| 82 | * user to set parameters on the transformation, set a URIResolver or ErrorListener, etc. |
| 83 | * |
| 84 | * @since Saxon 9.8 |
| 85 | */ |
| 86 | |
| 87 | public Xslt30Transformer getTransformer() { |
| 88 | return transformer; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | } |
| 93 |