initial checkin of the new xml-style configuration classes
[mir.git] / source / mir / config / ConfigChecker.java
1 package  mir.config;\r
2 \r
3 import java.util.*;\r
4 \r
5 public class ConfigChecker {\r
6   public final static int STRING = 0;\r
7   public final static int INTEGER = 1;\r
8   public final static int BOOLEAN = 2;\r
9   public final static int DOUBLE = 3;\r
10   public final static int PATH = 4;\r
11 //  public final static int ABSOLUTEPATH = 5;\r
12 //  public final static int ABSOLUTEURL = 6;\r
13 \r
14   private Node rootNode;\r
15 \r
16   public Node getRootNode() {\r
17     return rootNode;\r
18   }\r
19 \r
20   public ConfigChecker() {\r
21     super();\r
22 \r
23     rootNode = new Node();\r
24   }\r
25 \r
26   public void check(ConfigNode aNode) throws ConfigException {\r
27     getRootNode().check(aNode);\r
28   }\r
29 \r
30   public class Node {\r
31 \r
32     private Map subNodes;\r
33     private Vector constraints;\r
34 \r
35     public Node() {\r
36       subNodes = new HashMap();\r
37       constraints = new Vector();\r
38     }\r
39 \r
40     public Node getSubNode(String aName) {\r
41       Node subNode = (Node) subNodes.get(aName);\r
42 \r
43       if (subNode==null) {\r
44         subNode = new Node();\r
45         subNodes.put(aName, subNode);\r
46       }\r
47 \r
48       return subNode;\r
49     }\r
50 \r
51     public void addExistenceConstraint(String aPropertyName) {\r
52       constraints.add(new ExistenceConstraint(aPropertyName));\r
53     }\r
54 \r
55     public void addTypeConstraint(String aPropertyName, int aType) {\r
56       constraints.add(new TypeConstraint(aPropertyName, aType));\r
57     }\r
58 \r
59     public void addExistenceAndTypeConstraint(String aPropertyName, int aType) {\r
60       addExistenceConstraint(aPropertyName);\r
61       addTypeConstraint(aPropertyName, aType);\r
62     }\r
63 \r
64     public void check(ConfigNode aNode) throws ConfigException {\r
65       Iterator iterator;\r
66 \r
67       iterator=constraints.iterator();\r
68       while (iterator.hasNext()) {\r
69         ((Constraint) iterator.next()).check(aNode);\r
70       }\r
71 \r
72       iterator=subNodes.keySet().iterator();\r
73       while (iterator.hasNext()) {\r
74         Map.Entry entry = (Map.Entry) iterator.next();\r
75         ((Node) entry.getValue()).check(aNode.getSubNode((String) entry.getKey()));\r
76       }\r
77 \r
78     }\r
79 \r
80     private class Constraint {\r
81       protected String propertyName;\r
82 \r
83       Constraint(String aPropertyName) {\r
84         propertyName=aPropertyName;\r
85       }\r
86 \r
87       public void check(ConfigNode aNode) throws ConfigException {\r
88       };\r
89     }\r
90 \r
91     private class ExistenceConstraint extends Constraint {\r
92       ExistenceConstraint(String aPropertyName) {\r
93         super(aPropertyName);\r
94       }\r
95 \r
96       public void check(ConfigNode aNode) throws ConfigException {\r
97         aNode.getRequiredStringProperty(propertyName);\r
98       };\r
99     }\r
100 \r
101     private class TypeConstraint extends Constraint {\r
102       private int type;\r
103 \r
104       TypeConstraint(String aPropertyName, int aType) {\r
105         super(aPropertyName);\r
106 \r
107         type=aType;\r
108       }\r
109 \r
110       public void check(ConfigNode aNode) throws ConfigException {\r
111         switch(type) {\r
112           case INTEGER:\r
113             aNode.getOptionalIntegerProperty(propertyName, new Integer(0));\r
114             break;\r
115           case STRING:\r
116             aNode.getOptionalStringProperty(propertyName, "");\r
117             break;\r
118           case DOUBLE:\r
119             aNode.getOptionalDoubleProperty(propertyName, new Double(0.0));\r
120             break;\r
121           case BOOLEAN:\r
122             aNode.getOptionalBooleanProperty(propertyName, Boolean.FALSE);\r
123             break;\r
124           default:\r
125             throw new ConfigException("Invalid value for type in type constraint: "+new Integer(type).toString());\r
126         }\r
127       }\r
128     }\r
129   }\r
130 }\r