KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > tools > common > validation > Validator


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Validator.java March 24, 2003, 11:16 AM
26  */

27
28 package com.sun.enterprise.tools.common.validation;
29
30 import java.lang.reflect.Method JavaDoc;
31 import java.text.MessageFormat JavaDoc;
32 import java.util.ArrayList JavaDoc;
33 import java.util.Collection JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 import com.sun.enterprise.tools.common.validation.Constants;
37 import com.sun.enterprise.tools.common.validation.constraints.Constraint;
38 import com.sun.enterprise.tools.common.validation.util.BundleReader;
39 import com.sun.enterprise.tools.common.validation.util.Utils;
40 import com.sun.enterprise.tools.common.validation.Validatee;
41
42 /**
43  * Validator is an <code>Object</code> that knows how to validate its
44  * corresponding <Object>Object</code> called {@link <code>Validatee</code>}
45  * <p>
46  * Every Validatee has a corresponding {@link Validator} object and this object
47  * knows how to validate its <code>Validatee</code>. <code>Validator</code>
48  * maintains a list of {@link <code>Constraint</code>}s that needs to be applied
49  * for each of the element of its <code>Validatee</code>.
50  * <code>Constraint</code> objects are built using an two file , Validation File
51  * and Constraints File.
52  * {@ValidationManager} is an object that constructs the <code>Validator</code>
53  * objects for all the Validatees. <code>ValidationManager</code> maintains a
54  * map of xpaths to Validators. It constructs this by reading two files -
55  * Validation File and Constraints File. Validation File specifies
56  * constraints to be applied to the elements. Constraints File provides
57  * information about the Constraints such as Constraints class name, number and
58  * type of Constraint's constructor arguments.
59  * <p>
60  * Validations are performed, recursively.
61  * Two types of Validations are perfomed, Structural validations and specified
62  * validations. Structural validations are expressed through
63  * <code>{@link CardinalConstraint}</code>. <code>CardinalConstraint</code> is
64  * an implicit constraint. Its always applied to each of the element; you dont
65  * need to specify it explicitly. Whereas , specified Constaints need to be
66  * explicitly specified for each element you wanted to apply this
67  * <code>Constaint</code> to. You can also define, your own custom
68  * <code>Constraint</code>s and apply them to any elements.
69  *
70  * @see Constraint
71  * @see Validator
72  * @see ValidationManager
73  * @see CardianlConstraint
74  *
75  * @author Rajeshwar Patil
76  * @version %I%, %G%
77  */

