KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > applications > editwizard > Validator


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.applications.editwizard;
11
12 import org.w3c.dom.*;
13
14 /**
15  * This class validates pre-html documents for the EditWizard.
16  * It is mainly used to register invalid form and fields.
17  *
18  * @javadoc
19  * @author Kars Veling
20  * @author Pierre van Rooden
21  * @since MMBase-1.6
22  * @version $Id: Validator.java,v 1.8 2005/10/05 10:41:38 michiel Exp $
23  */

24
25 public class Validator {
26
27     public Validator() {
28     }
29
30     /**
31      * Validates the given prehtml-xml. Returns true if everything is valid, false if errors are found.
32      * Detailed information about the errors are placed as subnodes of the invalid fields.
33      * @param prehtml the prehtml which should be validated
34      * @param schema the wizardschema which is needed to validate at all
35      * @return True is the form is valid, false if it is invalid
36      */

37     public static boolean validate(Document prehtml, Document schema) {
38         int errorcount = 0;
39
40         // iterate through forms and validate each one of them.
41
NodeList forms = Utils.selectNodeList(prehtml, "/*/form");
42         for (int i=0; i<forms.getLength(); i++) {
43
44             Node form = forms.item(i);
45             errorcount+=validateForm(form);
46         }
47
48         // now, add an overview of all steps and validation-information
49
createStepsOverview(prehtml, schema);
50
51         // return true if no errors were found.
52
return errorcount==0;
53     }
54
55     /**
56      * This method can validate one form. It returns the number of invalid fields in the form.
57      *
58      * @param form The form to be validated.
59      * @return The number of invalid fields found.
60      */

61     public static int validateForm(Node form) {
62         int errorcount=0;
63         NodeList fields = Utils.selectNodeList(form, "field[@ftype]");
64         for (int i=0; i<fields.getLength(); i++) {
65             if (!validateField(fields.item(i))) errorcount++;
66         }
67         return errorcount;
68     }
69
70     /**
71      * This method validates one field.
72      *
73      * @param field the field which should be validated.
74      * @return true is the field is valid, false if it is invalid.
75      */

76     public static boolean validateField(Node field) {
77         String JavaDoc ftype = Utils.getAttribute(field,"ftype","");
78         String JavaDoc dttype = Utils.getAttribute(field, "dttype","string");
79
80         if (ftype.equals("data") || ftype.equals("startwizard")) return true; // ok.
81
String JavaDoc value = Utils.getText(Utils.selectSingleNode(field,"value"),"");
82
83         // clean up old validation info if exists
84
Node val = Utils.selectSingleNode(field, "validator");
85         if (val!=null) {
86             field.removeChild(val);
87         }
88
89         val = field.getOwnerDocument().createElement("validator");
90         field.appendChild(val);
91         Utils.setAttribute(val,"valid", "true");
92         int valueLength = value.length();
93         boolean required = Utils.getAttribute(field,"dtrequired", "false").equals("true");
94         if (required && valueLength==0) {
95             addValidationError(val, 1, "Value is required.");
96         } else {
97             if ("string".equals(dttype)) {
98                 int dtminlength = 0;
99                 try {
100                     dtminlength = Integer.parseInt(Utils.getAttribute(field,"dtminlength", "0"));
101                 } catch (Exception JavaDoc e) {
102                     // don't mind that
103
}
104                 if (valueLength<dtminlength) {
105                     addValidationError(val, 1, "Entered text is too short.");
106                 } else {
107                     int dtmaxlength = 640000;
108                     try {
109                         dtmaxlength = Integer.parseInt(Utils.getAttribute(field,"dtmaxlength","640000"));
110                     } catch (Exception JavaDoc e){
111                         // don't mind that again
112
}
113                     if (valueLength>dtmaxlength) {
114                         addValidationError(val, 1, "Entered text is too long.");
115                     }
116                 }
117             }
118             if ("int".equals(dttype) && valueLength!=0) {
119                 // int
120
int nr=0;
121                 boolean isint=false;
122                 try {
123                     nr = Integer.parseInt(value);
124                     isint=true;
125                 } catch (Exception JavaDoc e) {
126                     addValidationError(val, 1, "Entered value is not a valid integer number.");
127                 }
128     
129                 if (isint) {
130                     // check min and max bounds
131
int dtmin = Integer.parseInt(Utils.getAttribute(field,"dtmin","0"));
132                     int dtmax = Integer.parseInt(Utils.getAttribute(field,"dtmax","999999999"));
133                     if (nr<dtmin || nr>dtmax) {
134                         addValidationError(val, 1, "Integer value is too small or too large.");
135                     }
136                 }
137             }
138             if ("date".equals(dttype)) {
139                 // date
140
// TODO
141
}
142             if ("enum".equals(dttype)) {
143                 // enum
144
// TODO
145
}
146         }
147         return Utils.getAttribute(val,"valid").equals("true");
148     }
149
150     /**
151      * This method adds a validation error to the form.
152      *
153      * @param validationnode this node was validated
154      * @param errortype what kind of error was it?
155      * @param errormsg what errormessage should be placed.
156      */

157     public static void addValidationError(Node validationnode, int errortype, String JavaDoc errormsg) {
158         Utils.setAttribute(validationnode, "valid", "false");
159         Node msg = validationnode.getOwnerDocument().createElement("error");
160         Utils.setAttribute(msg,"type",errortype+"");
161         Utils.storeText(msg, errormsg);
162     }
163
164     /**
165      * Constructs an overview-node in the pre-html where all steps are described.
166      * Also, information about valid and non-valid form-schema's are placed in the steps-overview.
167      *
168      * The node looks like this:
169      * <pre>
170      * &lt;steps-validator valid="false" allowsave="false" allowcancel="true"&gt;
171      * &lt;step form-schema="schema-id" valid="true" /&gt;
172      * &lt;step form-schema="schema-id" valid="false" /&gt;
173      * &lt;/steps-validator&gt;
174      * </pre>
175      * THis method creates a steps-overview which can be used by the html to show what forms are valid and what are not.
176      *
177      * @param prehtml the prehtml data node
178      * @param schema the original wizardschema
179      */

180     public static void createStepsOverview(Document prehtml, Document schema) {
181
182         // remove a "steps-validator" node if it exists.
183
Node overview = Utils.selectSingleNode(prehtml, "/*/steps-overview");
184         if (overview!=null) prehtml.getDocumentElement().removeChild(overview);
185
186         // create new overview node
187
overview = prehtml.createElement("steps-validator");
188         Utils.setAttribute(overview, "allowcancel", "true");
189
190         int invalidforms = 0;
191
192         // iterate through all defined steps
193
NodeList steps = Utils.selectNodeList(schema, "/*/steps/step");
194         for (int i=0; i<steps.getLength(); i++) {
195             Node step = steps.item(i);
196             String JavaDoc schemaid = Utils.getAttribute(step, "form-schema", "");
197             if (!schemaid.equals("")) {
198                 // find the referred form and check if the form is valid.
199
Node form = Utils.selectSingleNode(prehtml, "/*/form[@id='"+schemaid+"']");
200                 if (form!=null) {
201                     Node validationerror = Utils.selectSingleNode(form, ".//field/validator[@valid='false']");
202
203                     boolean valid = (validationerror == null);
204                         // copy step information to overview node and store validation results.
205
Node newstep = prehtml.importNode(step.cloneNode(true), true);
206                         overview.appendChild(newstep);
207                     Utils.setAttribute(newstep, "valid", valid+"");
208                     if (!valid) invalidforms++;
209                 }
210             }
211         }
212
213         // store global information about validationresults
214
Utils.setAttribute(overview, "allowsave", (invalidforms==0) + "");
215         Utils.setAttribute(overview,"valid", (invalidforms==0) + "");
216
217         // store new overview node in the prehtml.
218
prehtml.getDocumentElement().appendChild(overview);
219     }
220
221 }
222
Popular Tags