KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > myvietnam > mvncore > configuration > ConfigurationXMLReader


1 package net.myvietnam.mvncore.configuration;
2
3 /* ====================================================================
4  * The Apache Software License, Version 1.1
5  *
6  * Copyright (c) 1999-2003 The Apache Software Foundation. All rights
7  * reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if
22  * any, must include the following acknowledgement:
23  * "This product includes software developed by the
24  * Apache Software Foundation (http://www.apache.org/)."
25  * Alternately, this acknowledgement may appear in the software itself,
26  * if and wherever such third-party acknowledgements normally appear.
27  *
28  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
29  * Foundation" must not be used to endorse or promote products derived
30  * from this software without prior written permission. For written
31  * permission, please contact apache@apache.org.
32  *
33  * 5. Products derived from this software may not be called "Apache"
34  * nor may "Apache" appear in their names without prior written
35  * permission of the Apache Software Foundation.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals on behalf of the Apache Software Foundation. For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  */

56
57 import java.io.IOException JavaDoc;
58
59 import org.xml.sax.Attributes JavaDoc;
60 import org.xml.sax.ContentHandler JavaDoc;
61 import org.xml.sax.DTDHandler JavaDoc;
62 import org.xml.sax.EntityResolver JavaDoc;
63 import org.xml.sax.ErrorHandler JavaDoc;
64 import org.xml.sax.InputSource JavaDoc;
65 import org.xml.sax.SAXException JavaDoc;
66 import org.xml.sax.XMLReader JavaDoc;
67 import org.xml.sax.helpers.AttributesImpl JavaDoc;
68
69 /**
70  * <p>A base class for &quot;faked&quot; <code>XMLReader</code> classes
71  * that transform a configuration object in a set of SAX parsing events.</p>
72  * <p>This class provides dummy implementations for most of the methods
73  * defined in the <code>XMLReader</code> interface that are not used for this
74  * special purpose. There will be concrete sub classes that process specific
75  * configuration classes.</p>
76  *
77  * @author <a HREF="mailto:oliver.heger@t-online.de">Oliver Heger</a>
78  * @version $Id: ConfigurationXMLReader.java,v 1.1 2003/12/09 08:25:30 huumai Exp $
79  */

