KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > slide > util > conf > AbstractConfiguration


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/AbstractConfiguration.java,v 1.5 2004/07/28 09:34:25 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:34:25 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.slide.util.conf;
25
26 import java.util.Enumeration JavaDoc;
27
28 /**
29  * This is an abstract <code>Configuration</code> implementation that deals
30  * with methods that can be abstracted away from underlying implementations.
31  *
32  * (Betaversion Productions)
33  * (Apache Software Foundation)
34  * (Apache Software Foundation, Exoffice Technologies)
35  * @version CVS $Revision: 1.5 $ $Date: 2004/07/28 09:34:25 $
36  */

37 public abstract class AbstractConfiguration implements Configuration {
38     /**
39      * The location string containing information about this
40      * <code>Configuration</code> location in the source file.
41      */

42     protected String JavaDoc location=null;
43
44     /**
45      * Construct a new <code>AbstractConfiguration</code> instance.
46      */

47     protected AbstractConfiguration() {
48         this(null,-1);
49     }
50
51     /**
52      * Construct a new <code>AbstractConfiguration</code> instance.
53      */

54     protected AbstractConfiguration(String JavaDoc source, int line) {
55         super();
56         this.location="";
57         if (source!=null) this.location=source;
58         if ((line>=0)&&(this.location.length()>0)) this.location+=" ";
59         if (line>0) this.location+="line "+line;
60         if (this.location.length()>0) this.location="("+this.location+")";
61         else this.location=null;
62     }
63
64     /**
65      * Returns the value of the configuration element as an <code>int</code>.
66      */

67     public int getValueAsInt()
68     throws ConfigurationException {
69         String JavaDoc value=this.getValue();
70         try {
71             if (value.startsWith("0x"))
72                 return(Integer.parseInt(value.substring(2),16));
73             else if (value.startsWith("0o"))
74                 return(Integer.parseInt(value.substring(2),8));
75             else if (value.startsWith("0b"))
76                 return(Integer.parseInt(value.substring(2),2));
77             else return(Integer.parseInt(value));
78         } catch (NumberFormatException JavaDoc e) {
79             throw new ConfigurationException("Cannot parse the value of the "+
80                 "configuration element \""+this.getName()+"\" as an integer",
81                 this);
82         }
83     }
84
85     /**
86      * Returns the value of the configuration element as a <code>long</code>.
87      */

88     public long getValueAsLong()
89     throws ConfigurationException {
90         String JavaDoc value=this.getValue();
91         try {
92             if (value.startsWith("0x"))
93                 return(Long.parseLong(value.substring(2),16));
94             else if (value.startsWith("0o"))
95                 return(Long.parseLong(value.substring(2),8));
96             else if (value.startsWith("0b"))
97                 return(Long.parseLong(value.substring(2),2));
98             else return(Integer.parseInt(value));
99         } catch (NumberFormatException JavaDoc e) {
100             throw new ConfigurationException("Cannot parse the value of the "+
101                 "configuration element \""+this.getName()+"\" as a long", this);
102         }
103     }
104
105     /**
106      * Returns the value of the configuration element as a <code>float</code>.
107      */

108     public float getValueAsFloat()
109     throws ConfigurationException {
110         String JavaDoc value=this.getValue();
111         try {
112             return(Float.valueOf(value).floatValue());
113         } catch (NumberFormatException JavaDoc e) {
114             throw new ConfigurationException("Cannot parse the value of the "+
115                 "configuration element \""+this.getName()+"\" as a float",
116                 this);
117         }
118     }
119
120     /**
121      * Returns the value of the configuration element as a <code>boolean</code>.
122      */

123     public boolean getValueAsBoolean()
124     throws ConfigurationException {
125         String JavaDoc value=this.getValue();
126         if (value.equals("true")) return(true);
127         if (value.equals("false")) return(false);
128         throw new ConfigurationException("Cannot parse the value of the "+
129             "configuration element \""+this.getName()+"\" as a boolean",
130             this);
131     }
132
133     /**
134      * Returns the value of the configuration element as a <code>String</code>.
135      */

136     public String JavaDoc getValue(String JavaDoc defaultValue) {
137         try {
138             return(this.getValue());
139         } catch (ConfigurationException e) {
140             return(defaultValue);
141         }
142     }
143
144     /**
145      * Returns the value of the configuration element as an <code>int</code>.
146      */

147     public int getValueAsInt(int defaultValue) {
148         try {
149             return(this.getValueAsInt());
150         } catch (ConfigurationException e) {
151             return(defaultValue);
152         }
153     }
154
155     /**
156      * Returns the value of the configuration element as a <code>long</code>.
157      */

158     public long getValueAsLong(long defaultValue) {
159         try {
160             return(this.getValueAsLong());
161         } catch (ConfigurationException e) {
162             return(defaultValue);
163         }
164     }
165
166     /**
167      * Returns the value of the configuration element as a <code>float</code>.
168      */

169     public float getValueAsFloat(float defaultValue) {
170         try {
171             return(this.getValueAsFloat());
172         } catch (ConfigurationException e) {
173             return(defaultValue);
174         }
175     }
176
177     /**
178      * Returns the value of the configuration element as a <code>boolean</code>.
179      */

180     public boolean getValueAsBoolean(boolean defaultValue) {
181         try {
182             return(this.getValueAsBoolean());
183         } catch (ConfigurationException e) {
184             return(defaultValue);
185         }
186     }
187
188     /**
189      * Returns the value of the attribute specified by its name as an
190      * <code>int</code>.
191      */

192     public int getAttributeAsInt(String JavaDoc name)
193     throws ConfigurationException {
194         String JavaDoc value=this.getAttribute(name);
195         try {
196             if (value.startsWith("0x"))
197                 return(Integer.parseInt(value.substring(2),16));
198             else if (value.startsWith("0o"))
199                 return(Integer.parseInt(value.substring(2),8));
200             else if (value.startsWith("0b"))
201                 return(Integer.parseInt(value.substring(2),2));
202             else return(Integer.parseInt(value));
203         } catch (NumberFormatException JavaDoc e) {
204             throw new ConfigurationException("Cannot parse the value of the "+
205                 "attribute \""+name+"\" of the configuration element \""+
206                 this.getName()+"\" as an integer",this);
207         }
208     }
209
210     /**
211      * Returns the value of the attribute specified by its name as a
212      * <code>long</code>.
213      */

214     public long getAttributeAsLong(String JavaDoc name)
215     throws ConfigurationException {
216         String JavaDoc value=this.getAttribute(name);
217         try {
218             if (value.startsWith("0x"))
219                 return(Long.parseLong(value.substring(2),16));
220             else if (value.startsWith("0o"))
221                 return(Long.parseLong(value.substring(2),8));
222             else if (value.startsWith("0b"))
223                 return(Long.parseLong(value.substring(2),2));
224             else return(Integer.parseInt(value));
225         } catch (NumberFormatException JavaDoc e) {
226             throw new ConfigurationException("Cannot parse the value of the "+
227                 "attribute \""+name+"\" of the configuration element \""+
228                 this.getName()+"\" as a long", this);
229         }
230     }
231
232     /**
233      * Returns the value of the attribute specified by its name as a
234      * <code>float</code>.
235      */

236     public float getAttributeAsFloat(String JavaDoc name)
237     throws ConfigurationException {
238         String JavaDoc value=this.getAttribute(name);
239         try {
240             return(Float.valueOf(value).floatValue());
241         } catch (NumberFormatException JavaDoc e) {
242             throw new ConfigurationException("Cannot parse the value of the "+
243                 "attribute \""+name+"\" of the configuration element \""+
244                 this.getName()+"\" as a float", this);
245         }
246     }
247
248     /**
249      * Returns the value of the attribute specified by its name as a
250      * <code>boolean</code>.
251      */

252     public boolean getAttributeAsBoolean(String JavaDoc name)
253     throws ConfigurationException {
254         String JavaDoc value=this.getAttribute(name);
255         if (value.equals("true")) return(true);
256         if (value.equals("false")) return(false);
257         throw new ConfigurationException("Cannot parse the value of the "+
258             "attribute \""+name+"\" of the configuration element \""+
259             this.getName()+"\" as a boolean", this);
260     }
261
262     /**
263      * Returns the value of the attribute specified by its name as a
264      * <code>String</code>.
265      */

266     public String JavaDoc getAttribute(String JavaDoc name, String JavaDoc defaultValue) {
267         try {
268             return(this.getAttribute(name));
269         } catch (ConfigurationException e) {
270             return(defaultValue);
271         }
272     }
273
274     /**
275      * Returns the value of the attribute specified by its name as an
276      * <code>int</code>.
277      */

278     public int getAttributeAsInt(String JavaDoc name, int defaultValue) {
279         try {
280             return(this.getAttributeAsInt(name));
281         } catch (ConfigurationException e) {
282             return(defaultValue);
283         }
284     }
285
286     /**
287      * Returns the value of the attribute specified by its name as a
288      * <code>long</code>.
289      */

290     public long getAttributeAsLong(String JavaDoc name, long defaultValue) {
291         try {
292             return(this.getAttributeAsLong(name));
293         } catch (ConfigurationException e) {
294             return(defaultValue);
295         }
296     }
297
298     /**
299      * Returns the value of the attribute specified by its name as a
300      * <code>float</code>.
301      */

302     public float getAttributeAsFloat(String JavaDoc name, float defaultValue) {
303         try {
304             return(this.getAttributeAsFloat(name));
305         } catch (ConfigurationException e) {
306             return(defaultValue);
307         }
308     }
309
310     /**
311      * Returns the value of the attribute specified by its name as a
312      * <code>boolean</code>.
313      */

314     public boolean getAttributeAsBoolean(String JavaDoc name, boolean defaultValue) {
315         try {
316             return(this.getAttributeAsBoolean(name));
317         } catch (ConfigurationException e) {
318             return(defaultValue);
319         }
320     }
321
322     /**
323      * Return the first <code>Configuration</code> object child of this
324      * associated with the given name.
325      */

326     public Configuration getConfiguration(String JavaDoc name) {
327         Enumeration JavaDoc e=this.getConfigurations(name);
328         if (e.hasMoreElements()) return((Configuration)e.nextElement());
329         return(null);
330     }
331
332     /**
333      * Return a <code>String</code> indicating the position of this
334      * configuration element in a source file or URI or <b>null</b>.
335      */

336     public String JavaDoc getLocation() {
337         return(this.location);
338     }
339 }
340
Popular Tags