KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/ConfigurationImpl.java,v 1.5 2004/07/28 09:34:23 ib Exp $
3  * $Revision: 1.5 $
4  * $Date: 2004/07/28 09:34:23 $
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 import java.util.Hashtable JavaDoc;
28 import java.util.Vector JavaDoc;
29 import java.util.NoSuchElementException JavaDoc;
30
31 /**
32  * This is the default <code>Configuration</code> implementation.
33  *
34  * (Betaversion Productions)
35  * (Apache Software Foundation)
36  * (Apache Software Foundation, Exoffice Technologies)
37  * @version CVS $Revision: 1.5 $ $Date: 2004/07/28 09:34:23 $
38  */

39 public class ConfigurationImpl extends AbstractConfiguration {
40
41     /** The configuration attributes table. */
42     private Hashtable JavaDoc attributes=new Hashtable JavaDoc();
43     /** The configuration children list grouped by name. */
44     private Hashtable JavaDoc children=new Hashtable JavaDoc();
45     /** The configuration element name. */
46     private String JavaDoc name=null;
47     /** The configuration element value. */
48     private String JavaDoc value=null;
49
50     /**
51      * Create a new <code>ConfigurationImpl</code> instance.
52      */

53     protected ConfigurationImpl() {
54         super();
55     }
56
57     /**
58      * Create a new <code>ConfigurationImpl</code> instance.
59      */

60     protected ConfigurationImpl(String JavaDoc name) {
61         super();
62         this.name=name;
63     }
64
65     /**
66      * Create a new <code>ConfigurationImpl</code> instance.
67      */

68     protected ConfigurationImpl(String JavaDoc name, String JavaDoc source, int line) {
69         super(source,line);
70         this.name=name;
71     }
72
73     /**
74      * Returns the name of this configuration element.
75      */

76     public String JavaDoc getName() {
77         return(this.name);
78     }
79
80     /**
81      * Returns the value of the configuration element as a <code>String</code>.
82      *
83      * @exception ConfigurationException If the value is not present.
84      */

85     public String JavaDoc getValue()
86     throws ConfigurationException {
87         if (this.value!=null) return(this.value);
88         throw new ConfigurationException("No value is associated with the "+
89             "configuration element \""+this.getName()+"\"", this);
90     }
91
92     /**
93      * Returns the value of the attribute specified by its name as a
94      * <code>String</code>.
95      *
96      * @exception ConfigurationException If the attribute is not present.
97      */

98     public String JavaDoc getAttribute(String JavaDoc name)
99     throws ConfigurationException {
100         String JavaDoc value=(String JavaDoc)this.attributes.get(name);
101         if (value!=null) return(value);
102         throw new ConfigurationException("No attribute named \""+name+"\" is "+
103             "associated with the configuration element \""+this.getName()+"\"",
104             this);
105     }
106
107     /**
108      * Return the first <code>Configuration</code> object child of this
109      * associated with the given name or <b>null</b>.
110      *
111      * <p>TODO this does not match the description in the interface
112      *
113      * @param name The name of the required child <code>Configuration</code>.
114      */

115     public Configuration getConfiguration(String JavaDoc name) {
116         Vector JavaDoc v=(Vector JavaDoc)this.children.get(name);
117         if ((v!=null) && (v.size()>0)) return((Configuration)v.firstElement());
118         return(null);
119     }
120
121     /**
122      * Return an <code>Enumeration</code> of <code>Configuration</code> objects
123      * children of this associated with the given name.
124      * <br>
125      * The returned <code>Enumeration</code> may be empty.
126      *
127      * @param name The name of the required children <code>Configuration</code>.
128      */

129     public Enumeration JavaDoc getConfigurations(String JavaDoc name) {
130         Vector JavaDoc v=(Vector JavaDoc)this.children.get(name);
131         if (v==null) return(new EmptyEnumerationImpl());
132         else return(v.elements());
133     }
134
135     /**
136      * Append data to the value of this configuration element.
137      */

138     protected void appendValueData(String JavaDoc value) {
139         if (this.value==null) this.value=value;
140         else this.value=this.value+value;
141     }
142
143     /**
144      * Add an attribute to this configuration element, returning its old
145      * value or <b>null</b>.
146      */

147     protected String JavaDoc addAttribute(String JavaDoc name, String JavaDoc value) {
148         return((String JavaDoc)this.attributes.put(name,value));
149     }
150
151     /**
152      * Add a child <code>Configuration</code> to this configuration element.
153      */

154     public void addConfiguration(Configuration conf) {
155         String JavaDoc name=conf.getName();
156         Vector JavaDoc v=(Vector JavaDoc)this.children.get(name);
157         if (v==null) {
158             v=new Vector JavaDoc();
159             this.children.put(name,v);
160         }
161         v.addElement(conf);
162     }
163
164     /** An empty <code>Enumeration</code> implementation. */
165     private class EmptyEnumerationImpl implements Enumeration JavaDoc {
166         /** Tests if this enumeration contains more elements. */
167         public boolean hasMoreElements() {
168             return(false);
169         }
170
171         /** Returns the next element of this enumeration. */
172         public Object JavaDoc nextElement() {
173             throw new NoSuchElementException JavaDoc();
174         }
175     }
176 }
177
Popular Tags