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