1 // XMLFilterImpl.java - base SAX2 filter implementation.
2 // http://www.saxproject.org
3 // Written by David Megginson
4 // NO WARRANTY! This class is in the Public Domain.
5 // $Id: XMLFilterImpl.java,v 1.9 2004/04/26 17:34:35 dmegginson Exp $
7 package org.xml.sax.helpers;
9 import java.io.IOException;
10 import org.xml.sax.Attributes;
11 import org.xml.sax.ContentHandler;
12 import org.xml.sax.DTDHandler;
13 import org.xml.sax.EntityResolver;
14 import org.xml.sax.ErrorHandler;
15 import org.xml.sax.InputSource;
16 import org.xml.sax.Locator;
17 import org.xml.sax.SAXException;
18 import org.xml.sax.SAXNotRecognizedException;
19 import org.xml.sax.SAXNotSupportedException;
20 import org.xml.sax.SAXParseException;
21 import org.xml.sax.XMLFilter;
22 import org.xml.sax.XMLReader;
26 * Base class for deriving an XML filter.
29 * <em>This module, both source code and documentation, is in the
30 * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
31 * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
32 * for further information.
35 * <p>This class is designed to sit between an {@link org.xml.sax.XMLReader
36 * XMLReader} and the client application's event handlers. By default, it
37 * does nothing but pass requests up to the reader and events
38 * on to the handlers unmodified, but subclasses can override
39 * specific methods to modify the event stream or the configuration
40 * requests as they pass through.</p>
43 * @author David Megginson
44 * @version 2.0.1 (sax2r2)
45 * @see org.xml.sax.XMLFilter
46 * @see org.xml.sax.XMLReader
47 * @see org.xml.sax.EntityResolver
48 * @see org.xml.sax.DTDHandler
49 * @see org.xml.sax.ContentHandler
50 * @see org.xml.sax.ErrorHandler
52 public class XMLFilterImpl
53 implements XMLFilter, EntityResolver, DTDHandler, ContentHandler, ErrorHandler
57 ////////////////////////////////////////////////////////////////////
59 ////////////////////////////////////////////////////////////////////
63 * Construct an empty XML filter, with no parent.
65 * <p>This filter will have no parent: you must assign a parent
66 * before you start a parse or do any configuration with
67 * setFeature or setProperty, unless you use this as a pure event
68 * consumer rather than as an {@link XMLReader}.</p>
70 * @see org.xml.sax.XMLReader#setFeature
71 * @see org.xml.sax.XMLReader#setProperty
74 public XMLFilterImpl ()
81 * Construct an XML filter with the specified parent.
83 * @param parent the XML reader from which this filter receives its events.
88 public XMLFilterImpl (XMLReader parent)
96 ////////////////////////////////////////////////////////////////////
97 // Implementation of org.xml.sax.XMLFilter.
98 ////////////////////////////////////////////////////////////////////
102 * Set the parent reader.
104 * <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
105 * this filter will obtain its events and to which it will pass its
106 * configuration requests. The parent may itself be another filter.</p>
108 * <p>If there is no parent reader set, any attempt to parse
109 * or to set or get a feature or property will fail.</p>
111 * @param parent The parent XML reader.
114 public void setParent (XMLReader parent)
116 this.parent = parent;
121 * Get the parent reader.
123 * @return The parent XML reader, or null if none is set.
126 public XMLReader getParent ()
133 ////////////////////////////////////////////////////////////////////
134 // Implementation of org.xml.sax.XMLReader.
135 ////////////////////////////////////////////////////////////////////
139 * Set the value of a feature.
141 * <p>This will always fail if the parent is null.</p>
143 * @param name The feature name.
144 * @param value The requested feature value.
145 * @exception org.xml.sax.SAXNotRecognizedException If the feature
146 * value can't be assigned or retrieved from the parent.
147 * @exception org.xml.sax.SAXNotSupportedException When the
148 * parent recognizes the feature name but
149 * cannot set the requested value.
151 public void setFeature (String name, boolean value)
152 throws SAXNotRecognizedException, SAXNotSupportedException
154 if (parent != null) {
155 parent.setFeature(name, value);
157 throw new SAXNotRecognizedException("Feature: " + name);
163 * Look up the value of a feature.
165 * <p>This will always fail if the parent is null.</p>
167 * @param name The feature name.
168 * @return The current value of the feature.
169 * @exception org.xml.sax.SAXNotRecognizedException If the feature
170 * value can't be assigned or retrieved from the parent.
171 * @exception org.xml.sax.SAXNotSupportedException When the
172 * parent recognizes the feature name but
173 * cannot determine its value at this time.
175 public boolean getFeature (String name)
176 throws SAXNotRecognizedException, SAXNotSupportedException
178 if (parent != null) {
179 return parent.getFeature(name);
181 throw new SAXNotRecognizedException("Feature: " + name);
187 * Set the value of a property.
189 * <p>This will always fail if the parent is null.</p>
191 * @param name The property name.
192 * @param value The requested property value.
193 * @exception org.xml.sax.SAXNotRecognizedException If the property
194 * value can't be assigned or retrieved from the parent.
195 * @exception org.xml.sax.SAXNotSupportedException When the
196 * parent recognizes the property name but
197 * cannot set the requested value.
199 public void setProperty (String name, Object value)
200 throws SAXNotRecognizedException, SAXNotSupportedException
202 if (parent != null) {
203 parent.setProperty(name, value);
205 throw new SAXNotRecognizedException("Property: " + name);
211 * Look up the value of a property.
213 * @param name The property name.
214 * @return The current value of the property.
215 * @exception org.xml.sax.SAXNotRecognizedException If the property
216 * value can't be assigned or retrieved from the parent.
217 * @exception org.xml.sax.SAXNotSupportedException When the
218 * parent recognizes the property name but
219 * cannot determine its value at this time.
221 public Object getProperty (String name)
222 throws SAXNotRecognizedException, SAXNotSupportedException
224 if (parent != null) {
225 return parent.getProperty(name);
227 throw new SAXNotRecognizedException("Property: " + name);
233 * Set the entity resolver.
235 * @param resolver The new entity resolver.
237 public void setEntityResolver (EntityResolver resolver)
239 entityResolver = resolver;
244 * Get the current entity resolver.
246 * @return The current entity resolver, or null if none was set.
248 public EntityResolver getEntityResolver ()
250 return entityResolver;
255 * Set the DTD event handler.
257 * @param handler the new DTD handler
259 public void setDTDHandler (DTDHandler handler)
261 dtdHandler = handler;
266 * Get the current DTD event handler.
268 * @return The current DTD handler, or null if none was set.
270 public DTDHandler getDTDHandler ()
277 * Set the content event handler.
279 * @param handler the new content handler
281 public void setContentHandler (ContentHandler handler)
283 contentHandler = handler;
288 * Get the content event handler.
290 * @return The current content handler, or null if none was set.
292 public ContentHandler getContentHandler ()
294 return contentHandler;
299 * Set the error event handler.
301 * @param handler the new error handler
303 public void setErrorHandler (ErrorHandler handler)
305 errorHandler = handler;
310 * Get the current error event handler.
312 * @return The current error handler, or null if none was set.
314 public ErrorHandler getErrorHandler ()
323 * @param input The input source for the document entity.
324 * @exception org.xml.sax.SAXException Any SAX exception, possibly
325 * wrapping another exception.
326 * @exception java.io.IOException An IO exception from the parser,
327 * possibly from a byte stream or character stream
328 * supplied by the application.
330 public void parse (InputSource input)
331 throws SAXException, IOException
341 * @param systemId The system identifier as a fully-qualified URI.
342 * @exception org.xml.sax.SAXException Any SAX exception, possibly
343 * wrapping another exception.
344 * @exception java.io.IOException An IO exception from the parser,
345 * possibly from a byte stream or character stream
346 * supplied by the application.
348 public void parse (String systemId)
349 throws SAXException, IOException
351 parse(new InputSource(systemId));
356 ////////////////////////////////////////////////////////////////////
357 // Implementation of org.xml.sax.EntityResolver.
358 ////////////////////////////////////////////////////////////////////
362 * Filter an external entity resolution.
364 * @param publicId The entity's public identifier, or null.
365 * @param systemId The entity's system identifier.
366 * @return A new InputSource or null for the default.
367 * @exception org.xml.sax.SAXException The client may throw
368 * an exception during processing.
369 * @exception java.io.IOException The client may throw an
370 * I/O-related exception while obtaining the
373 public InputSource resolveEntity (String publicId, String systemId)
374 throws SAXException, IOException
376 if (entityResolver != null) {
377 return entityResolver.resolveEntity(publicId, systemId);
385 ////////////////////////////////////////////////////////////////////
386 // Implementation of org.xml.sax.DTDHandler.
387 ////////////////////////////////////////////////////////////////////
391 * Filter a notation declaration event.
393 * @param name The notation name.
394 * @param publicId The notation's public identifier, or null.
395 * @param systemId The notation's system identifier, or null.
396 * @exception org.xml.sax.SAXException The client may throw
397 * an exception during processing.
399 public void notationDecl (String name, String publicId, String systemId)
402 if (dtdHandler != null) {
403 dtdHandler.notationDecl(name, publicId, systemId);
409 * Filter an unparsed entity declaration event.
411 * @param name The entity name.
412 * @param publicId The entity's public identifier, or null.
413 * @param systemId The entity's system identifier, or null.
414 * @param notationName The name of the associated notation.
415 * @exception org.xml.sax.SAXException The client may throw
416 * an exception during processing.
418 public void unparsedEntityDecl (String name, String publicId,
419 String systemId, String notationName)
422 if (dtdHandler != null) {
423 dtdHandler.unparsedEntityDecl(name, publicId, systemId,
430 ////////////////////////////////////////////////////////////////////
431 // Implementation of org.xml.sax.ContentHandler.
432 ////////////////////////////////////////////////////////////////////
436 * Filter a new document locator event.
438 * @param locator The document locator.
440 public void setDocumentLocator (Locator locator)
442 this.locator = locator;
443 if (contentHandler != null) {
444 contentHandler.setDocumentLocator(locator);
450 * Filter a start document event.
452 * @exception org.xml.sax.SAXException The client may throw
453 * an exception during processing.
455 public void startDocument ()
458 if (contentHandler != null) {
459 contentHandler.startDocument();
465 * Filter an end document event.
467 * @exception org.xml.sax.SAXException The client may throw
468 * an exception during processing.
470 public void endDocument ()
473 if (contentHandler != null) {
474 contentHandler.endDocument();
480 * Filter a start Namespace prefix mapping event.
482 * @param prefix The Namespace prefix.
483 * @param uri The Namespace URI.
484 * @exception org.xml.sax.SAXException The client may throw
485 * an exception during processing.
487 public void startPrefixMapping (String prefix, String uri)
490 if (contentHandler != null) {
491 contentHandler.startPrefixMapping(prefix, uri);
497 * Filter an end Namespace prefix mapping event.
499 * @param prefix The Namespace prefix.
500 * @exception org.xml.sax.SAXException The client may throw
501 * an exception during processing.
503 public void endPrefixMapping (String prefix)
506 if (contentHandler != null) {
507 contentHandler.endPrefixMapping(prefix);
513 * Filter a start element event.
515 * @param uri The element's Namespace URI, or the empty string.
516 * @param localName The element's local name, or the empty string.
517 * @param qName The element's qualified (prefixed) name, or the empty
519 * @param atts The element's attributes.
520 * @exception org.xml.sax.SAXException The client may throw
521 * an exception during processing.
523 public void startElement (String uri, String localName, String qName,
527 if (contentHandler != null) {
528 contentHandler.startElement(uri, localName, qName, atts);
534 * Filter an end element event.
536 * @param uri The element's Namespace URI, or the empty string.
537 * @param localName The element's local name, or the empty string.
538 * @param qName The element's qualified (prefixed) name, or the empty
540 * @exception org.xml.sax.SAXException The client may throw
541 * an exception during processing.
543 public void endElement (String uri, String localName, String qName)
546 if (contentHandler != null) {
547 contentHandler.endElement(uri, localName, qName);
553 * Filter a character data event.
555 * @param ch An array of characters.
556 * @param start The starting position in the array.
557 * @param length The number of characters to use from the array.
558 * @exception org.xml.sax.SAXException The client may throw
559 * an exception during processing.
561 public void characters (char ch[], int start, int length)
564 if (contentHandler != null) {
565 contentHandler.characters(ch, start, length);
571 * Filter an ignorable whitespace event.
573 * @param ch An array of characters.
574 * @param start The starting position in the array.
575 * @param length The number of characters to use from the array.
576 * @exception org.xml.sax.SAXException The client may throw
577 * an exception during processing.
579 public void ignorableWhitespace (char ch[], int start, int length)
582 if (contentHandler != null) {
583 contentHandler.ignorableWhitespace(ch, start, length);
589 * Filter a processing instruction event.
591 * @param target The processing instruction target.
592 * @param data The text following the target.
593 * @exception org.xml.sax.SAXException The client may throw
594 * an exception during processing.
596 public void processingInstruction (String target, String data)
599 if (contentHandler != null) {
600 contentHandler.processingInstruction(target, data);
606 * Filter a skipped entity event.
608 * @param name The name of the skipped entity.
609 * @exception org.xml.sax.SAXException The client may throw
610 * an exception during processing.
612 public void skippedEntity (String name)
615 if (contentHandler != null) {
616 contentHandler.skippedEntity(name);
622 ////////////////////////////////////////////////////////////////////
623 // Implementation of org.xml.sax.ErrorHandler.
624 ////////////////////////////////////////////////////////////////////
628 * Filter a warning event.
630 * @param e The warning as an exception.
631 * @exception org.xml.sax.SAXException The client may throw
632 * an exception during processing.
634 public void warning (SAXParseException e)
637 if (errorHandler != null) {
638 errorHandler.warning(e);
644 * Filter an error event.
646 * @param e The error as an exception.
647 * @exception org.xml.sax.SAXException The client may throw
648 * an exception during processing.
650 public void error (SAXParseException e)
653 if (errorHandler != null) {
654 errorHandler.error(e);
660 * Filter a fatal error event.
662 * @param e The error as an exception.
663 * @exception org.xml.sax.SAXException The client may throw
664 * an exception during processing.
666 public void fatalError (SAXParseException e)
669 if (errorHandler != null) {
670 errorHandler.fatalError(e);
676 ////////////////////////////////////////////////////////////////////
678 ////////////////////////////////////////////////////////////////////
682 * Set up before a parse.
684 * <p>Before every parse, check whether the parent is
685 * non-null, and re-register the filter for all of the
688 private void setupParse ()
690 if (parent == null) {
691 throw new NullPointerException("No parent for filter");
693 parent.setEntityResolver(this);
694 parent.setDTDHandler(this);
695 parent.setContentHandler(this);
696 parent.setErrorHandler(this);
701 ////////////////////////////////////////////////////////////////////
703 ////////////////////////////////////////////////////////////////////
705 private XMLReader parent = null;
706 private Locator locator = null;
707 private EntityResolver entityResolver = null;
708 private DTDHandler dtdHandler = null;
709 private ContentHandler contentHandler = null;
710 private ErrorHandler errorHandler = null;
714 // end of XMLFilterImpl.java