org.apache.commons.validator
Class ValidatorAction

java.lang.Object
  extended by org.apache.commons.validator.ValidatorAction
All Implemented Interfaces:
java.io.Serializable

public class ValidatorAction
extends java.lang.Object
implements java.io.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.

See Also:
Serialized Form

Field Summary
private  java.lang.String classname
          The full class name of the class containing the validation method associated with this action.
private  java.util.List dependencyList
          An internal List representation of the other ValidatorActions this one depends on (if any).
private  java.lang.String depends
          The other ValidatorActions that this one depends on.
private  java.lang.Object instance
          If the java method matching the correct signature isn't static, the instance is stored in the action.
private  java.lang.String javascript
          An optional field to containing a JavaScript representation of the java method assocated with this action.
private  java.lang.String jsFunction
          An optional field to contain the class path to be used to retrieve the JavaScript function.
private  java.lang.String jsFunctionName
          An optional field to contain the name to be used if JavaScript is generated.
private static org.apache.commons.logging.Log log
          Logger.
private  java.lang.String method
          The full method name of the validation to be performed.
private  java.util.List methodParameterList
          An internal List representation of all the validation method's parameters defined in the methodParams String.
private  java.lang.String methodParams
           The method signature of the validation method.
private  java.lang.String msg
          The default error message associated with this action.
private  java.lang.String name
          The name of the validation.
private  java.lang.Class[] parameterClasses
          The Class objects for each entry in methodParameterList.
private  java.lang.Class validationClass
          The Class object loaded from the classname.
private  java.lang.reflect.Method validationMethod
          The Method object loaded from the method name.
 
Constructor Summary
ValidatorAction()
           
 
Method Summary
(package private)  boolean executeValidationMethod(Field field, java.util.Map params, ValidatorResults results, int pos)
          Dynamically runs the validation method for this validator and returns true if the data is valid.
private  java.lang.String formatJavascriptFileName()
           
private  java.lang.String generateJsFunction()
          Used to generate the javascript name when it is not specified.
private  java.lang.ClassLoader getClassLoader(java.util.Map params)
          Returns the ClassLoader set in the Validator contained in the parameter Map.
 java.lang.String getClassname()
          Gets the class of the validator action.
 java.lang.Object getClassnameInstance()
          Deprecated. This will be removed after Validator 1.1.2
 java.util.Collection getDependencies()
          Deprecated. Use getDependencyList() instead.
 java.util.List getDependencyList()
          Returns the dependent validator names as an unmodifiable List.
 java.lang.String getDepends()
          Gets the dependencies of the validator action as a comma separated list of validator names.
 java.lang.String getJavascript()
          Gets the Javascript equivalent of the java class and method associated with this action.
 java.lang.String getJsFunctionName()
          Gets the Javascript function name.
 java.lang.String getMethod()
          Gets the name of method being called for the validator action.
 java.lang.String getMethodParams()
          Gets the method parameters for the method.
 java.util.List getMethodParamsList()
          Deprecated. This will be removed after Validator 1.1.2
 java.lang.String getMsg()
          Gets the message associated with the validator action.
 java.lang.String getName()
          Gets the name of the validator action.
private  java.lang.Object[] getParameterValues(java.util.Map params)
          Converts a List of parameter class names into their values contained in the parameters Map.
private  java.lang.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.
private  void handleIndexedField(Field field, int pos, java.lang.Object[] paramValues)
          Modifies the paramValue array with indexed fields.
protected  void init()
          Initialize based on set.
 boolean isDependency(java.lang.String validatorName)
          Checks whether or not the value passed in is in the depends field.
private  boolean isValid(java.lang.Object result)
          If the result object is a Boolean, it will return its value.
private  boolean javascriptAlreadyLoaded()
           
protected  void loadJavascriptFunction()
          Load the javascript function specified by the given path.
private  void loadParameterClasses(java.lang.ClassLoader loader)
          Converts a List of parameter class names into their Class objects.
private  void loadValidationClass(java.lang.ClassLoader loader)
          Load the Class object for the configured validation class name.
private  void loadValidationMethod()
          Load the Method object for the configured validation method name.
