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.
33 import mir.config.exceptions.ConfigInvalidPropertyTypeException;
34 import mir.config.exceptions.ConfigMissingPropertyException;
36 import java.util.HashMap;
40 public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {
41 private Map properties;
43 private String locationDescription;
46 public ConfigSimpleNode() {
50 public ConfigSimpleNode(String aLocationDescription) {
51 this("", aLocationDescription);
54 public ConfigSimpleNode(String aPath, String aLocationDescription) {
58 locationDescription = aLocationDescription;
59 properties = new HashMap();
60 subNodes = new HashMap();
63 // ConfigNodeBuilder helpers:
64 private String makeSubNodePath(String aSubNode) {
65 if ((path != null) && (path.length() > 0)) {
66 return path + "/" + aSubNode;
72 private String makePropertyPath(String aProperty) {
73 if ((path != null) && (path.length() > 0)) {
74 return path + "/" + aProperty;
80 public ConfigNodeBuilder mimicSubNode(String aName,
81 String aLocationDescription) {
82 ConfigNodeBuilder result =
83 new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
88 // ConfigNodeBuilder methods:
89 public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
90 if (subNodes.containsKey(aName)) {
91 return (ConfigNodeBuilder) subNodes.get(aName);
93 ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
94 subNodes.put(aName, result);
100 public void addProperty(String aName, String aValue,
101 String anUnexpandedValue, String aLocationDescription) {
102 properties.put(aName,
103 new property(aValue, anUnexpandedValue, aLocationDescription,
104 makePropertyPath(aName)));
107 // ConfigNode helpers
108 public boolean hasProperty(String aPropertyName) {
109 return properties.containsKey(aPropertyName);
112 public property getProperty(String aPropertyName) {
113 return (property) properties.get(aPropertyName);
116 private property getRequiredProperty(String aPropertyName)
117 throws ConfigMissingPropertyException {
118 if (!hasProperty(aPropertyName)) {
119 throw new ConfigMissingPropertyException("required property \"" +
120 aPropertyName + "\" not found", getLocationDescription());
123 return getProperty(aPropertyName);
126 // ConfigNode methods:
127 public String getLocationDescription() {
128 return getPath() + " (" + locationDescription + ")";
131 public String getPath() {
135 public ConfigNode getSubNode(String aSubNodeName) {
136 if (subNodes.containsKey(aSubNodeName)) {
137 return (ConfigNode) subNodes.get(aSubNodeName);
139 return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
143 public Boolean getRequiredBooleanProperty(String aPropertyName)
144 throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
145 return getRequiredProperty(aPropertyName).interpretAsBoolean();
148 public Integer getRequiredIntegerProperty(String aPropertyName)
149 throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
150 return getRequiredProperty(aPropertyName).interpretAsInteger();
153 public String getRequiredStringProperty(String aPropertyName)
154 throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
155 return getRequiredProperty(aPropertyName).interpretAsString();
158 public Double getRequiredDoubleProperty(String aPropertyName)
159 throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
160 return getRequiredProperty(aPropertyName).interpretAsDouble();
163 public Boolean getOptionalBooleanProperty(String aPropertyName,
164 Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
165 if (!hasProperty(aPropertyName)) {
166 return aDefaultValue;
168 return getProperty(aPropertyName).interpretAsBoolean();
172 public Integer getOptionalIntegerProperty(String aPropertyName,
173 Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
174 if (!hasProperty(aPropertyName)) {
175 return aDefaultValue;
177 return getProperty(aPropertyName).interpretAsInteger();
181 public String getOptionalStringProperty(String aPropertyName,
182 String aDefaultValue) throws ConfigInvalidPropertyTypeException {
183 if (!hasProperty(aPropertyName)) {
184 return aDefaultValue;
186 return getProperty(aPropertyName).interpretAsString();
190 public Double getOptionalDoubleProperty(String aPropertyName,
191 Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
192 if (!hasProperty(aPropertyName)) {
193 return aDefaultValue;
195 return getProperty(aPropertyName).interpretAsDouble();
199 // property helper class
200 private class property {
201 private String value;
202 private String unexpandedValue;
204 private String locationDescription;
206 public property(String aValue, String anUnexpandedValue,
207 String aLocationDescription, String aPath) {
209 unexpandedValue = anUnexpandedValue;
210 locationDescription = aLocationDescription;
214 public String getValue() {
218 public String getUnexpandedValue() {
219 return unexpandedValue;
222 public String getPath() {
226 public String getLocationDescription() {
227 return getPath() + " (" + locationDescription + ")";
230 public String getValueDescription() {
231 return "\"" + value + "\" (\"" + unexpandedValue + "\")";
234 public Boolean interpretAsBoolean()
235 throws ConfigInvalidPropertyTypeException {
236 if (value.equals("1")) {
238 } else if (value.equals("0")) {
239 return Boolean.FALSE;
241 throw new ConfigInvalidPropertyTypeException(getValueDescription() +
242 " is not a boolean", getLocationDescription());
246 public String interpretAsString() throws ConfigInvalidPropertyTypeException {
250 public Integer interpretAsInteger()
251 throws ConfigInvalidPropertyTypeException {
253 return Integer.valueOf(value);
254 } catch (Throwable e) {
255 throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
256 unexpandedValue + "\") is not an integer", getLocationDescription());
260 public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
262 return Double.valueOf(value);
263 } catch (Throwable e) {
264 throw new ConfigInvalidPropertyTypeException("\"" + value + "\" (\"" +
265 unexpandedValue + "\") is not a double", getLocationDescription());