KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > validation > Validator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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

70 class Validator {
71     /* A class implementation comment can go here. */
72     
73     private static String JavaDoc SIZE_PREFIX = "size"; //NOI18N
74

75     /**
76      * A map that stores <code>Constraint</code> lists for the elements
77      * of the <code>Validatee</code> that this Object corresponds to.
78      */

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

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

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

151     Collection JavaDoc validateIndividualProperty(String JavaDoc property,
152             String JavaDoc absoluteDtdName, String JavaDoc fieldName){
153         //This method --fetches the constraint list for the specified element
154
// --applies the constraints to the specified element
155

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

188     private Collection JavaDoc validateProperty(String JavaDoc elementName,
189             String JavaDoc elementDtdName, Validatee validatee){
190         //This method --fetches the value of the specified element
191
// --fetches the constraint list for the specified element
192
// --applies the constraints to the specified element
193

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

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

266     private Collection JavaDoc validate(String JavaDoc value, ArrayList JavaDoc constraints,
267             String JavaDoc name){
268         ArrayList JavaDoc failed_constrains = new ArrayList JavaDoc();
269         int size = constraints.size();
270         Constraint constraint = null;
271         for(int i=0; i<size; i++) {
272             constraint = (Constraint)constraints.get(i);
273             failed_constrains.addAll(constraint.match(value, name));
274         }
275         return failed_constrains;
276     }
277 }
278
Popular Tags