1 21 22 package org.apache.commons.validator; 23 24 import java.io.Serializable ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 31 import org.apache.commons.collections.FastHashMap; 33 44 public class Form implements Serializable { 45 46 49 protected String name = null; 50 51 57 protected List lFields = new ArrayList (); 58 59 63 protected FastHashMap hFields = new FastHashMap(); 64 65 69 protected String inherit = null; 70 71 75 private boolean processed = false; 76 77 80 public String getName() { 81 return name; 82 } 83 84 87 public void setName(String name) { 88 this.name = name; 89 } 90 91 94 public void addField(Field f) { 95 this.lFields.add(f); 96 this.hFields.put(f.getKey(), f); 97 } 98 99 103 public List getFields() { 104 return Collections.unmodifiableList(lFields); 105 } 106 107 112 public Field getField(String fieldName) { 113 return (Field) this.hFields.get(fieldName); 114 } 115 116 120 public boolean containsField(String fieldName) { 121 return this.hFields.containsKey(fieldName); 122 } 123 124 128 protected void process(Map globalConstants, Map constants, Map forms) { 129 if (isProcessed()) { 130 return; 131 } 132 133 int n = 0; if (isExtending()) { 135 Form parent = (Form) forms.get(inherit); 136 if (parent != null) { 137 if (!parent.isProcessed()) { 138 parent.process(constants, globalConstants, forms); 140 } 141 for (Iterator i = parent.getFields().iterator(); i.hasNext();) { 142 Field f = (Field) i.next(); 143 if (hFields.get(f.getKey()) == null) { 145 lFields.add(n, f); 146 hFields.put(f.getKey(), f); 147 n++; 148 } 149 } 150 } 151 } 152 hFields.setFast(true); 153 for (Iterator i = lFields.listIterator(n); i.hasNext();) { 155 Field f = (Field) i.next(); 156 f.process(globalConstants, constants); 157 } 158 159 processed = true; 160 } 161 162 165 public String toString() { 166 StringBuffer results = new StringBuffer (); 167 168 results.append("Form: "); 169 results.append(name); 170 results.append("\n"); 171 172 for (Iterator i = lFields.iterator(); i.hasNext();) { 173 results.append("\tField: \n"); 174 results.append(i.next()); 175 results.append("\n"); 176 } 177 178 return results.toString(); 179 } 180 181 190 ValidatorResults validate(Map params, Map actions, int page) 191 throws ValidatorException { 192 193 ValidatorResults results = new ValidatorResults(); 194 params.put(Validator.VALIDATOR_RESULTS_PARAM, results); 195 196 Iterator fields = this.lFields.iterator(); 197 while (fields.hasNext()) { 198 Field field = (Field) fields.next(); 199 200 params.put(Validator.FIELD_PARAM, field); 201 202 if (field.getPage() <= page) { 203 results.merge(field.validate(params, actions)); 204 } 205 } 206 207 return results; 208 } 209 210 215 public boolean isProcessed() { 216 return processed; 217 } 218 219 223 public String getExtends() { 224 return inherit; 225 } 226 227 231 public void setExtends(String inherit) { 232 this.inherit = inherit; 233 } 234 235 239 public boolean isExtending() { 240 return inherit != null; 241 } 242 243 247 protected Map getFieldMap() { 248 return hFields; 249 } 250 } | Popular Tags |