80 public abstract class ConfigurationXMLReader implements XMLReader JavaDoc
81 {
82     /** Constant for the namespace URI.*/
83     protected static final String JavaDoc NS_URI = "";
84
85     /** Constant for the default name of the root element.*/
86     private static final String JavaDoc DEFAULT_ROOT_NAME = "config";
87
88     /** An empty attributes object.*/
89     private static final Attributes JavaDoc EMPTY_ATTRS = new AttributesImpl JavaDoc();
90
91     /** Stores the content handler.*/
92     private ContentHandler JavaDoc contentHandler;
93
94     /** Stores an exception that occurred during parsing.*/
95     private SAXException JavaDoc exception;
96
97     /** Stores the name for the root element.*/
98     private String JavaDoc rootName;
99
100     /**
101      * Creates a new instance of <code>ConfigurationXMLReader</code>.
102      */

103     protected ConfigurationXMLReader()
104     {
105         super();
106         setRootName(DEFAULT_ROOT_NAME);
107     }
108
109     /**
110      * Parses the acutal configuration object. The passed system ID will be
111      * ignored.
112      * @param systemId the system ID (ignored)
113      * @throws IOException if no configuration was specified
114      * @throws SAXException if an error occurs during parsing
115      */

116     public void parse(String JavaDoc systemId) throws IOException JavaDoc, SAXException JavaDoc
117     {
118         parseConfiguration();
119     }
120
121     /**
122      * Parses the acutal configuration object. The passed input source will be
123      * ignored.
124      * @param input the input source (ignored)
125      * @throws IOException if no configuration was specified
126      * @throws SAXException if an error occurs during parsing
127      */

128     public void parse(InputSource JavaDoc input) throws IOException JavaDoc, SAXException JavaDoc
129     {
130         parseConfiguration();
131     }
132
133     /**
134      * Dummy implementation of the interface method.
135      * @param name the name of the feature
136      * @return always <b>false</b> (no features are supported)
137      */

138     public boolean getFeature(String JavaDoc name)
139     {
140         return false;
141     }
142
143     /**
144      * Dummy implementation of the interface method.
145      * @param name the name of the feature to be set
146      * @param value the value of the feature
147      */

148     public void setFeature(String JavaDoc name, boolean value)
149     {
150     }
151
152     /**
153      * Returns the actually set content handler.
154      * @return the content handler
155      */

156     public ContentHandler JavaDoc getContentHandler()
157     {
158         return contentHandler;
159     }
160
161     /**
162      * Sets the content handler. The object specified here will receive SAX
163      * events during parsing.
164      * @param handler the content handler
165      */

166     public void setContentHandler(ContentHandler JavaDoc handler)
167     {
168         contentHandler = handler;
169     }
170
171     /**
172      * Returns the DTD handler. This class does not support DTD handlers,
173      * so this method always returns <b>null</b>.
174      * @return the DTD handler
175      */

176     public DTDHandler JavaDoc getDTDHandler()
177     {
178         return null;
179     }
180
181     /**
182      * Sets the DTD handler. The passed value is ignored.
183      * @param handler the handler to be set
184      */

185     public void setDTDHandler(DTDHandler JavaDoc handler)
186     {
187     }
188
189     /**
190      * Returns the entity resolver. This class does not support an entity
191      * resolver, so this method always returns <b>null</b>.
192      * @return the entity resolver
193      */

194     public EntityResolver JavaDoc getEntityResolver()
195     {
196         return null;
197     }
198
199     /**
200      * Sets the entity resolver. The passed value is ignored.
201      * @param resolver the entity resolver
202      */

203     public void setEntityResolver(EntityResolver JavaDoc resolver)
204     {
205     }
206
207     /**
208      * Returns the error handler. This class does not support an error handler,
209      * so this method always returns <b>null</b>.
210      * @return the error handler
211      */

212     public ErrorHandler JavaDoc getErrorHandler()
213     {
214         return null;
215     }
216
217     /**
218      * Sets the error handler. The passed value is ignored.
219      * @param handler the error handler
220      */

221     public void setErrorHandler(ErrorHandler JavaDoc handler)
222     {
223     }
224
225     /**
226      * Dummy implementation of the interface method. No properties are
227      * supported, so this method always returns <b>null</b>.
228      * @param name the name of the requested property
229      * @return the property value
230      */

231     public Object JavaDoc getProperty(String JavaDoc name)
232     {
233         return null;
234     }
235
236     /**
237      * Dummy implementation of the interface method. No properties are
238      * supported, so a call of this method just has no effect.
239      * @param name the property name
240      * @param value the property value
241      */

242     public void setProperty(String JavaDoc name, Object JavaDoc value)
243     {
244     }
245
246     /**
247      * Returns the name to be used for the root element.
248      * @return the name for the root element
249      */

250     public String JavaDoc getRootName()
251     {
252         return rootName;
253     }
254
255     /**
256      * Sets the name for the root element.
257      * @param string the name for the root element.
258      */

259     public void setRootName(String JavaDoc string)
260     {
261         rootName = string;
262     }
263
264     /**
265      * Fires a SAX element start event.
266      * @param name the name of the actual element
267      * @param attribs the attributes of this element (can be <b>null</b>)
268      */

269     protected void fireElementStart(String JavaDoc name, Attributes JavaDoc attribs)
270     {
271         if (getException() == null)
272         {
273             try
274             {
275                 Attributes JavaDoc at = (attribs == null) ? EMPTY_ATTRS : attribs;
276                 getContentHandler().startElement(NS_URI, name, name, at);
277             } /* try */
278             catch (SAXException JavaDoc ex)
279             {
280                 exception = ex;
281             } /* catch */
282         } /* if */
283     }
284
285     /**
286      * Fires a SAX element end event.
287      * @param name the name of the affected element
288      */

289     protected void fireElementEnd(String JavaDoc name)
290     {
291         if (getException() == null)
292         {
293             try
294             {
295                 getContentHandler().endElement(NS_URI, name, name);
296             } /* try */
297             catch (SAXException JavaDoc ex)
298             {
299                 exception = ex;
300             } /* catch */
301         } /* if */
302     }
303
304     /**
305      * Fires a SAX characters event.
306      * @param text the text
307      */

308     protected void fireCharacters(String JavaDoc text)
309     {
310         if (getException() == null)
311         {
312             try
313             {
314                 char[] ch = text.toCharArray();
315                 getContentHandler().characters(ch, 0, ch.length);
316             } /* try */
317             catch (SAXException JavaDoc ex)
318             {
319                 exception = ex;
320             } /* catch */
321         } /* if */
322     }
323
324     /**
325      * Returns a reference to an exception that occurred during parsing.
326      * @return a SAXExcpetion or <b>null</b> if none occurred
327      */

328     public SAXException JavaDoc getException()
329     {
330         return exception;
331     }
332
333     /**
334      * Parses the configuration object and generates SAX events. This is the
335      * main processing method.
336      * @throws IOException if no configuration has been specified
337      * @throws SAXException if an error occurs during parsing
338      */

339     protected void parseConfiguration() throws IOException JavaDoc, SAXException JavaDoc
340     {
341         if (getParsedConfiguration() == null)
342         {
343             throw new IOException JavaDoc("No configuration specified!");
344         } /* if */
345
346         if (getContentHandler() != null)
347         {
348             exception = null;
349             getContentHandler().startDocument();
350             processKeys();
351             if (getException() != null)
352             {
353                 throw getException();
354             } /* if */
355             getContentHandler().endDocument();
356         } /* if */
357     }
358
359     /**
360      * Returns a reference to the configuration that is parsed by this object.
361      * @return the parsed configuration
362      */

363     public abstract Configuration getParsedConfiguration();
364
365     /**
366      * Processes all keys stored in the actual configuration. This method is
367      * called by <code>parseConfiguration()</code> to start the main parsing
368      * process. <code>parseConfiguration()</code> calls the content handler's
369      * <code>startDocument()</code> and <code>endElement()</code> methods
370      * and cares for exception handling. The remaining actions are left to this
371      * method that must be implemented in a concrete sub class.
372      * @throws IOException if an IO error occurs
373      * @throws SAXException if a SAX error occurs
374      */

375     protected abstract void processKeys() throws IOException JavaDoc, SAXException JavaDoc;
376 }
377
Popular Tags