5 import mir.config.exceptions.*;
\r
7 public class ConfigChecker {
\r
8 public final static int STRING = 0;
\r
9 public final static int INTEGER = 1;
\r
10 public final static int BOOLEAN = 2;
\r
11 public final static int DOUBLE = 3;
\r
12 public final static int PATH = 4;
\r
13 // public final static int ABSOLUTEPATH = 5;
\r
14 // public final static int ABSOLUTEURL = 6;
\r
16 private Node rootNode;
\r
18 public Node getRootNode() {
\r
22 public ConfigChecker() {
\r
25 rootNode = new Node();
\r
28 public void check(ConfigNode aNode) throws ConfigFailure {
\r
29 getRootNode().check(aNode);
\r
34 private Map subNodes;
\r
35 private Vector constraints;
\r
38 subNodes = new HashMap();
\r
39 constraints = new Vector();
\r
42 public Node getSubNode(String aName) {
\r
43 Node subNode = (Node) subNodes.get(aName);
\r
45 if (subNode==null) {
\r
46 subNode = new Node();
\r
47 subNodes.put(aName, subNode);
\r
53 public void addExistenceConstraint(String aPropertyName) {
\r
54 constraints.add(new ExistenceConstraint(aPropertyName));
\r
57 public void addTypeConstraint(String aPropertyName, int aType) {
\r
58 constraints.add(new TypeConstraint(aPropertyName, aType));
\r
61 public void addExistenceAndTypeConstraint(String aPropertyName, int aType) {
\r
62 addExistenceConstraint(aPropertyName);
\r
63 addTypeConstraint(aPropertyName, aType);
\r
66 public void check(ConfigNode aNode) throws ConfigFailure {
\r
69 iterator=constraints.iterator();
\r
70 while (iterator.hasNext()) {
\r
71 ((Constraint) iterator.next()).check(aNode);
\r
74 iterator=subNodes.keySet().iterator();
\r
75 while (iterator.hasNext()) {
\r
76 Map.Entry entry = (Map.Entry) iterator.next();
\r
77 ((Node) entry.getValue()).check(aNode.getSubNode((String) entry.getKey()));
\r
82 private class Constraint {
\r
83 protected String propertyName;
\r
85 Constraint(String aPropertyName) {
\r
86 propertyName=aPropertyName;
\r
89 public void check(ConfigNode aNode) throws ConfigFailure {
\r
93 private class ExistenceConstraint extends Constraint {
\r
94 ExistenceConstraint(String aPropertyName) {
\r
95 super(aPropertyName);
\r
98 public void check(ConfigNode aNode) throws ConfigFailure {
\r
99 aNode.getRequiredStringProperty(propertyName);
\r
103 private class TypeConstraint extends Constraint {
\r
106 TypeConstraint(String aPropertyName, int aType) {
\r
107 super(aPropertyName);
\r
112 public void check(ConfigNode aNode) throws ConfigFailure {
\r
115 aNode.getOptionalIntegerProperty(propertyName, new Integer(0));
\r
118 aNode.getOptionalStringProperty(propertyName, "");
\r
121 aNode.getOptionalDoubleProperty(propertyName, new Double(0.0));
\r
124 aNode.getOptionalBooleanProperty(propertyName, Boolean.FALSE);
\r
127 throw new ConfigFailure("Invalid value for type in type constraint: "+new Integer(type).toString());
\r