001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id$ */
019:
020: package org.apache.fop.util;
021:
022: import java.io.IOException;
023:
024: import org.xml.sax.Attributes;
025: import org.xml.sax.ContentHandler;
026: import org.xml.sax.DTDHandler;
027: import org.xml.sax.EntityResolver;
028: import org.xml.sax.ErrorHandler;
029: import org.xml.sax.InputSource;
030: import org.xml.sax.Locator;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.SAXParseException;
033: import org.xml.sax.ext.LexicalHandler;
034:
035: /**
036: * SAX 2 Event Handler which simply delegates all calls to another ContentHandler. Subclasses can
037: * do additional processing. This class is the passive counterpart to XMLFilterImpl.
038: * <p>
039: * The ContentHandler is the only instance that is required. All others (DTDHandler,
040: * EntityResolver, LexicalHandler and ErrorHandler) may be ignored.
041: *
042: */
043: public class DelegatingContentHandler implements EntityResolver,
044: DTDHandler, ContentHandler, LexicalHandler, ErrorHandler {
045:
046: private ContentHandler delegate;
047: private EntityResolver entityResolver;
048: private DTDHandler dtdHandler;
049: private LexicalHandler lexicalHandler;
050: private ErrorHandler errorHandler;
051:
052: /**
053: * Main constructor.
054: */
055: public DelegatingContentHandler() {
056: //nop
057: }
058:
059: /**
060: * @return the delegate that all ContentHandler events are forwarded to
061: */
062: public ContentHandler getDelegateContentHandler() {
063: return this .delegate;
064: }
065:
066: /**
067: * Sets the delegate ContentHandler that all events are forwarded to.
068: * @param handler the delegate instance
069: */
070: public void setDelegateContentHandler(ContentHandler handler) {
071: this .delegate = handler;
072: }
073:
074: /**
075: * Sets the delegate EntityResolver.
076: * @param resolver the delegate instance
077: */
078: public void setDelegateEntityResolver(EntityResolver resolver) {
079: this .entityResolver = resolver;
080: }
081:
082: /**
083: * Sets the delegate DTDHandler.
084: * @param handler the delegate instance
085: */
086: public void setDelegateDTDHandler(DTDHandler handler) {
087: this .dtdHandler = handler;
088: }
089:
090: /**
091: * Sets the delegate LexicalHandler.
092: * @param handler the delegate instance
093: */
094: public void setDelegateLexicalHandler(LexicalHandler handler) {
095: this .lexicalHandler = handler;
096: }
097:
098: /**
099: * Sets the delegate ErrorHandler.
100: * @param handler the delegate instance
101: */
102: public void setDelegateErrorHandler(ErrorHandler handler) {
103: this .errorHandler = handler;
104: }
105:
106: // ==== EntityResolver
107:
108: /**
109: * {@inheritDoc}
110: */
111: public InputSource resolveEntity(String publicId, String systemId)
112: throws SAXException, IOException {
113: if (entityResolver != null) {
114: return entityResolver.resolveEntity(publicId, systemId);
115: } else {
116: return null;
117: }
118: }
119:
120: // ==== DTDHandler
121:
122: /**
123: * {@inheritDoc}
124: */
125: public void notationDecl(String name, String publicId,
126: String systemId) throws SAXException {
127: if (dtdHandler != null) {
128: dtdHandler.notationDecl(name, publicId, systemId);
129: }
130: }
131:
132: /**
133: * {@inheritDoc}
134: */
135: public void unparsedEntityDecl(String name, String publicId,
136: String systemId, String notationName) throws SAXException {
137: if (dtdHandler != null) {
138: dtdHandler.unparsedEntityDecl(name, publicId, systemId,
139: notationName);
140: }
141: }
142:
143: // ==== ContentHandler
144:
145: /**
146: * {@inheritDoc}
147: */
148: public void setDocumentLocator(Locator locator) {
149: delegate.setDocumentLocator(locator);
150: }
151:
152: /**
153: * {@inheritDoc}
154: */
155: public void startDocument() throws SAXException {
156: delegate.startDocument();
157: }
158:
159: /**
160: * {@inheritDoc}
161: */
162: public void endDocument() throws SAXException {
163: delegate.endDocument();
164: }
165:
166: /**
167: * {@inheritDoc}
168: */
169: public void startPrefixMapping(String prefix, String uri)
170: throws SAXException {
171: delegate.startPrefixMapping(prefix, uri);
172: }
173:
174: /**
175: * {@inheritDoc}
176: */
177: public void endPrefixMapping(String prefix) throws SAXException {
178: delegate.endPrefixMapping(prefix);
179: }
180:
181: /**
182: * {@inheritDoc}
183: */
184: public void startElement(String uri, String localName,
185: String qName, Attributes atts) throws SAXException {
186: delegate.startElement(uri, localName, qName, atts);
187: }
188:
189: /**
190: * {@inheritDoc}
191: */
192: public void endElement(String uri, String localName, String qName)
193: throws SAXException {
194: delegate.endElement(uri, localName, qName);
195: }
196:
197: /**
198: * {@inheritDoc}
199: */
200: public void characters(char[] ch, int start, int length)
201: throws SAXException {
202: delegate.characters(ch, start, length);
203: }
204:
205: /**
206: * {@inheritDoc}
207: */
208: public void ignorableWhitespace(char[] ch, int start, int length)
209: throws SAXException {
210: delegate.ignorableWhitespace(ch, start, length);
211: }
212:
213: /**
214: * {@inheritDoc}
215: */
216: public void processingInstruction(String target, String data)
217: throws SAXException {
218: delegate.processingInstruction(target, data);
219: }
220:
221: /**
222: * {@inheritDoc}
223: */
224: public void skippedEntity(String name) throws SAXException {
225: delegate.skippedEntity(name);
226: }
227:
228: // ==== LexicalHandler
229:
230: /**
231: * {@inheritDoc}
232: */
233: public void startDTD(String name, String publicId, String systemId)
234: throws SAXException {
235: if (lexicalHandler != null) {
236: lexicalHandler.startDTD(name, publicId, systemId);
237: }
238:
239: }
240:
241: /**
242: * {@inheritDoc}
243: */
244: public void endDTD() throws SAXException {
245: if (lexicalHandler != null) {
246: lexicalHandler.endDTD();
247: }
248: }
249:
250: /**
251: * {@inheritDoc}
252: */
253: public void startEntity(String name) throws SAXException {
254: if (lexicalHandler != null) {
255: lexicalHandler.startEntity(name);
256: }
257: }
258:
259: /**
260: * {@inheritDoc}
261: */
262: public void endEntity(String name) throws SAXException {
263: if (lexicalHandler != null) {
264: lexicalHandler.endEntity(name);
265: }
266: }
267:
268: /**
269: * {@inheritDoc}
270: */
271: public void startCDATA() throws SAXException {
272: if (lexicalHandler != null) {
273: lexicalHandler.startCDATA();
274: }
275: }
276:
277: /**
278: * {@inheritDoc}
279: */
280: public void endCDATA() throws SAXException {
281: if (lexicalHandler != null) {
282: lexicalHandler.endCDATA();
283: }
284: }
285:
286: /**
287: * {@inheritDoc} int, int)
288: */
289: public void comment(char[] ch, int start, int length)
290: throws SAXException {
291: if (lexicalHandler != null) {
292: lexicalHandler.comment(ch, start, length);
293: }
294: }
295:
296: // ==== ErrorHandler
297:
298: /**
299: * {@inheritDoc}
300: */
301: public void warning(SAXParseException exception)
302: throws SAXException {
303: if (errorHandler != null) {
304: errorHandler.warning(exception);
305: }
306: }
307:
308: /**
309: * {@inheritDoc}
310: */
311: public void error(SAXParseException exception) throws SAXException {
312: if (errorHandler != null) {
313: errorHandler.error(exception);
314: }
315: }
316:
317: /**
318: * {@inheritDoc}
319: */
320: public void fatalError(SAXParseException exception)
321: throws SAXException {
322: if (errorHandler != null) {
323: errorHandler.fatalError(exception);
324: }
325: }
326:
327: }
|