KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > xml > ContentHandlerWrapper


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.xml;
17
18 import org.apache.avalon.excalibur.pool.Recyclable;
19 import org.xml.sax.Attributes JavaDoc;
20 import org.xml.sax.ContentHandler JavaDoc;
21 import org.xml.sax.Locator JavaDoc;
22 import org.xml.sax.SAXException JavaDoc;
23 import org.xml.sax.ext.LexicalHandler JavaDoc;
24
25 /**
26  * This class is an utility class "wrapping" around a SAX version 2.0
27  * <code>ContentHandler</code> and forwarding it those events received throug
28  * its <code>XMLConsumers</code> interface.
29  * <br>
30  *
31  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
32  * (Apache Software Foundation, Computer Associates)
33  * @version CVS $Id: ContentHandlerWrapper.java 30932 2004-07-29 17:35:38Z vgritsenko $
34  */

35 public class ContentHandlerWrapper extends AbstractXMLConsumer implements Recyclable {
36
37     /** The current <code>ContentHandler</code>. */
38     protected ContentHandler JavaDoc contentHandler;
39
40     /** The optional <code>LexicalHandler</code> */
41     protected LexicalHandler JavaDoc lexicalHandler;
42
43     /**
44      * Create a new <code>ContentHandlerWrapper</code> instance.
45      */

46     public ContentHandlerWrapper() {
47         super();
48      }
49
50     /**
51      * Create a new <code>ContentHandlerWrapper</code> instance.
52      */

53     public ContentHandlerWrapper(ContentHandler JavaDoc contentHandler) {
54         this();
55         this.setContentHandler(contentHandler);
56     }
57
58     /**
59      * Create a new <code>ContentHandlerWrapper</code> instance.
60      */

61     public ContentHandlerWrapper(ContentHandler JavaDoc contentHandler,
62                                  LexicalHandler JavaDoc lexicalHandler) {
63         this();
64         this.setContentHandler(contentHandler);
65         this.setLexicalHandler(lexicalHandler);
66     }
67
68     /**
69      * Set the <code>ContentHandler</code> that will receive XML data.
70      *
71      * @exception IllegalStateException If the <code>ContentHandler</code>
72      * was already set.
73      */

74     public void setContentHandler(ContentHandler JavaDoc contentHandler)
75     throws IllegalStateException JavaDoc {
76         if (this.contentHandler!=null) throw new IllegalStateException JavaDoc();
77         this.contentHandler=contentHandler;
78     }
79
80     /**
81      * Set the <code>LexicalHandler</code> that will receive XML data.
82      *
83      * @exception IllegalStateException If the <code>LexicalHandler</code>
84      * was already set.
85      */

86     public void setLexicalHandler(LexicalHandler JavaDoc lexicalHandler)
87     throws IllegalStateException JavaDoc {
88         if (this.lexicalHandler!=null) throw new IllegalStateException JavaDoc();
89         this.lexicalHandler=lexicalHandler;
90     }
91
92     public void recycle () {
93         this.contentHandler = null;
94         this.lexicalHandler = null;
95     }
96
97     /**
98      * Receive an object for locating the origin of SAX document events.
99      */

100     public void setDocumentLocator (Locator JavaDoc locator) {
101         if (this.contentHandler==null) return;
102         else this.contentHandler.setDocumentLocator(locator);
103     }
104
105     /**
106      * Receive notification of the beginning of a document.
107      */

108     public void startDocument ()
109     throws SAXException JavaDoc {
110         if (this.contentHandler==null)
111             throw new SAXException JavaDoc("ContentHandler not set");
112         this.contentHandler.startDocument();
113     }
114
115     /**
116      * Receive notification of the end of a document.
117      */

118     public void endDocument ()
119     throws SAXException JavaDoc {
120         this.contentHandler.endDocument();
121     }
122
123     /**
124      * Begin the scope of a prefix-URI Namespace mapping.
125      */

126     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
127     throws SAXException JavaDoc {
128         if (this.contentHandler==null)
129             throw new SAXException JavaDoc("ContentHandler not set");
130         this.contentHandler.startPrefixMapping(prefix, uri);
131     }
132
133     /**
134      * End the scope of a prefix-URI mapping.
135      */

136     public void endPrefixMapping(String JavaDoc prefix)
137     throws SAXException JavaDoc {
138         this.contentHandler.endPrefixMapping(prefix);
139     }
140
141     /**
142      * Receive notification of the beginning of an element.
143      */

144     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
145     throws SAXException JavaDoc {
146         this.contentHandler.startElement(uri, loc, raw, a);
147     }
148
149
150     /**
151      * Receive notification of the end of an element.
152      */

153     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
154     throws SAXException JavaDoc {
155         this.contentHandler.endElement(uri, loc, raw);
156     }
157
158     /**
159      * Receive notification of character data.
160      */

161     public void characters(char ch[], int start, int len)
162     throws SAXException JavaDoc {
163         this.contentHandler.characters(ch,start,len);
164     }
165
166     /**
167      * Receive notification of ignorable whitespace in element content.
168      */

169     public void ignorableWhitespace(char ch[], int start, int len)
170     throws SAXException JavaDoc {
171         this.contentHandler.ignorableWhitespace(ch,start,len);
172     }
173
174     /**
175      * Receive notification of a processing instruction.
176      */

177     public void processingInstruction(String JavaDoc target, String JavaDoc data)
178     throws SAXException JavaDoc {
179         this.contentHandler.processingInstruction(target,data);
180     }
181
182     /**
183      * Receive notification of a skipped entity.
184      *
185      * @param name The name of the skipped entity. If it is a parameter
186      * entity, the name will begin with '%'.
187      */

188     public void skippedEntity(String JavaDoc name)
189     throws SAXException JavaDoc {
190         this.contentHandler.skippedEntity(name);
191     }
192
193         /**
194      * Report the start of DTD declarations, if any.
195      *
196      * @param name The document type name.
197      * @param publicId The declared public identifier for the external DTD
198      * subset, or null if none was declared.
199      * @param systemId The declared system identifier for the external DTD
200      * subset, or null if none was declared.
201      */

202     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
203     throws SAXException JavaDoc {
204         if (this.lexicalHandler != null)
205             this.lexicalHandler.startDTD(name, publicId, systemId);
206     }
207
208     /**
209      * Report the end of DTD declarations.
210      */

211     public void endDTD()
212     throws SAXException JavaDoc {
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 JavaDoc name)
224     throws SAXException JavaDoc {
225         if (this.lexicalHandler != null)
226             this.lexicalHandler.startEntity(name);
227     }
228
229     /**
230      * Report the end of an entity.
231      *
232      * @param name The name of the entity that is ending.
233      */

234     public void endEntity(String JavaDoc name)
235     throws SAXException JavaDoc {
236         if (this.lexicalHandler != null)
237             this.lexicalHandler.endEntity(name);
238     }
239
240     /**
241      * Report the start of a CDATA section.
242      */

243     public void startCDATA()
244     throws SAXException JavaDoc {
245         if (this.lexicalHandler != null)
246             this.lexicalHandler.startCDATA();
247     }
248
249     /**
250      * Report the end of a CDATA section.
251      */

252     public void endCDATA()
253     throws SAXException JavaDoc {
254         if (this.lexicalHandler != null)
255             this.lexicalHandler.endCDATA();
256     }
257
258
259     /**
260      * Report an XML comment anywhere in the document.
261      *
262      * @param ch An array holding the characters in the comment.
263      * @param start The starting position in the array.
264      * @param len The number of characters to use from the array.
265      */

266     public void comment(char ch[], int start, int len)
267     throws SAXException JavaDoc {
268         if (this.lexicalHandler != null)
269             this.lexicalHandler.comment(ch, start, len);
270     }
271
272 }
273
Popular Tags