the new config-class based on apache.commons.config
[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
32 package  mir.config;\r
33 \r
34 import java.util.HashMap;
35 import java.util.Map;
36
37 import mir.config.exceptions.ConfigInvalidPropertyTypeException;
38 import mir.config.exceptions.ConfigMissingPropertyException;
39 \r
40 public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {\r
41   private Map properties;\r
42   private Map subNodes;\r
43   private String locationDescription;\r
44   private String path;\r
45 \r
46   public ConfigSimpleNode() {\r
47     this("", "");\r
48   }\r
49 \r
50   public ConfigSimpleNode(String aLocationDescription) {\r
51     this("", aLocationDescription);\r
52   }\r
53 \r
54   public ConfigSimpleNode(String aPath, String aLocationDescription) {\r
55     super ();\r
56 \r
57     path = aPath;\r
58     locationDescription = aLocationDescription;\r
59     properties = new HashMap();\r
60     subNodes = new HashMap();\r
61   }\r
62 \r
63 // ConfigNodeBuilder helpers:\r
64 \r
65   private String makeSubNodePath(String aSubNode) {\r
66     if (path!=null && path.length()>0)\r
67       return path+"/"+aSubNode;\r
68     else\r
69       return aSubNode;\r
70   }\r
71 \r
72   private String makePropertyPath(String aProperty) {\r
73     if (path!=null && path.length()>0)\r
74       return path+"/"+aProperty;\r
75     else\r
76       return aProperty;\r
77   }\r
78 \r
79   public ConfigNodeBuilder mimicSubNode(String aName, String aLocationDescription) {\r
80     ConfigNodeBuilder result = new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);\r
81 \r
82     return result;\r
83   }\r
84 \r
85 // ConfigNodeBuilder methods:\r
86 \r
87   public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {\r
88     if (subNodes.containsKey(aName)) {\r
89       return (ConfigNodeBuilder) subNodes.get(aName);\r
90     }\r
91     else {\r
92       ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);\r
93       subNodes.put(aName, result);\r
94 \r
95       return result;\r
96     }\r
97   }\r
98 \r
99   public void addProperty(String aName, String aValue, String anUnexpandedValue, String aLocationDescription) {\r
100     properties.put(aName, new property(aValue, anUnexpandedValue, aLocationDescription, makePropertyPath(aName)));\r
101   }\r
102 \r
103 // ConfigNode helpers\r
104 \r
105   public boolean hasProperty(String aPropertyName) {\r
106     return properties.containsKey(aPropertyName);\r
107   }\r
108 \r
109   public property getProperty(String aPropertyName) {\r
110     return (property) properties.get(aPropertyName);\r
111   }\r
112 \r
113   private property getRequiredProperty(String aPropertyName) throws ConfigMissingPropertyException {\r
114     if (!hasProperty(aPropertyName)) {\r
115       throw new ConfigMissingPropertyException("required property \""+aPropertyName+"\" not found", getLocationDescription());\r
116     }\r
117 \r
118     return getProperty(aPropertyName);\r
119   }\r
120 \r
121 \r
122 // ConfigNode methods:\r
123 \r
124   public String getLocationDescription() {\r
125     return getPath()+" ("+locationDescription+")";\r
126   };\r
127 \r
128   public String getPath() {\r
129     return path;\r
130   };\r
131 \r
132 \r
133   public ConfigNode getSubNode(String aSubNodeName) {\r
134     if (subNodes.containsKey(aSubNodeName)) {\r
135       return (ConfigNode) subNodes.get(aSubNodeName);\r
136     }\r
137     else\r
138     {\r
139       return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);\r
140     }\r
141   }\r
142 \r
143   public Boolean getRequiredBooleanProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
144     return getRequiredProperty(aPropertyName).interpretAsBoolean();\r
145   }\r
146 \r
147   public Integer getRequiredIntegerProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
148     return getRequiredProperty(aPropertyName).interpretAsInteger();\r
149   }\r
150 \r
151   public String getRequiredStringProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
152     return getRequiredProperty(aPropertyName).interpretAsString();\r
153   }\r
154 \r
155   public Double getRequiredDoubleProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
156     return getRequiredProperty(aPropertyName).interpretAsDouble();\r
157   }\r
158 \r
159 \r
160   public Boolean getOptionalBooleanProperty(String aPropertyName, Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
161     if (!hasProperty(aPropertyName)) {\r
162       return aDefaultValue;\r
163     }\r
164     else {\r
165       return getProperty(aPropertyName).interpretAsBoolean();\r
166     }\r
167   }\r
168 \r
169   public Integer getOptionalIntegerProperty(String aPropertyName, Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
170     if (!hasProperty(aPropertyName)) {\r
171       return aDefaultValue;\r
172     }\r
173     else {\r
174       return getProperty(aPropertyName).interpretAsInteger();\r
175     }\r
176   }\r
177 \r
178   public String getOptionalStringProperty(String aPropertyName, String aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
179     if (!hasProperty(aPropertyName)) {\r
180       return aDefaultValue;\r
181     }\r
182     else {\r
183       return getProperty(aPropertyName).interpretAsString();\r
184     }\r
185   }\r
186 \r
187   public Double getOptionalDoubleProperty(String aPropertyName, Double aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
188     if (!hasProperty(aPropertyName)) {\r
189       return aDefaultValue;\r
190     }\r
191     else {\r
192       return getProperty(aPropertyName).interpretAsDouble();\r
193     }\r
194   }\r
195 \r
196 // property helper class\r
197 \r
198   private class property {\r
199     private String value;\r
200     private String unexpandedValue;\r
201     private String path;\r
202     private String locationDescription;\r
203 \r
204     public property( String aValue, String anUnexpandedValue, String aLocationDescription, String aPath ) {\r
205       value = aValue;\r
206       unexpandedValue = anUnexpandedValue;\r
207       locationDescription = aLocationDescription;\r
208       path = aPath;\r
209     }\r
210 \r
211     public String getValue() {\r
212       return value;\r
213     }\r
214 \r
215     public String getUnexpandedValue() {\r
216       return unexpandedValue;\r
217     }\r
218 \r
219     public String getPath() {\r
220       return path;\r
221     }\r
222 \r
223     public String getLocationDescription() {\r
224       return getPath()+" ("+locationDescription+")";\r
225     }\r
226 \r
227     public String getValueDescription() {\r
228       return "\""+value+"\" (\""+unexpandedValue+"\")";\r
229     }\r
230 \r
231     public Boolean interpretAsBoolean() throws ConfigInvalidPropertyTypeException {\r
232       if (value.equals("1"))\r
233         return Boolean.TRUE;\r
234       else if (value.equals("0"))\r
235         return Boolean.FALSE;\r
236       else\r
237         throw new ConfigInvalidPropertyTypeException(getValueDescription() + " is not a boolean", getLocationDescription());\r
238     }\r
239 \r
240     public String interpretAsString() throws ConfigInvalidPropertyTypeException {\r
241       return value;\r
242     }\r
243 \r
244     public Integer interpretAsInteger() throws ConfigInvalidPropertyTypeException {\r
245       try {\r
246         return Integer.valueOf(value);\r
247       }\r
248       catch (Throwable e) {\r
249         throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not an integer", getLocationDescription());\r
250       }\r
251     }\r
252 \r
253     public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {\r
254       try {\r
255         return Double.valueOf(value);\r
256       }\r
257       catch (Throwable e) {\r
258         throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not a double", getLocationDescription());\r
259       }\r
260     }\r
261   }\r
262 }\r
263 \r
264 \r