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