KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > framework > configuration > AbstractConfiguration


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

55 package org.apache.avalon.framework.configuration;
56
57 /**
58  * This is an abstract <code>Configuration</code> implementation that deals
59  * with methods that can be abstracted away from underlying implementations.
60  *
61  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
62  * @version CVS $Revision: 1.28 $ $Date: 2003/02/11 16:19:27 $
63  */

64 public abstract class AbstractConfiguration
65     implements Configuration
66 {
67     /**
68      * Returns the prefix of the namespace. This is only used as a serialization
69      * hint, therefore is not part of the client API. It should be included in
70      * all Configuration implementations though.
71      * @return A non-null String (defaults to "")
72      * @throws ConfigurationException if no prefix was defined (prefix is
73      * <code>null</code>.
74      * @since 4.1
75      */

76     protected abstract String JavaDoc getPrefix() throws ConfigurationException;
77
78     /**
79      * Returns the value of the configuration element as an <code>int</code>.
80      *
81      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
82      * numbers begin with 0b, all other values are assumed to be decimal.
83      *
84      * @throws ConfigurationException if an error occurs
85      * @return the value
86      */

87     public int getValueAsInteger()
88         throws ConfigurationException
89     {
90         final String JavaDoc value = getValue().trim();
91         try
92         {
93             if( value.startsWith( "0x" ) )
94             {
95                 return Integer.parseInt( value.substring( 2 ), 16 );
96             }
97             else if( value.startsWith( "0o" ) )
98             {
99                 return Integer.parseInt( value.substring( 2 ), 8 );
100             }
101             else if( value.startsWith( "0b" ) )
102             {
103                 return Integer.parseInt( value.substring( 2 ), 2 );
104             }
105             else
106             {
107                 return Integer.parseInt( value );
108             }
109         }
110         catch( final Exception JavaDoc nfe )
111         {
112             final String JavaDoc message =
113                 "Cannot parse the value \"" + value
114                 + "\" as an integer in the configuration element \""
115                 + getName() + "\" at " + getLocation();
116             throw new ConfigurationException( message );
117         }
118     }
119
120     /**
121      * Returns the value of the configuration element as an <code>int</code>.
122      *
123      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
124      * numbers begin with 0b, all other values are assumed to be decimal.
125      *
126      * @param defaultValue the default value to return if value malformed or empty
127      * @return the value
128      */

129     public int getValueAsInteger( final int defaultValue )
130     {
131         try
132         {
133             return getValueAsInteger();
134         }
135         catch( final ConfigurationException ce )
136         {
137             return defaultValue;
138         }
139     }
140
141     /**
142      * Returns the value of the configuration element as a <code>long</code>.
143      *
144      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
145      * numbers begin with 0b, all other values are assumed to be decimal.
146      *
147      * @throws ConfigurationException if an error occurs
148      * @return the value
149      */

150     public long getValueAsLong()
151         throws ConfigurationException
152     {
153         final String JavaDoc value = getValue().trim();
154         try
155         {
156             if( value.startsWith( "0x" ) )
157             {
158                 return Long.parseLong( value.substring( 2 ), 16 );
159             }
160             else if( value.startsWith( "0o" ) )
161             {
162                 return Long.parseLong( value.substring( 2 ), 8 );
163             }
164             else if( value.startsWith( "0b" ) )
165             {
166                 return Long.parseLong( value.substring( 2 ), 2 );
167             }
168             else
169             {
170                 return Long.parseLong( value );
171             }
172         }
173         catch( final Exception JavaDoc nfe )
174         {
175             final String JavaDoc message =
176                 "Cannot parse the value \"" + value
177                 + "\" as a long in the configuration element \""
178                 + getName() + "\" at " + getLocation();
179             throw new ConfigurationException( message );
180         }
181     }
182
183     /**
184      * Returns the value of the configuration element as a <code>long</code>.
185      *
186      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
187      * numbers begin with 0b, all other values are assumed to be decimal.
188      *
189      * @param defaultValue the default value to return if value malformed or empty
190      * @return the value
191      */

192     public long getValueAsLong( final long defaultValue )
193     {
194         try
195         {
196             return getValueAsLong();
197         }
198         catch( final ConfigurationException ce )
199         {
200             return defaultValue;
201         }
202     }
203
204     /**
205      * Returns the value of the configuration element as a <code>float</code>.
206      *
207      * @throws ConfigurationException if an error occurs
208      * @return the value
209      */

210     public float getValueAsFloat()
211         throws ConfigurationException
212     {
213         final String JavaDoc value = getValue().trim();
214         try
215         {
216             return Float.parseFloat( value );
217         }
218         catch( final Exception JavaDoc nfe )
219         {
220             final String JavaDoc message =
221                 "Cannot parse the value \"" + value
222                 + "\" as a float in the configuration element \""
223                 + getName() + "\" at " + getLocation();
224             throw new ConfigurationException( message );
225         }
226     }
227
228     /**
229      * Returns the value of the configuration element as a <code>float</code>.
230      *
231      * @param defaultValue the default value to return if value malformed or empty
232      * @return the value
233      */

234     public float getValueAsFloat( final float defaultValue )
235     {
236         try
237         {
238             return getValueAsFloat();
239         }
240         catch( final ConfigurationException ce )
241         {
242             return ( defaultValue );
243         }
244     }
245
246     /**
247      * Returns the value of the configuration element as a <code>boolean</code>.
248      *
249      * @throws ConfigurationException if an error occurs
250      * @return the value
251      */

252     public boolean getValueAsBoolean()
253         throws ConfigurationException
254     {
255         final String JavaDoc value = getValue().trim();
256
257         if( isTrue( value ) )
258         {
259             return true;
260         }
261         else if( isFalse( value ) )
262         {
263             return false;
264         }
265         else
266         {
267             final String JavaDoc message =
268                 "Cannot parse the value \"" + value
269                 + "\" as a boolean in the configuration element \""
270                 + getName() + "\" at " + getLocation();
271             throw new ConfigurationException( message );
272         }
273     }
274
275     /**
276      * Returns the value of the configuration element as a <code>boolean</code>.
277      *
278      * @param defaultValue the default value to return if value malformed or empty
279      * @return the value
280      */

281     public boolean getValueAsBoolean( final boolean defaultValue )
282     {
283         try
284         {
285             return getValueAsBoolean();
286         }
287         catch( final ConfigurationException ce )
288         {
289             return defaultValue;
290         }
291     }
292
293     /**
294      * Returns the value of the configuration element as a <code>String</code>.
295      *
296      * @param defaultValue the default value to return if value malformed or empty
297      * @return the value
298      */

299     public String JavaDoc getValue( final String JavaDoc defaultValue )
300     {
301         try
302         {
303             return getValue();
304         }
305         catch( final ConfigurationException ce )
306         {
307             return defaultValue;
308         }
309     }
310
311     /**
312      * Returns the value of the attribute specified by its name as an
313      * <code>int</code>.
314      *
315      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
316      * numbers begin with 0b, all other values are assumed to be decimal.
317      *
318      * @param name the name of the attribute
319      * @throws ConfigurationException if an error occurs
320      * @return the value
321      */

322     public int getAttributeAsInteger( final String JavaDoc name )
323         throws ConfigurationException
324     {
325         final String JavaDoc value = getAttribute( name ).trim();
326         try
327         {
328             if( value.startsWith( "0x" ) )
329             {
330                 return Integer.parseInt( value.substring( 2 ), 16 );
331             }
332             else if( value.startsWith( "0o" ) )
333             {
334                 return Integer.parseInt( value.substring( 2 ), 8 );
335             }
336             else if( value.startsWith( "0b" ) )
337             {
338                 return Integer.parseInt( value.substring( 2 ), 2 );
339             }
340             else
341             {
342                 return Integer.parseInt( value );
343             }
344         }
345         catch( final Exception JavaDoc nfe )
346         {
347             final String JavaDoc message =
348                 "Cannot parse the value \"" + value
349                 + "\" as an integer in the attribute \""
350                 + name + "\" at " + getLocation();
351             throw new ConfigurationException( message );
352         }
353     }
354
355     /**
356      * Returns the value of the attribute specified by its name as an
357      * <code>int</code>.
358      *
359      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
360      * numbers begin with 0b, all other values are assumed to be decimal.
361      *
362      * @param name the name of the attribute
363      * @param defaultValue the default value to return if value malformed or empty
364      * @return the value
365      */

366     public int getAttributeAsInteger( final String JavaDoc name, final int defaultValue )
367     {
368         try
369         {
370             return getAttributeAsInteger( name );
371         }
372         catch( final ConfigurationException ce )
373         {
374             return defaultValue;
375         }
376     }
377
378     /**
379      * Returns the value of the attribute specified by its name as a
380      * <code>long</code>.
381      *
382      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
383      * numbers begin with 0b, all other values are assumed to be decimal.
384      *
385      * @param name the name of the attribute
386      * @throws ConfigurationException if an error occurs
387      * @return the value
388      */

389     public long getAttributeAsLong( final String JavaDoc name )
390         throws ConfigurationException
391     {
392         final String JavaDoc value = getAttribute( name );
393
394         try
395         {
396             if( value.startsWith( "0x" ) )
397             {
398                 return Long.parseLong( value.substring( 2 ), 16 );
399             }
400             else if( value.startsWith( "0o" ) )
401             {
402                 return Long.parseLong( value.substring( 2 ), 8 );
403             }
404             else if( value.startsWith( "0b" ) )
405             {
406                 return Long.parseLong( value.substring( 2 ), 2 );
407             }
408             else
409             {
410                 return Long.parseLong( value );
411             }
412         }
413         catch( final Exception JavaDoc nfe )
414         {
415             final String JavaDoc message =
416                 "Cannot parse the value \"" + value
417                 + "\" as a long in the attribute \""
418                 + name + "\" at " + getLocation();
419             throw new ConfigurationException( message );
420         }
421     }
422
423     /**
424      * Returns the value of the attribute specified by its name as a
425      * <code>long</code>.
426      *
427      * Hexadecimal numbers begin with 0x, Octal numbers begin with 0o and binary
428      * numbers begin with 0b, all other values are assumed to be decimal.
429      *
430      * @param name the name of the attribute
431      * @param defaultValue the default value to return if value malformed or empty
432      * @return the value
433      */

434     public long getAttributeAsLong( final String JavaDoc name, final long defaultValue )
435     {
436         try
437         {
438             return getAttributeAsLong( name );
439         }
440         catch( final ConfigurationException ce )
441         {
442             return defaultValue;
443         }
444     }
445
446     /**
447      * Returns the value of the attribute specified by its name as a
448      * <code>float</code>.
449      *
450      * @param name the name of the attribute
451      * @throws ConfigurationException if an error occurs
452      * @return the value
453      */

454     public float getAttributeAsFloat( final String JavaDoc name )
455         throws ConfigurationException
456     {
457         final String JavaDoc value = getAttribute( name );
458         try
459         {
460             return Float.parseFloat( value );
461         }
462         catch( final Exception JavaDoc e )
463         {
464             final String JavaDoc message =
465                 "Cannot parse the value \"" + value
466                 + "\" as a float in the attribute \""
467                 + name + "\" at " + getLocation();
468             throw new ConfigurationException( message );
469         }
470     }
471
472     /**
473      * Returns the value of the attribute specified by its name as a
474      * <code>float</code>.
475      *
476      * @param name the name of the attribute
477      * @param defaultValue the default value to return if value malformed or empty
478      * @return the value
479      */

480     public float getAttributeAsFloat( final String JavaDoc name, final float defaultValue )
481     {
482         try
483         {
484             return getAttributeAsFloat( name );
485         }
486         catch( final ConfigurationException ce )
487         {
488             return defaultValue;
489         }
490     }
491
492     /**
493      * Returns the value of the attribute specified by its name as a
494      * <code>boolean</code>.
495      *
496      * @param name the name of the attribute
497      * @throws ConfigurationException if an error occurs
498      * @return the value
499      */

500     public boolean getAttributeAsBoolean( final String JavaDoc name )
501         throws ConfigurationException
502     {
503         final String JavaDoc value = getAttribute( name );
504
505         if( isTrue( value ) )
506         {
507             return true;
508         }
509         else if( isFalse( value ) )
510         {
511             return false;
512         }
513         else
514         {
515             final String JavaDoc message =
516                 "Cannot parse the value \"" + value
517                 + "\" as a boolean in the attribute \""
518                 + name + "\" at " + getLocation();
519             throw new ConfigurationException( message );
520         }
521     }
522
523     private boolean isTrue( final String JavaDoc value )
524     {
525         return value.equalsIgnoreCase( "true" )
526             || value.equalsIgnoreCase( "yes" )
527             || value.equalsIgnoreCase( "on" )
528             || value.equalsIgnoreCase( "1" );
529     }
530
531     private boolean isFalse( final String JavaDoc value )
532     {
533         return value.equalsIgnoreCase( "false" )
534             || value.equalsIgnoreCase( "no" )
535             || value.equalsIgnoreCase( "off" )
536             || value.equalsIgnoreCase( "0" );
537     }
538
539     /**
540      * Returns the value of the attribute specified by its name as a
541      * <code>boolean</code>.
542      *
543      * @param name the name of the attribute
544      * @param defaultValue the default value to return if value malformed or empty
545      * @return the value
546      */

547     public boolean getAttributeAsBoolean( final String JavaDoc name, final boolean defaultValue )
548     {
549         try
550         {
551             return getAttributeAsBoolean( name );
552         }
553         catch( final ConfigurationException ce )
554         {
555             return defaultValue;
556         }
557     }
558
559     /**
560      * Returns the value of the attribute specified by its name as a
561      * <code>String</code>.
562      *
563      * @param name the name of the attribute
564      * @param defaultValue the default value to return if value malformed or empty
565      * @return the value
566      */

567     public String JavaDoc getAttribute( final String JavaDoc name, final String JavaDoc defaultValue )
568     {
569         try
570         {
571             return getAttribute( name );
572         }
573         catch( final ConfigurationException ce )
574         {
575             return defaultValue;
576         }
577     }
578
579     /**
580      * Return the first <code>Configuration</code> object child of this
581      * associated with the given name. If no such child exists, a new one
582      * will be created.
583      *
584      * @param name the name of the child
585      * @return the child Configuration
586      */

587     public Configuration getChild( final String JavaDoc name )
588     {
589         return getChild( name, true );
590     }
591
592     /**
593      * Return the first <code>Configuration</code> object child of this
594      * associated with the given name.
595      *
596      * @param name the name of the child
597      * @param createNew true if you want to create a new Configuration object if none exists
598      * @return the child Configuration
599      */

600     public Configuration getChild( final String JavaDoc name, final boolean createNew )
601     {
602         final Configuration[] children = getChildren( name );
603         if( children.length > 0 )
604         {
605             return children[ 0 ];
606         }
607         else
608         {
609             if( createNew )
610             {
611                 return new DefaultConfiguration( name, "-" );
612             }
613             else
614             {
615                 return null;
616             }
617         }
618     }
619 }
620
Popular Tags