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