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