78 class Validator {
79     /* A class implementation comment can go here. */
80     
81     private static String JavaDoc SIZE_PREFIX = "size"; //NOI18N
82

83     /**
84      * A map that stores <code>Constraint</code> lists for the elements
85      * of the <code>Validatee</code> that this Object corresponds to.
86      */

87     private HashMap JavaDoc elementToConstraints = null;
88
89     private Utils utils = null;
90
91     /** Creates a new instance of Validator */
92     Validator() {
93         elementToConstraints = new HashMap JavaDoc();
94         utils = new Utils();
95     }
96
97
98     /**
99      * Adds the given <code>Constraint</code> for the given element in the
100      * map maintained by this object. This maps maintains Constraint lists for
101      * each of the elements of the Corresponding Validatee.
102      *
103      * @param element the element for which the given
104      * <code>constraint</code> to be added to its list
105      * @param constraint the given <code>Constraint</code> to be added
106      * to the list of Constraints of the given <code>element</code>.
107      */

108     void addElementConstraint(String JavaDoc element, Constraint constraint){
109         ArrayList JavaDoc list = (ArrayList JavaDoc) elementToConstraints.get(element);
110         if(null == list) {
111             list = new ArrayList JavaDoc();
112         }
113         list.add(constraint);
114         elementToConstraints.put(element, list);
115     }
116
117
118     /**
119      * Validates the given element. The given element could be a
120      * single element or an array.
121      *
122      * @param elementName the element to be validated
123      * @param elementDtdName the dtd name of the element to be validated
124      * @param validatee the <code>Validatee</code> object that this object
125      * corresponds to.
126      *
127      * @return <code>Collection</code> the Collection of
128      * <code>ConstraintFailure</code> Objects. Collection is empty
129      * if there are no failures.
130      */

131     Collection JavaDoc validate(String JavaDoc elementName,String JavaDoc elementDtdName,
132                 Validatee validatee){
133         ArrayList JavaDoc failures = new ArrayList JavaDoc();
134         boolean isIndexed = validatee.isIndexed(elementName);
135
136         if(isIndexed){
137             failures.addAll(validateProperties(elementName,
138                 elementDtdName, validatee));
139         } else {
140             failures.addAll(validateProperty(elementName,
141                 elementDtdName, validatee));
142         }
143         return failures;
144     }
145
146
147     /**
148      * Validates the given Element.
149      *
150      * @param property the Element to be validated
151      * @param absoluteDtdName the complete dtd name of <code>property</code>
152      * @param fieldName the name of the GUI field, associated
153      * with <code>property</code>
154      *
155      * @return <code>Collection</code> the Collection of
156      * <code>ConstraintFailure</code> Objects. Collection is empty
157      * if there are no failures.
158      */

159     Collection JavaDoc validateIndividualProperty(String JavaDoc property,
160             String JavaDoc absoluteDtdName, String JavaDoc fieldName){
161         //This method --fetches the constraint list for the specified element
162
// --applies the constraints to the specified element
163

164         ArrayList JavaDoc failures = new ArrayList JavaDoc();
165         String JavaDoc elementDtdName = utils.getName(absoluteDtdName,
166                 Constants.XPATH_DELIMITER_CHAR);
167
168         ArrayList JavaDoc constraintList =
169             (ArrayList JavaDoc)elementToConstraints.get(elementDtdName);
170
171         if(constraintList != null) {
172             failures.addAll(validate(property, constraintList, fieldName));
173         } else {
174             ///String format =
175
/// BundleReader.getValue("MSG_No_definition_for"); //NOI18N
176
/// Object[] arguments =
177
/// new Object[]{"Constraints", absoluteDtdName}; //NOI18N
178
///System.out.println(MessageFormat.format(format, arguments));
179
}
180         return failures;
181     }
182
183
184     /**
185      * Validates the given element. The given element is a scalar element.
186      *
187      * @param elementName the element to be validated
188      * @param elementDtdName the dtd name of the element to be validated
189      * @param validatee the <code>Validatee</code> object that this object
190      * corresponds to.
191      *
192      * @return <code>Collection</code> the Collection of
193      * <code>ConstraintFailure</code> Objects. Collection is empty
194      * if there are no failures.
195      */

196     private Collection JavaDoc validateProperty(String JavaDoc elementName,
197             String JavaDoc elementDtdName, Validatee validatee){
198         //This method --fetches the value of the specified element
199
// --fetches the constraint list for the specified element
200
// --applies the constraints to the specified element
201

202         ArrayList JavaDoc failures = new ArrayList JavaDoc();
203         ArrayList JavaDoc constraintList =
204             (ArrayList JavaDoc)elementToConstraints.get(elementDtdName);
205         String JavaDoc name = validatee.getXPath() + Constants.XPATH_DELIMITER +
206             elementDtdName;
207         if(constraintList != null) {
208             String JavaDoc property = (String JavaDoc)validatee.getElement(elementName);
209             failures.addAll(validate(property, constraintList, name));
210         } else {
211             ///String format =
212
/// BundleReader.getValue("MSG_No_definition_for"); //NOI18N
213
/// Object[] arguments = new Object[]{"Constraints", name}; //NOI18N
214
///System.out.println(MessageFormat.format(format, arguments));
215
}
216         return failures;
217     }
218
219
220     /**
221      * Validates the given element. The given element is an array element.
222      *
223      * @param elementName the element to be validated
224      * @param elementDtdName the dtd name of the element to be validated
225      * @param validatee the <code>Validatee</code> object that this object
226      * corresponds to.
227      *
228      * @return <code>Collection</code> the Collection of
229      * <code>ConstraintFailure</code> Objects. Collection is empty
230      * if there are no failures.
231      */

232     private Collection JavaDoc validateProperties(String JavaDoc elementName,
233             String JavaDoc elementDtdName, Validatee validatee){
234         ArrayList JavaDoc failures = new ArrayList JavaDoc();
235         int noOfElements = 0;
236         String JavaDoc sizeMethodName = utils.methodNameFromBeanName(
237             elementName, SIZE_PREFIX);
238         Method JavaDoc sizeMethod = validatee.getMethod(sizeMethodName);
239         noOfElements = ((Integer JavaDoc)validatee.invoke(sizeMethod)).intValue();
240         
241         ArrayList JavaDoc constraintList =
242             (ArrayList JavaDoc)elementToConstraints.get(elementDtdName);
243         if(constraintList != null) {
244             for(int i=0; i<noOfElements; i++){
245                 String JavaDoc property = (String JavaDoc)validatee.getElement(elementName,
246                     i);
247                 String JavaDoc name = validatee.getXPath() + Constants.XPATH_DELIMITER +
248                     utils.getIndexedName(elementDtdName, i);
249                 failures.addAll(validate(property, constraintList, name));
250             }
251         } else {
252             ///String format =
253
/// BundleReader.getValue("MSG_No_definition_for"); //NOI18N
254
/// Object[] arguments =
255
/// new Object[]{"Constraints", elementName}; //NOI18N
256
///System.out.println(MessageFormat.format(format, arguments));
257
}
258         return failures;
259     }
260
261
262     /**
263      * Applies the given set of <code>constraints</code> to the given string
264      * Returns set of constraints that the given string fails to satisfy.
265      *
266      * @param value the value to be validated
267      * @param constraints the given set of <code>Constraint</code>s
268      * @param name the name of the element being validated
269      *
270      * @return <code>Collection</code> the Collection of
271      * <code>ConstraintFailure</code> Objects. Collection is empty
272      * if there are no failures.
273      */

274     private Collection JavaDoc validate(String JavaDoc value, ArrayList JavaDoc constraints,
275             String JavaDoc name){
276         ArrayList JavaDoc failed_constrains = new ArrayList JavaDoc();
277         int size = constraints.size();
278         Constraint constraint = null;
279         for(int i=0; i<size; i++) {
280             constraint = (Constraint)constraints.get(i);
281             failed_constrains.addAll(constraint.match(value, name));
282         }
283         return failed_constrains;
284     }
285 }
286
Popular Tags