* to your version of the file, but you are not obligated to do so. If you do
* not wish to do so, delete this exception statement from your version.
*/
+package mir.config;
+
+import mir.config.exceptions.ConfigInvalidPropertyTypeException;
+import mir.config.exceptions.ConfigMissingPropertyException;
-package mir.config;\r
-\r
import java.util.HashMap;
import java.util.Map;
-import mir.config.exceptions.ConfigInvalidPropertyTypeException;
-import mir.config.exceptions.ConfigMissingPropertyException;
-\r
-public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {\r
- private Map properties;\r
- private Map subNodes;\r
- private String locationDescription;\r
- private String path;\r
-\r
- public ConfigSimpleNode() {\r
- this("", "");\r
- }\r
-\r
- public ConfigSimpleNode(String aLocationDescription) {\r
- this("", aLocationDescription);\r
- }\r
-\r
- public ConfigSimpleNode(String aPath, String aLocationDescription) {\r
- super ();\r
-\r
- path = aPath;\r
- locationDescription = aLocationDescription;\r
- properties = new HashMap();\r
- subNodes = new HashMap();\r
- }\r
-\r
-// ConfigNodeBuilder helpers:\r
-\r
- private String makeSubNodePath(String aSubNode) {\r
- if (path!=null && path.length()>0)\r
- return path+"/"+aSubNode;\r
- else\r
- return aSubNode;\r
- }\r
-\r
- private String makePropertyPath(String aProperty) {\r
- if (path!=null && path.length()>0)\r
- return path+"/"+aProperty;\r
- else\r
- return aProperty;\r
- }\r
-\r
- public ConfigNodeBuilder mimicSubNode(String aName, String aLocationDescription) {\r
- ConfigNodeBuilder result = new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);\r
-\r
- return result;\r
- }\r
-\r
-// ConfigNodeBuilder methods:\r
-\r
- public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {\r
- if (subNodes.containsKey(aName)) {\r
- return (ConfigNodeBuilder) subNodes.get(aName);\r
- }\r
- else {\r
- ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);\r
- subNodes.put(aName, result);\r
-\r
- return result;\r
- }\r
- }\r
-\r
- public void addProperty(String aName, String aValue, String anUnexpandedValue, String aLocationDescription) {\r
- properties.put(aName, new property(aValue, anUnexpandedValue, aLocationDescription, makePropertyPath(aName)));\r
- }\r
-\r
-// ConfigNode helpers\r
-\r
- public boolean hasProperty(String aPropertyName) {\r
- return properties.containsKey(aPropertyName);\r
- }\r
-\r
- public property getProperty(String aPropertyName) {\r
- return (property) properties.get(aPropertyName);\r
- }\r
-\r
- private property getRequiredProperty(String aPropertyName) throws ConfigMissingPropertyException {\r
- if (!hasProperty(aPropertyName)) {\r
- throw new ConfigMissingPropertyException("required property \""+aPropertyName+"\" not found", getLocationDescription());\r
- }\r
-\r
- return getProperty(aPropertyName);\r
- }\r
-\r
-\r
-// ConfigNode methods:\r
-\r
- public String getLocationDescription() {\r
- return getPath()+" ("+locationDescription+")";\r
- };\r
-\r
- public String getPath() {\r
- return path;\r
- };\r
-\r
-\r
- public ConfigNode getSubNode(String aSubNodeName) {\r
- if (subNodes.containsKey(aSubNodeName)) {\r
- return (ConfigNode) subNodes.get(aSubNodeName);\r
- }\r
- else\r
- {\r
- return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);\r
- }\r
- }\r
-\r
- public Boolean getRequiredBooleanProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
- return getRequiredProperty(aPropertyName).interpretAsBoolean();\r
- }\r
-\r
- public Integer getRequiredIntegerProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
- return getRequiredProperty(aPropertyName).interpretAsInteger();\r
- }\r
-\r
- public String getRequiredStringProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
- return getRequiredProperty(aPropertyName).interpretAsString();\r
- }\r
-\r
- public Double getRequiredDoubleProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {\r
- return getRequiredProperty(aPropertyName).interpretAsDouble();\r
- }\r
-\r
-\r
- public Boolean getOptionalBooleanProperty(String aPropertyName, Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
- if (!hasProperty(aPropertyName)) {\r
- return aDefaultValue;\r
- }\r
- else {\r
- return getProperty(aPropertyName).interpretAsBoolean();\r
- }\r
- }\r
-\r
- public Integer getOptionalIntegerProperty(String aPropertyName, Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
- if (!hasProperty(aPropertyName)) {\r
- return aDefaultValue;\r
- }\r
- else {\r
- return getProperty(aPropertyName).interpretAsInteger();\r
- }\r
- }\r
-\r
- public String getOptionalStringProperty(String aPropertyName, String aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
- if (!hasProperty(aPropertyName)) {\r
- return aDefaultValue;\r
- }\r
- else {\r
- return getProperty(aPropertyName).interpretAsString();\r
- }\r
- }\r
-\r
- public Double getOptionalDoubleProperty(String aPropertyName, Double aDefaultValue) throws ConfigInvalidPropertyTypeException {\r
- if (!hasProperty(aPropertyName)) {\r
- return aDefaultValue;\r
- }\r
- else {\r
- return getProperty(aPropertyName).interpretAsDouble();\r
- }\r
- }\r
-\r
-// property helper class\r
-\r
- private class property {\r
- private String value;\r
- private String unexpandedValue;\r
- private String path;\r
- private String locationDescription;\r
-\r
- public property( String aValue, String anUnexpandedValue, String aLocationDescription, String aPath ) {\r
- value = aValue;\r
- unexpandedValue = anUnexpandedValue;\r
- locationDescription = aLocationDescription;\r
- path = aPath;\r
- }\r
-\r
- public String getValue() {\r
- return value;\r
- }\r
-\r
- public String getUnexpandedValue() {\r
- return unexpandedValue;\r
- }\r
-\r
- public String getPath() {\r
- return path;\r
- }\r
-\r
- public String getLocationDescription() {\r
- return getPath()+" ("+locationDescription+")";\r
- }\r
-\r
- public String getValueDescription() {\r
- return "\""+value+"\" (\""+unexpandedValue+"\")";\r
- }\r
-\r
- public Boolean interpretAsBoolean() throws ConfigInvalidPropertyTypeException {\r
- if (value.equals("1"))\r
- return Boolean.TRUE;\r
- else if (value.equals("0"))\r
- return Boolean.FALSE;\r
- else\r
- throw new ConfigInvalidPropertyTypeException(getValueDescription() + " is not a boolean", getLocationDescription());\r
- }\r
-\r
- public String interpretAsString() throws ConfigInvalidPropertyTypeException {\r
- return value;\r
- }\r
-\r
- public Integer interpretAsInteger() throws ConfigInvalidPropertyTypeException {\r
- try {\r
- return Integer.valueOf(value);\r
- }\r
- catch (Throwable e) {\r
- throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not an integer", getLocationDescription());\r
- }\r
- }\r
-\r
- public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {\r
- try {\r
- return Double.valueOf(value);\r
- }\r
- catch (Throwable e) {\r
- throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not a double", getLocationDescription());\r
- }\r
- }\r
- }\r
-}\r
-\r
-\r
+
+public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {
+ private Map properties;
+ private Map subNodes;
+ private String locationDescription;
+ private String path;
+
+ public ConfigSimpleNode() {
+ this("", "");
+ }
+
+ public ConfigSimpleNode(String aLocationDescription) {
+ this("", aLocationDescription);
+ }
+
+ public ConfigSimpleNode(String aPath, String aLocationDescription) {
+ super();
+
+ path = aPath;
+ locationDescription = aLocationDescription;
+ properties = new HashMap();
+ subNodes = new HashMap();
+ }
+
+ // ConfigNodeBuilder helpers:
+ private String makeSubNodePath(String aSubNode) {
+ if ((path != null) && (path.length() > 0)) {
+ return path + "/" + aSubNode;
+ } else {
+ return aSubNode;
+ }
+ }
+
+ private String makePropertyPath(String aProperty) {
+ if ((path != null) && (path.length() > 0)) {
+ return path + "/" + aProperty;
+ } else {
+ return aProperty;
+ }
+ }
+
+ public ConfigNodeBuilder mimicSubNode(String aName,
+ String aLocationDescription) {
+ ConfigNodeBuilder result =
+ new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
+
+ return result;
+ }
+
+ // ConfigNodeBuilder methods:
+ public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
+ if (subNodes.containsKey(aName)) {
+ return (ConfigNodeBuilder) subNodes.get(aName);
+ } else {
+ ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
+ subNodes.put(aName, result);
+
+ return result;
+ }
+ }
+
+ public void addProperty(String aName, String aValue,
+ String anUnexpandedValue, String aLocationDescription) {
+ properties.put(aName,
+ new property(aValue, anUnexpandedValue, aLocationDescription,
+ makePropertyPath(aName)));
+ }
+
+ // ConfigNode helpers
+ public boolean hasProperty(String aPropertyName) {
+ return properties.containsKey(aPropertyName);
+ }
+
+ public property getProperty(String aPropertyName) {
+ return (property) properties.get(aPropertyName);
+ }
+
+ private property getRequiredProperty(String aPropertyName)
+ throws ConfigMissingPropertyException {
+ if (!hasProperty(aPropertyName)) {
+ throw new ConfigMissingPropertyException("required property \"" +
+ aPropertyName + "\" not found", getLocationDescription());
+ }
+
+ return getProperty(aPropertyName);
+ }
+
+ // ConfigNode methods:
+ public String getLocationDescription() {
+ return getPath() + " (" + locationDescription + ")";
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public ConfigNode getSubNode(String aSubNodeName) {
+ if (subNodes.containsKey(aSubNodeName)) {
+ return (ConfigNode) subNodes.get(aSubNodeName);
+ } else {
+ return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
+ }
+ }
+
+ public Boolean getRequiredBooleanProperty(String aPropertyName)
+ throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
+ return getRequiredProperty(aPropertyName).interpretAsBoolean();
+ }
+
+ public Integer getRequiredIntegerProperty(String aPropertyName)
+ throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
+ return getRequiredProperty(aPropertyName).interpretAsInteger();
+ }
+
+ public String getRequiredStringProperty(String aPropertyName)
+ throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
+ return getRequiredProperty(aPropertyName).interpretAsString();
+ }
+
+ public Double getRequiredDoubleProperty(String aPropertyName)
+ throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
+ return getRequiredProperty(aPropertyName).interpretAsDouble();
+ }
+
+ public Boolean getOptionalBooleanProperty(String aPropertyName,
+ Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
+ if (!hasProperty(aPropertyName)) {
+ return aDefaultValue;
+ } else {
+ return getProperty(aPropertyName).interpretAsBoolean();
+ }
+ }
+
+ public Integer getOptionalIntegerProperty(String aPropertyName,
+ Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
+ if (!hasProperty(aPropertyName)) {
+ return aDefaultValue;
+ } else {
+ return getProperty(aPropertyName).interpretAsInteger();
+ }
+ }
+
+ public String getOptionalStringProperty(String aPropertyName,
+ String aDefaultValue) throws ConfigInvalidPropertyTypeException {
+ if (!hasProperty(aPropertyName)) {
+ return aDefaultValue;
+ } else {
+ return getProperty(aPropertyName).interpretAsString();
+ }
+ }
+
+ public Double getOptionalDoubleProperty(String aPropertyName,
+ Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
+ if (!hasProperty(aPropertyName)) {
+ return aDefaultValue;
+ } else {
+ return getProperty(aPropertyName).interpretAsDouble();
+ }
+ }
+
+ // property helper class
+ private class property {
+ private String value;
+ private String unexpandedValue;
+ private String path;
+ private String locationDescription;
+
+ public property(String aValue, String anUnexpandedValue,
+ String aLocationDescription, String aPath) {
+ value = aValue;
+ unexpandedValue = anUnexpandedValue;
+ locationDescription = aLocationDescription;
+ path = aPath;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public String getUnexpandedValue() {
+ return unexpandedValue;
+ }
+
+ public String getPath() {
+ return path;
+ }
+
+ public String getLocationDescription() {
+ return getPath() + " (" + locationDescription + ")";
+ }
+
+ public String getValueDescription() {
+ return "\"" + value + "\" (\"" + unexpandedValue + "\")";
+ }
+
+ public Boolean interpretAsBoolean()
+ throws ConfigInvalidPropertyTypeException {
+ if (value.equals("1")) {
+ return Boolean.TRUE;
+ } else if (value.equals("0")) {
+ return Boolean.FALSE;
+ } else {
+ throw new ConfigInvalidPropertyTypeException(getValueDescription() +
+ " is not a boolean", getLocationDescription());
+ }
+ }
+
+ public String interpretAsString() throws ConfigInvalidPropertyTypeException {
+ return value;
+ }
+
+ public Integer interpretAsInteger()
+ throws ConfigInvalidPropertyTypeException {
+ try {
+ return Integer.valueOf(value);
+ } catch (Throwable e) {
+ throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
+ unexpandedValue + "\") is not an integer", getLocationDescription());
+ }
+ }
+
+ public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
+ try {
+ return Double.valueOf(value);
+ } catch (Throwable e) {
+ throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
+ unexpandedValue + "\") is not a double", getLocationDescription());
+ }
+ }
+ }
+}