KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/util/conf/ConfigurationElement.java,v 1.4 2004/07/28 09:34:24 ib Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/07/28 09:34:24 $
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.*;
27
28 /**
29  * <code>ConfigurationElement</code> is a class implementing the
30  * <code>Configuration</code> interface using a tree of Elements as
31  * configuration container. The <code>Elements</code> tree is generated from the
32  * configuration DOM. Each <code>Element</code> is encapsulated by a
33  * <code>ConfigurationElement</code>.
34  *
35  *
36  * @version (CVS $Revision: 1.4 $ $Date: 2004/07/28 09:34:24 $)
37  */

38 public class ConfigurationElement extends AbstractConfiguration {
39
40     private Element content;
41     private Vector children;
42
43     public ConfigurationElement(Element content) {
44         this.content = content;
45         children = new Vector();
46         try {
47             for (Enumeration e = content.getChildren(); e.hasMoreElements();) {
48                 children.addElement(new ConfigurationElement((Element) e.nextElement()));
49             }
50         } catch (ConfigurationException e) {
51         }
52     }
53
54     public String JavaDoc getName() {
55         return content.getName();
56     }
57
58     public String JavaDoc getValue() {
59         return content.getData();
60     }
61
62     public String JavaDoc getAttribute(String JavaDoc name)
63     throws ConfigurationException {
64         String JavaDoc attribute = content.getAttributeValue(name);
65         if (attribute == null) {
66             throw new ConfigurationException("No attribute named \"" + name + "\" is " +
67                 "associated with the configuration element \"" + this.getName() + "\"",
68                 this);
69         }
70         return attribute;
71     }
72
73     public Configuration getConfiguration(String JavaDoc name)
74     throws ConfigurationException {
75
76         int index = name.indexOf('.');
77         if (index == -1) {
78             for (Enumeration e = children.elements(); e.hasMoreElements();) {
79                 Configuration c = (Configuration) e.nextElement();
80                 if (c.getName().equals(name)) {
81                     return c;
82                 }
83             }
84         } else {
85             return getConfiguration(name.substring(0, index)).getConfiguration(name.substring(index + 1));
86         }
87         throw new ConfigurationException("No Configuration named \"" + name + "\" is " +
88             "associated with the configuration element \"" + this.getName() + "\"", this);
89     }
90
91     public Enumeration getConfigurations(String JavaDoc name)
92     throws ConfigurationException {
93
94         int index = name.indexOf('.');
95         if (index == -1) {
96             Vector v = new Vector();
97             for (Enumeration e = children.elements(); e.hasMoreElements();) {
98                 Configuration c = (Configuration) e.nextElement();
99                 if (c.getName().equals(name)) {
100                     v.addElement(c);
101                 }
102             }
103             return v.elements();
104         } else {
105             return getConfiguration(name.substring(0, index)).getConfigurations(name.substring(index + 1));
106         }
107     }
108 }
109
Popular Tags