001: // XMLFilterImpl.java - base SAX2 filter implementation.
002: // http://www.saxproject.org
003: // Written by David Megginson
004: // NO WARRANTY! This class is in the Public Domain.
005:
006: // $Id: XMLFilterImpl.java,v 1.1.1.1 2002/05/03 23:29:43 yuvalo Exp $
007:
008: package org.xml.sax.helpers;
009:
010: import java.io.IOException;
011:
012: import org.xml.sax.XMLReader;
013: import org.xml.sax.XMLFilter;
014: import org.xml.sax.InputSource;
015: import org.xml.sax.Locator;
016: import org.xml.sax.Attributes;
017: import org.xml.sax.EntityResolver;
018: import org.xml.sax.DTDHandler;
019: import org.xml.sax.ContentHandler;
020: import org.xml.sax.ErrorHandler;
021: import org.xml.sax.SAXException;
022: import org.xml.sax.SAXParseException;
023: import org.xml.sax.SAXNotSupportedException;
024: import org.xml.sax.SAXNotRecognizedException;
025:
026: /**
027: * Base class for deriving an XML filter.
028: *
029: * <blockquote>
030: * <em>This module, both source code and documentation, is in the
031: * Public Domain, and comes with <strong>NO WARRANTY</strong>.</em>
032: * See <a href='http://www.saxproject.org'>http://www.saxproject.org</a>
033: * for further information.
034: * </blockquote>
035: *
036: * <p>This class is designed to sit between an {@link org.xml.sax.XMLReader
037: * XMLReader} and the client application's event handlers. By default, it
038: * does nothing but pass requests up to the reader and events
039: * on to the handlers unmodified, but subclasses can override
040: * specific methods to modify the event stream or the configuration
041: * requests as they pass through.</p>
042: *
043: * @since SAX 2.0
044: * @author David Megginson
045: * @version 2.0.1 (sax2r2)
046: * @see org.xml.sax.XMLFilter
047: * @see org.xml.sax.XMLReader
048: * @see org.xml.sax.EntityResolver
049: * @see org.xml.sax.DTDHandler
050: * @see org.xml.sax.ContentHandler
051: * @see org.xml.sax.ErrorHandler
052: */
053: public class XMLFilterImpl implements XMLFilter, EntityResolver,
054: DTDHandler, ContentHandler, ErrorHandler {
055:
056: ////////////////////////////////////////////////////////////////////
057: // Constructors.
058: ////////////////////////////////////////////////////////////////////
059:
060: /**
061: * Construct an empty XML filter, with no parent.
062: *
063: * <p>This filter will have no parent: you must assign a parent
064: * before you start a parse or do any configuration with
065: * setFeature or setProperty, unless you use this as a pure event
066: * consumer rather than as an {@link XMLReader}.</p>
067: *
068: * @see org.xml.sax.XMLReader#setFeature
069: * @see org.xml.sax.XMLReader#setProperty
070: * @see #setParent
071: */
072: public XMLFilterImpl() {
073: super ();
074: }
075:
076: /**
077: * Construct an XML filter with the specified parent.
078: *
079: * @see #setParent
080: * @see #getParent
081: */
082: public XMLFilterImpl(XMLReader parent) {
083: super ();
084: setParent(parent);
085: }
086:
087: ////////////////////////////////////////////////////////////////////
088: // Implementation of org.xml.sax.XMLFilter.
089: ////////////////////////////////////////////////////////////////////
090:
091: /**
092: * Set the parent reader.
093: *
094: * <p>This is the {@link org.xml.sax.XMLReader XMLReader} from which
095: * this filter will obtain its events and to which it will pass its
096: * configuration requests. The parent may itself be another filter.</p>
097: *
098: * <p>If there is no parent reader set, any attempt to parse
099: * or to set or get a feature or property will fail.</p>
100: *
101: * @param parent The parent XML reader.
102: * @see #getParent
103: */
104: public void setParent(XMLReader parent) {
105: this .parent = parent;
106: }
107:
108: /**
109: * Get the parent reader.
110: *
111: * @return The parent XML reader, or null if none is set.
112: * @see #setParent
113: */
114: public XMLReader getParent() {
115: return parent;
116: }
117:
118: ////////////////////////////////////////////////////////////////////
119: // Implementation of org.xml.sax.XMLReader.
120: ////////////////////////////////////////////////////////////////////
121:
122: /**
123: * Set the value of a feature.
124: *
125: * <p>This will always fail if the parent is null.</p>
126: *
127: * @param name The feature name.
128: * @param value The requested feature value.
129: * @exception org.xml.sax.SAXNotRecognizedException If the feature
130: * value can't be assigned or retrieved from the parent.
131: * @exception org.xml.sax.SAXNotSupportedException When the
132: * parent recognizes the feature name but
133: * cannot set the requested value.
134: */
135: public void setFeature(String name, boolean value)
136: throws SAXNotRecognizedException, SAXNotSupportedException {
137: if (parent != null) {
138: parent.setFeature(name, value);
139: } else {
140: throw new SAXNotRecognizedException("Feature: " + name);
141: }
142: }
143:
144: /**
145: * Look up the value of a feature.
146: *
147: * <p>This will always fail if the parent is null.</p>
148: *
149: * @param name The feature name.
150: * @return The current value of the feature.
151: * @exception org.xml.sax.SAXNotRecognizedException If the feature
152: * value can't be assigned or retrieved from the parent.
153: * @exception org.xml.sax.SAXNotSupportedException When the
154: * parent recognizes the feature name but
155: * cannot determine its value at this time.
156: */
157: public boolean getFeature(String name)
158: throws SAXNotRecognizedException, SAXNotSupportedException {
159: if (parent != null) {
160: return parent.getFeature(name);
161: } else {
162: throw new SAXNotRecognizedException("Feature: " + name);
163: }
164: }
165:
166: /**
167: * Set the value of a property.
168: *
169: * <p>This will always fail if the parent is null.</p>
170: *
171: * @param name The property name.
172: * @param value The requested property value.
173: * @exception org.xml.sax.SAXNotRecognizedException If the property
174: * value can't be assigned or retrieved from the parent.
175: * @exception org.xml.sax.SAXNotSupportedException When the
176: * parent recognizes the property name but
177: * cannot set the requested value.
178: */
179: public void setProperty(String name, Object value)
180: throws SAXNotRecognizedException, SAXNotSupportedException {
181: if (parent != null) {
182: parent.setProperty(name, value);
183: } else {
184: throw new SAXNotRecognizedException("Property: " + name);
185: }
186: }
187:
188: /**
189: * Look up the value of a property.
190: *
191: * @param name The property name.
192: * @return The current value of the property.
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 determine its value at this time.
198: */
199: public Object getProperty(String name)
200: throws SAXNotRecognizedException, SAXNotSupportedException {
201: if (parent != null) {
202: return parent.getProperty(name);
203: } else {
204: throw new SAXNotRecognizedException("Property: " + name);
205: }
206: }
207:
208: /**
209: * Set the entity resolver.
210: *
211: * @param resolver The new entity resolver.
212: */
213: public void setEntityResolver(EntityResolver resolver) {
214: entityResolver = resolver;
215: }
216:
217: /**
218: * Get the current entity resolver.
219: *
220: * @return The current entity resolver, or null if none was set.
221: */
222: public EntityResolver getEntityResolver() {
223: return entityResolver;
224: }
225:
226: /**
227: * Set the DTD event handler.
228: *
229: * @param resolver The new DTD handler.
230: */
231: public void setDTDHandler(DTDHandler handler) {
232: dtdHandler = handler;
233: }
234:
235: /**
236: * Get the current DTD event handler.
237: *
238: * @return The current DTD handler, or null if none was set.
239: */
240: public DTDHandler getDTDHandler() {
241: return dtdHandler;
242: }
243:
244: /**
245: * Set the content event handler.
246: *
247: * @param resolver The new content handler.
248: */
249: public void setContentHandler(ContentHandler handler) {
250: contentHandler = handler;
251: }
252:
253: /**
254: * Get the content event handler.
255: *
256: * @return The current content handler, or null if none was set.
257: */
258: public ContentHandler getContentHandler() {
259: return contentHandler;
260: }
261:
262: /**
263: * Set the error event handler.
264: *
265: * @param handle The new error handler.
266: */
267: public void setErrorHandler(ErrorHandler handler) {
268: errorHandler = handler;
269: }
270:
271: /**
272: * Get the current error event handler.
273: *
274: * @return The current error handler, or null if none was set.
275: */
276: public ErrorHandler getErrorHandler() {
277: return errorHandler;
278: }
279:
280: /**
281: * Parse a document.
282: *
283: * @param input The input source for the document entity.
284: * @exception org.xml.sax.SAXException Any SAX exception, possibly
285: * wrapping another exception.
286: * @exception java.io.IOException An IO exception from the parser,
287: * possibly from a byte stream or character stream
288: * supplied by the application.
289: */
290: public void parse(InputSource input) throws SAXException,
291: IOException {
292: setupParse();
293: parent.parse(input);
294: }
295:
296: /**
297: * Parse a document.
298: *
299: * @param systemId The system identifier as a fully-qualified URI.
300: * @exception org.xml.sax.SAXException Any SAX exception, possibly
301: * wrapping another exception.
302: * @exception java.io.IOException An IO exception from the parser,
303: * possibly from a byte stream or character stream
304: * supplied by the application.
305: */
306: public void parse(String systemId) throws SAXException, IOException {
307: parse(new InputSource(systemId));
308: }
309:
310: ////////////////////////////////////////////////////////////////////
311: // Implementation of org.xml.sax.EntityResolver.
312: ////////////////////////////////////////////////////////////////////
313:
314: /**
315: * Filter an external entity resolution.
316: *
317: * @param publicId The entity's public identifier, or null.
318: * @param systemId The entity's system identifier.
319: * @return A new InputSource or null for the default.
320: * @exception org.xml.sax.SAXException The client may throw
321: * an exception during processing.
322: * @exception java.io.IOException The client may throw an
323: * I/O-related exception while obtaining the
324: * new InputSource.
325: */
326: public InputSource resolveEntity(String publicId, String systemId)
327: throws SAXException, IOException {
328: if (entityResolver != null) {
329: return entityResolver.resolveEntity(publicId, systemId);
330: } else {
331: return null;
332: }
333: }
334:
335: ////////////////////////////////////////////////////////////////////
336: // Implementation of org.xml.sax.DTDHandler.
337: ////////////////////////////////////////////////////////////////////
338:
339: /**
340: * Filter a notation declaration event.
341: *
342: * @param name The notation name.
343: * @param publicId The notation's public identifier, or null.
344: * @param systemId The notation's system identifier, or null.
345: * @exception org.xml.sax.SAXException The client may throw
346: * an exception during processing.
347: */
348: public void notationDecl(String name, String publicId,
349: String systemId) throws SAXException {
350: if (dtdHandler != null) {
351: dtdHandler.notationDecl(name, publicId, systemId);
352: }
353: }
354:
355: /**
356: * Filter an unparsed entity declaration event.
357: *
358: * @param name The entity name.
359: * @param publicId The entity's public identifier, or null.
360: * @param systemId The entity's system identifier, or null.
361: * @param notationName The name of the associated notation.
362: * @exception org.xml.sax.SAXException The client may throw
363: * an exception during processing.
364: */
365: public void unparsedEntityDecl(String name, String publicId,
366: String systemId, String notationName) throws SAXException {
367: if (dtdHandler != null) {
368: dtdHandler.unparsedEntityDecl(name, publicId, systemId,
369: notationName);
370: }
371: }
372:
373: ////////////////////////////////////////////////////////////////////
374: // Implementation of org.xml.sax.ContentHandler.
375: ////////////////////////////////////////////////////////////////////
376:
377: /**
378: * Filter a new document locator event.
379: *
380: * @param locator The document locator.
381: */
382: public void setDocumentLocator(Locator locator) {
383: this .locator = locator;
384: if (contentHandler != null) {
385: contentHandler.setDocumentLocator(locator);
386: }
387: }
388:
389: /**
390: * Filter a start document event.
391: *
392: * @exception org.xml.sax.SAXException The client may throw
393: * an exception during processing.
394: */
395: public void startDocument() throws SAXException {
396: if (contentHandler != null) {
397: contentHandler.startDocument();
398: }
399: }
400:
401: /**
402: * Filter an end document event.
403: *
404: * @exception org.xml.sax.SAXException The client may throw
405: * an exception during processing.
406: */
407: public void endDocument() throws SAXException {
408: if (contentHandler != null) {
409: contentHandler.endDocument();
410: }
411: }
412:
413: /**
414: * Filter a start Namespace prefix mapping event.
415: *
416: * @param prefix The Namespace prefix.
417: * @param uri The Namespace URI.
418: * @exception org.xml.sax.SAXException The client may throw
419: * an exception during processing.
420: */
421: public void startPrefixMapping(String prefix, String uri)
422: throws SAXException {
423: if (contentHandler != null) {
424: contentHandler.startPrefixMapping(prefix, uri);
425: }
426: }
427:
428: /**
429: * Filter an end Namespace prefix mapping event.
430: *
431: * @param prefix The Namespace prefix.
432: * @exception org.xml.sax.SAXException The client may throw
433: * an exception during processing.
434: */
435: public void endPrefixMapping(String prefix) throws SAXException {
436: if (contentHandler != null) {
437: contentHandler.endPrefixMapping(prefix);
438: }
439: }
440:
441: /**
442: * Filter a start element event.
443: *
444: * @param uri The element's Namespace URI, or the empty string.
445: * @param localName The element's local name, or the empty string.
446: * @param qName The element's qualified (prefixed) name, or the empty
447: * string.
448: * @param atts The element's attributes.
449: * @exception org.xml.sax.SAXException The client may throw
450: * an exception during processing.
451: */
452: public void startElement(String uri, String localName,
453: String qName, Attributes atts) throws SAXException {
454: if (contentHandler != null) {
455: contentHandler.startElement(uri, localName, qName, atts);
456: }
457: }
458:
459: /**
460: * Filter an end element event.
461: *
462: * @param uri The element's Namespace URI, or the empty string.
463: * @param localName The element's local name, or the empty string.
464: * @param qName The element's qualified (prefixed) name, or the empty
465: * string.
466: * @exception org.xml.sax.SAXException The client may throw
467: * an exception during processing.
468: */
469: public void endElement(String uri, String localName, String qName)
470: throws SAXException {
471: if (contentHandler != null) {
472: contentHandler.endElement(uri, localName, qName);
473: }
474: }
475:
476: /**
477: * Filter a character data event.
478: *
479: * @param ch An array of characters.
480: * @param start The starting position in the array.
481: * @param length The number of characters to use from the array.
482: * @exception org.xml.sax.SAXException The client may throw
483: * an exception during processing.
484: */
485: public void characters(char ch[], int start, int length)
486: throws SAXException {
487: if (contentHandler != null) {
488: contentHandler.characters(ch, start, length);
489: }
490: }
491:
492: /**
493: * Filter an ignorable whitespace event.
494: *
495: * @param ch An array of characters.
496: * @param start The starting position in the array.
497: * @param length The number of characters to use from the array.
498: * @exception org.xml.sax.SAXException The client may throw
499: * an exception during processing.
500: */
501: public void ignorableWhitespace(char ch[], int start, int length)
502: throws SAXException {
503: if (contentHandler != null) {
504: contentHandler.ignorableWhitespace(ch, start, length);
505: }
506: }
507:
508: /**
509: * Filter a processing instruction event.
510: *
511: * @param target The processing instruction target.
512: * @param data The text following the target.
513: * @exception org.xml.sax.SAXException The client may throw
514: * an exception during processing.
515: */
516: public void processingInstruction(String target, String data)
517: throws SAXException {
518: if (contentHandler != null) {
519: contentHandler.processingInstruction(target, data);
520: }
521: }
522:
523: /**
524: * Filter a skipped entity event.
525: *
526: * @param name The name of the skipped entity.
527: * @exception org.xml.sax.SAXException The client may throw
528: * an exception during processing.
529: */
530: public void skippedEntity(String name) throws SAXException {
531: if (contentHandler != null) {
532: contentHandler.skippedEntity(name);
533: }
534: }
535:
536: ////////////////////////////////////////////////////////////////////
537: // Implementation of org.xml.sax.ErrorHandler.
538: ////////////////////////////////////////////////////////////////////
539:
540: /**
541: * Filter a warning event.
542: *
543: * @param e The warning as an exception.
544: * @exception org.xml.sax.SAXException The client may throw
545: * an exception during processing.
546: */
547: public void warning(SAXParseException e) throws SAXException {
548: if (errorHandler != null) {
549: errorHandler.warning(e);
550: }
551: }
552:
553: /**
554: * Filter an error event.
555: *
556: * @param e The error as an exception.
557: * @exception org.xml.sax.SAXException The client may throw
558: * an exception during processing.
559: */
560: public void error(SAXParseException e) throws SAXException {
561: if (errorHandler != null) {
562: errorHandler.error(e);
563: }
564: }
565:
566: /**
567: * Filter a fatal error event.
568: *
569: * @param e The error as an exception.
570: * @exception org.xml.sax.SAXException The client may throw
571: * an exception during processing.
572: */
573: public void fatalError(SAXParseException e) throws SAXException {
574: if (errorHandler != null) {
575: errorHandler.fatalError(e);
576: }
577: }
578:
579: ////////////////////////////////////////////////////////////////////
580: // Internal methods.
581: ////////////////////////////////////////////////////////////////////
582:
583: /**
584: * Set up before a parse.
585: *
586: * <p>Before every parse, check whether the parent is
587: * non-null, and re-register the filter for all of the
588: * events.</p>
589: */
590: private void setupParse() {
591: if (parent == null) {
592: throw new NullPointerException("No parent for filter");
593: }
594: parent.setEntityResolver(this );
595: parent.setDTDHandler(this );
596: parent.setContentHandler(this );
597: parent.setErrorHandler(this );
598: }
599:
600: ////////////////////////////////////////////////////////////////////
601: // Internal state.
602: ////////////////////////////////////////////////////////////////////
603:
604: private XMLReader parent = null;
605: private Locator locator = null;
606: private EntityResolver entityResolver = null;
607: private DTDHandler dtdHandler = null;
608: private ContentHandler contentHandler = null;
609: private ErrorHandler errorHandler = null;
610:
611: }
612:
613: // end of XMLFilterImpl.java
|