| 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 org.xml.sax.*; |
| 11 | import org.xml.sax.ext.LexicalHandler; |
| 12 | |
| 13 | import java.io.IOException; |
| 14 | |
| 15 | |
| 16 | /** |
| 17 | * <B>AbstractFilterImpl</B> is skeletal implementation of the SAX XMLFilter interface, used |
| 18 | * for both streaming and non-streaming filters. |
| 19 | * |
| 20 | * @author Michael H. Kay |
| 21 | */ |
| 22 | |
| 23 | public abstract class AbstractXMLFilter implements XMLFilter { |
| 24 | |
| 25 | XMLReader parser; |
| 26 | ContentHandler contentHandler; // destination for output of this filter |
| 27 | LexicalHandler lexicalHandler; // destination for output of this filter |
| 28 | |
| 29 | |
| 30 | ////////////////////////////////////////////////////////////////// |
| 31 | // Implement XMLFilter interface methods |
| 32 | ////////////////////////////////////////////////////////////////// |
| 33 | |
| 34 | /** |
| 35 | * Set the parent reader. |
| 36 | * <p>This method allows the application to link the filter to |
| 37 | * a parent reader (which may be another filter). The argument |
| 38 | * may not be null.</p> |
| 39 | * |
| 40 | * @param parent The parent reader (the supplier of SAX events). |
| 41 | */ |
| 42 | |
| 43 | public void setParent(XMLReader parent) { |
| 44 | parser = parent; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Get the parent reader. |
| 49 | * <p>This method allows the application to query the parent |
| 50 | * reader (which may be another filter). It is generally a |
| 51 | * bad idea to perform any operations on the parent reader |
| 52 | * directly: they should all pass through this filter.</p> |
| 53 | * |
| 54 | * @return The parent filter, or null if none has been set. |
| 55 | */ |
| 56 | |
| 57 | public XMLReader getParent() { |
| 58 | return parser; |
| 59 | } |
| 60 | |
| 61 | /////////////////////////////////////////////////////////////////// |
| 62 | // implement XMLReader interface methods |
| 63 | /////////////////////////////////////////////////////////////////// |
| 64 | |
| 65 | /** |
| 66 | * Look up the value of a feature. |
| 67 | * <p>The feature name is any fully-qualified URI. It is |
| 68 | * possible for an XMLReader to recognize a feature name but |
| 69 | * to be unable to return its value; this is especially true |
| 70 | * in the case of an adapter for a SAX1 Parser, which has |
| 71 | * no way of knowing whether the underlying parser is |
| 72 | * performing validation or expanding external entities.</p> |
| 73 | * <p>All XMLReaders are required to recognize the |
| 74 | * http://xml.org/sax/features/namespaces and the |
| 75 | * http://xml.org/sax/features/namespace-prefixes feature names.</p> |
| 76 | * |
| 77 | * @param name The feature name, which is a fully-qualified URI. |
| 78 | * @return The current state of the feature (true or false). |
| 79 | * @throws SAXNotRecognizedException |
| 80 | * When the |
| 81 | * XMLReader does not recognize the feature name. |
| 82 | * @throws SAXNotSupportedException |
| 83 | * When the |
| 84 | * XMLReader recognizes the feature name but |
| 85 | * cannot determine its value at this time. |
| 86 | * @see #setFeature |
| 87 | */ |
| 88 | |
| 89 | public boolean getFeature(String name) |
| 90 | throws SAXNotRecognizedException, SAXNotSupportedException { |
| 91 | return parser.getFeature(name); |
| 92 | } |
| 93 | |
| 94 | |
| 95 | /** |
| 96 | * Set the state of a feature. |
| 97 | * <p>The feature name is any fully-qualified URI. It is |
| 98 | * possible for an XMLReader to recognize a feature name but |
| 99 | * to be unable to set its value</p> |
| 100 | * <p>All XMLReaders are required to support setting |
| 101 | * http://xml.org/sax/features/namespaces to true and |
| 102 | * http://xml.org/sax/features/namespace-prefixes to false.</p> |
| 103 | * <p>Some feature values may be immutable or mutable only |
| 104 | * in specific contexts, such as before, during, or after |
| 105 | * a parse.</p> |
| 106 | * |
| 107 | * @param name The feature name, which is a fully-qualified URI. |
| 108 | * @param value The requested state of the feature (true or false). |
| 109 | * @throws SAXNotRecognizedException |
| 110 | * When the |
| 111 | * XMLReader does not recognize the feature name. |
| 112 | * @throws SAXNotSupportedException |
| 113 | * When the |
| 114 | * XMLReader recognizes the feature name but |
| 115 | * cannot set the requested value. |
| 116 | * @see #getFeature |
| 117 | */ |
| 118 | |
| 119 | public void setFeature(String name, boolean value) |
| 120 | throws SAXNotRecognizedException, SAXNotSupportedException { |
| 121 | parser.setFeature(name, value); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Look up the value of a property. |
| 126 | * <p>The property name is any fully-qualified URI. It is |
| 127 | * possible for an XMLReader to recognize a property name but |
| 128 | * to be unable to return its state.</p> |
| 129 | * <p>XMLReaders are not required to recognize any specific |
| 130 | * property names, though an initial core set is documented for |
| 131 | * SAX2.</p> |
| 132 | * <p>Some property values may be available only in specific |
| 133 | * contexts, such as before, during, or after a parse.</p> |
| 134 | * <p>Implementors are free (and encouraged) to invent their own properties, |
| 135 | * using names built on their own URIs.</p> |
| 136 | * |
| 137 | * @param name The property name, which is a fully-qualified URI. |
| 138 | * @return The current value of the property. |
| 139 | * @throws SAXNotRecognizedException |
| 140 | * When the |
| 141 | * XMLReader does not recognize the property name. |
| 142 | * @see #setProperty |
| 143 | */ |
| 144 | |
| 145 | public Object getProperty(String name) |
| 146 | throws SAXNotRecognizedException { |
| 147 | if (name.equals("http://xml.org/sax/properties/lexical-handler")) { |
| 148 | return lexicalHandler; |
| 149 | } else { |
| 150 | throw new SAXNotRecognizedException(name); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | |
| 155 | /** |
| 156 | * Set the value of a property. |
| 157 | * <p>The property name is any fully-qualified URI. It is |
| 158 | * possible for an XMLReader to recognize a property name but |
| 159 | * to be unable to set its value.</p> |
| 160 | * <p>XMLReaders are not required to recognize setting |
| 161 | * any specific property names, though a core set is provided with |
| 162 | * SAX2.</p> |
| 163 | * <p>Some property values may be immutable or mutable only |
| 164 | * in specific contexts, such as before, during, or after |
| 165 | * a parse.</p> |
| 166 | * <p>This method is also the standard mechanism for setting |
| 167 | * extended handlers.</p> |
| 168 | * |
| 169 | * @param name The property name, which is a fully-qualified URI. |
| 170 | * @param value The requested value for the property. |
| 171 | * @throws SAXNotRecognizedException |
| 172 | * When the |
| 173 | * XMLReader does not recognize the property name. |
| 174 | * @throws SAXNotSupportedException |
| 175 | * When the |
| 176 | * XMLReader recognizes the property name but |
| 177 | * cannot set the requested value. |
| 178 | */ |
| 179 | |
| 180 | public void setProperty(String name, Object value) |
| 181 | throws SAXNotRecognizedException, SAXNotSupportedException { |
| 182 | if (name.equals("http://xml.org/sax/properties/lexical-handler")) { |
| 183 | if (value instanceof LexicalHandler) { |
| 184 | lexicalHandler = (LexicalHandler) value; |
| 185 | } else { |
| 186 | throw new SAXNotSupportedException( |
| 187 | "Lexical Handler must be instance of org.xml.sax.ext.LexicalHandler"); |
| 188 | } |
| 189 | } else { |
| 190 | throw new SAXNotRecognizedException(name); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Register a content handler to receive the output of the transformation |
| 196 | * filter. If the content handler is also a LexicalHandler, and if no LexicalHandler |
| 197 | * is separately registered, the ContentHandler will also act as the LexicalHandler |
| 198 | */ |
| 199 | |
| 200 | public void setContentHandler(ContentHandler handler) { |
| 201 | contentHandler = handler; |
| 202 | if (handler instanceof LexicalHandler && lexicalHandler == null) { |
| 203 | lexicalHandler = (LexicalHandler) handler; |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Get the ContentHandler registered using setContentHandler() |
| 209 | */ |
| 210 | |
| 211 | public ContentHandler getContentHandler() { |
| 212 | return contentHandler; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Allow an application to register an entity resolver. |
| 218 | * <p>If the application does not register an entity resolver, |
| 219 | * the XMLReader will perform its own default resolution.</p> |
| 220 | * <p>Applications may register a new or different resolver in the |
| 221 | * middle of a parse, and the SAX parser must begin using the new |
| 222 | * resolver immediately.</p> |
| 223 | * |
| 224 | * @param resolver The entity resolver. |
| 225 | * @throws NullPointerException If the resolver |
| 226 | * argument is null. |
| 227 | * @see #getEntityResolver |
| 228 | */ |
| 229 | |
| 230 | public void setEntityResolver(EntityResolver resolver) { |
| 231 | // XSLT output does not use entities, so the resolver is never used |
| 232 | } |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Return the current entity resolver. |
| 237 | * |
| 238 | * @return Always null, since no entity resolver is used even if one |
| 239 | * is supplied. |
| 240 | * @see #setEntityResolver |
| 241 | */ |
| 242 | |
| 243 | /*@Nullable*/ |
| 244 | public EntityResolver getEntityResolver() { |
| 245 | return null; |
| 246 | } |
| 247 | |
| 248 | |
| 249 | /** |
| 250 | * Allow an application to register a DTD event handler. |
| 251 | * <p>If the application does not register a DTD handler, all DTD |
| 252 | * events reported by the SAX parser will be silently ignored.</p> |
| 253 | * <p>Applications may register a new or different handler in the |
| 254 | * middle of a parse, and the SAX parser must begin using the new |
| 255 | * handler immediately.</p> |
| 256 | * |
| 257 | * @param handler The DTD handler. |
| 258 | * @throws NullPointerException If the handler |
| 259 | * argument is null. |
| 260 | * @see #getDTDHandler |
| 261 | */ |
| 262 | |
| 263 | public void setDTDHandler(DTDHandler handler) { |
| 264 | // XSLT output does not include a DTD |
| 265 | } |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Return the current DTD handler. |
| 270 | * |
| 271 | * @return Always null, since no DTD handler is used even if one has been |
| 272 | * supplied. |
| 273 | * @see #setDTDHandler |
| 274 | */ |
| 275 | |
| 276 | /*@Nullable*/ |
| 277 | public DTDHandler getDTDHandler() { |
| 278 | return null; |
| 279 | } |
| 280 | |
| 281 | |
| 282 | /** |
| 283 | * Allow an application to register an error event handler. |
| 284 | * <p>If the application does not register an error handler, all |
| 285 | * error events reported by the SAX parser will be silently |
| 286 | * ignored; however, normal processing may not continue. It is |
| 287 | * highly recommended that all SAX applications implement an |
| 288 | * error handler to avoid unexpected bugs.</p> |
| 289 | * <p>Applications may register a new or different handler in the |
| 290 | * middle of a parse, and the SAX parser must begin using the new |
| 291 | * handler immediately.</p> |
| 292 | * |
| 293 | * @param handler The error handler. |
| 294 | * @throws NullPointerException If the handler |
| 295 | * argument is null. |
| 296 | * @see #getErrorHandler |
| 297 | */ |
| 298 | |
| 299 | public void setErrorHandler(ErrorHandler handler) { |
| 300 | // No effect |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Return the current error handler. |
| 305 | * |
| 306 | * @return The current error handler, or null if none |
| 307 | * has been registered. |
| 308 | * @see #setErrorHandler |
| 309 | */ |
| 310 | /*@Nullable*/ |
| 311 | public ErrorHandler getErrorHandler() { |
| 312 | return null; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Parse (that is, transform) an XML document given a system identifier (URI). |
| 317 | * <p>This method is a shortcut for the common case of reading a |
| 318 | * document from a system identifier. It is the exact |
| 319 | * equivalent of the following:</p> |
| 320 | * <pre> |
| 321 | * parse(new InputSource(systemId)); |
| 322 | * </pre> |
| 323 | * <p>If the system identifier is a URL, it must be fully resolved |
| 324 | * by the application before it is passed to the parser.</p> |
| 325 | * |
| 326 | * @param systemId The system identifier (URI). |
| 327 | * @throws SAXException Any SAX exception, possibly |
| 328 | * wrapping another exception. |
| 329 | * @throws IOException An IO exception from the parser, |
| 330 | * possibly from a byte stream or character stream |
| 331 | * supplied by the application. |
| 332 | * @see #parse(InputSource) |
| 333 | */ |
| 334 | |
| 335 | public void parse(String systemId) throws IOException, SAXException { |
| 336 | InputSource input = new InputSource(systemId); |
| 337 | parse(input); |
| 338 | } |
| 339 | |
| 340 | } |
| 341 |