5 import mir.config.exceptions.*;
\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
13 public ConfigSimpleNode() {
\r
17 public ConfigSimpleNode(String aLocationDescription) {
\r
18 this("", aLocationDescription);
\r
21 public ConfigSimpleNode(String aPath, String aLocationDescription) {
\r
25 locationDescription = aLocationDescription;
\r
26 properties = new HashMap();
\r
27 subNodes = new HashMap();
\r
30 // ConfigNodeBuilder helpers:
\r
32 private String makeSubNodePath(String aSubNode) {
\r
33 if (path!=null && path.length()>0)
\r
34 return path+"/"+aSubNode;
\r
39 private String makePropertyPath(String aProperty) {
\r
40 if (path!=null && path.length()>0)
\r
41 return path+"/"+aProperty;
\r
46 public ConfigNodeBuilder mimicSubNode(String aName, String aLocationDescription) {
\r
47 ConfigNodeBuilder result = new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
\r
52 // ConfigNodeBuilder methods:
\r
54 public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
\r
55 if (subNodes.containsKey(aName)) {
\r
56 return (ConfigNodeBuilder) subNodes.get(aName);
\r
59 ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
\r
60 subNodes.put(aName, result);
\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
70 // ConfigNode helpers
\r
72 public boolean hasProperty(String aPropertyName) {
\r
73 return properties.containsKey(aPropertyName);
\r
76 public property getProperty(String aPropertyName) {
\r
77 return (property) properties.get(aPropertyName);
\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
85 return getProperty(aPropertyName);
\r
89 // ConfigNode methods:
\r
91 public String getLocationDescription() {
\r
92 return getPath()+" ("+locationDescription+")";
\r
95 public String getPath() {
\r
100 public ConfigNode getSubNode(String aSubNodeName) {
\r
101 if (subNodes.containsKey(aSubNodeName)) {
\r
102 return (ConfigNode) subNodes.get(aSubNodeName);
\r
106 return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
\r
110 public Boolean getRequiredBooleanProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
111 return getRequiredProperty(aPropertyName).interpretAsBoolean();
\r
114 public Integer getRequiredIntegerProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
115 return getRequiredProperty(aPropertyName).interpretAsInteger();
\r
118 public String getRequiredStringProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
119 return getRequiredProperty(aPropertyName).interpretAsString();
\r
122 public Double getRequiredDoubleProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
123 return getRequiredProperty(aPropertyName).interpretAsDouble();
\r
127 public Boolean getOptionalBooleanProperty(String aPropertyName, Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
128 if (!hasProperty(aPropertyName)) {
\r
129 return aDefaultValue;
\r
132 return getProperty(aPropertyName).interpretAsBoolean();
\r
136 public Integer getOptionalIntegerProperty(String aPropertyName, Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
137 if (!hasProperty(aPropertyName)) {
\r
138 return aDefaultValue;
\r
141 return getProperty(aPropertyName).interpretAsInteger();
\r
145 public String getOptionalStringProperty(String aPropertyName, String aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
146 if (!hasProperty(aPropertyName)) {
\r
147 return aDefaultValue;
\r
150 return getProperty(aPropertyName).interpretAsString();
\r
154 public Double getOptionalDoubleProperty(String aPropertyName, Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
155 if (!hasProperty(aPropertyName)) {
\r
156 return aDefaultValue;
\r
159 return getProperty(aPropertyName).interpretAsDouble();
\r
163 // property helper class
\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
171 public property( String aValue, String anUnexpandedValue, String aLocationDescription, String aPath ) {
\r
173 unexpandedValue = anUnexpandedValue;
\r
174 locationDescription = aLocationDescription;
\r
178 public String getValue() {
\r
182 public String getUnexpandedValue() {
\r
183 return unexpandedValue;
\r
186 public String getPath() {
\r
190 public String getLocationDescription() {
\r
191 return getPath()+" ("+locationDescription+")";
\r
194 public String getValueDescription() {
\r
195 return "\""+value+"\" (\""+unexpandedValue+"\")";
\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
204 throw new ConfigInvalidPropertyTypeException(getValueDescription() + " is not a boolean", getLocationDescription());
\r
207 public String interpretAsString() throws ConfigInvalidPropertyTypeException {
\r
211 public Integer interpretAsInteger() throws ConfigInvalidPropertyTypeException {
\r
213 return Integer.valueOf(value);
\r
215 catch (Throwable e) {
\r
216 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not an integer", getLocationDescription());
\r
220 public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
\r
222 return Double.valueOf(value);
\r
224 catch (Throwable e) {
\r
225 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not a double", getLocationDescription());
\r