7d0c185005244bdb3bb7b9fcb3a3d2dc6c6c4eac
[mir.git] / source / mir / config / ConfigSimpleNode.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31 package mir.config;
32
33 import mir.config.exceptions.ConfigInvalidPropertyTypeException;
34 import mir.config.exceptions.ConfigMissingPropertyException;
35
36 import java.util.HashMap;
37 import java.util.Map;
38
39
40 public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {
41   private Map properties;
42   private Map subNodes;
43   private String locationDescription;
44   private String path;
45
46   public ConfigSimpleNode() {
47     this("", "");
48   }
49
50   public ConfigSimpleNode(String aLocationDescription) {
51     this("", aLocationDescription);
52   }
53
54   public ConfigSimpleNode(String aPath, String aLocationDescription) {
55     super();
56
57     path = aPath;
58     locationDescription = aLocationDescription;
59     properties = new HashMap();
60     subNodes = new HashMap();
61   }
62
63   // ConfigNodeBuilder helpers:
64   private String makeSubNodePath(String aSubNode) {
65     if ((path != null) && (path.length() > 0)) {
66       return path + "/" + aSubNode;
67     } else {
68       return aSubNode;
69     }
70   }
71
72   private String makePropertyPath(String aProperty) {
73     if ((path != null) && (path.length() > 0)) {
74       return path + "/" + aProperty;
75     } else {
76       return aProperty;
77     }
78   }
79
80   public ConfigNodeBuilder mimicSubNode(String aName,
81     String aLocationDescription) {
82     ConfigNodeBuilder result =
83       new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
84
85     return result;
86   }
87
88   // ConfigNodeBuilder methods:
89   public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
90     if (subNodes.containsKey(aName)) {
91       return (ConfigNodeBuilder) subNodes.get(aName);
92     } else {
93       ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
94       subNodes.put(aName, result);
95
96       return result;
97     }
98   }
99
100   public void addProperty(String aName, String aValue,
101     String anUnexpandedValue, String aLocationDescription) {
102     properties.put(aName,
103       new property(aValue, anUnexpandedValue, aLocationDescription,
104         makePropertyPath(aName)));
105   }
106
107   // ConfigNode helpers
108   public boolean hasProperty(String aPropertyName) {
109     return properties.containsKey(aPropertyName);
110   }
111
112   public property getProperty(String aPropertyName) {
113     return (property) properties.get(aPropertyName);
114   }
115
116   private property getRequiredProperty(String aPropertyName)
117     throws ConfigMissingPropertyException {
118     if (!hasProperty(aPropertyName)) {
119       throw new ConfigMissingPropertyException("required property \"" +
120         aPropertyName + "\" not found", getLocationDescription());
121     }
122
123     return getProperty(aPropertyName);
124   }
125
126   // ConfigNode methods:
127   public String getLocationDescription() {
128     return getPath() + " (" + locationDescription + ")";
129   }
130
131   public String getPath() {
132     return path;
133   }
134
135   public ConfigNode getSubNode(String aSubNodeName) {
136     if (subNodes.containsKey(aSubNodeName)) {
137       return (ConfigNode) subNodes.get(aSubNodeName);
138     } else {
139       return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
140     }
141   }
142
143   public Boolean getRequiredBooleanProperty(String aPropertyName)
144     throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
145     return getRequiredProperty(aPropertyName).interpretAsBoolean();
146   }
147
148   public Integer getRequiredIntegerProperty(String aPropertyName)
149     throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
150     return getRequiredProperty(aPropertyName).interpretAsInteger();
151   }
152
153   public String getRequiredStringProperty(String aPropertyName)
154     throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
155     return getRequiredProperty(aPropertyName).interpretAsString();
156   }
157
158   public Double getRequiredDoubleProperty(String aPropertyName)
159     throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
160     return getRequiredProperty(aPropertyName).interpretAsDouble();
161   }
162
163   public Boolean getOptionalBooleanProperty(String aPropertyName,
164     Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
165     if (!hasProperty(aPropertyName)) {
166       return aDefaultValue;
167     } else {
168       return getProperty(aPropertyName).interpretAsBoolean();
169     }
170   }
171
172   public Integer getOptionalIntegerProperty(String aPropertyName,
173     Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
174     if (!hasProperty(aPropertyName)) {
175       return aDefaultValue;
176     } else {
177       return getProperty(aPropertyName).interpretAsInteger();
178     }
179   }
180
181   public String getOptionalStringProperty(String aPropertyName,
182     String aDefaultValue) throws ConfigInvalidPropertyTypeException {
183     if (!hasProperty(aPropertyName)) {
184       return aDefaultValue;
185     } else {
186       return getProperty(aPropertyName).interpretAsString();
187     }
188   }
189
190   public Double getOptionalDoubleProperty(String aPropertyName,
191     Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
192     if (!hasProperty(aPropertyName)) {
193       return aDefaultValue;
194     } else {
195       return getProperty(aPropertyName).interpretAsDouble();
196     }
197   }
198
199   // property helper class
200   private class property {
201     private String value;
202     private String unexpandedValue;
203     private String path;
204     private String locationDescription;
205
206     public property(String aValue, String anUnexpandedValue,
207       String aLocationDescription, String aPath) {
208       value = aValue;
209       unexpandedValue = anUnexpandedValue;
210       locationDescription = aLocationDescription;
211       path = aPath;
212     }
213
214     public String getValue() {
215       return value;
216     }
217
218     public String getUnexpandedValue() {
219       return unexpandedValue;
220     }
221
222     public String getPath() {
223       return path;
224     }
225
226     public String getLocationDescription() {
227       return getPath() + " (" + locationDescription + ")";
228     }
229
230     public String getValueDescription() {
231       return "\"" + value + "\" (\"" + unexpandedValue + "\")";
232     }
233
234     public Boolean interpretAsBoolean()
235       throws ConfigInvalidPropertyTypeException {
236       if (value.equals("1")) {
237         return Boolean.TRUE;
238       } else if (value.equals("0")) {
239         return Boolean.FALSE;
240       } else {
241         throw new ConfigInvalidPropertyTypeException(getValueDescription() +
242           " is not a boolean", getLocationDescription());
243       }
244     }
245
246     public String interpretAsString() throws ConfigInvalidPropertyTypeException {
247       return value;
248     }
249
250     public Integer interpretAsInteger()
251       throws ConfigInvalidPropertyTypeException {
252       try {
253         return Integer.valueOf(value);
254       } catch (Throwable e) {
255         throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
256           unexpandedValue + "\") is not an integer", getLocationDescription());
257       }
258     }
259
260     public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
261       try {
262         return Double.valueOf(value);
263       } catch (Throwable e) {
264         throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
265           unexpandedValue + "\") is not a double", getLocationDescription());
266       }
267     }
268   }
269 }