X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=source%2Fmir%2Fconfig%2FConfigSimpleNode.java;fp=source%2Fmir%2Fconfig%2FConfigSimpleNode.java;h=7d0c185005244bdb3bb7b9fcb3a3d2dc6c6c4eac;hb=f567b9f58915e30a57245ac564636379a5cc26ac;hp=70f20adf7937ab02c85cfb3bb26874053a4bdf71;hpb=bde722e252c5d04d3898849fe0df85f9e7a9ab8d;p=mir.git diff --git a/source/mir/config/ConfigSimpleNode.java b/source/mir/config/ConfigSimpleNode.java index 70f20adf..7d0c1850 100755 --- a/source/mir/config/ConfigSimpleNode.java +++ b/source/mir/config/ConfigSimpleNode.java @@ -28,237 +28,242 @@ * 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; - import java.util.HashMap; import java.util.Map; -import mir.config.exceptions.ConfigInvalidPropertyTypeException; -import mir.config.exceptions.ConfigMissingPropertyException; - -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()); - } - } - } -} - - + +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()); + } + } + } +}