haul 02/04/28 12:03:10
Modified: src/java/org/apache/cocoon/acting
AbstractValidatorAction.java
FormValidatorAction.java
Log:
* work with arrays (use getAttributeValues() method instead of
getAttributeValue())
* new constraint for strings: one-of lists all allowed values.
Revision Changes Path
1.9 +171 -75
xml-cocoon2/src/java/org/apache/cocoon/acting/AbstractValidatorAction.java
Index: AbstractValidatorAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/acting/AbstractValidatorAction.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- AbstractValidatorAction.java 27 Mar 2002 13:29:29 -0000 1.8
+++ AbstractValidatorAction.java 28 Apr 2002 19:03:10 -0000 1.9
@@ -134,6 +134,8 @@
* by redefinition in a constraint-set. Thus if e.g. a database field can take
* at maximum 200 character, this property can be set globally.
*
+* Values in parameter arrays are validated individually and the worst
+* error is reported back.
*
* <h3>The attributes recognized in "constraint-set"</h3>
* <table>
@@ -146,7 +148,7 @@
* </table>
* @author <a href="mailto:[EMAIL PROTECTED]">Martin Man</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a>
-* @version CVS $Id: AbstractValidatorAction.java,v 1.8 2002/03/27 13:29:29
vgritsenko Exp $
+* @version CVS $Id: AbstractValidatorAction.java,v 1.9 2002/04/28 19:03:10 haul Exp
$
*/
public abstract class AbstractValidatorAction
extends AbstractComplementaryConfigurableAction
@@ -170,7 +172,8 @@
String type = null;
int i = 0;
- getLogger().debug ("Validating parameter: " + name);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating parameter: " + name);
/* try to find matching param description in conf tree */
try {
@@ -183,32 +186,60 @@
}
if (!found) {
- getLogger().debug("Description for parameter "
- + name + " not found");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("Description for parameter "
+ + name + " not found");
return null;
}
/* check parameter's type */
type = conf[i].getAttribute ("type");
} catch (Exception e) {
- getLogger().debug("No type specified for parameter " + name);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("No type specified for parameter " + name);
return null;
}
/*
* Validation phase
*/
- if ("string".equals (type)) {
- return validateString(name, constraints, conf[i], params);
- } else if ("long".equals (type)) {
- return validateLong(name, constraints, conf[i], params, isString);
- } else if ("double".equals (type)) {
- return validateDouble(name, constraints, conf[i], params, isString);
+ Object value = params.get(name);
+
+ if (value!=null && value.getClass().isArray()) {
+ Object[] values = (Object[]) value;
+ ValidatorActionHelper vaH = null;
+ ValidatorActionResult vaR = ValidatorActionResult.OK;
+ for (int j=0; j<values.length; j++) {
+ value = values[j];
+ if ("string".equals (type)) {
+ vaH = validateString(name, constraints, conf[i], params, value);
+ } else if ("long".equals (type)) {
+ vaH = validateLong(name, constraints, conf[i], params,
isString, value);
+ } else if ("double".equals (type)) {
+ vaH = validateDouble(name, constraints, conf[i], params,
isString, value);
+ } else {
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Unknown type " + type
+ + " specified for parameter " + name);
+ return null;
+ }
+ vaR = (vaR.getPos() < vaH.getResult().getPos() ? vaH.getResult() :
vaR );
+ }
+ return new ValidatorActionHelper(vaH.getObject(), vaR);
} else {
- getLogger().debug ("Unknown type " + type
- + " specified for parameter " + name);
+ if ("string".equals (type)) {
+ return validateString(name, constraints, conf[i], params, value);
+ } else if ("long".equals (type)) {
+ return validateLong(name, constraints, conf[i], params, isString,
value);
+ } else if ("double".equals (type)) {
+ return validateDouble(name, constraints, conf[i], params, isString,
value);
+ } else {
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Unknown type " + type
+ + " specified for parameter " + name);
+ }
+ return null;
}
- return null;
}
/**
@@ -216,13 +247,14 @@
* constraints are not null they are validated as well.
*/
private ValidatorActionHelper validateString(String name, Configuration
constraints,
- Configuration conf, Map params) {
- Object param = params.get(name);
+ Configuration conf, Map params, Object param) {
+
String value = null;
String dflt = getDefault(conf, constraints);
boolean nullable = getNullable(conf, constraints);
- getLogger().debug ("Validating string parameter " + name);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating string parameter " + name);
try {
value = getStringValue(param);
} catch (Exception e) {
@@ -230,7 +262,8 @@
return new ValidatorActionHelper(value, ValidatorActionResult.ERROR);
}
if (value == null) {
- getLogger().debug ("String parameter " + name + " is null");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter " + name + " is null");
if (!nullable) {
return new ValidatorActionHelper(value,
ValidatorActionResult.ISNULL);
} else {
@@ -239,11 +272,17 @@
}
if (constraints != null) {
String eq = constraints.getAttribute ("equals-to", "");
+ eq = conf.getAttribute ("equals-to", eq);
+
String eqp = constraints.getAttribute ("equals-to-param", "");
+ eqp = conf.getAttribute ("equals-to-param", eqp);
String regex = conf.getAttribute ("matches-regex", "");
regex = constraints.getAttribute ( "matches-regex", regex);
+ String oneOf = conf.getAttribute ("one-of", "");
+ oneOf = constraints.getAttribute ( "one-of", oneOf);
+
Long minlen = getAttributeAsLong (conf, "min-len", null);
minlen = getAttributeAsLong (constraints, "min-len", minlen);
@@ -252,10 +291,12 @@
// Validate whether param is equal to constant
if (!"".equals (eq)) {
- getLogger().debug ("String parameter "
- + name + " should be equal to " + eq);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter "
+ + name + " should be equal to " + eq);
if (!value.equals (eq)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
}
@@ -264,52 +305,85 @@
// FIXME: take default value of param being compared with into
// account?
if (!"".equals (eqp)) {
- getLogger().debug ("String parameter "
- + name + " should be equal to " + params.get (eqp));
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter "
+ + name + " should be equal to " + params.get
(eqp));
if (!value.equals (params.get (eqp))) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
}
// Validate whether param length is at least of minimum length
if (minlen != null) {
- getLogger().debug ("String parameter "
- + name + " should be at least " + minlen + " characters
long");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter "
+ + name + " should be at least " + minlen + "
characters long");
if ( value.length() < minlen.longValue() ) {
- getLogger().debug ("and it is shorter (" +
- value.length() + ")" );
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is shorter (" +
+ value.length() + ")" );
return new ValidatorActionHelper ( value,
ValidatorActionResult.TOOSMALL);
}
}
// Validate whether param length is at most of maximum length
if (maxlen != null) {
- getLogger().debug ("String parameter "
- + name + " should be at most " + maxlen + " characters
long");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter "
+ + name + " should be at most " + maxlen + "
characters long");
if ( value.length() > maxlen.longValue() ) {
- getLogger().debug ("and it is longer (" +
- value.length() + ")" );
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is longer (" +
+ value.length() + ")" );
return new ValidatorActionHelper ( value,
ValidatorActionResult.TOOLARGE);
}
}
// Validate wheter param matches regular expression
if (!"".equals (regex)) {
- getLogger().debug ("String parameter " + name +
- " should match regexp \"" + regex + "\"" );
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("String parameter " + name +
+ " should match regexp \"" + regex + "\"" );
try {
RE r = new RE ( regex );
if ( !r.match(value) ) {
- getLogger().debug("and it does not match");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("and it does not match");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
};
} catch ( RESyntaxException rese ) {
- getLogger().error ("String parameter " + name +
- " regex error ", rese);
+ if (getLogger().isDebugEnabled())
+ getLogger().error ("String parameter " + name +
+ " regex error ", rese);
+ return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
+ }
+ }
+
+ // Validates against a set of possibilities
+ if (!"".equals(oneOf)){
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("String parameter " + name +
+ " should be one of \"" + oneOf +"\"" );
+ if (!oneOf.startsWith("|"))
+ oneOf="|"+oneOf;
+ if (!oneOf.endsWith("|"))
+ oneOf=oneOf+"|";
+ if (value.indexOf("|") != -1){
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("String parameter " + name +
+ "contains \"|\" - can't validate that." );
+ return new ValidatorActionHelper(value,
ValidatorActionResult.ERROR);
+ }
+ if (oneOf.indexOf("|"+value+"|") == -1) {
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not" );
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
+ return new ValidatorActionHelper ( value, ValidatorActionResult.OK);
+
}
}
@@ -321,14 +395,15 @@
* constraints are not null they are validated as well.
*/
private ValidatorActionHelper validateLong(String name, Configuration
constraints,
- Configuration conf, Map params, boolean is_string) {
- Object param = params.get (name);
+ Configuration conf, Map params, boolean is_string, Object param) {
+
boolean nullable = getNullable (conf, constraints);
Long value = null;
Long dflt = getLongValue(getDefault(conf, constraints), true);
- getLogger().debug ("Validating long parameter "
- + name + " (encoded in a string: " + is_string + ")");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating long parameter "
+ + name + " (encoded in a string: " + is_string +
")");
try {
value = getLongValue(param, is_string);
} catch (Exception e) {
@@ -336,7 +411,8 @@
return new ValidatorActionHelper(value, ValidatorActionResult.ERROR);
}
if (value == null) {
- getLogger().debug ("Long parameter " + name + " is null");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Long parameter " + name + " is null");
if (!nullable) {
return new ValidatorActionHelper(value,
ValidatorActionResult.ISNULL);
} else {
@@ -355,11 +431,13 @@
// Validate whether param is equal to constant
if (eq != null) {
- getLogger().debug ("Long parameter "
- + name + " should be equal to " + eq);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Long parameter "
+ + name + " should be equal to " + eq);
if (!value.equals(eq)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
}
@@ -368,39 +446,46 @@
// FIXME: take default value of param being compared with into
// account?
if (!"".equals (eqp)) {
- getLogger().debug ("Long parameter "
- + name + " should be equal to " + params.get (eqp));
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Long parameter "
+ + name + " should be equal to " + params.get
(eqp));
// Request parameter is stored as string.
// Need to convert it beforehand.
try {
Long _eqp = new Long ( Long.parseLong((String) params.get(eqp))
);
if (!value.equals (_eqp)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper(value,
ValidatorActionResult.NOMATCH);
}
} catch ( NumberFormatException nfe ) {
- getLogger().debug("Long parameter "+ name +": "+eqp+" is no
long", nfe);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("Long parameter "+ name +": "+eqp+" is no
long", nfe);
return new ValidatorActionHelper(value,
ValidatorActionResult.NOMATCH);
}
}
// Validate wheter param is at least min
if (min != null) {
- getLogger().debug ("Long parameter "
- + name + " should be at least " + min);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Long parameter "
+ + name + " should be at least " + min);
if (min.compareTo(value)>0) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.TOOSMALL);
}
}
// Validate wheter param is at most max
if (max != null) {
- getLogger().debug ("Long parameter "
- + name + " should be at most " + max);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Long parameter "
+ + name + " should be at most " + max);
if (max.compareTo(value)<0) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.TOOLARGE);
}
}
@@ -413,14 +498,15 @@
* constraints are not null they are validated as well.
*/
private ValidatorActionHelper validateDouble(String name, Configuration
constraints,
- Configuration conf, Map params, boolean is_string) {
- Object param = params.get(name);
+ Configuration conf, Map params, boolean is_string, Object param) {
+
boolean nullable = getNullable(conf, constraints);
Double value = null;
Double dflt = getDoubleValue(getDefault(conf, constraints), true);
- getLogger().debug ("Validating double parameter "
- + name + " (encoded in a string: " + is_string + ")");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating double parameter "
+ + name + " (encoded in a string: " + is_string +
")");
try {
value = getDoubleValue(param, is_string);
} catch (Exception e) {
@@ -428,7 +514,8 @@
return new ValidatorActionHelper(value, ValidatorActionResult.ERROR);
}
if (value == null) {
- getLogger().debug ("double parameter " + name + " is null");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("double parameter " + name + " is null");
if (!nullable) {
return new ValidatorActionHelper(value,
ValidatorActionResult.ISNULL);
} else {
@@ -447,11 +534,13 @@
// Validate whether param is equal to constant
if (eq != null) {
- getLogger().debug ("Double parameter "
- + name + " should be equal to " + eq);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Double parameter "
+ + name + " should be equal to " + eq);
if (!value.equals (eq)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
}
@@ -460,38 +549,45 @@
// FIXME: take default value of param being compared with into
// account?
if (!"".equals (eqp)) {
- getLogger().debug ("Double parameter "
- + name + " should be equal to " + params.get (eqp));
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Double parameter "
+ + name + " should be equal to " + params.get
(eqp));
// Request parameter is stored as string.
// Need to convert it beforehand.
try {
Double _eqp = new Double ( Double.parseDouble((String)
params.get(eqp)));
if (!value.equals (_eqp)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
} catch ( NumberFormatException nfe ) {
- getLogger().debug("Double parameter "+ name +": "+eqp+" is no
double", nfe);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug("Double parameter "+ name +": "+eqp+" is
no double", nfe);
return new ValidatorActionHelper ( value,
ValidatorActionResult.NOMATCH);
}
}
// Validate wheter param is at least min
if (min != null) {
- getLogger().debug ("Double parameter "
- + name + " should be at least " + min);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Double parameter "
+ + name + " should be at least " + min);
if (0 > value.compareTo(min)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper (value,
ValidatorActionResult.TOOSMALL);
}
}
// Validate wheter param is at most max
if (max != null) {
- getLogger().debug ("Double parameter "
- + name + " should be at most " + max);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Double parameter "
+ + name + " should be at most " + max);
if (0<value.compareTo(max)) {
- getLogger().debug ("and it is not");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("and it is not");
return new ValidatorActionHelper (value,
ValidatorActionResult.TOOLARGE);
}
}
@@ -551,7 +647,7 @@
/**
* Returns the value of 'nullable' attribute from given configuration or
- * from given constraints, value present in constrints takes precedence,
+ * from given constraints, value present in constraints takes precedence,
* false when attribute is not present in either of them.
*/
private boolean getNullable(Configuration conf, Configuration cons) {
@@ -628,4 +724,4 @@
return dflt;
}
}
-}
\ No newline at end of file
+}
1.9 +45 -21
xml-cocoon2/src/java/org/apache/cocoon/acting/FormValidatorAction.java
Index: FormValidatorAction.java
===================================================================
RCS file:
/home/cvs/xml-cocoon2/src/java/org/apache/cocoon/acting/FormValidatorAction.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- FormValidatorAction.java 21 Mar 2002 04:09:19 -0000 1.8
+++ FormValidatorAction.java 28 Apr 2002 19:03:10 -0000 1.9
@@ -103,9 +103,12 @@
* to XSPs. Mind you that redirections create new request objects and thus
* the result is not available for the target page.
*
+ * All values for a parameter are read through the
+ * getParameterValues() method and validated seperately.
+ *
* @author <a href="mailto:[EMAIL PROTECTED]">Martin Man</a>
* @author <a href="mailto:[EMAIL PROTECTED]">Christian Haul</a>
- * @version CVS $Id: FormValidatorAction.java,v 1.8 2002/03/21 04:09:19 vgritsenko
Exp $
+ * @version CVS $Id: FormValidatorAction.java,v 1.9 2002/04/28 19:03:10 haul Exp $
*/
public class FormValidatorAction extends AbstractValidatorAction implements
ThreadSafe
{
@@ -118,7 +121,8 @@
/* check request validity */
if (req == null) {
- getLogger().debug ("No request object");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("No request object");
return null;
}
@@ -145,8 +149,9 @@
* old obsoleted method
*/
if (!"".equals (valstr.trim ())) {
- getLogger().debug ("Validating parameters "
- + "as specified via 'validate' parameter");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating parameters "
+ + "as specified via 'validate' parameter");
/* get list of params to be validated */
String[] rparams = Tokenizer.tokenize (valstr, ",", false);
@@ -158,7 +163,8 @@
for (int i = 0; i < rparams.length; i ++) {
name = rparams[i];
if (name == null || "".equals (name.trim ())) {
- getLogger().debug ("Wrong syntax of the 'validate'
parameter");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Wrong syntax of the 'validate'
parameter");
return null;
}
name = name.trim ();
@@ -169,7 +175,8 @@
result = validateParameter (name, null, desc,
params, true);
if (!result.isOK()) {
- getLogger().debug ("Validation failed for parameter " +
name);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validation failed for parameter " +
name);
allOK = false;
}
actionMap.put (name, result.getObject());
@@ -180,8 +187,9 @@
* new set-based method
*/
if (!"".equals (valsetstr.trim ())) {
- getLogger().debug ("Validating parameters "
- + "from given constraint-set " + valsetstr);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validating parameters "
+ + "from given constraint-set " + valsetstr);
Configuration cset = null;
String setname = null;
int j = 0;
@@ -194,9 +202,10 @@
}
}
if (!found) {
- getLogger().debug ("Given set "
- + valsetstr
- + " does not exist in a description file");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Given set "
+ + valsetstr
+ + " does not exist in a description
file");
return null;
}
cset = csets[j];
@@ -207,26 +216,38 @@
ValidatorActionHelper result = null;
String name = null;
HashMap params = new HashMap (set.length);
- getLogger().debug ("Given set "
- + valsetstr
- + " contains " + set.length + " rules");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Given set "
+ + valsetstr
+ + " contains " + set.length + " rules");
/* put required params into hash */
for (int i = 0; i < set.length; i ++) {
name = set[i].getAttribute ("name", "");
if ("".equals (name.trim ())) {
- getLogger().debug ("Wrong syntax "
- + " of 'validate' children nr. " + i);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Wrong syntax "
+ + " of 'validate' children nr. " +
i);
return null;
}
name = name.trim ();
- params.put (name, req.getParameter (name));
+ Object[] values = req.getParameterValues(name);
+ if (values != null) {
+ switch (values.length) {
+ case 0: params.put(name,null); break;
+ case 1: params.put(name,values[0]); break;
+ default: params.put(name,values);
+ }
+ } else {
+ params.put(name,values);
+ }
}
for (int i = 0; i < set.length; i ++) {
name = set[i].getAttribute ("name", null);
result = validateParameter (name, set[i],
desc, params, true);
if (!result.isOK()) {
- getLogger().debug ("Validation failed for parameter " +
name);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("Validation failed for parameter " +
name);
allOK = false;
}
actionMap.put (name, result.getObject());
@@ -236,16 +257,19 @@
if (!allOK) {
// if any validation failed return an empty map
actionMap = null;
- getLogger().debug ("All form params validated. An error occurred.");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("All form params validated. An error
occurred.");
} else {
- getLogger().debug ("All form params successfully validated");
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("All form params successfully validated");
}
// store validation results in request attribute
req.setAttribute(Constants.XSP_FORMVALIDATOR_PATH, resultMap);
//return Collections.unmodifiableMap (actionMap);
return actionMap;
} catch (Exception e) {
- getLogger().debug ("exception: ", e);
+ if (getLogger().isDebugEnabled())
+ getLogger().debug ("exception: ", e);
}
return null;
}
----------------------------------------------------------------------
In case of troubles, e-mail: [EMAIL PROTECTED]
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]