1 10 package org.mmbase.applications.editwizard; 11 12 import org.w3c.dom.*; 13 14 24 25 public class Validator { 26 27 public Validator() { 28 } 29 30 37 public static boolean validate(Document prehtml, Document schema) { 38 int errorcount = 0; 39 40 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 createStepsOverview(prehtml, schema); 50 51 return errorcount==0; 53 } 54 55 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 76 public static boolean validateField(Node field) { 77 String ftype = Utils.getAttribute(field,"ftype",""); 78 String dttype = Utils.getAttribute(field, "dttype","string"); 79 80 if (ftype.equals("data") || ftype.equals("startwizard")) return true; String value = Utils.getText(Utils.selectSingleNode(field,"value"),""); 82 83 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 e) { 102 } 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 e){ 111 } 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 nr=0; 121 boolean isint=false; 122 try { 123 nr = Integer.parseInt(value); 124 isint=true; 125 } catch (Exception e) { 126 addValidationError(val, 1, "Entered value is not a valid integer number."); 127 } 128 129 if (isint) { 130 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 } 142 if ("enum".equals(dttype)) { 143 } 146 } 147 return Utils.getAttribute(val,"valid").equals("true"); 148 } 149 150 157 public static void addValidationError(Node validationnode, int errortype, String 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 180 public static void createStepsOverview(Document prehtml, Document schema) { 181 182 Node overview = Utils.selectSingleNode(prehtml, "/*/steps-overview"); 184 if (overview!=null) prehtml.getDocumentElement().removeChild(overview); 185 186 overview = prehtml.createElement("steps-validator"); 188 Utils.setAttribute(overview, "allowcancel", "true"); 189 190 int invalidforms = 0; 191 192 NodeList steps = Utils.selectNodeList(schema, "/*/steps/step"); 194 for (int i=0; i<steps.getLength(); i++) { 195 Node step = steps.item(i); 196 String schemaid = Utils.getAttribute(step, "form-schema", ""); 197 if (!schemaid.equals("")) { 198 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 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 Utils.setAttribute(overview, "allowsave", (invalidforms==0) + ""); 215 Utils.setAttribute(overview,"valid", (invalidforms==0) + ""); 216 217 prehtml.getDocumentElement().appendChild(overview); 219 } 220 221 } 222 | Popular Tags |