org.apache.commons.validator

Class ValidatorAction

public class ValidatorAction extends Object implements 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.
Field Summary
Stringclassname
The full class name of the class containing the validation method associated with this action.
ListdependencyList
An internal List representation of the other ValidatorActions this one depends on (if any).
Stringdepends
The other ValidatorActions that this one depends on.
Objectinstance
If the java method matching the correct signature isn't static, the instance is stored in the action.
Stringjavascript
An optional field to containing a JavaScript representation of the java method assocated with this action.
StringjsFunction
An optional field to contain the class path to be used to retrieve the JavaScript function.
StringjsFunctionName
An optional field to contain the name to be used if JavaScript is generated.
static Loglog
Logger.
Stringmethod
The full method name of the validation to be performed.
ListmethodParameterList
An internal List representation of all the validation method's parameters defined in the methodParams String.
StringmethodParams

The method signature of the validation method.

Stringmsg
The default error message associated with this action.
Stringname
The name of the validation.
Class[]parameterClasses
The Class objects for each entry in methodParameterList.
ClassvalidationClass
The Class object loaded from the classname.
MethodvalidationMethod
The Method object loaded from the method name.
Method Summary
booleanexecuteValidationMethod(Field field, Map params, ValidatorResults results, int pos)
Dynamically runs the validation method for this validator and returns true if the data is valid.
StringformatJavascriptFileName()
StringgenerateJsFunction()
Used to generate the javascript name when it is not specified.
ClassLoadergetClassLoader(Map params)
Returns the ClassLoader set in the Validator contained in the parameter Map.
StringgetClassname()
Gets the class of the validator action.
ObjectgetClassnameInstance()
Gets an instance based on the validator action's classname.
CollectiongetDependencies()
Gets the dependencies as a Collection.
ListgetDependencyList()
Returns the dependent validator names as an unmodifiable List.
StringgetDepends()
Gets the dependencies of the validator action as a comma separated list of validator names.
StringgetJavascript()
Gets the Javascript equivalent of the java class and method associated with this action.
StringgetJsFunctionName()
Gets the Javascript function name.
StringgetMethod()
Gets the name of method being called for the validator action.
StringgetMethodParams()
Gets the method parameters for the method.
ListgetMethodParamsList()
Gets the method parameters for the method as an unmodifiable List.
StringgetMsg()
Gets the message associated with the validator action.
StringgetName()
Gets the name of the validator action.
Object[]getParameterValues(Map params)
Converts a List of parameter class names into their values contained in the parameters Map.
ObjectgetValidationClassInstance()
Return an instance of the validation class or null if the validation method is static so does not require an instance to be executed.
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.
booleanisValid(Object result)
If the result object is a Boolean, it will return its value.
booleanjavascriptAlreadyLoaded()
protected voidloadJavascriptFunction()
Load the javascript function specified by the given path.
voidloadParameterClasses(ClassLoader loader)
Converts a List of parameter class names into their Class objects.
voidloadValidationClass(ClassLoader loader)
Load the Class object for the configured validation class name.
voidloadValidationMethod()
Load the Method object for the configured validation method name.
booleanonlyReturnErrors(Map params)
Returns the onlyReturnErrors setting in the Validator contained in the parameter Map.
voidprocess(Map globalConstants)
Creates a FastHashMap for the isDependency method based on depends.
StringreadJavascriptFile(String javascriptFileName)
Read a javascript function from a file.
voidsetClassname(String classname)
Sets the class of the validator action.
voidsetClassnameInstance(Object instance)
Sets an instance based on the validator action's classname.
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.
voidsetMethod(String method)
Sets the name of method being called for the validator action.
voidsetMethodParams(String methodParams)
Sets the method parameters for the method.
voidsetMsg(String msg)
Sets the message associated with the validator action.
voidsetName(String name)
Sets the name of the validator action.
StringtoString()
Returns a string representation of the object.

Field Detail

classname

private String classname
The full class name of the class containing the validation method associated with this action.

dependencyList

private List dependencyList
An internal List representation of the other ValidatorActions 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().

depends

private String depends
The other ValidatorActions that this one depends on. If any errors occur in an action that this one depends on, this action will not be processsed.

instance

private Object 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.

javascript

private String javascript
An optional field to containing a JavaScript representation of the java method assocated with this action.

jsFunction

private String jsFunction
An optional field to contain the class path to be used to retrieve the JavaScript function.

jsFunctionName

private String jsFunctionName
An optional field to contain the name to be used if JavaScript is generated.

log

private static final Log log
Logger.

method

private String method
The full method name of the validation to be performed. The method must be thread safe.

methodParameterList

private List methodParameterList
An internal List representation of all the validation method's parameters defined in the methodParams String.

methodParams

private String 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.Object is reserved for the JavaBean that is being validated. The ValidatorAction and Field that are associated with a field's validation will automatically be populated if they are specified in the method signature.

msg

private String msg
The default error message associated with this action.

