Source code: com/aendvari/griffin/validation/Validation.java
1 /*
2 * Validation.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 */
7
8 package com.aendvari.griffin.validation;
9
10 import java.io.*;
11 import java.util.*;
12 import java.beans.*;
13
14 import java.lang.reflect.*;
15 import java.lang.NoSuchMethodException;
16 import java.lang.IllegalAccessException;
17
18 import com.aendvari.common.util.MultiHashMap;
19 import com.aendvari.common.model.*;
20
21 import com.aendvari.griffin.validation.dataset.*;
22 import com.aendvari.griffin.validation.validator.*;
23 import com.aendvari.griffin.validation.validators.simple.*;
24
25
26 /**
27 * <p>
28 * Constructs a {@link Validation} for validating a
29 * {@link com.aendvari.griffin.validation.dataset.Dataset}.
30 * </p>
31 *
32 * <p>
33 * Constructs a {@link java.util.Map#get(Object)} of {@link ValidationHandler}'s
34 * for looking up methods for validation/error calls.
35 * </p>
36 *
37 * @author Scott Milne
38 *
39 */
40
41 public class Validation
42 {
43 /* Inner Classes */
44
45
46 /* Variables */
47
48
49 /** An <code>HashMap</code> of <code>ValidationHandler</code> objects to use for looking up methods for validation/error calls. */
50 private HashMap handlers;
51
52 /** The configuration file */
53 private String configurationFile;
54
55 /** The ValidationReader class. */
56 private ValidationReader reader;
57
58
59 /* Constructors. */
60
61 /**
62 * Constructs a <code>Validation</code> instance.
63 *
64 * @param modelTreeFactory A {@link ModelTreeFactory} for parsing descriptors.
65 * @param resourceLoader A {@link com.aendvari.common.util.ResourceLoader} class.
66 *
67 */
68
69 public Validation( ModelTreeFactory modelTreeFactory, Class resourceLoader )
70 throws Exception
71 {
72 handlers = new HashMap();
73 prepareValidation( modelTreeFactory, resourceLoader );
74 }
75
76 /**
77 * Initialize this <code>Validation</code> with given handlers.
78 *
79 * @param setHandlers An array[] of {@link ValidationHandler} instances.
80 *
81 */
82
83 private void intializeHandlers( ValidationHandler[] setHandlers )
84 {
85 if( setHandlers != null )
86 {
87 int index = 0;
88 for ( index=0; index<setHandlers.length; index++ )
89 {
90 ValidationHandler handler = setHandlers[index];
91 handlers.put( handler.getName(), handler );
92 }
93 }
94 }
95
96 /**
97 * Prepares the validation for execution.
98 *
99 * @param modelTreeFactory A {@link ModelTreeFactory} for parsing descriptors.
100 * @param resourceLoader A {@link com.aendvari.common.util.ResourceLoader} class.
101 *
102 */
103
104 private void prepareValidation( ModelTreeFactory modelTreeFactory, Class resourceLoader )
105 throws Exception
106 {
107 // create specific ModelTree implementation provided
108 ModelTree modelTree = modelTreeFactory.createModelTree();
109
110 // create a validation configuration reader
111 reader = new ValidationReader();
112
113 // parse the configuration file
114 reader.read(modelTree, resourceLoader);
115
116 // update the map of handlers from loaded instances
117 HashMap newHandlers = reader.getHandlers();
118 handlers.putAll(newHandlers);
119
120 // we don't need the modelTree anymore, so clear it
121 modelTree = null;
122 }
123
124 /**
125 * Validate the data at the given {@link ModelNode} using the validators defined in the given
126 * validation configuration file.
127 *
128 * @param datasetNode The {@link ModelNode} of which to extract the data for a {@link Dataset}.
129 *
130 */
131
132 public void validate( ModelNode datasetNode )
133 throws Exception
134 {
135 // execute the validations
136 executeValidation( datasetNode );
137 }
138
139 /**
140 * Validate the data at the given {@link ModelNode} using the validators defined in the given
141 * validation configuration file.
142 *
143 * @param datasetNode The {@link ModelNode} of which to extract the data to be validated from.
144 * @param setHandlers An array[] of {@link ValidationHandler} instances.
145 *
146 */
147
148 public void validate( ModelNode datasetNode, ValidationHandler[] setHandlers )
149 throws Exception
150 {
151 // intialize with any given handlers
152 intializeHandlers( setHandlers );
153
154 // execute the validations
155 executeValidation( datasetNode );
156 }
157
158 /**
159 * Executes all validate methods on each {@link Property} of each {@link Dataset}.
160 *
161 * @param datasetNode The {@link ModelNode} of which to extract the data to be validated from.
162 */
163
164 private void executeValidation( ModelNode datasetNode )
165 throws Exception
166 {
167 // retrieve the data objects derived from the configration
168 Collection datasets = reader.getDatasets();
169 MultiHashMap validators = reader.getValidators();
170
171 // for each property in each dataset, run the validator(s) for them
172 Iterator datasetIterator = datasets.iterator();
173
174 while ( datasetIterator.hasNext() )
175 {
176 Dataset dataset = (Dataset)datasetIterator.next();
177
178 dataset.executeValidation( datasetNode, validators, handlers );
179 }
180 }
181 }
182