1 16 package org.apache.cocoon.transformation.helpers; 17 18 import org.apache.avalon.framework.configuration.Configuration; 19 import org.apache.avalon.framework.configuration.ConfigurationException; 20 import org.apache.avalon.framework.configuration.SAXConfigurationHandler; 21 import org.apache.excalibur.source.Source; 22 23 import org.apache.cocoon.Constants; 24 import org.apache.cocoon.acting.ConfigurationHelper; 25 import org.apache.cocoon.acting.ValidatorActionResult; 26 import org.apache.cocoon.components.source.SourceUtil; 27 import org.apache.cocoon.environment.ObjectModelHelper; 28 import org.apache.cocoon.environment.Request; 29 import org.apache.cocoon.environment.SourceResolver; 30 31 import org.apache.avalon.framework.logger.Logger; 32 33 import java.util.HashMap ; 34 import java.util.Map ; 35 36 42 public class FormValidatorHelper { 43 44 private static Map configurations = new HashMap (); 45 46 49 50 String current_descriptor = null; 51 boolean current_reloadable = true; 52 Logger current_logger = null; 53 String current_constraint_set = null; 54 String current_parameter = null; 55 SourceResolver current_resolver = null; 56 57 public FormValidatorHelper(String descriptor, boolean reloadable, 58 Logger logger, SourceResolver resolver) { 59 current_descriptor = descriptor; 60 current_reloadable = reloadable; 61 current_logger = logger; 62 current_resolver = resolver; 63 } 64 65 public FormValidatorHelper(String descriptor, boolean reloadable, 66 Logger logger, SourceResolver resolver, 67 String constraintset) { 68 current_descriptor = descriptor; 69 current_reloadable = reloadable; 70 current_logger = logger; 71 current_resolver = resolver; 72 current_constraint_set = constraintset; 73 } 74 75 78 public void setParameter(String parameter) { 79 current_parameter = parameter; 80 } 81 82 86 public void setConstraintSet(String constraintset) { 87 current_constraint_set = constraintset; 88 } 89 90 96 public static Object getAttribute(Map objectModel, String name) { 97 Request request = ObjectModelHelper.getRequest(objectModel); 98 return request.getAttribute(name); 99 } 100 101 108 public static Map getResults(Map objectModel) { 109 Request request = ObjectModelHelper.getRequest(objectModel); 110 return (Map ) request.getAttribute(Constants.XSP_FORMVALIDATOR_PATH); 111 } 112 113 114 122 public static ValidatorActionResult getParamResult(Map objectModel, 123 String name) { 124 ValidatorActionResult result = ValidatorActionResult.NOTPRESENT; 125 Map param_result = getResults(objectModel); 126 if (param_result != null) { 127 result = (ValidatorActionResult) param_result.get(name); 128 } 129 return (result != null? result : ValidatorActionResult.NOTPRESENT); 130 } 131 132 139 public ValidatorActionResult getParamResult(Map objectModel) { 140 ValidatorActionResult result = ValidatorActionResult.NOTPRESENT; 141 Map param_result = getResults(objectModel); 142 if (param_result != null) { 143 result = (ValidatorActionResult) param_result.get(current_parameter); 144 } 145 return (result != null? result : ValidatorActionResult.NOTPRESENT); 146 } 147 148 149 158 public static boolean isOK(Map objectModel, String name) { 159 return getParamResult(objectModel, name).equals(ValidatorActionResult.OK); 160 } 161 162 170 public boolean isOK(Map objectModel) { 171 return isOK(objectModel, current_parameter); 172 } 173 174 175 184 public static boolean isError(Map objectModel, String name) { 185 return getParamResult(objectModel, name).ge(ValidatorActionResult.ERROR); 186 } 187 188 196 public boolean isError(Map objectModel) { 197 return isError(objectModel, current_parameter); 198 } 199 200 201 209 public static boolean isNull(Map objectModel, String name) { 210 return getParamResult(objectModel, name).equals(ValidatorActionResult.ISNULL); 211 } 212 213 221 public boolean isNull(Map objectModel) { 222 return isNull(objectModel, current_parameter); 223 } 224 225 226 235 public static boolean isTooSmall(Map objectModel, String name) { 236 boolean ok = getParamResult(objectModel, name).equals(ValidatorActionResult.TOOSMALL); 237 238 if (!ok) { 239 ok = isNull(objectModel, name); 240 } 241 242 return ok; 243 } 244 245 253 public boolean isTooSmall(Map objectModel) { 254 return isTooSmall(objectModel, current_parameter); 255 } 256 257 258 267 public static boolean isTooLarge(Map objectModel, String name) { 268 return (getParamResult(objectModel, name) == ValidatorActionResult.TOOLARGE); 269 } 270 271 279 public boolean isTooLarge(Map objectModel) { 280 return isTooLarge(objectModel, current_parameter); 281 } 282 283 284 294 public static boolean isNoMatch(Map objectModel, String name) { 295 return getParamResult(objectModel, name).equals(ValidatorActionResult.NOMATCH); 296 } 297 298 307 public boolean isNoMatch(Map objectModel) { 308 return isNoMatch(objectModel, current_parameter); 309 } 310 311 312 319 public static boolean isNotPresent(Map objectModel, String name) { 320 return getParamResult(objectModel, name).equals(ValidatorActionResult.NOTPRESENT); 321 } 322 323 329 public boolean isNotPresent(Map objectModel) { 330 return isNotPresent(objectModel, current_parameter); 331 } 332 333 334 354 355 protected static Configuration getConfiguration(String descriptor, SourceResolver resolver, 356 boolean reloadable, Logger logger) 357 throws ConfigurationException { 358 359 if (descriptor == null) { 360 throw new ConfigurationException("The form descriptor is not set!"); 361 } 362 363 ConfigurationHelper conf = null; 364 synchronized (FormValidatorHelper.configurations) { 365 Source source = null; 366 try { 367 source = resolver.resolveURI(descriptor); 368 conf = (ConfigurationHelper) FormValidatorHelper.configurations.get(source.getURI()); 369 if (conf == null || (reloadable && conf.lastModified != source.getLastModified())) { 370 logger.debug("(Re)Loading " + descriptor); 371 372 if (conf == null) { 373 conf = new ConfigurationHelper(); 374 } 375 376 SAXConfigurationHandler builder = new SAXConfigurationHandler(); 377 SourceUtil.toSAX(source, builder); 378 379 conf.lastModified = source.getLastModified(); 380 conf.configuration = builder.getConfiguration(); 381 382 FormValidatorHelper.cacheConfiguration(source.getURI(), conf); 383 } else { 384 logger.debug("Using cached configuration for " + descriptor); 385 } 386 } catch (Exception e) { 387 logger.error("Could not configure Database mapping environment", e); 388 throw new ConfigurationException("Error trying to load configurations for resource: " + source.getURI()); 389 } finally { 390 resolver.release(source); 391 } 392 } 393 394 return conf.configuration; 395 } 396 397 400 private static void cacheConfiguration(String descriptor, ConfigurationHelper conf) { 401 synchronized (FormValidatorHelper.configurations) { 402 FormValidatorHelper.configurations.put(descriptor, conf); 403 } 404 } 405 406 415 protected static Configuration getConfigurationByName(Configuration[] conf, 416 String name, 417 Logger logger 418 ) { 419 int j = 0; 420 boolean found = false; 421 String setname = null; 422 for (j = 0; j < conf.length; j++) { 423 setname = conf[j].getAttribute("name", ""); 424 if (name.trim().equals(setname.trim())) { 425 found = true; 426 break; 427 } 428 } 429 if (!found) { 430 logger.debug("FormValidatorHelper.getConfigurationByName: configuration " + name + " not found."); 431 return null; 432 } 433 return conf[j]; 434 } 435 436 450 public static String getParameterAttributes(String descriptor, 451 SourceResolver resolver, 452 boolean reloadable, 453 String constraintset, 454 String parameter, 455 String attribute, 456 Logger logger 457 ) { 458 try { 459 Configuration conf = getConfiguration(descriptor, resolver, reloadable, logger); 460 Configuration[] desc = conf.getChildren("parameter"); 461 Configuration[] csets = conf.getChildren("constraint-set"); 462 463 Configuration cset = getConfigurationByName(csets, constraintset, logger); 464 465 Configuration[] set = cset.getChildren("validate"); 466 Configuration constraints = getConfigurationByName(set, parameter, logger); 467 Configuration descr = getConfigurationByName(desc, parameter, logger); 468 return constraints.getAttribute(attribute, descr.getAttribute(attribute, "")); 469 } catch (Exception e) { 470 logger.debug("FormValidatorHelper.getParameterAttributes Exception " + e); 471 } 472 473 return ""; 474 } 475 476 477 484 public String getParameterAttribute(String attribute) { 485 return FormValidatorHelper.getParameterAttributes(current_descriptor, 486 current_resolver, 487 current_reloadable, 488 current_constraint_set, 489 current_parameter, 490 attribute, 491 current_logger); 492 } 493 494 501 public String getParameterAttribute(String parameter, String attribute) { 502 return FormValidatorHelper.getParameterAttributes(current_descriptor, 503 current_resolver, 504 current_reloadable, 505 current_constraint_set, 506 parameter, 507 attribute, 508 current_logger); 509 } 510 } 511 | Popular Tags |