Debian Codegraph @

net/sf/saxon/jaxp/FilterImpl.java

raw symbols SCIP
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.event.ContentHandlerProxy;
12 import org.xml.sax.InputSource;
13 import org.xml.sax.SAXException;
14
15 import javax.xml.transform.Transformer;
16 import javax.xml.transform.TransformerException;
17 import javax.xml.transform.sax.SAXSource;
18 import java.io.IOException;
19
20
21 /**
22 * <B>FilterImpl</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.
24 *
25 * @author Michael H. Kay
26 */
27
28 public class FilterImpl extends AbstractXMLFilter {
29
30 private TransformerImpl transformer;
31
32 FilterImpl(TransformerImpl transformer) {
33 this.transformer = transformer;
34 }
35
36 /**
37 * Parse an XML document - In the context of a Transformer, this means
38 * perform a transformation. The method is equivalent to transform().
39 *
40 * @param input The input source (the XML document to be transformed)
41 * @throws org.xml.sax.SAXException Any SAX exception, possibly
42 * wrapping another exception.
43 * @throws java.io.IOException An IO exception from the parser,
44 * possibly from a byte stream or character stream
45 * supplied by the application.
46 * @see org.xml.sax.InputSource
47 * @see #parse(java.lang.String)
48 * @see #setEntityResolver
49 * @see #setDTDHandler
50 * @see #setContentHandler
51 * @see #setErrorHandler
52 */
53
54 public void parse(InputSource input) throws IOException, SAXException {
55 if (parser == null) {
56 try {
57 parser = Version.platform.loadParser();
58 } catch (Exception err) {
59 throw new SAXException(err);
60 }
61 }
62 SAXSource source = new SAXSource();
63 source.setInputSource(input);
64 source.setXMLReader(parser);
65 ContentHandlerProxy result = new ContentHandlerProxy();
66 result.setPipelineConfiguration(transformer.getConfiguration().makePipelineConfiguration());
67 result.setUnderlyingContentHandler(contentHandler);
68
69 if (lexicalHandler != null) {
70 result.setLexicalHandler(lexicalHandler);
71 }
72 try {
73 //result.open();
74 result.setOutputProperties(transformer.getOutputProperties());
75 transformer.transform(source, result);
76 } catch (TransformerException err) {
77 Throwable cause = err.getException();
78 if (cause != null && cause instanceof SAXException) {
79 throw (SAXException) cause;
80 } else if (cause != null && cause instanceof IOException) {
81 throw (IOException) cause;
82 } else {
83 throw new SAXException(err);
84 }
85 }
86
87
88 }
89
90 /**
91 * Get the underlying Transformer. This is a Saxon-specific method that allows the
92 * user to set parameters on the transformation, set a URIResolver or ErrorListener, etc.
93 *
94 * @since Saxon 7.2
95 */
96
97 public Transformer getTransformer() {
98 return transformer;
99 }
100
101
102 }
103