Author: niallp
Date: Fri Sep 16 20:34:41 2005
New Revision: 289694
URL: http://svn.apache.org/viewcvs?rev=289694&view=rev
Log:
Add support the new 'resource' and 'bundle' attributes for Variables in Commons
Validator 1.2.0 (see Bug #32522). Variables can now be localized - for example
the format of a date can be put in the message resources and the key specified
in the validator config.
<field property="orderdate" depends="date">
<var resource="true" bundle="validation">
<var-name>datePattern</var-name>
<var-value>orderdate.pattern</var-value>
</var>
<field>
Also missing variables now cause validation to fail with a standard "System
Error" message shown to the user and details of the problem logged. Previously
validation just passed with no indication there was any kind of problem.
Added:
struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties
(with props)
Modified:
struts/core/trunk/src/java/org/apache/struts/validator/FieldChecks.java
struts/core/trunk/src/java/org/apache/struts/validator/Resources.java
struts/core/trunk/src/java/org/apache/struts/validator/validwhen/ValidWhen.java
struts/taglib/trunk/src/java/org/apache/struts/taglib/html/JavascriptValidatorTag.java
Modified:
struts/core/trunk/src/java/org/apache/struts/validator/FieldChecks.java
URL:
http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/validator/FieldChecks.java?rev=289694&r1=289693&r2=289694&view=diff
==============================================================================
--- struts/core/trunk/src/java/org/apache/struts/validator/FieldChecks.java
(original)
+++ struts/core/trunk/src/java/org/apache/struts/validator/FieldChecks.java Fri
Sep 16 20:34:41 2005
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright 2000-2004 The Apache Software Foundation.
+ * Copyright 2000-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,8 +34,10 @@
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.util.ValidatorUtils;
+import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.RequestUtils;
+import org.apache.struts.util.MessageResources;
/**
* <p>
@@ -57,6 +59,14 @@
*/
private static final Log log = LogFactory.getLog(FieldChecks.class);
+ /**
+ * The message resources for this package.
+ */
+ private static MessageResources sysmsgs =
+ MessageResources.getMessageResources(
+ "org.apache.struts.validator.LocalStrings");
+
+
public static final String FIELD_TEST_NULL = "NULL";
public static final String FIELD_TEST_NOTNULL = "NOTNULL";
public static final String FIELD_TEST_EQUAL = "EQUAL";
@@ -217,11 +227,13 @@
Validator validator,
HttpServletRequest request) {
- String mask = field.getVarValue("mask");
String value = null;
value = evaluateBean(bean, field);
try {
+
+ String mask = Resources.getVarValue("mask", field, validator,
request, true);
+
if (!GenericValidator.isBlankOrNull(value)
&& !GenericValidator.matchRegexp(value, mask)) {
@@ -234,9 +246,10 @@
return true;
}
} catch (Exception e) {
- log.error(e.getMessage(), e);
+ processFailure(errors, field, "mask", e);
+ return false;
}
- return true;
+
}
@@ -736,8 +749,15 @@
Object result = null;
String value = null;
value = evaluateBean(bean, field);
- String datePattern = field.getVarValue("datePattern");
- String datePatternStrict = field.getVarValue("datePatternStrict");
+ boolean isStrict = false;
+ String datePattern = Resources.getVarValue("datePattern", field,
validator, request, false);
+ if (GenericValidator.isBlankOrNull(datePattern)) {
+ datePattern = Resources.getVarValue("datePatternStrict", field,
validator, request, false);
+ if (!GenericValidator.isBlankOrNull(datePattern)) {
+ isStrict = true;
+ }
+ }
+
Locale locale = RequestUtils.getUserLocale(request, null);
if (GenericValidator.isBlankOrNull(value)) {
@@ -745,12 +765,10 @@
}
try {
- if (datePattern != null && datePattern.length() > 0) {
- result = GenericTypeValidator.formatDate(value, datePattern,
false);
- } else if (datePatternStrict != null && datePatternStrict.length()
> 0) {
- result = GenericTypeValidator.formatDate(value,
datePatternStrict, true);
- } else {
+ if (GenericValidator.isBlankOrNull(datePattern)) {
result = GenericTypeValidator.formatDate(value, locale);
+ } else {
+ result = GenericTypeValidator.formatDate(value, datePattern,
isStrict);
}
} catch (Exception e) {
log.error(e.getMessage(), e);
@@ -790,9 +808,15 @@
if (!GenericValidator.isBlankOrNull(value)) {
try {
+ String minVar = Resources.getVarValue("min", field, validator,
request, true);
+ String maxVar = Resources.getVarValue("max", field, validator,
request, true);
long longValue = Long.parseLong(value);
- long min = Long.parseLong(field.getVarValue("min"));
- long max = Long.parseLong(field.getVarValue("max"));
+ long min = Long.parseLong(minVar);
+ long max = Long.parseLong(maxVar);
+ if (min > max) {
+ throw new IllegalArgumentException(sysmsgs.getMessage(
+ "invalid.range", minVar, maxVar));
+ }
if (!GenericValidator.isInRange(longValue, min, max)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -800,7 +824,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "longRange", e);
return false;
}
}
@@ -835,9 +859,15 @@
if (!GenericValidator.isBlankOrNull(value)) {
try {
+ String minVar = Resources.getVarValue("min", field, validator,
request, true);
+ String maxVar = Resources.getVarValue("max", field, validator,
request, true);
+ int min = Integer.parseInt(minVar);
+ int max = Integer.parseInt(maxVar);
int intValue = Integer.parseInt(value);
- int min = Integer.parseInt(field.getVarValue("min"));
- int max = Integer.parseInt(field.getVarValue("max"));
+ if (min > max) {
+ throw new IllegalArgumentException(sysmsgs.getMessage(
+ "invalid.range", minVar, maxVar));
+ }
if (!GenericValidator.isInRange(intValue, min, max)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -845,7 +875,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "intRange", e);
return false;
}
}
@@ -879,9 +909,15 @@
if (!GenericValidator.isBlankOrNull(value)) {
try {
+ String minVar = Resources.getVarValue("min", field, validator,
request, true);
+ String maxVar = Resources.getVarValue("max", field, validator,
request, true);
double doubleValue = Double.parseDouble(value);
- double min = Double.parseDouble(field.getVarValue("min"));
- double max = Double.parseDouble(field.getVarValue("max"));
+ double min = Double.parseDouble(minVar);
+ double max = Double.parseDouble(maxVar);
+ if (min > max) {
+ throw new IllegalArgumentException(sysmsgs.getMessage(
+ "invalid.range", minVar, maxVar));
+ }
if (!GenericValidator.isInRange(doubleValue, min, max)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -889,7 +925,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "doubleRange", e);
return false;
}
}
@@ -923,9 +959,15 @@
if (!GenericValidator.isBlankOrNull(value)) {
try {
+ String minVar = Resources.getVarValue("min", field, validator,
request, true);
+ String maxVar = Resources.getVarValue("max", field, validator,
request, true);
float floatValue = Float.parseFloat(value);
- float min = Float.parseFloat(field.getVarValue("min"));
- float max = Float.parseFloat(field.getVarValue("max"));
+ float min = Float.parseFloat(minVar);
+ float max = Float.parseFloat(maxVar);
+ if (min > max) {
+ throw new IllegalArgumentException(sysmsgs.getMessage(
+ "invalid.range", minVar, maxVar));
+ }
if (!GenericValidator.isInRange(floatValue, min, max)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -933,7 +975,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "floatRange", e);
return false;
}
}
@@ -1039,7 +1081,8 @@
if (value != null) {
try {
- int max = Integer.parseInt(field.getVarValue("maxlength"));
+ String maxVar = Resources.getVarValue("maxlength", field,
validator, request, true);
+ int max = Integer.parseInt(maxVar);
if (!GenericValidator.maxLength(value, max)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -1047,7 +1090,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "floatRange", e);
return false;
}
}
@@ -1082,7 +1125,8 @@
if (!GenericValidator.isBlankOrNull(value)) {
try {
- int min = Integer.parseInt(field.getVarValue("minlength"));
+ String minVar = Resources.getVarValue("minlength", field,
validator, request, true);
+ int min = Integer.parseInt(minVar);
if (!GenericValidator.minLength(value, min)) {
errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
@@ -1090,7 +1134,7 @@
return false;
}
} catch (Exception e) {
- errors.add(field.getKey(),
Resources.getActionMessage(validator, request, va, field));
+ processFailure(errors, field, "minlength", e);
return false;
}
}
@@ -1142,18 +1186,22 @@
}
// Get the options and schemes Vars
- boolean allowallschemes =
"true".equalsIgnoreCase(field.getVarValue("allowallschemes"));
+ String allowallschemesVar = Resources.getVarValue("allowallschemes",
field, validator, request, false);
+ boolean allowallschemes = "true".equalsIgnoreCase(allowallschemesVar);
int options = allowallschemes ? UrlValidator.ALLOW_ALL_SCHEMES : 0;
- if ("true".equalsIgnoreCase(field.getVarValue("allow2slashes"))) {
+ String allow2slashesVar = Resources.getVarValue("allow2slashes",
field, validator, request, false);
+ if ("true".equalsIgnoreCase(allow2slashesVar)) {
options += UrlValidator.ALLOW_2_SLASHES;
}
- if ("true".equalsIgnoreCase(field.getVarValue("nofragments"))) {
+ String nofragmentsVar = Resources.getVarValue("nofragments", field,
validator, request, false);
+ if ("true".equalsIgnoreCase(nofragmentsVar)) {
options += UrlValidator.NO_FRAGMENTS;
}
- String schemesVar = allowallschemes ? null :
field.getVarValue("schemes");
+ String schemesVar = allowallschemes ? null :
+ Resources.getVarValue("schemes", field, validator,
request, false);
// No options or schemes - use GenericValidator as default
if (options == 0 && schemesVar == null) {
@@ -1187,6 +1235,24 @@
errors.add(field.getKey(), Resources.getActionMessage(validator,
request, va, field));
return false;
}
+ }
+
+ /**
+ * Process a validation failure.
+ */
+ private static void processFailure(ActionMessages errors,
+ Field field,
+ String validator,
+ Throwable t) {
+ // Log the error
+ String logErrorMsg = sysmsgs.getMessage("validation.failed",
+ validator, field.getProperty(), t.toString());
+ log.error(logErrorMsg);
+
+ // Add general "system error" message to show to the user
+ String userErrorMsg = sysmsgs.getMessage("system.error");
+ errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
+
}
/**
Added:
struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties
URL:
http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties?rev=289694&view=auto
==============================================================================
---
struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties
(added)
+++
struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties
Fri Sep 16 20:34:41 2005
@@ -0,0 +1,5 @@
+system.error=SYSTEM ERROR: Check logs for details.
+validation.failed={0} validation failed for property {1}: {2}
+variable.missing=Variable {0} is missing.
+variable.resource.notfound=Key {1} not found for Variable {0} in bundle {2}.
+invalid.range=Minimum value {0} is greater than maximum value {1}
Propchange:
struts/core/trunk/src/java/org/apache/struts/validator/LocalStrings.properties
------------------------------------------------------------------------------
svn:eol-style = native
Modified: struts/core/trunk/src/java/org/apache/struts/validator/Resources.java
URL:
http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/validator/Resources.java?rev=289694&r1=289693&r2=289694&view=diff
==============================================================================
--- struts/core/trunk/src/java/org/apache/struts/validator/Resources.java
(original)
+++ struts/core/trunk/src/java/org/apache/struts/validator/Resources.java Fri
Sep 16 20:34:41 2005
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright 2000-2004 The Apache Software Foundation.
+ * Copyright 2000-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,9 +26,12 @@
import org.apache.commons.validator.Arg;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Msg;
+import org.apache.commons.validator.Var;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorResources;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
@@ -47,6 +50,18 @@
public class Resources {
/**
+ * The message resources for this package.
+ */
+ private static MessageResources sysmsgs =
+ MessageResources.getMessageResources(
+ "org.apache.struts.validator.LocalStrings");
+
+ /**
+ * <p>Commons Logging instance.</p>
+ */
+ private static Log log = LogFactory.getLog(Resources.class);
+
+ /**
* Resources key the <code>ServletContext</code> is stored under.
*/
private static String SERVLET_CONTEXT_PARAM =
"javax.servlet.ServletContext";
@@ -126,6 +141,86 @@
}
+ /**
+ * Get the value of a variable.
+ * @param varName The variable name
+ * @param field the validator Field
+ * @param validator The Validator
+ * @param request the servlet request
+ * @param required Whether the variable is mandatory
+ * @return The variable's value
+ */
+ public static String getVarValue(String varName,
+ Field field,
+ Validator validator,
+ HttpServletRequest request,
+ boolean required) {
+
+ Var var = field.getVar(varName);
+ if (var == null) {
+
+ String msg = sysmsgs.getMessage("variable.missing", varName);
+ if (required) {
+ throw new IllegalArgumentException(msg);
+ }
+ if (log.isDebugEnabled()) {
+ log.debug(field.getProperty() + ": " + msg);
+ }
+ return null;
+ }
+
+ ServletContext application = (ServletContext)validator
+ .getParameterValue(SERVLET_CONTEXT_PARAM);
+ return getVarValue(var, application, request, required);
+ }
+
+ /**
+ * Get the value of a variable.
+ * @param var the validator variable
+ * @param application The ServletContext
+ * @param request the servlet request
+ * @param required Whether the variable is mandatory
+ * @return The variables values
+ */
+ public static String getVarValue(Var var,
+ ServletContext application,
+ HttpServletRequest request,
+ boolean required) {
+
+ String varName = var.getName();
+ String varValue = var.getValue();
+
+ // Non-resource variable
+ if (!var.isResource()) {
+ return varValue;
+ }
+
+ // Get the message resources
+ String bundle = var.getBundle();
+ MessageResources messages =
+ getMessageResources(application, request, bundle);
+
+ // Retrieve variable's value from message resources
+ Locale locale = RequestUtils.getUserLocale(request, null);
+ String value = messages.getMessage(locale, varValue, null);
+
+ // Not found in message resources
+ if (value == null && required) {
+ throw new IllegalArgumentException(
+ sysmsgs.getMessage("variable.resource.notfound",
+ varName, varValue, bundle));
+ }
+
+ if (log.isDebugEnabled()) {
+ log.debug("Var=[" + varName + "], " +
+ "bundle=[" + bundle + "], " +
+ "key=[" + varValue + "], " +
+ "value=[" + value + "]");
+ }
+
+ return value;
+
+ }
/**
* Gets the <code>Locale</code> sensitive value based on the key passed in.
Modified:
struts/core/trunk/src/java/org/apache/struts/validator/validwhen/ValidWhen.java
URL:
http://svn.apache.org/viewcvs/struts/core/trunk/src/java/org/apache/struts/validator/validwhen/ValidWhen.java?rev=289694&r1=289693&r2=289694&view=diff
==============================================================================
---
struts/core/trunk/src/java/org/apache/struts/validator/validwhen/ValidWhen.java
(original)
+++
struts/core/trunk/src/java/org/apache/struts/validator/validwhen/ValidWhen.java
Fri Sep 16 20:34:41 2005
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright 2003,2004 The Apache Software Foundation.
+ * Copyright 2003-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
import org.apache.struts.validator.Resources;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.struts.util.MessageResources;
/**
* This class contains the validwhen validation that is used in the
@@ -46,6 +47,13 @@
private static final Log log = LogFactory.getLog(ValidWhen.class);
/**
+ * The message resources for this package.
+ */
+ private static MessageResources sysmsgs =
+ MessageResources.getMessageResources(
+ "org.apache.struts.validator.LocalStrings");
+
+ /**
* Returns true if <code>obj</code> is null or a String.
*/
private static boolean isString(Object obj) {
@@ -103,11 +111,15 @@
value = ValidatorUtils.getValueAsString(bean, field.getProperty());
}
- String test = field.getVarValue("test");
- if (test == null) {
- String msg = "ValidWhen Error 'test' parameter is missing for
field ' " + field.getKey() + "'";
- errors.add(field.getKey(), new ActionMessage(msg, false));
- log.error(msg);
+ String test = null;
+ try {
+ test = Resources.getVarValue("test", field, validator, request,
true);
+ } catch(IllegalArgumentException ex) {
+ String logErrorMsg = sysmsgs.getMessage("validation.failed",
+ "validwhen", field.getProperty(), ex.toString());
+ log.error(logErrorMsg);
+ String userErrorMsg = sysmsgs.getMessage("system.error");
+ errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
return false;
}
@@ -116,10 +128,10 @@
try {
lexer = new ValidWhenLexer(new StringReader(test));
} catch (Exception ex) {
- String msg = "ValidWhenLexer Error for field ' " + field.getKey()
+ "' - " + ex;
- errors.add(field.getKey(), new ActionMessage(msg + " - " + ex,
false));
- log.error(msg);
- log.debug(msg, ex);
+ String logErrorMsg = "ValidWhenLexer Error for field ' " +
field.getKey() + "' - " + ex;
+ log.error(logErrorMsg);
+ String userErrorMsg = sysmsgs.getMessage("system.error");
+ errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
return false;
}
@@ -128,10 +140,10 @@
try {
parser = new ValidWhenParser(lexer);
} catch (Exception ex) {
- String msg = "ValidWhenParser Error for field ' " + field.getKey()
+ "' - " + ex;
- errors.add(field.getKey(), new ActionMessage(msg, false));
- log.error(msg);
- log.debug(msg, ex);
+ String logErrorMsg = "ValidWhenParser Error for field ' " +
field.getKey() + "' - " + ex;
+ log.error(logErrorMsg);
+ String userErrorMsg = sysmsgs.getMessage("system.error");
+ errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
return false;
}
@@ -146,14 +158,10 @@
} catch (Exception ex) {
- // errors.add(
- // field.getKey(),
- // Resources.getActionMessage(validator, request, va, field));
-
- String msg = "ValidWhen Error for field ' " + field.getKey() + "'
- " + ex;
- errors.add(field.getKey(), new ActionMessage(msg, false));
- log.error(msg);
- log.debug(msg, ex);
+ String logErrorMsg = "ValidWhen Error for field ' " +
field.getKey() + "' - " + ex;
+ log.error(logErrorMsg);
+ String userErrorMsg = sysmsgs.getMessage("system.error");
+ errors.add(field.getKey(), new ActionMessage(userErrorMsg, false));
return false;
}
Modified:
struts/taglib/trunk/src/java/org/apache/struts/taglib/html/JavascriptValidatorTag.java
URL:
http://svn.apache.org/viewcvs/struts/taglib/trunk/src/java/org/apache/struts/taglib/html/JavascriptValidatorTag.java?rev=289694&r1=289693&r2=289694&view=diff
==============================================================================
---
struts/taglib/trunk/src/java/org/apache/struts/taglib/html/JavascriptValidatorTag.java
(original)
+++
struts/taglib/trunk/src/java/org/apache/struts/taglib/html/JavascriptValidatorTag.java
Fri Sep 16 20:34:41 2005
@@ -1,7 +1,7 @@
/*
* $Id$
*
- * Copyright 2001-2004 The Apache Software Foundation.
+ * Copyright 2001-2005 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -503,7 +503,7 @@
while (varsIterator.hasNext()) {
String varName = (String) varsIterator.next();
Var var = (Var) vars.get(varName);
- String varValue = var.getValue();
+ String varValue = Resources.getVarValue(var, application,
request, false);
String jsType = var.getJsType();
// skip requiredif variables field, fieldIndexed,
fieldTest, fieldValue
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]