Class ValidatorAction
java.lang.Object
org.apache.commons.validator.ValidatorAction
- All Implemented Interfaces:
Serializable
Contains the information to dynamically create and run a validation
method. This is the class representation of a pluggable validator that can
be defined in an xml file with the <validator> element.
Note: The validation method is assumed to be thread safe.
- Version:
- $Revision: 1713331 $
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringThe full class name of the class containing the validation method associated with this action.An internal List representation of the otherValidatorActions this one depends on (if any).private StringThe otherValidatorActions that this one depends on.private ObjectIf the java method matching the correct signature isn't static, the instance is stored in the action.private StringAn optional field to containing a JavaScript representation of the java method assocated with this action.private StringAn optional field to contain the class path to be used to retrieve the JavaScript function.private StringAn optional field to contain the name to be used if JavaScript is generated.private org.apache.commons.logging.LogLogger.private StringThe full method name of the validation to be performed.An internal List representation of all the validation method's parameters defined in the methodParams String.private StringThe method signature of the validation method.private StringThe default error message associated with this action.private StringThe name of the validation.private Class<?>[]The Class objects for each entry in methodParameterList.private static final longprivate Class<?> The Class object loaded from the classname.private MethodThe Method object loaded from the method name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) booleanexecuteValidationMethod(Field field, Map<String, Object> params, ValidatorResults results, int pos) Dynamically runs the validation method for this validator and returns true if the data is valid.private Stringprivate StringUsed to generate the javascript name when it is not specified.private ClassLoadergetClassLoader(Map<String, Object> params) Returns the ClassLoader set in the Validator contained in the parameter Map.Gets the class of the validator action.Returns the dependent validator names as an unmodifiableList.Gets the dependencies of the validator action as a comma separated list of validator names.Gets the Javascript equivalent of the java class and method associated with this action.Gets the Javascript function name.private org.apache.commons.logging.LoggetLog()Accessor method for Log instance.Gets the name of method being called for the validator action.Gets the method parameters for the method.getMsg()Gets the message associated with the validator action.getName()Gets the name of the validator action.private Object[]getParameterValues(Map<String, ? super Object> params) Converts a List of parameter class names into their values contained in the parameters Map.private ObjectReturn an instance of the validation class or null if the validation method is static so does not require an instance to be executed.private voidhandleIndexedField(Field field, int pos, Object[] paramValues) Modifies the paramValue array with indexed fields.protected voidinit()Initialize based on set.booleanisDependency(String validatorName) Checks whether or not the value passed in is in the depends field.private booleanIf the result object is aBoolean, it will return its value.private booleanprotected voidLoad the javascript function specified by the given path.private voidloadParameterClasses(ClassLoader loader) Converts a List of parameter class names into their Class objects.private voidloadValidationClass(ClassLoader loader) Load the Class object for the configured validation class name.private voidLoad the Method object for the configured validation method name.private booleanonlyReturnErrors(Map<String, Object> params) Returns the onlyReturnErrors setting in the Validator contained in the parameter Map.private StringreadJavascriptFile(String javascriptFileName) Read a javascript function from a file.voidsetClassname(String classname) Sets the class of the validator action.voidsetDepends(String depends) Sets the dependencies of the validator action.voidsetJavascript(String javascript) Sets the Javascript equivalent of the java class and method associated with this action.voidsetJsFunction(String jsFunction) Sets the fully qualified class path of the Javascript function.voidsetJsFunctionName(String jsFunctionName) Sets the Javascript function name.voidSets the name of method being called for the validator action.voidsetMethodParams(String methodParams) Sets the method parameters for the method.voidSets the message associated with the validator action.voidSets the name of the validator action.toString()Returns a string representation of the object.
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
log
private transient org.apache.commons.logging.Log logLogger. -
name
The name of the validation. -
classname
The full class name of the class containing the validation method associated with this action. -
validationClass
The Class object loaded from the classname. -
method
The full method name of the validation to be performed. The method must be thread safe. -
validationMethod
The Method object loaded from the method name. -
methodParams
The method signature of the validation method. This should be a comma delimited list of the full class names of each parameter in the correct order that the method takes.
Note:
java.lang.Objectis reserved for the JavaBean that is being validated. TheValidatorActionandFieldthat are associated with a field's validation will automatically be populated if they are specified in the method signature. -
parameterClasses
The Class objects for each entry in methodParameterList. -
depends
The otherValidatorActions that this one depends on. If any errors occur in an action that this one depends on, this action will not be processsed. -
msg
The default error message associated with this action. -
jsFunctionName
An optional field to contain the name to be used if JavaScript is generated. -
jsFunction
An optional field to contain the class path to be used to retrieve the JavaScript function. -
javascript
An optional field to containing a JavaScript representation of the java method assocated with this action. -
instance
If the java method matching the correct signature isn't static, the instance is stored in the action. This assumes the method is thread safe. -
dependencyList
An internal List representation of the otherValidatorActions this one depends on (if any). This List gets updated whenever setDepends() gets called. This is synchronized so a call to setDepends() (which clears the List) won't interfere with a call to isDependency(). -
methodParameterList
-
-
Constructor Details
-
ValidatorAction
public ValidatorAction()
-
-
Method Details
-
getName
-
setName
Sets the name of the validator action.- Parameters:
name- Validator Action name.
-
getClassname
Gets the class of the validator action.- Returns:
- Class name of the validator Action.
-
setClassname
Sets the class of the validator action.- Parameters:
classname- Class name of the validator Action.
-
getMethod
Gets the name of method being called for the validator action.- Returns:
- The method name.
-
setMethod
Sets the name of method being called for the validator action.- Parameters:
method- The method name.
-
getMethodParams
Gets the method parameters for the method.- Returns:
- Method's parameters.
-
setMethodParams
Sets the method parameters for the method.- Parameters:
methodParams- A comma separated list of parameters.
-
getDepends
Gets the dependencies of the validator action as a comma separated list of validator names.- Returns:
- The validator action's dependencies.
-
setDepends
Sets the dependencies of the validator action.- Parameters:
depends- A comma separated list of validator names.
-
getMsg
Gets the message associated with the validator action.- Returns:
- The message for the validator action.
-
setMsg
Sets the message associated with the validator action.- Parameters:
msg- The message for the validator action.
-
getJsFunctionName
Gets the Javascript function name. This is optional and can be used instead of validator action name for the name of the Javascript function/object.- Returns:
- The Javascript function name.
-
setJsFunctionName
Sets the Javascript function name. This is optional and can be used instead of validator action name for the name of the Javascript function/object.- Parameters:
jsFunctionName- The Javascript function name.
-
setJsFunction
Sets the fully qualified class path of the Javascript function.This is optional and can be used instead of the setJavascript(). Attempting to call both
setJsFunctionandsetJavascriptwill result in anIllegalStateExceptionbeing thrown.If neither setJsFunction or setJavascript is set then validator will attempt to load the default javascript definition.
Examples If in the validator.xml : #1: <validator name="tire" jsFunction="com.yourcompany.project.tireFuncion"> Validator will attempt to load com.yourcompany.project.validateTireFunction.js from its class path. #2: <validator name="tire"> Validator will use the name attribute to try and load org.apache.commons.validator.javascript.validateTire.js which is the default javascript definition.- Parameters:
jsFunction- The Javascript function's fully qualified class path.
-
getJavascript
Gets the Javascript equivalent of the java class and method associated with this action.- Returns:
- The Javascript validation.
-
setJavascript
Sets the Javascript equivalent of the java class and method associated with this action.- Parameters:
javascript- The Javascript validation.
-
init
protected void init()Initialize based on set. -
loadJavascriptFunction
protected void loadJavascriptFunction()Load the javascript function specified by the given path. For this implementation, thejsFunctionproperty should contain a fully qualified package and script name, separated by periods, to be loaded from the class loader that created this instance. TODO if the path begins with a '/' the path will be intepreted as absolute, and remain unchanged. If this fails then it will attempt to treat the path as a file path. It is assumed the script ends with a '.js'. -
readJavascriptFile
-
formatJavascriptFileName
- Returns:
- A filename suitable for passing to a ClassLoader.getResourceAsStream() method.
-
javascriptAlreadyLoaded
private boolean javascriptAlreadyLoaded()- Returns:
- true if the javascript for this action has already been loaded.
-
generateJsFunction
Used to generate the javascript name when it is not specified. -
isDependency
Checks whether or not the value passed in is in the depends field.- Parameters:
validatorName- Name of the dependency to check.- Returns:
- Whether the named validator is a dependant.
-
getDependencyList
-
toString
-
executeValidationMethod
boolean executeValidationMethod(Field field, Map<String, Object> params, ValidatorResults results, int pos) throws ValidatorExceptionDynamically runs the validation method for this validator and returns true if the data is valid.- Parameters:
field-params- A Map of class names to parameter values.results-pos- The index of the list property to validate if it's indexed.- Throws:
ValidatorException
-
loadValidationMethod
Load the Method object for the configured validation method name.- Throws:
ValidatorException
-
loadValidationClass
Load the Class object for the configured validation class name.- Parameters:
loader- The ClassLoader used to load the Class object.- Throws:
ValidatorException
-
loadParameterClasses
Converts a List of parameter class names into their Class objects. Stores the output inparameterClasses. This array is in the same order as the given List and is suitable for passing to the validation method.- Throws:
ValidatorException- if a class cannot be loaded.
-
getParameterValues
Converts a List of parameter class names into their values contained in the parameters Map.- Parameters:
params- A Map of class names to parameter values.- Returns:
- An array containing the value object for each parameter. This array is in the same order as the given List and is suitable for passing to the validation method.
-
getValidationClassInstance
Return an instance of the validation class or null if the validation method is static so does not require an instance to be executed.- Throws:
ValidatorException
-
handleIndexedField
private void handleIndexedField(Field field, int pos, Object[] paramValues) throws ValidatorException Modifies the paramValue array with indexed fields.- Parameters:
field-pos-paramValues-- Throws:
ValidatorException
-
isValid
If the result object is aBoolean, it will return its value. If not it will returnfalseif the object isnullandtrueif it isn't. -
getClassLoader
Returns the ClassLoader set in the Validator contained in the parameter Map. -
onlyReturnErrors
-
getLog
private org.apache.commons.logging.Log getLog()Accessor method for Log instance. The Log instance variable is transient and accessing it through this method ensures it is re-initialized when this instance is de-serialized.- Returns:
- The Log instance.
-