name

private String name
The name of the validation.

parameterClasses

private Class[] parameterClasses
The Class objects for each entry in methodParameterList.

validationClass

private Class validationClass
The Class object loaded from the classname.

validationMethod

private Method validationMethod
The Method object loaded from the method name.

Method Detail

executeValidationMethod

boolean executeValidationMethod(Field field, Map params, ValidatorResults results, int pos)
Dynamically 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

formatJavascriptFileName

private String formatJavascriptFileName()

Returns: A filename suitable for passing to a ClassLoader.getResourceAsStream() method.

generateJsFunction

private String generateJsFunction()
Used to generate the javascript name when it is not specified.

getClassLoader

private ClassLoader getClassLoader(Map params)
Returns the ClassLoader set in the Validator contained in the parameter Map.

getClassname

public String getClassname()
Gets the class of the validator action.

getClassnameInstance

public Object getClassnameInstance()

Deprecated: This will be removed after Validator 1.1.2

Gets an instance based on the validator action's classname.

getDependencies

public Collection getDependencies()

Deprecated: Use getDependencyList() instead.

Gets the dependencies as a Collection.

getDependencyList

public List getDependencyList()
Returns the dependent validator names as an unmodifiable List.

getDepends

public String getDepends()
Gets the dependencies of the validator action as a comma separated list of validator names.

getJavascript

public String getJavascript()
Gets the Javascript equivalent of the java class and method associated with this action.

getJsFunctionName

public String 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.

getMethod

public String getMethod()
Gets the name of method being called for the validator action.

getMethodParams

public String getMethodParams()
Gets the method parameters for the method.

getMethodParamsList

public List getMethodParamsList()

Deprecated: This will be removed after Validator 1.1.2

Gets the method parameters for the method as an unmodifiable List.

getMsg

public String getMsg()
Gets the message associated with the validator action.

getName

public String getName()
Gets the name of the validator action.

getParameterValues

private Object[] getParameterValues(Map params)
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

private Object 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.

handleIndexedField

private void handleIndexedField(Field field, int pos, Object[] paramValues)
Modifies the paramValue array with indexed fields.

Parameters: field pos paramValues

init

protected void init()
Initialize based on set.

isDependency

public boolean isDependency(String validatorName)
Checks whether or not the value passed in is in the depends field.

isValid

private boolean isValid(Object result)
If the result object is a Boolean, it will return its value. If not it will return false if the object is null and true if it isn't.

javascriptAlreadyLoaded

private boolean javascriptAlreadyLoaded()

Returns: true if the javascript for this action has already been loaded.

loadJavascriptFunction

protected void loadJavascriptFunction()
Load the javascript function specified by the given path. For this implementation, the jsFunction property 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'.

loadParameterClasses

private void loadParameterClasses(ClassLoader loader)
Converts a List of parameter class names into their Class objects.

Returns: An array containing the Class object for each parameter. 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.

loadValidationClass

private void loadValidationClass(ClassLoader loader)
Load the Class object for the configured validation class name.

Parameters: loader The ClassLoader used to load the Class object.

Throws: ValidatorException

loadValidationMethod

private void loadValidationMethod()
Load the Method object for the configured validation method name.

Throws: ValidatorException

onlyReturnErrors

private boolean onlyReturnErrors(Map params)
Returns the onlyReturnErrors setting in the Validator contained in the parameter Map.

process

public void process(Map globalConstants)

Deprecated: This functionality has been moved to other methods. It's no longer required to call this method to initialize this object.

Creates a FastHashMap for the isDependency method based on depends.

readJavascriptFile

private String readJavascriptFile(String javascriptFileName)
Read a javascript function from a file.

Parameters: javascriptFileName The file containing the javascript.

Returns: The javascript function or null if it could not be loaded.

setClassname

public void setClassname(String classname)
Sets the class of the validator action.

setClassnameInstance

public void setClassnameInstance(Object instance)

Deprecated: This will be removed after Validator 1.1.2

Sets an instance based on the validator action's classname.

setDepends

public void setDepends(String depends)
Sets the dependencies of the validator action.

Parameters: depends A comma separated list of validator names.

setJavascript

public void setJavascript(String javascript)
Sets the Javascript equivalent of the java class and method associated with this action.

setJsFunction

public void setJsFunction(String jsFunction)
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 setJsFunction and setJavascript will result in an IllegalStateException being 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.
 

setJsFunctionName

public void setJsFunctionName(String jsFunctionName)
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.

setMethod

public void setMethod(String method)
Sets the name of method being called for the validator action.

setMethodParams

public void setMethodParams(String methodParams)
Sets the method parameters for the method.

Parameters: methodParams A comma separated list of parameters.

setMsg

public void setMsg(String msg)
Sets the message associated with the validator action.

setName

public void setName(String name)
Sets the name of the validator action.

toString

public String toString()
Returns a string representation of the object.
Copyright (c) 2001-2004 Apache Software Foundation