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: package org.apache.cocoon.xml;
018:
019: import org.apache.avalon.excalibur.pool.Recyclable;
020: import org.xml.sax.Attributes;
021: import org.xml.sax.ContentHandler;
022: import org.xml.sax.Locator;
023: import org.xml.sax.SAXException;
024: import org.xml.sax.ext.LexicalHandler;
025:
026: /**
027: * This class is an utility class "wrapping" around a SAX version 2.0
028: * <code>ContentHandler</code> and forwarding it those events received throug
029: * its <code>XMLConsumers</code> interface.
030: * <br>
031: *
032: * @author <a href="mailto:dims@yahoo.com">Davanum Srinivas</a>
033: * (Apache Software Foundation, Computer Associates)
034: * @version CVS $Id: ContentHandlerWrapper.java 433543 2006-08-22 06:22:54Z crossley $
035: */
036: public class ContentHandlerWrapper extends AbstractXMLConsumer
037: implements Recyclable {
038:
039: /** The current <code>ContentHandler</code>. */
040: protected ContentHandler contentHandler;
041:
042: /** The optional <code>LexicalHandler</code> */
043: protected LexicalHandler lexicalHandler;
044:
045: /**
046: * Create a new <code>ContentHandlerWrapper</code> instance.
047: */
048: public ContentHandlerWrapper() {
049: super ();
050: }
051:
052: /**
053: * Create a new <code>ContentHandlerWrapper</code> instance.
054: */
055: public ContentHandlerWrapper(ContentHandler contentHandler) {
056: this ();
057: this .setContentHandler(contentHandler);
058: }
059:
060: /**
061: * Create a new <code>ContentHandlerWrapper</code> instance.
062: */
063: public ContentHandlerWrapper(ContentHandler contentHandler,
064: LexicalHandler lexicalHandler) {
065: this ();
066: this .setContentHandler(contentHandler);
067: this .setLexicalHandler(lexicalHandler);
068: }
069:
070: /**
071: * Set the <code>ContentHandler</code> that will receive XML data.
072: *
073: * @exception IllegalStateException If the <code>ContentHandler</code>
074: * was already set.
075: */
076: public void setContentHandler(ContentHandler contentHandler)
077: throws IllegalStateException {
078: if (this .contentHandler != null)
079: throw new IllegalStateException();
080: this .contentHandler = contentHandler;
081: }
082:
083: /**
084: * Set the <code>LexicalHandler</code> that will receive XML data.
085: *
086: * @exception IllegalStateException If the <code>LexicalHandler</code>
087: * was already set.
088: */
089: public void setLexicalHandler(LexicalHandler lexicalHandler)
090: throws IllegalStateException {
091: if (this .lexicalHandler != null)
092: throw new IllegalStateException();
093: this .lexicalHandler = lexicalHandler;
094: }
095:
096: public void recycle() {
097: this .contentHandler = null;
098: this .lexicalHandler = null;
099: }
100:
101: /**
102: * Receive an object for locating the origin of SAX document events.
103: */
104: public void setDocumentLocator(Locator locator) {
105: if (this .contentHandler == null)
106: return;
107: else
108: this .contentHandler.setDocumentLocator(locator);
109: }
110:
111: /**
112: * Receive notification of the beginning of a document.
113: */
114: public void startDocument() throws SAXException {
115: if (this .contentHandler == null)
116: throw new SAXException("ContentHandler not set");
117: this .contentHandler.startDocument();
118: }
119:
120: /**
121: * Receive notification of the end of a document.
122: */
123: public void endDocument() throws SAXException {
124: this .contentHandler.endDocument();
125: }
126:
127: /**
128: * Begin the scope of a prefix-URI Namespace mapping.
129: */
130: public void startPrefixMapping(String prefix, String uri)
131: throws SAXException {
132: if (this .contentHandler == null)
133: throw new SAXException("ContentHandler not set");
134: this .contentHandler.startPrefixMapping(prefix, uri);
135: }
136:
137: /**
138: * End the scope of a prefix-URI mapping.
139: */
140: public void endPrefixMapping(String prefix) throws SAXException {
141: this .contentHandler.endPrefixMapping(prefix);
142: }
143:
144: /**
145: * Receive notification of the beginning of an element.
146: */
147: public void startElement(String uri, String loc, String raw,
148: Attributes a) throws SAXException {
149: this .contentHandler.startElement(uri, loc, raw, a);
150: }
151:
152: /**
153: * Receive notification of the end of an element.
154: */
155: public void endElement(String uri, String loc, String raw)
156: throws SAXException {
157: this .contentHandler.endElement(uri, loc, raw);
158: }
159:
160: /**
161: * Receive notification of character data.
162: */
163: public void characters(char ch[], int start, int len)
164: throws SAXException {
165: this .contentHandler.characters(ch, start, len);
166: }
167:
168: /**
169: * Receive notification of ignorable whitespace in element content.
170: */
171: public void ignorableWhitespace(char ch[], int start, int len)
172: throws SAXException {
173: this .contentHandler.ignorableWhitespace(ch, start, len);
174: }
175:
176: /**
177: * Receive notification of a processing instruction.
178: */
179: public void processingInstruction(String target, String data)
180: throws SAXException {
181: this .contentHandler.processingInstruction(target, data);
182: }
183:
184: /**
185: * Receive notification of a skipped entity.
186: *
187: * @param name The name of the skipped entity. If it is a parameter
188: * entity, the name will begin with '%'.
189: */
190: public void skippedEntity(String name) throws SAXException {
191: this .contentHandler.skippedEntity(name);
192: }
193:
194: /**
195: * Report the start of DTD declarations, if any.
196: *
197: * @param name The document type name.
198: * @param publicId The declared public identifier for the external DTD
199: * subset, or null if none was declared.
200: * @param systemId The declared system identifier for the external DTD
201: * subset, or null if none was declared.
202: */
203: public void startDTD(String name, String publicId, String systemId)
204: throws SAXException {
205: if (this .lexicalHandler != null)
206: this .lexicalHandler.startDTD(name, publicId, systemId);
207: }
208:
209: /**
210: * Report the end of DTD declarations.
211: */
212: public void endDTD() throws SAXException {
213: if (this .lexicalHandler != null)
214: this .lexicalHandler.endDTD();
215: }
216:
217: /**
218: * Report the beginning of an entity.
219: *
220: * @param name The name of the entity. If it is a parameter entity, the
221: * name will begin with '%'.
222: */
223: public void startEntity(String name) throws SAXException {
224: if (this .lexicalHandler != null)
225: this .lexicalHandler.startEntity(name);
226: }
227:
228: /**
229: * Report the end of an entity.
230: *
231: * @param name The name of the entity that is ending.
232: */
233: public void endEntity(String name) throws SAXException {
234: if (this .lexicalHandler != null)
235: this .lexicalHandler.endEntity(name);
236: }
237:
238: /**
239: * Report the start of a CDATA section.
240: */
241: public void startCDATA() throws SAXException {
242: if (this .lexicalHandler != null)
243: this .lexicalHandler.startCDATA();
244: }
245:
246: /**
247: * Report the end of a CDATA section.
248: */
249: public void endCDATA() throws SAXException {
250: if (this .lexicalHandler != null)
251: this .lexicalHandler.endCDATA();
252: }
253:
254: /**
255: * Report an XML comment anywhere in the document.
256: *
257: * @param ch An array holding the characters in the comment.
258: * @param start The starting position in the array.
259: * @param len The number of characters to use from the array.
260: */
261: public void comment(char ch[], int start, int len)
262: throws SAXException {
263: if (this.lexicalHandler != null)
264: this.lexicalHandler.comment(ch, start, len);
265: }
266:
267: }
|