KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.apache.excalibur.xml.sax;
18
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  * {@link ContentHandler} and forwarding it those events received throug
28  * its {@link XMLConsumer}s interface.
29  * <br>
30  *
31  * @deprecated Moved to org.apache.excalibur.xml.sax package. Modified to be
32  * thread safe.
33  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
34  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:20 $
35  */

36 public class ContentHandlerWrapper
37     extends AbstractXMLConsumer
38 {
39     /** The current {@link ContentHandler}. */
40     private ContentHandler JavaDoc m_contentHandler;
41
42     /** The optional {@link LexicalHandler} */
43     private LexicalHandler JavaDoc m_lexicalHandler;
44
45     /**
46      * Create a new <code>ContentHandlerWrapper</code> instance.
47      */

48     public ContentHandlerWrapper()
49     {
50     }
51
52     /**
53      * Create a new <code>ContentHandlerWrapper</code> instance.
54      */

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

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

76     public void setContentHandler( final ContentHandler JavaDoc contentHandler )
77         throws IllegalStateException JavaDoc
78     {
79         if( null != m_contentHandler )
80         {
81             throw new IllegalStateException JavaDoc();
82         }
83         m_contentHandler = contentHandler;
84     }
85
86     /**
87      * Set the {@link LexicalHandler} that will receive XML data.
88      *
89      * @exception IllegalStateException If the {@link LexicalHandler}
90      * was already set.
91      */

92     public void setLexicalHandler( final LexicalHandler JavaDoc lexicalHandler )
93         throws IllegalStateException JavaDoc
94     {
95         if( null != m_lexicalHandler )
96         {
97             throw new IllegalStateException JavaDoc();
98         }
99         m_lexicalHandler = lexicalHandler;
100     }
101
102     /**
103      * Receive an object for locating the origin of SAX document events.
104      */

105     public void setDocumentLocator( final Locator JavaDoc locator )
106     {
107         if( null == m_contentHandler )
108         {
109             return;
110         }
111         else
112         {
113             m_contentHandler.setDocumentLocator( locator );
114         }
115     }
116
117     /**
118      * Receive notification of the beginning of a document.
119      */

120     public void startDocument()
121         throws SAXException JavaDoc
122     {
123         if( null == m_contentHandler )
124         {
125             final String JavaDoc message = "ContentHandler not set";
126             throw new SAXException JavaDoc( message );
127         }
128         m_contentHandler.startDocument();
129     }
130
131     /**
132      * Receive notification of the end of a document.
133      */

134     public void endDocument()
135         throws SAXException JavaDoc
136     {
137         m_contentHandler.endDocument();
138     }
139
140     /**
141      * Begin the scope of a prefix-URI Namespace mapping.
142      */

143     public void startPrefixMapping( final String JavaDoc prefix,
144                                     final String JavaDoc uri )
145         throws SAXException JavaDoc
146     {
147         if( null == m_contentHandler )
148         {
149             final String JavaDoc message = "ContentHandler not set";
150             throw new SAXException JavaDoc( message );
151         }
152         m_contentHandler.startPrefixMapping( prefix, uri );
153     }
154
155     /**
156      * End the scope of a prefix-URI mapping.
157      */

158     public void endPrefixMapping( final String JavaDoc prefix )
159         throws SAXException JavaDoc
160     {
161         m_contentHandler.endPrefixMapping( prefix );
162     }
163
164     /**
165      * Receive notification of the beginning of an element.
166      */

167     public void startElement( final String JavaDoc uri,
168                               final String JavaDoc loc,
169                               final String JavaDoc raw,
170                               final Attributes JavaDoc a )
171         throws SAXException JavaDoc
172     {
173         m_contentHandler.startElement( uri, loc, raw, a );
174     }
175
176     /**
177      * Receive notification of the end of an element.
178      */

179     public void endElement( final String JavaDoc uri,
180                             final String JavaDoc loc,
181                             final String JavaDoc raw )
182         throws SAXException JavaDoc
183     {
184         m_contentHandler.endElement( uri, loc, raw );
185     }
186
187     /**
188      * Receive notification of character data.
189      */

190     public void characters( final char[] ch,
191                             final int start,
192                             final int len )
193         throws SAXException JavaDoc
194     {
195         m_contentHandler.characters( ch, start, len );
196     }
197
198     /**
199      * Receive notification of ignorable whitespace in element content.
200      */

201     public void ignorableWhitespace( final char[] ch,
202                                      final int start,
203                                      final int len )
204         throws SAXException JavaDoc
205     {
206         m_contentHandler.ignorableWhitespace( ch, start, len );
207     }
208
209     /**
210      * Receive notification of a processing instruction.
211      */

212     public void processingInstruction( final String JavaDoc target,
213                                        final String JavaDoc data )
214         throws SAXException JavaDoc
215     {
216         m_contentHandler.processingInstruction( target, data );
217     }
218
219     /**
220      * Receive notification of a skipped entity.
221      *
222      * @param name The name of the skipped entity. If it is a parameter
223      * entity, the name will begin with '%'.
224      */

225     public void skippedEntity( final String JavaDoc name )
226         throws SAXException JavaDoc
227     {
228         m_contentHandler.skippedEntity( name );
229     }
230
231     /**
232      * Report the start of DTD declarations, if any.
233      *
234      * @param name The document type name.
235      * @param publicId The declared public identifier for the external DTD
236      * subset, or null if none was declared.
237      * @param systemId The declared system identifier for the external DTD
238      * subset, or null if none was declared.
239      */

240     public void startDTD( final String JavaDoc name,
241                           final String JavaDoc publicId,
242                           final String JavaDoc systemId )
243         throws SAXException JavaDoc
244     {
245         if( null != m_lexicalHandler )
246         {
247             m_lexicalHandler.startDTD( name, publicId, systemId );
248         }
249     }
250
251     /**
252      * Report the end of DTD declarations.
253      */

254     public void endDTD()
255         throws SAXException JavaDoc
256     {
257         if( null != m_lexicalHandler )
258         {
259             m_lexicalHandler.endDTD();
260         }
261     }
262
263     /**
264      * Report the beginning of an entity.
265      *
266      * @param name The name of the entity. If it is a parameter entity, the
267      * name will begin with '%'.
268      */

269     public void startEntity( final String JavaDoc name )
270         throws SAXException JavaDoc
271     {
272         if( null != m_lexicalHandler )
273         {
274             m_lexicalHandler.startEntity( name );
275         }
276     }
277
278     /**
279      * Report the end of an entity.
280      *
281      * @param name The name of the entity that is ending.
282      */

283     public void endEntity( final String JavaDoc name )
284         throws SAXException JavaDoc
285     {
286         if( null != m_lexicalHandler )
287         {
288             m_lexicalHandler.endEntity( name );
289         }
290     }
291
292     /**
293      * Report the start of a CDATA section.
294      */

295     public void startCDATA()
296         throws SAXException JavaDoc
297     {
298         if( null != m_lexicalHandler )
299         {
300             m_lexicalHandler.startCDATA();
301         }
302     }
303
304     /**
305      * Report the end of a CDATA section.
306      */

307     public void endCDATA()
308         throws SAXException JavaDoc
309     {
310         if( null != m_lexicalHandler )
311         {
312             m_lexicalHandler.endCDATA();
313         }
314     }
315
316     /**
317      * Report an XML comment anywhere in the document.
318      *
319      * @param ch An array holding the characters in the comment.
320      * @param start The starting position in the array.
321      * @param len The number of characters to use from the array.
322      */

323     public void comment( final char[] ch,
324                          final int start,
325                          final int len )
326         throws SAXException JavaDoc
327     {
328         if( null != m_lexicalHandler )
329         {
330             m_lexicalHandler.comment( ch, start, len );
331         }
332     }
333 }
334
Popular Tags