private  boolean onlyReturnErrors(java.util.Map params)
          Returns the onlyReturnErrors setting in the Validator contained in the parameter Map.
 void process(java.util.Map globalConstants)
          Deprecated. This functionality has been moved to other methods. It's no longer required to call this method to initialize this object.
private  java.lang.String readJavascriptFile(java.lang.String javascriptFileName)
          Read a javascript function from a file.
 void setClassname(java.lang.String classname)
          Sets the class of the validator action.
 void setClassnameInstance(java.lang.Object instance)
          Deprecated. This will be removed after Validator 1.1.2
 void setDepends(java.lang.String depends)
          Sets the dependencies of the validator action.
 void setJavascript(java.lang.String javascript)
          Sets the Javascript equivalent of the java class and method associated with this action.
 void setJsFunction(java.lang.String jsFunction)
          Sets the fully qualified class path of the Javascript function.
 void setJsFunctionName(java.lang.String jsFunctionName)
          Sets the Javascript function name.
 void setMethod(java.lang.String method)
          Sets the name of method being called for the validator action.
 void setMethodParams(java.lang.String methodParams)
          Sets the method parameters for the method.
 void setMsg(java.lang.String msg)
          Sets the message associated with the validator action.
 void setName(java.lang.String name)
          Sets the name of the validator action.
 java.lang.String toString()
          Returns a string representation of the object.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

log

private static final org.apache.commons.logging.Log log
Logger.


name

private java.lang.String name
The name of the validation.


classname

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


validationClass

private java.lang.Class validationClass
The Class object loaded from the classname.


method

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


validationMethod

private java.lang.reflect.Method validationMethod
The Method object loaded from the method name.


methodParams

private java.lang.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.


parameterClasses

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


depends

private java.lang.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.


msg

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


jsFunctionName

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


jsFunction

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


javascript

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


instance

private java.lang.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.


dependencyList

private java.util.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().


methodParameterList

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

Constructor Detail

ValidatorAction

public ValidatorAction()
Method Detail

getName

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


setName

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


getClassname

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


setClassname

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


getMethod

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


setMethod

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


getMethodParams

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


setMethodParams

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

Parameters:
methodParams - A comma separated list of parameters.

getMethodParamsList

public java.util.List getMethodParamsList()
Deprecated. This will be removed after Validator 1.1.2

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


getDepends

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


setDepends

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

Parameters:
depends - A comma separated list of validator names.

getMsg

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


setMsg

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


getJsFunctionName

public java.lang.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.


setJsFunctionName

public void setJsFunctionName(java.lang.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.


setJsFunction

public void setJsFunction(java.lang.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.
 


getJavascript

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


setJavascript

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


getClassnameInstance

public java.lang.Object getClassnameInstance()
Deprecated. This will be removed after Validator 1.1.2

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


setClassnameInstance

public void setClassnameInstance(java.lang.Object instance)
Deprecated. This will be removed after Validator 1.1.2

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


init

protected void init()
Initialize based on set.


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'.


readJavascriptFile

private java.lang.String readJavascriptFile(java.lang.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.

formatJavascriptFileName

private java.lang.String 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

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


process

public void process(java.util.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.


isDependency

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


getDependencies

public java.util.Collection getDependencies()
Deprecated. Use getDependencyList() instead.

Gets the dependencies as a Collection.


getDependencyList

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


toString

public java.lang.String toString()
Returns a string representation of the object.

Overrides:
toString in class java.lang.Object

executeValidationMethod

boolean executeValidationMethod(Field field,
                                java.util.Map params,
                                ValidatorResults results,
                                int pos)
                          throws ValidatorException
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

loadValidationMethod

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

Throws:
ValidatorException

loadValidationClass

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

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

loadParameterClasses

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

Throws:
ValidatorException - if a class cannot be loaded.

getParameterValues

private java.lang.Object[] getParameterValues(java.util.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 java.lang.Object getValidationClassInstance()
                                             throws ValidatorException
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,
                                java.lang.Object[] paramValues)
                         throws ValidatorException
Modifies the paramValue array with indexed fields.

Parameters:
field -
pos -
paramValues -
Throws:
ValidatorException

isValid

private boolean isValid(java.lang.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.


getClassLoader

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


onlyReturnErrors

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



Copyright (c) 2001-2004 Apache Software Foundation