2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
36 import mir.config.exceptions.*;
\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
44 public ConfigSimpleNode() {
\r
48 public ConfigSimpleNode(String aLocationDescription) {
\r
49 this("", aLocationDescription);
\r
52 public ConfigSimpleNode(String aPath, String aLocationDescription) {
\r
56 locationDescription = aLocationDescription;
\r
57 properties = new HashMap();
\r
58 subNodes = new HashMap();
\r
61 // ConfigNodeBuilder helpers:
\r
63 private String makeSubNodePath(String aSubNode) {
\r
64 if (path!=null && path.length()>0)
\r
65 return path+"/"+aSubNode;
\r
70 private String makePropertyPath(String aProperty) {
\r
71 if (path!=null && path.length()>0)
\r
72 return path+"/"+aProperty;
\r
77 public ConfigNodeBuilder mimicSubNode(String aName, String aLocationDescription) {
\r
78 ConfigNodeBuilder result = new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
\r
83 // ConfigNodeBuilder methods:
\r
85 public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
\r
86 if (subNodes.containsKey(aName)) {
\r
87 return (ConfigNodeBuilder) subNodes.get(aName);
\r
90 ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
\r
91 subNodes.put(aName, result);
\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
101 // ConfigNode helpers
\r
103 public boolean hasProperty(String aPropertyName) {
\r
104 return properties.containsKey(aPropertyName);
\r
107 public property getProperty(String aPropertyName) {
\r
108 return (property) properties.get(aPropertyName);
\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
116 return getProperty(aPropertyName);
\r
120 // ConfigNode methods:
\r
122 public String getLocationDescription() {
\r
123 return getPath()+" ("+locationDescription+")";
\r
126 public String getPath() {
\r
131 public ConfigNode getSubNode(String aSubNodeName) {
\r
132 if (subNodes.containsKey(aSubNodeName)) {
\r
133 return (ConfigNode) subNodes.get(aSubNodeName);
\r
137 return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
\r
141 public Boolean getRequiredBooleanProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
142 return getRequiredProperty(aPropertyName).interpretAsBoolean();
\r
145 public Integer getRequiredIntegerProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
146 return getRequiredProperty(aPropertyName).interpretAsInteger();
\r
149 public String getRequiredStringProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
150 return getRequiredProperty(aPropertyName).interpretAsString();
\r
153 public Double getRequiredDoubleProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
154 return getRequiredProperty(aPropertyName).interpretAsDouble();
\r
158 public Boolean getOptionalBooleanProperty(String aPropertyName, Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
159 if (!hasProperty(aPropertyName)) {
\r
160 return aDefaultValue;
\r
163 return getProperty(aPropertyName).interpretAsBoolean();
\r
167 public Integer getOptionalIntegerProperty(String aPropertyName, Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
168 if (!hasProperty(aPropertyName)) {
\r
169 return aDefaultValue;
\r
172 return getProperty(aPropertyName).interpretAsInteger();
\r
176 public String getOptionalStringProperty(String aPropertyName, String aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
177 if (!hasProperty(aPropertyName)) {
\r
178 return aDefaultValue;
\r
181 return getProperty(aPropertyName).interpretAsString();
\r
185 public Double getOptionalDoubleProperty(String aPropertyName, Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
186 if (!hasProperty(aPropertyName)) {
\r
187 return aDefaultValue;
\r
190 return getProperty(aPropertyName).interpretAsDouble();
\r
194 // property helper class
\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
202 public property( String aValue, String anUnexpandedValue, String aLocationDescription, String aPath ) {
\r
204 unexpandedValue = anUnexpandedValue;
\r
205 locationDescription = aLocationDescription;
\r
209 public String getValue() {
\r
213 public String getUnexpandedValue() {
\r
214 return unexpandedValue;
\r
217 public String getPath() {
\r
221 public String getLocationDescription() {
\r
222 return getPath()+" ("+locationDescription+")";
\r
225 public String getValueDescription() {
\r
226 return "\""+value+"\" (\""+unexpandedValue+"\")";
\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
235 throw new ConfigInvalidPropertyTypeException(getValueDescription() + " is not a boolean", getLocationDescription());
\r
238 public String interpretAsString() throws ConfigInvalidPropertyTypeException {
\r
242 public Integer interpretAsInteger() throws ConfigInvalidPropertyTypeException {
\r
244 return Integer.valueOf(value);
\r
246 catch (Throwable e) {
\r
247 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not an integer", getLocationDescription());
\r
251 public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
\r
253 return Double.valueOf(value);
\r
255 catch (Throwable e) {
\r
256 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not a double", getLocationDescription());
\r