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.
34 import java.util.HashMap;
37 import mir.config.exceptions.ConfigInvalidPropertyTypeException;
38 import mir.config.exceptions.ConfigMissingPropertyException;
40 public class ConfigSimpleNode implements ConfigNode, ConfigNodeBuilder {
\r
41 private Map properties;
\r
42 private Map subNodes;
\r
43 private String locationDescription;
\r
44 private String path;
\r
46 public ConfigSimpleNode() {
\r
50 public ConfigSimpleNode(String aLocationDescription) {
\r
51 this("", aLocationDescription);
\r
54 public ConfigSimpleNode(String aPath, String aLocationDescription) {
\r
58 locationDescription = aLocationDescription;
\r
59 properties = new HashMap();
\r
60 subNodes = new HashMap();
\r
63 // ConfigNodeBuilder helpers:
\r
65 private String makeSubNodePath(String aSubNode) {
\r
66 if (path!=null && path.length()>0)
\r
67 return path+"/"+aSubNode;
\r
72 private String makePropertyPath(String aProperty) {
\r
73 if (path!=null && path.length()>0)
\r
74 return path+"/"+aProperty;
\r
79 public ConfigNodeBuilder mimicSubNode(String aName, String aLocationDescription) {
\r
80 ConfigNodeBuilder result = new ConfigSimpleNode(makeSubNodePath(aName), aLocationDescription);
\r
85 // ConfigNodeBuilder methods:
\r
87 public ConfigNodeBuilder makeSubNode(String aName, String aLocationDescription) {
\r
88 if (subNodes.containsKey(aName)) {
\r
89 return (ConfigNodeBuilder) subNodes.get(aName);
\r
92 ConfigNodeBuilder result = mimicSubNode(aName, aLocationDescription);
\r
93 subNodes.put(aName, result);
\r
99 public void addProperty(String aName, String aValue, String anUnexpandedValue, String aLocationDescription) {
\r
100 properties.put(aName, new property(aValue, anUnexpandedValue, aLocationDescription, makePropertyPath(aName)));
\r
103 // ConfigNode helpers
\r
105 public boolean hasProperty(String aPropertyName) {
\r
106 return properties.containsKey(aPropertyName);
\r
109 public property getProperty(String aPropertyName) {
\r
110 return (property) properties.get(aPropertyName);
\r
113 private property getRequiredProperty(String aPropertyName) throws ConfigMissingPropertyException {
\r
114 if (!hasProperty(aPropertyName)) {
\r
115 throw new ConfigMissingPropertyException("required property \""+aPropertyName+"\" not found", getLocationDescription());
\r
118 return getProperty(aPropertyName);
\r
122 // ConfigNode methods:
\r
124 public String getLocationDescription() {
\r
125 return getPath()+" ("+locationDescription+")";
\r
128 public String getPath() {
\r
133 public ConfigNode getSubNode(String aSubNodeName) {
\r
134 if (subNodes.containsKey(aSubNodeName)) {
\r
135 return (ConfigNode) subNodes.get(aSubNodeName);
\r
139 return (ConfigNode) mimicSubNode(aSubNodeName, locationDescription);
\r
143 public Boolean getRequiredBooleanProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
144 return getRequiredProperty(aPropertyName).interpretAsBoolean();
\r
147 public Integer getRequiredIntegerProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
148 return getRequiredProperty(aPropertyName).interpretAsInteger();
\r
151 public String getRequiredStringProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
152 return getRequiredProperty(aPropertyName).interpretAsString();
\r
155 public Double getRequiredDoubleProperty(String aPropertyName) throws ConfigMissingPropertyException, ConfigInvalidPropertyTypeException {
\r
156 return getRequiredProperty(aPropertyName).interpretAsDouble();
\r
160 public Boolean getOptionalBooleanProperty(String aPropertyName, Boolean aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
161 if (!hasProperty(aPropertyName)) {
\r
162 return aDefaultValue;
\r
165 return getProperty(aPropertyName).interpretAsBoolean();
\r
169 public Integer getOptionalIntegerProperty(String aPropertyName, Integer aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
170 if (!hasProperty(aPropertyName)) {
\r
171 return aDefaultValue;
\r
174 return getProperty(aPropertyName).interpretAsInteger();
\r
178 public String getOptionalStringProperty(String aPropertyName, String aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
179 if (!hasProperty(aPropertyName)) {
\r
180 return aDefaultValue;
\r
183 return getProperty(aPropertyName).interpretAsString();
\r
187 public Double getOptionalDoubleProperty(String aPropertyName, Double aDefaultValue) throws ConfigInvalidPropertyTypeException {
\r
188 if (!hasProperty(aPropertyName)) {
\r
189 return aDefaultValue;
\r
192 return getProperty(aPropertyName).interpretAsDouble();
\r
196 // property helper class
\r
198 private class property {
\r
199 private String value;
\r
200 private String unexpandedValue;
\r
201 private String path;
\r
202 private String locationDescription;
\r
204 public property( String aValue, String anUnexpandedValue, String aLocationDescription, String aPath ) {
\r
206 unexpandedValue = anUnexpandedValue;
\r
207 locationDescription = aLocationDescription;
\r
211 public String getValue() {
\r
215 public String getUnexpandedValue() {
\r
216 return unexpandedValue;
\r
219 public String getPath() {
\r
223 public String getLocationDescription() {
\r
224 return getPath()+" ("+locationDescription+")";
\r
227 public String getValueDescription() {
\r
228 return "\""+value+"\" (\""+unexpandedValue+"\")";
\r
231 public Boolean interpretAsBoolean() throws ConfigInvalidPropertyTypeException {
\r
232 if (value.equals("1"))
\r
233 return Boolean.TRUE;
\r
234 else if (value.equals("0"))
\r
235 return Boolean.FALSE;
\r
237 throw new ConfigInvalidPropertyTypeException(getValueDescription() + " is not a boolean", getLocationDescription());
\r
240 public String interpretAsString() throws ConfigInvalidPropertyTypeException {
\r
244 public Integer interpretAsInteger() throws ConfigInvalidPropertyTypeException {
\r
246 return Integer.valueOf(value);
\r
248 catch (Throwable e) {
\r
249 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not an integer", getLocationDescription());
\r
253 public Double interpretAsDouble() throws ConfigInvalidPropertyTypeException {
\r
255 return Double.valueOf(value);
\r
257 catch (Throwable e) {
\r
258 throw new ConfigInvalidPropertyTypeException("\""+value+"\" (\""+unexpandedValue+"\") is not a double", getLocationDescription());
\r