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
14 private Node rootNode;
\r
16 public Node getRootNode() {
\r
20 public ConfigChecker() {
\r
23 rootNode = new Node();
\r
26 public void check(ConfigNode aNode) throws ConfigException {
\r
27 getRootNode().check(aNode);
\r
32 private Map subNodes;
\r
33 private Vector constraints;
\r
36 subNodes = new HashMap();
\r
37 constraints = new Vector();
\r
40 public Node getSubNode(String aName) {
\r
41 Node subNode = (Node) subNodes.get(aName);
\r
43 if (subNode==null) {
\r
44 subNode = new Node();
\r
45 subNodes.put(aName, subNode);
\r
51 public void addExistenceConstraint(String aPropertyName) {
\r
52 constraints.add(new ExistenceConstraint(aPropertyName));
\r
55 public void addTypeConstraint(String aPropertyName, int aType) {
\r
56 constraints.add(new TypeConstraint(aPropertyName, aType));
\r
59 public void addExistenceAndTypeConstraint(String aPropertyName, int aType) {
\r
60 addExistenceConstraint(aPropertyName);
\r
61 addTypeConstraint(aPropertyName, aType);
\r
64 public void check(ConfigNode aNode) throws ConfigException {
\r
67 iterator=constraints.iterator();
\r
68 while (iterator.hasNext()) {
\r
69 ((Constraint) iterator.next()).check(aNode);
\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
80 private class Constraint {
\r
81 protected String propertyName;
\r
83 Constraint(String aPropertyName) {
\r
84 propertyName=aPropertyName;
\r
87 public void check(ConfigNode aNode) throws ConfigException {
\r
91 private class ExistenceConstraint extends Constraint {
\r
92 ExistenceConstraint(String aPropertyName) {
\r
93 super(aPropertyName);
\r
96 public void check(ConfigNode aNode) throws ConfigException {
\r
97 aNode.getRequiredStringProperty(propertyName);
\r
101 private class TypeConstraint extends Constraint {
\r
104 TypeConstraint(String aPropertyName, int aType) {
\r
105 super(aPropertyName);
\r
110 public void check(ConfigNode aNode) throws ConfigException {
\r
113 aNode.getOptionalIntegerProperty(propertyName, new Integer(0));
\r
116 aNode.getOptionalStringProperty(propertyName, "");
\r
119 aNode.getOptionalDoubleProperty(propertyName, new Double(0.0));
\r
122 aNode.getOptionalBooleanProperty(propertyName, Boolean.FALSE);
\r
125 throw new ConfigException("Invalid value for type in type constraint: "+new Integer(type).toString());
\r