rleland 2002/10/17 20:41:10
Modified: conf/share validator-rules.xml
src/share/org/apache/struts/util StrutsValidator.java
Added: src/share/org/apache/struts/validator FieldChecks.java
Resources.java
Log:
Commit the renamed classes
StrutsValidator->FieldCheck
StrutsValidatorUtil-> Resources
Revision Changes Path
1.12 +17 -17 jakarta-struts/conf/share/validator-rules.xml
Index: validator-rules.xml
===================================================================
RCS file: /home/cvs/jakarta-struts/conf/share/validator-rules.xml,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- validator-rules.xml 18 Oct 2002 01:35:02 -0000 1.11
+++ validator-rules.xml 18 Oct 2002 03:41:10 -0000 1.12
@@ -41,7 +41,7 @@
<global>
<validator name="required"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateRequired"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -82,7 +82,7 @@
</validator>
<validator name="requiredif"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateRequiredIf"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -94,7 +94,7 @@
</validator>
<validator name="minlength"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateMinLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -136,7 +136,7 @@
<validator name="maxlength"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateMaxLength"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -178,7 +178,7 @@
<validator name="mask"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateMask"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -229,7 +229,7 @@
<validator name="byte"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateByte"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -274,7 +274,7 @@
<validator name="short"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateShort"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -320,7 +320,7 @@
<validator name="integer"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateInteger"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -366,7 +366,7 @@
<validator name="long"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateLong"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -378,7 +378,7 @@
<validator name="float"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateFloat"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -424,7 +424,7 @@
<validator name="double"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateDouble"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -436,7 +436,7 @@
<validator name="date"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateDate"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -599,7 +599,7 @@
<!-- range is deprecated use rangeInt instead -->
<validator name="range"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateIntRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -618,7 +618,7 @@
</validator>
<validator name="intRange"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateIntRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -662,7 +662,7 @@
</validator>
<validator name="floatRange"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateFloatRange"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -706,7 +706,7 @@
</validator>
<validator name="creditCard"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateCreditCard"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
@@ -784,7 +784,7 @@
<validator name="email"
- classname="org.apache.struts.validator.ValidatorChecks"
+ classname="org.apache.struts.validator.FieldChecks"
method="validateEmail"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
1.13 +29 -29
jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java
Index: StrutsValidator.java
===================================================================
RCS file:
/home/cvs/jakarta-struts/src/share/org/apache/struts/util/StrutsValidator.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- StrutsValidator.java 18 Oct 2002 01:35:02 -0000 1.12
+++ StrutsValidator.java 18 Oct 2002 03:41:10 -0000 1.13
@@ -72,7 +72,7 @@
* In general passin in a null or blank will return a null Object or a false
* boolean. However, nulls and blanks do not result in an error being added to the
* errors.
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks }
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks }
*
*@author David Winterfeldt
*@author James Turner
@@ -94,7 +94,7 @@
*
* Checks if the field isn't null and length of the field is greater than zero
* not including whitespace.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateRequired(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateRequired(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -110,7 +110,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateRequired(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateRequired(bean,va,field,errors,request);
}
@@ -120,7 +120,7 @@
*
* Checks if the field matches the regular expression in the field's mask
attribute.
* </p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateMask(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateMask(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -136,7 +136,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateMask(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateMask(bean,va,field,errors,request);
}
@@ -144,7 +144,7 @@
* <p>
*
* Checks if the field can safely be converted to a byte primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateByte(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateByte(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -160,7 +160,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateByte(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateByte(bean,va,field,errors,request);
}
@@ -168,7 +168,7 @@
* <p>
*
* Checks if the field can safely be converted to a short primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateShort(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateShort(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -183,7 +183,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateShort(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateShort(bean,va,field,errors,request);
}
@@ -191,7 +191,7 @@
* <p>
*
* Checks if the field can safely be converted to an int primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateInteger(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateInteger(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -206,7 +206,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateInteger(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateInteger(bean,va,field,errors,request);
}
@@ -214,7 +214,7 @@
* <p>
*
* Checks if the field can safely be converted to a long primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateLong(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateLong(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -229,7 +229,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateLong(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateLong(bean,va,field,errors,request);
}
@@ -237,7 +237,7 @@
* <p>
*
* Checks if the field can safely be converted to a float primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateFloat(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateFloat(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -252,7 +252,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateFloat(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateFloat(bean,va,field,errors,request);
}
@@ -260,7 +260,7 @@
* <p>
*
* Checks if the field can safely be converted to a double primitive.</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateDouble(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateDouble(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -275,7 +275,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateDouble(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateDouble(bean,va,field,errors,request);
}
@@ -290,7 +290,7 @@
* variable is specified, then the field gets the DateFormat.SHORT format for
* the locale. The setLenient method is set to <code>false</code> for all
variations.
* </p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateDate(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateDate(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
@@ -306,7 +306,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateDate(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateDate(bean,va,field,errors,request);
}
/**
@@ -314,7 +314,7 @@
*
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).</p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateIntRange(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateIntRange(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
@@ -328,7 +328,7 @@
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
- return org.apache.struts.validator.ValidatorChecks.validateIntRange(bean,
va, field, errors, request);
+ return org.apache.struts.validator.FieldChecks.validateIntRange(bean, va,
field, errors, request);
}
@@ -336,7 +336,7 @@
* <p>
*
* Checks if the field is a valid credit card number.</p> <p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateCreditCard(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateCreditCard(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
* Translated to Java by Ted Husted (<a
href="mailto:husted@;apache.org">[EMAIL PROTECTED]
* </a>).<br>
@@ -357,7 +357,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateCreditCard(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateCreditCard(bean,va,field,errors,request);
}
@@ -365,7 +365,7 @@
* <p>
*
* Checks if a field has a valid e-mail address.</p> <p>
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateEmail(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateEmail(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*
* Based on a script by Sandeep V. Tamhankar ([EMAIL PROTECTED]),
http://javascript.internet.com
* </p>
@@ -384,7 +384,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateEmail(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateEmail(bean,va,field,errors,request);
}
@@ -394,7 +394,7 @@
* Checks if the field's length is less than or equal to the maximum value.
* A <code>Null</code> will be considered an error.</p>
*
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateMaxLength(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateMaxLength(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
@@ -409,7 +409,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateMaxLength(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateMaxLength(bean,va,field,errors,request);
}
@@ -419,7 +419,7 @@
* Checks if the field's length is greater than or equal to the minimum value.
* A <code>Null</code> will be considered an error.</p>
*
- *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.ValidatorChecks#validateMinLength(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
+ *@deprecated As of Struts 1.1b3, replaced by {@link
org.apache.struts.validator.FieldChecks#validateMinLength(Object,ValidatorAction,Field,ActionErrors,HttpServletRequest)}
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
@@ -434,7 +434,7 @@
ActionErrors errors,
HttpServletRequest request) {
- return
org.apache.struts.validator.ValidatorChecks.validateMinLength(bean,va,field,errors,request);
+ return
org.apache.struts.validator.FieldChecks.validateMinLength(bean,va,field,errors,request);
}
}
1.1
jakarta-struts/src/share/org/apache/struts/validator/FieldChecks.java
Index: FieldChecks.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.struts.validator;
import java.io.Serializable;
import java.util.Date;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.GenericTypeValidator;
import org.apache.commons.validator.GenericValidator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorUtil;
import org.apache.struts.action.ActionErrors;
/**
* <p>
*
* This class contains the default validations that are used in the
validator-rules.xml
* file.</p>
*
* In general passing in a null or blank will return a null Object or a false
* boolean. However, nulls and blanks do not result in an error being added to the
* errors.
*
*@author David Winterfeldt
*@author James Turner
*@author Rob Leland
*@since Struts 1.1
*/
public class FieldChecks implements Serializable {
/**
* Commons Logging instance.
*/
private static Log LOG = LogFactory.getLog(FieldChecks.class);
public final static String FIELD_TEST_NULL = "NULL";
public final static String FIELD_TEST_NOTNULL = "NOTNULL";
public final static String FIELD_TEST_EQUAL = "EQUAL";
/**
* <p>
*
* Checks if the field isn't null and length of the field is greater than zero
* not including whitespace.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if meets stated requirements, False otherwise
*/
public static boolean validateRequired(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (GenericValidator.isBlankOrNull(value)) {
errors.add(field.getKey(),
Resources.getActionError(request, va, field));
return false;
} else {
return true;
}
}
/**
* <p>
*
* Checks if the field isn't null based on the values of other fields
* </p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param validator The <code>Validator</code> instance, used to access other
field values.
*@param request Current request object.
*@return True if meets stated requirements, False otherwise
*/
public static boolean validateRequiredIf(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
org.apache.commons.validator.Validator
validator,
HttpServletRequest request) {
Object form =
validator.getResource(org.apache.commons.validator.Validator.BEAN_KEY);
String value = null;
boolean required = false;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
int i = 0;
String fieldJoin = "AND";
if (!GenericValidator.isBlankOrNull(field.getVarValue("field-join"))) {
fieldJoin = field.getVarValue("field-join");
}
if (fieldJoin.equalsIgnoreCase("AND")) {
required = true;
}
while (!GenericValidator.isBlankOrNull(field.getVarValue("field[" + i +
"]"))) {
String dependProp = field.getVarValue("field[" + i + "]");
String dependTest = field.getVarValue("field-test[" + i + "]");
String dependTestValue = field.getVarValue("field-value[" + i + "]");
String dependIndexed = field.getVarValue("field-indexed[" + i + "]");
if (dependIndexed == null)
dependIndexed = "false";
String dependVal = null;
boolean this_required = false;
if (field.isIndexed() && dependIndexed.equalsIgnoreCase("true")) {
String key = field.getKey();
if ((key.indexOf("[") > -1) &&
(key.indexOf("]") > -1)) {
String ind = key.substring(0, key.indexOf(".") + 1);
dependProp = ind + dependProp;
}
}
dependVal = ValidatorUtil.getValueAsString(form, dependProp);
if (dependTest.equals(FIELD_TEST_NULL)) {
if ((dependVal != null) && (dependVal.length() > 0)) {
this_required = false;
} else {
this_required = true;
}
}
if (dependTest.equals(FIELD_TEST_NOTNULL)) {
if ((dependVal != null) && (dependVal.length() > 0)) {
this_required = true;
} else {
this_required = false;
}
}
if (dependTest.equals(FIELD_TEST_EQUAL)) {
this_required = dependTestValue.equalsIgnoreCase(dependVal);
}
if (fieldJoin.equalsIgnoreCase("AND")) {
required = required && this_required;
} else {
required = required || this_required;
}
i++;
}
if (required) {
if ((value != null) && (value.length() > 0)) {
return true;
} else {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Checks if the field matches the regular expression in the field's mask
attribute.
* </p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if field matches mask, false otherwise.
*/
public static boolean validateMask(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String mask = field.getVarValue("mask");
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean,
field.getProperty());
}
try {
if (!GenericValidator.isBlankOrNull(value) &&
!GenericValidator.matchRegexp(value, mask)) {
errors.add(field.getKey(),
Resources.getActionError(request, va,
field));
return false;
} else {
return true;
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
return true;
}
/**
* <p>
*
* Checks if the field can safely be converted to a byte primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Byte if valid, a null otherwise.
*/
public static Byte validateByte(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Byte result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatByte(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field can safely be converted to a short primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Short if valid, otherwise a null.
*/
public static Short validateShort(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Short result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatShort(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field can safely be converted to an int primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return An Integer if valid, a null otherwise.
*/
public static Integer validateInteger(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Integer result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatInt(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field can safely be converted to a long primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Long if valid, a null otherwise.
*/
public static Long validateLong(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Long result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatLong(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field can safely be converted to a float primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Float if valid, a null otherwise.
*/
public static Float validateFloat(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Float result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatFloat(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field can safely be converted to a double primitive.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Double if valid, a null otherwise.
*/
public static Double validateDouble(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Double result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatDouble(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if the field is a valid date. If the field has a datePattern variable,
* that will be used to format <code>java.text.SimpleDateFormat</code>. If the
* field has a datePatternStrict variable, that will be used to format
<code>java.text.SimpleDateFormat</code>
* and the length will be checked so '2/12/1999' will not pass validation with
* the format 'MM/dd/yyyy' because the month isn't two digits. If no datePattern
* variable is specified, then the field gets the DateFormat.SHORT format for
* the locale. The setLenient method is set to <code>false</code> for all
variations.
* </p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return A Date if valid, a null if blank or invalid.
*/
public static Date validateDate(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Date result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String datePattern = field.getVarValue("datePattern");
String datePatternStrict = field.getVarValue("datePatternStrict");
Locale locale = Resources.getLocale(request);
if (!GenericValidator.isBlankOrNull(value)) {
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 {
result = GenericTypeValidator.formatDate(value, locale);
}
} catch (Exception e) {
LOG.error(e.getMessage(), e);
}
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).</p>
*@deprecated As of Struts 1.1b3, replaced by {@link
#validateIntRange(java.lang.Object,org.apache.commons.validator.ValidatorAction,org.apache.commons.validator.Field,org.apache.struts.action.ActionErrors,javax.servlet.http.HttpServletRequest)}
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if in range, false otherwise.
*/
public static boolean validateRange(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
return validateIntRange(bean, va, field, errors, request);
}
/**
* <p>
*
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if in range, false otherwise.
*/
public static boolean validateIntRange(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String sMin = field.getVarValue("min");
String sMax = field.getVarValue("max");
if (!GenericValidator.isBlankOrNull(value)) {
try {
int iValue = Integer.parseInt(value);
int min = Integer.parseInt(sMin);
int max = Integer.parseInt(sMax);
if (!GenericValidator.isInRange(iValue, min, max)) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if in range, false otherwise.
*/
public static boolean validateDoubleRange(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String sMin = field.getVarValue("min");
String sMax = field.getVarValue("max");
if (!GenericValidator.isBlankOrNull(value)) {
try {
double dValue = Double.parseDouble(value);
double min = Double.parseDouble(sMin);
double max = Double.parseDouble(sMax);
if (!GenericValidator.isInRange(dValue, min, max)) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Checks if a fields value is within a range (min & max specified in the
* vars attribute).</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if in range, false otherwise.
*/
public static boolean validateFloatRange(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String sMin = field.getVarValue("min");
String sMax = field.getVarValue("max");
if (!GenericValidator.isBlankOrNull(value)) {
try {
float fValue = Float.parseFloat(value);
float min = Float.parseFloat(sMin);
float max = Float.parseFloat(sMax);
if (!GenericValidator.isInRange(fValue, min, max)) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Checks if the field is a valid credit card number.</p> <p>
*
* Translated to Java by Ted Husted (<a
href="mailto:husted@;apache.org">[EMAIL PROTECTED]
* </a>).<br>
* Reference Sean M. Burke's script at
http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
* </p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return The credit card as a Long, a null if invalid, blank, or null.
*/
public static Long validateCreditCard(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
Long result = null;
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value)) {
result = GenericTypeValidator.formatCreditCard(value);
if (result == null) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
}
}
return result;
}
/**
* <p>
*
* Checks if a field has a valid e-mail address.</p> <p>
*
* Based on a script by Sandeep V. Tamhankar ([EMAIL PROTECTED]),
http://javascript.internet.com
* </p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if valid, false otherwise.
*/
public static boolean validateEmail(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
if (!GenericValidator.isBlankOrNull(value) &&
!GenericValidator.isEmail(value)) {
errors.add(field.getKey(), Resources.getActionError(request, va, field));
return false;
} else {
return true;
}
}
/**
* <p>
*
* Checks if the field's length is less than or equal to the maximum value.
* A <code>Null</code> will be considered an error.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if stated conditions met.
*/
public static boolean validateMaxLength(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String sMaxLength = field.getVarValue("maxlength");
if (value != null) {
try {
int max = Integer.parseInt(sMaxLength);
if (!GenericValidator.maxLength(value, max)) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Checks if the field's length is greater than or equal to the minimum value.
* A <code>Null</code> will be considered an error.</p>
*
*@param bean The bean validation is being performed on.
*@param va The <code>ValidatorAction</code> that is currently being
performed.
*@param field The <code>Field</code> object associated with the current
* field being validated.
*@param errors The <code>ActionErrors</code> object to add errors to if any
* validation errors occur.
*@param request Current request object.
*@return True if stated conditions met.
*/
public static boolean validateMinLength(Object bean,
ValidatorAction va, Field field,
ActionErrors errors,
HttpServletRequest request) {
String value = null;
if (isString(bean)) {
value = (String) bean;
} else {
value = ValidatorUtil.getValueAsString(bean, field.getProperty());
}
String sMinLength = field.getVarValue("minlength");
if (value != null) {
try {
int min = Integer.parseInt(sMinLength);
if (!GenericValidator.minLength(value, min)) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
} catch (Exception e) {
errors.add(field.getKey(), Resources.getActionError(request, va,
field));
return false;
}
}
return true;
}
/**
* <p>
*
* Return <code>true</code> if the specified object is a String or a
<code>null</code>
* value.</p>
*
*@param o Object to be tested
*@return The string value
*/
private static boolean isString(Object o) {
if (o == null) {
return (true);
}
return (String.class.isInstance(o));
}
}
1.1
jakarta-struts/src/share/org/apache/struts/validator/Resources.java
Index: Resources.java
===================================================================
/*
* $Header:
/home/cvs/jakarta-struts/src/share/org/apache/struts/validator/Resources.java,v 1.1
2002/10/18 03:41:10 rleland Exp $
* $Revision: 1.1 $
* $Date: 2002/10/18 03:41:10 $
*
* ====================================================================
*
* The Apache Software License, Version 1.1
*
* Copyright (c) 1999 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if
* any, must include the following acknowlegement:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "The Jakarta Project", "Struts", and "Apache Software
* Foundation" must not be used to endorse or promote products derived
* from this software without prior written permission. For written
* permission, please contact [EMAIL PROTECTED]
*
* 5. Products derived from this software may not be called "Apache"
* nor may "Apache" appear in their names without prior written
* permission of the Apache Group.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.struts.validator;
import java.util.Locale;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.util.MessageResources;
import org.apache.commons.validator.Arg;
import org.apache.commons.validator.Field;
import org.apache.commons.validator.Validator;
import org.apache.commons.validator.ValidatorAction;
import org.apache.commons.validator.ValidatorResources;
import org.apache.struts.validator.ValidatorPlugIn;
import org.apache.struts.config.ApplicationConfig;
import org.apache.struts.Globals;
/**
* <p>This class helps provides some useful methods for retrieving objects
* from different scopes of the application.</p>
*
* @author David Winterfeldt
* @author Eddie Bush
* @version $Revision: 1.1 $ $Date: 2002/10/18 03:41:10 $
* @since Struts 1.1
*/
public class Resources {
/**
* Resources key the <code>ServletContext</code> is stored under.
*/
public static String SERVLET_CONTEXT_KEY = "javax.servlet.ServletContext";
/**
* Resources key the <code>HttpServletRequest</code> is stored under.
*/
public static String HTTP_SERVLET_REQUEST_KEY =
"javax.servlet.http.HttpServletRequest";
/**
* Resources key the <code>ActionErrors</code> is stored under.
*/
public static String ACTION_ERRORS_KEY = "org.apache.struts.action.ActionErrors";
private static Locale defaultLocale = Locale.getDefault();
/**
* Retrieve <code>ValidatorResources</code> for the module.
*
* @deprecated In Struts 1.1b3 This method can only return the resources for the
default
* module. Use getValidatorResources(HttpServletRequest, ServletContext)
* to get the resources for the current application module.
*/
public static ValidatorResources getValidatorResources(ServletContext
application) {
return
(ValidatorResources)application.getAttribute(ValidatorPlugIn.VALIDATOR_KEY);
}
/**
* Retrieve <code>ValidatorResources</code> for the current module.
*/
public static ValidatorResources getValidatorResources(ServletContext
application,HttpServletRequest request) {
return (ValidatorResources)
application.getAttribute(ValidatorPlugIn.VALIDATOR_KEY +
((ApplicationConfig)request.getAttribute(Globals.APPLICATION_KEY)).getPrefix());
}
/**
* Retrieve <code>MessageResources</code> for the application module.
*
* @deprecated This method can only return the resources for the default
* module. Use getMessageResources(HttpServletRequest) to get the
* resources for the current application module.
*/
public static MessageResources getMessageResources(ServletContext application) {
return (MessageResources)application.getAttribute(Action.MESSAGES_KEY);
}
/**
* Retrieve <code>MessageResources</code> for the application module.
*/
public static MessageResources getMessageResources(HttpServletRequest request) {
return (MessageResources)request.getAttribute(Action.MESSAGES_KEY);
}
/**
* Get the <code>Locale</code> of the current user.
*/
public static Locale getLocale(HttpServletRequest request) {
Locale locale = null;
try {
locale = (Locale) request.getSession().getAttribute(Action.LOCALE_KEY);
} catch (IllegalStateException e) { // Invalidated session
locale = null;
}
if (locale == null) {
locale = defaultLocale;
}
return locale;
}
/**
* Gets the <code>Locale</code> sensitive value based on the key passed in.
*/
public static String getMessage(MessageResources messages, Locale locale, String
key) {
String message = null;
if (messages != null) {
message = messages.getMessage(locale, key);
}
if (message == null) {
message = "";
}
return message;
}
/**
* Gets the <code>Locale</code> sensitive value based on the key passed in.
*/
public static String getMessage(HttpServletRequest request, String key) {
MessageResources messages = getMessageResources(request);
return getMessage(messages, getLocale(request), key);
}
/**
* Gets the locale sensitive message based on the <code>ValidatorAction</code>
message and the
* <code>Field</code>'s arg objects.
*/
public static String getMessage(MessageResources messages, Locale locale,
ValidatorAction va, Field field) {
String arg[] = getArgs(va.getName(), messages, locale, field);
String msg = (field.getMsg(va.getName()) != null ? field.getMsg(va.getName())
: va.getMsg());
return messages.getMessage(locale, msg, arg[0], arg[1], arg[2], arg[3]);
}
/**
* Gets the <code>ActionError</code> based on the <code>ValidatorAction</code>
message and the
* <code>Field</code>'s arg objects.
*/
public static ActionError getActionError(HttpServletRequest request,
ValidatorAction va, Field field) {
String arg[] = getArgs(va.getName(), getMessageResources(request),
getLocale(request), field);
String msg = (field.getMsg(va.getName()) != null ? field.getMsg(va.getName())
: va.getMsg());
return new ActionError(msg, arg[0], arg[1], arg[2], arg[3]);
}
/**
* Gets the message arguments based on the current <code>ValidatorAction</code>
* and <code>Field</code>.
*/
public static String[] getArgs(String actionName, MessageResources messages,
Locale locale, Field field) {
Arg arg0 = field.getArg0(actionName);
Arg arg1 = field.getArg1(actionName);
Arg arg2 = field.getArg2(actionName);
Arg arg3 = field.getArg3(actionName);
String sArg0 = null;
String sArg1 = null;
String sArg2 = null;
String sArg3 = null;
if (arg0 != null) {
if (arg0.getResource()) {
sArg0 = getMessage(messages, locale, arg0.getKey());
} else {
sArg0 = arg0.getKey();
}
}
if (arg1 != null) {
if (arg1.getResource()) {
sArg1 = getMessage(messages, locale, arg1.getKey());
} else {
sArg1 = arg1.getKey();
}
}
if (arg2 != null) {
if (arg2.getResource()) {
sArg2 = getMessage(messages, locale, arg2.getKey());
} else {
sArg2 = arg2.getKey();
}
}
if (arg3 != null) {
if (arg3.getResource()) {
sArg3 = getMessage(messages, locale, arg3.getKey());
} else {
sArg3 = arg3.getKey();
}
}
return new String[] { sArg0, sArg1, sArg2, sArg3 };
}
/**
* Initialize the <code>Validator</code> to perform validation.
*
* @param key The key that the validation rules are under
* (the form elements name attribute).
* @param request The current request object.
* @param errors The object any errors will be stored in.
*/
public static Validator initValidator(String key, Object bean,
ServletContext application,
HttpServletRequest request,
ActionErrors errors, int page) {
ValidatorResources resources =
Resources.getValidatorResources(application,request);
Locale locale = Resources.getLocale(request);
Validator validator = new Validator(resources, key);
validator.setUseContextClassLoader(true);
validator.setPage(page);
validator.addResource(SERVLET_CONTEXT_KEY, application);
validator.addResource(HTTP_SERVLET_REQUEST_KEY, request);
validator.addResource(Validator.LOCALE_KEY, locale);
validator.addResource(ACTION_ERRORS_KEY, errors);
validator.addResource(Validator.BEAN_KEY, bean);
return validator;
}
}
--
To unsubscribe, e-mail: <mailto:struts-dev-unsubscribe@;jakarta.apache.org>
For additional commands, e-mail: <mailto:struts-dev-help@;jakarta.apache.org>