1 15 package org.apache.tapestry.valid; 16 17 import java.text.MessageFormat ; 18 import java.util.HashMap ; 19 import java.util.Locale ; 20 import java.util.Map ; 21 import java.util.ResourceBundle ; 22 23 import org.apache.hivemind.ApplicationRuntimeException; 24 import org.apache.hivemind.HiveMind; 25 import org.apache.hivemind.Resource; 26 import org.apache.hivemind.util.ClasspathResource; 27 import org.apache.hivemind.util.PropertyUtils; 28 import org.apache.tapestry.IEngine; 29 import org.apache.tapestry.IForm; 30 import org.apache.tapestry.IMarkupWriter; 31 import org.apache.tapestry.IRequestCycle; 32 import org.apache.tapestry.IScript; 33 import org.apache.tapestry.PageRenderSupport; 34 import org.apache.tapestry.TapestryUtils; 35 import org.apache.tapestry.engine.IScriptSource; 36 import org.apache.tapestry.form.FormEventType; 37 import org.apache.tapestry.form.IFormComponent; 38 import org.apache.tapestry.html.Body; 39 40 46 47 public abstract class BaseValidator implements IValidator 48 { 49 55 56 public static final String FIELD_SYMBOL = "field"; 57 58 64 65 public static final String VALIDATOR_SYMBOL = "validator"; 66 67 73 74 public static final String FORM_SYMBOL = "form"; 75 76 85 86 public static final String FUNCTION_SYMBOL = "function"; 87 88 private boolean _required; 89 90 91 92 private String _requiredMessage; 93 94 97 98 private boolean _clientScriptingEnabled = false; 99 100 103 104 public BaseValidator() 105 { 106 } 107 108 113 public BaseValidator(String initializer) 114 { 115 PropertyUtils.configureProperties(this, initializer); 116 } 117 118 protected BaseValidator(boolean required) 119 { 120 _required = required; 121 } 122 123 public boolean isRequired() 124 { 125 return _required; 126 } 127 128 public void setRequired(boolean required) 129 { 130 _required = required; 131 } 132 133 152 153 protected String getPattern(String override, String key, Locale locale) 154 { 155 if (override != null) 156 return override; 157 158 ResourceBundle strings = ResourceBundle.getBundle( 159 "org.apache.tapestry.valid.ValidationStrings", 160 locale); 161 162 return strings.getString(key); 163 } 164 165 177 178 protected String formatString(String pattern, Object [] args) 179 { 180 return MessageFormat.format(pattern, args); 181 } 182 183 188 189 protected String formatString(String pattern, Object arg) 190 { 191 return formatString(pattern, new Object [] 192 { arg }); 193 } 194 195 200 201 protected String formatString(String pattern, Object arg1, Object arg2) 202 { 203 return formatString(pattern, new Object [] 204 { arg1, arg2 }); 205 } 206 207 212 213 protected boolean checkRequired(IFormComponent field, String value) throws ValidatorException 214 { 215 boolean isEmpty = HiveMind.isBlank(value); 216 217 if (_required && isEmpty) 218 throw new ValidatorException(buildRequiredMessage(field), ValidationConstraint.REQUIRED); 219 220 return isEmpty; 221 } 222 223 228 229 protected String buildRequiredMessage(IFormComponent field) 230 { 231 String pattern = getPattern(_requiredMessage, "field-is-required", field.getPage() 232 .getLocale()); 233 234 return formatString(pattern, field.getDisplayName()); 235 } 236 237 242 243 public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer, 244 IRequestCycle cycle) 245 { 246 } 247 248 271 272 protected void processValidatorScript(String scriptPath, IRequestCycle cycle, 273 IFormComponent field, Map symbols) 274 { 275 IEngine engine = field.getPage().getEngine(); 276 IScriptSource source = engine.getScriptSource(); 277 IForm form = field.getForm(); 278 279 Map finalSymbols = (symbols == null) ? new HashMap () : symbols; 280 281 finalSymbols.put(FIELD_SYMBOL, field); 282 finalSymbols.put(FORM_SYMBOL, form); 283 finalSymbols.put(VALIDATOR_SYMBOL, this); 284 285 Resource location = new ClasspathResource(engine.getClassResolver(), scriptPath); 286 287 IScript script = source.getScript(location); 288 289 292 PageRenderSupport pageRenderSupport = TapestryUtils.getPageRenderSupport(cycle, field); 293 294 script.execute(cycle, pageRenderSupport, finalSymbols); 295 } 296 297 306 307 public boolean isClientScriptingEnabled() 308 { 309 return _clientScriptingEnabled; 310 } 311 312 public void setClientScriptingEnabled(boolean clientScriptingEnabled) 313 { 314 _clientScriptingEnabled = clientScriptingEnabled; 315 } 316 317 public String getRequiredMessage() 318 { 319 return _requiredMessage; 320 } 321 322 328 329 public void setRequiredMessage(String string) 330 { 331 _requiredMessage = string; 332 } 333 334 } | Popular Tags |