KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > xml > ContentHandlerWrapper


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.xml;
9
10 import org.apache.avalon.excalibur.pool.Recyclable;
11 import org.xml.sax.Attributes JavaDoc;
12 import org.xml.sax.ContentHandler JavaDoc;
13 import org.xml.sax.Locator JavaDoc;
14 import org.xml.sax.SAXException JavaDoc;
15 import org.xml.sax.ext.LexicalHandler JavaDoc;
16
17 /**
18  * This class is an utility class "wrapping" around a SAX version 2.0
19  * <code>ContentHandler</code> and forwarding it those events received throug
20  * its <code>XMLConsumers</code> interface.
21  * <br>
22  *
23  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
24  * (Apache Software Foundation, Computer Associates)
25  * @version CVS $Revision: 1.3 $ $Date: 2001/12/11 09:53:38 $
26  */

27 public class ContentHandlerWrapper extends AbstractXMLConsumer implements Recyclable {
28
29     /** The current <code>ContentHandler</code>. */
30     protected ContentHandler JavaDoc contentHandler;
31
32     /** The optional <code>LexicalHandler</code> */
33     protected LexicalHandler JavaDoc lexicalHandler;
34
35     /**
36      * Create a new <code>ContentHandlerWrapper</code> instance.
37      */

38     public ContentHandlerWrapper() {
39         super();
40      }
41
42     /**
43      * Create a new <code>ContentHandlerWrapper</code> instance.
44      */

45     public ContentHandlerWrapper(ContentHandler JavaDoc contentHandler) {
46         this();
47         this.setContentHandler(contentHandler);
48     }
49
50     /**
51      * Create a new <code>ContentHandlerWrapper</code> instance.
52      */

53     public ContentHandlerWrapper(ContentHandler JavaDoc contentHandler,
54                                  LexicalHandler JavaDoc lexicalHandler) {
55         this();
56         this.setContentHandler(contentHandler);
57         this.setLexicalHandler(lexicalHandler);
58     }
59
60     /**
61      * Set the <code>ContentHandler</code> that will receive XML data.
62      *
63      * @exception IllegalStateException If the <code>ContentHandler</code>
64      * was already set.
65      */

66     public void setContentHandler(ContentHandler JavaDoc contentHandler)
67     throws IllegalStateException JavaDoc {
68         if (this.contentHandler!=null) throw new IllegalStateException JavaDoc();
69         this.contentHandler=contentHandler;
70     }
71
72     /**
73      * Set the <code>LexicalHandler</code> that will receive XML data.
74      *
75      * @exception IllegalStateException If the <code>LexicalHandler</code>
76      * was already set.
77      */

78     public void setLexicalHandler(LexicalHandler JavaDoc lexicalHandler)
79     throws IllegalStateException JavaDoc {
80         if (this.lexicalHandler!=null) throw new IllegalStateException JavaDoc();
81         this.lexicalHandler=lexicalHandler;
82     }
83
84     public void recycle () {
85         this.contentHandler = null;
86         this.lexicalHandler = null;
87     }
88
89     /**
90      * Receive an object for locating the origin of SAX document events.
91      */

92     public void setDocumentLocator (Locator JavaDoc locator) {
93         if (this.contentHandler==null) return;
94         else this.contentHandler.setDocumentLocator(locator);
95     }
96
97     /**
98      * Receive notification of the beginning of a document.
99      */

100     public void startDocument ()
101     throws SAXException JavaDoc {
102         if (this.contentHandler==null)
103             throw new SAXException JavaDoc("ContentHandler not set");
104         this.contentHandler.startDocument();
105     }
106
107     /**
108      * Receive notification of the end of a document.
109      */

110     public void endDocument ()
111     throws SAXException JavaDoc {
112         this.contentHandler.endDocument();
113     }
114
115     /**
116      * Begin the scope of a prefix-URI Namespace mapping.
117      */

118     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
119     throws SAXException JavaDoc {
120         if (this.contentHandler==null)
121             throw new SAXException JavaDoc("ContentHandler not set");
122         this.contentHandler.startPrefixMapping(prefix, uri);
123     }
124
125     /**
126      * End the scope of a prefix-URI mapping.
127      */

128     public void endPrefixMapping(String JavaDoc prefix)
129     throws SAXException JavaDoc {
130         this.contentHandler.endPrefixMapping(prefix);
131     }
132
133     /**
134      * Receive notification of the beginning of an element.
135      */

136     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw, Attributes JavaDoc a)
137     throws SAXException JavaDoc {
138         this.contentHandler.startElement(uri, loc, raw, a);
139     }
140
141
142     /**
143      * Receive notification of the end of an element.
144      */

145     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc raw)
146     throws SAXException JavaDoc {
147         this.contentHandler.endElement(uri, loc, raw);
148     }
149
150     /**
151      * Receive notification of character data.
152      */

153     public void characters(char ch[], int start, int len)
154     throws SAXException JavaDoc {
155         this.contentHandler.characters(ch,start,len);
156     }
157
158     /**
159      * Receive notification of ignorable whitespace in element content.
160      */

161     public void ignorableWhitespace(char ch[], int start, int len)
162     throws SAXException JavaDoc {
163         this.contentHandler.ignorableWhitespace(ch,start,len);
164     }
165
166     /**
167      * Receive notification of a processing instruction.
168      */

169     public void processingInstruction(String JavaDoc target, String JavaDoc data)
170     throws SAXException JavaDoc {
171         this.contentHandler.processingInstruction(target,data);
172     }
173
174     /**
175      * Receive notification of a skipped entity.
176      *
177      * @param name The name of the skipped entity. If it is a parameter
178      * entity, the name will begin with '%'.
179      */

180     public void skippedEntity(String JavaDoc name)
181     throws SAXException JavaDoc {
182         this.contentHandler.skippedEntity(name);
183     }
184
185         /**
186      * Report the start of DTD declarations, if any.
187      *
188      * @param name The document type name.
189      * @param publicId The declared public identifier for the external DTD
190      * subset, or null if none was declared.
191      * @param systemId The declared system identifier for the external DTD
192      * subset, or null if none was declared.
193      */

194     public void startDTD(String JavaDoc name, String JavaDoc publicId, String JavaDoc systemId)
195     throws SAXException JavaDoc {
196         if (this.lexicalHandler != null)
197             this.lexicalHandler.startDTD(name, publicId, systemId);
198     }
199
200     /**
201      * Report the end of DTD declarations.
202      */

203     public void endDTD()
204     throws SAXException JavaDoc {
205         if (this.lexicalHandler != null)
206             this.lexicalHandler.endDTD();
207     }
208
209     /**
210      * Report the beginning of an entity.
211      *
212      * @param name The name of the entity. If it is a parameter entity, the
213      * name will begin with '%'.
214      */

215     public void startEntity(String JavaDoc name)
216     throws SAXException JavaDoc {
217         if (this.lexicalHandler != null)
218             this.lexicalHandler.startEntity(name);
219     }
220
221     /**
222      * Report the end of an entity.
223      *
224      * @param name The name of the entity that is ending.
225      */

226     public void endEntity(String JavaDoc name)
227     throws SAXException JavaDoc {
228         if (this.lexicalHandler != null)
229             this.lexicalHandler.endEntity(name);
230     }
231
232     /**
233      * Report the start of a CDATA section.
234      */

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

244     public void endCDATA()
245     throws SAXException JavaDoc {
246         if (this.lexicalHandler != null)
247             this.lexicalHandler.endCDATA();
248     }
249
250
251     /**
252      * Report an XML comment anywhere in the document.
253      *
254      * @param ch An array holding the characters in the comment.
255      * @param start The starting position in the array.
256      * @param len The number of characters to use from the array.
257      */

258     public void comment(char ch[], int start, int len)
259     throws SAXException JavaDoc {
260         if (this.lexicalHandler != null)
261             this.lexicalHandler.comment(ch, start, len);
262     }
263
264 }
265
Popular Tags