org.apache.commons.validator

Class Field

Implemented Interfaces:
Cloneable, Serializable

public class Field
extends java.lang.Object
implements Cloneable, Serializable

This contains the list of pluggable validators to run on a field and any message information and variables to perform the validations and generate error messages. Instances of this class are configured with a <field> xml element. The use of FastHashMap is deprecated and will be replaced in a future release.
See Also:
Form

Field Summary

static String
ARG_DEFAULT
Deprecated.
private static String
DEFAULT_ARG
This is the value that will be used as a key if the Arg name field has no value.
protected static String
TOKEN_END
static String
TOKEN_INDEXED
This indicates an indexed property is being referenced.
protected static String
TOKEN_START
protected static String
TOKEN_VAR
protected Map[]
args
Holds Maps of arguments.
private List
dependencyList
Internal representation of this.depends String as a List.
protected String
depends
A comma separated list of validator's this field depends on.
protected int
fieldOrder
protected FastHashMap
hArg0
Deprecated. This variable is no longer used, use args instead.
protected FastHashMap
hArg1
Deprecated. This variable is no longer used, use args instead.
protected FastHashMap
hArg2
Deprecated. This variable is no longer used, use args instead.
protected FastHashMap
hArg3
Deprecated. This variable is no longer used, use args instead.
protected FastHashMap
hDependencies
Deprecated. This is no longer used.
protected FastHashMap
hMsgs
protected FastHashMap
hVars
protected String
indexedListProperty
protected String
indexedProperty
protected String
key
protected int
page
protected String
property

Method Summary

void
addArg(Arg arg)
Add an Arg to the replacement argument list.
void
addArg0(Arg arg)
Deprecated. Use addArg(Arg) instead.
void
addArg1(Arg arg)
Deprecated. Use addArg(Arg) instead.
void
addArg2(Arg arg)
Deprecated. Use addArg(Arg) instead.
void
addArg3(Arg arg)
Deprecated. Use addArg(Arg) instead.
void
addMsg(Msg msg)
Add a Msg to the Field.
void
addVar(String name, String value, String jsType)
Add a Var, based on the values passed in, to the Field.
void
addVar(Var v)
Add a Var to the Field.
void
addVarParam(String name, String value, String jsType)
Deprecated. Use addVar(String, String, String) instead.
Object
clone()
Creates and returns a copy of this object.
private void
ensureArgsCapacity(Arg arg)
Ensures that the args array can hold the given arg.
void
generateKey()
Generate correct key value.
Arg
getArg(String key, int position)
Gets the Arg object at the given position.
Arg
getArg(int position)
Gets the default Arg object at the given position.
Arg
getArg0()
Deprecated. Use getArg(0) instead.
Arg
getArg0(String key)
Deprecated. Use getArg(String, 0) instead.
Arg
getArg1()
Deprecated. Use getArg(1) instead.
Arg
getArg1(String key)
Deprecated. Use getArg(String, 1) instead.
Arg
getArg2()
Deprecated. Use getArg(2) instead.
Arg
getArg2(String key)
Deprecated. Use getArg(String, 2) instead.
Arg
getArg3()
Deprecated. Use getArg(3) instead.
Arg
getArg3(String key)
Deprecated. Use getArg(String, 3) instead.
Arg[]
getArgs(String key)
Retrieves the Args for the given validator name.
Collection
getDependencies()
Deprecated. Use getDependencyList() instead.
List
getDependencyList()
Gets an unmodifiable List of the dependencies in the same order they were defined in parameter passed to the setDepends() method.
String
getDepends()
Gets the validation rules for this field as a comma separated list.
int
getFieldOrder()
Gets the position of the Field in the validation list.
String
getIndexedListProperty()
Gets the indexed property name of the field.
String
getIndexedProperty()
Gets the indexed property name of the field.
(package private) Object[]
getIndexedProperty(Object bean)
Returns an indexed property from the object we're validating.
String
getKey()
Gets a unique key based on the property and indexedProperty fields.
Msg
getMessage(String key)
Retrieve a message object.
Map
getMessages()
The Field's messages are returned as an unmodifiable Map.
String
getMsg(String key)
Retrieve a message value.
int
getPage()
Gets the page value that the Field is associated with for validation.
String
getProperty()
Gets the property name of the field.
Var
getVar(String mainKey)
Retrieve a variable.
String
getVarValue(String mainKey)
Retrieve a variable's value.
Map
getVars()
The Field's variables are returned as an unmodifiable Map.
private void
handleMissingAction(String name)
Called when a validator name is used in a depends clause but there is no know ValidatorAction configured for that name.
private void
internalProcessMessageComponents(String key, String replaceValue)
Replace the args key value with the key/value pairs passed in.
boolean
isDependency(String validatorName)
Checks if the validator is listed as a dependency.
boolean
isIndexed()
If there is a value specified for the indexedProperty field then true will be returned.
void
process(Map globalConstants, Map constants)
Deprecated. This method is called by the framework.
private void
processArg(String key, String replaceValue)
Replace the arg Collection key value with the key/value pairs passed in.
void
processMessageComponents(String key, String replaceValue)
Deprecated. This is an internal setup method that clients don't need to call.
private void
processVars(String key, String replaceValue)
Replace the vars value with the key/value pairs passed in.
private boolean
runDependentValidators(ValidatorAction va, ValidatorResults results, Map actions, Map params, int pos)
Calls all of the validators that this validator depends on.
void
setDepends(String depends)
Sets the validation rules for this field as a comma separated list.
void
setFieldOrder(int fieldOrder)
Sets the position of the Field in the validation list.
void
setIndexedListProperty(String indexedListProperty)
Sets the indexed property name of the field.
void
setIndexedProperty(String indexedProperty)
Sets the indexed property name of the field.
void
setKey(String key)
Sets a unique key for the field.
void
setPage(int page)
Sets the page value that the Field is associated with for validation.
void
setProperty(String property)
Sets the property name of the field.
String
toString()
Returns a string representation of the object.
(package private) ValidatorResults
validate(Map params, Map actions)
Run the configured validations on this field.
private boolean
validateForRule(ValidatorAction va, ValidatorResults results, Map actions, Map params, int pos)
Executes the given ValidatorAction and all ValidatorActions that it depends on.

Field Details

ARG_DEFAULT

public static final String ARG_DEFAULT

Deprecated.

This is the value that will be used as a key if the Arg name field has no value.

DEFAULT_ARG

private static final String DEFAULT_ARG
This is the value that will be used as a key if the Arg name field has no value.

TOKEN_END

protected static final String TOKEN_END

TOKEN_INDEXED

public static final String TOKEN_INDEXED
This indicates an indexed property is being referenced.

TOKEN_START

protected static final String TOKEN_START

TOKEN_VAR

protected static final String TOKEN_VAR

args

protected Map[] args
Holds Maps of arguments. args[0] returns the Map for the first replacement argument. Start with a 0 length array so that it will only grow to the size of the highest argument position.
Since:
Validator 1.1

dependencyList

private List dependencyList
Internal representation of this.depends String as a List. This List gets updated whenever setDepends() gets called. This List is synchronized so a call to setDepends() (which clears the List) won't interfere with a call to isDependency().

depends

protected String depends
A comma separated list of validator's this field depends on.

fieldOrder

protected int fieldOrder

hArg0

protected FastHashMap hArg0

Deprecated. This variable is no longer used, use args instead.


hArg1

protected FastHashMap hArg1

Deprecated. This variable is no longer used, use args instead.


hArg2

protected FastHashMap hArg2

Deprecated. This variable is no longer used, use args instead.


hArg3

protected FastHashMap hArg3

Deprecated. This variable is no longer used, use args instead.


hDependencies

protected FastHashMap hDependencies

Deprecated. This is no longer used.


hMsgs

protected FastHashMap hMsgs

hVars

protected FastHashMap hVars

indexedListProperty

protected String indexedListProperty

indexedProperty

protected String indexedProperty

key

protected String key

page

protected int page

property

protected String property

Method Details

addArg

public void addArg(Arg arg)
Add an Arg to the replacement argument list.
Since:
Validator 1.1

addArg0

public void addArg0(Arg arg)

Deprecated. Use addArg(Arg) instead.

Add a Arg to the arg0 list.

addArg1

public void addArg1(Arg arg)

Deprecated. Use addArg(Arg) instead.

Add a Arg to the arg1 list.

addArg2

public void addArg2(Arg arg)

Deprecated. Use addArg(Arg) instead.

Add a Arg to the arg2 list.

addArg3

public void addArg3(Arg arg)

Deprecated. Use addArg(Arg) instead.

Add a Arg to the arg3 list.

addMsg

public void addMsg(Msg msg)
Add a Msg to the Field.

addVar

public void addVar(String name,
                   String value,
                   String jsType)
Add a Var, based on the values passed in, to the Field.
Parameters:
name -
value -
jsType -

addVar

public void addVar(Var v)
Add a Var to the Field.

addVarParam

public void addVarParam(String name,
                        String value,
                        String jsType)

Deprecated. Use addVar(String, String, String) instead.

Add a Var, based on the values passed in, to the Field.

clone

public Object clone()
Creates and returns a copy of this object.

ensureArgsCapacity

private void ensureArgsCapacity(Arg arg)
Ensures that the args array can hold the given arg. Resizes the array as necessary.
Parameters:
arg - Determine if the args array is long enough to store this arg's position.

generateKey

public void generateKey()
Generate correct key value.

getArg

public Arg getArg(String key,
                  int position)
Gets the Arg object at the given position. If the key finds a null value then the default value will be retrieved.
Parameters:
key - The name the Arg is stored under. If not found, the default Arg for the given position (if any) will be retrieved.
position - The Arg number to find.
Returns:
The Arg with the given name and position or null if not found.
Since:
Validator 1.1

getArg

public Arg getArg(int position)
Gets the default Arg object at the given position.
Returns:
The default Arg or null if not found.
Since:
Validator 1.1

getArg0

public Arg getArg0()

Deprecated. Use getArg(0) instead.

Gets the default arg0 Arg object.

getArg0

public Arg getArg0(String key)

Deprecated. Use getArg(String, 0) instead.

Gets the arg0 Arg object based on the key passed in. If the key finds a null value then the default value will be retrieved.

getArg1

public Arg getArg1()

Deprecated. Use getArg(1) instead.

Gets the default arg1 Arg object.

getArg1

public Arg getArg1(String key)

Deprecated. Use getArg(String, 1) instead.

Gets the arg1 Arg object based on the key passed in. If the key finds a null value then the default value will try to be retrieved.

getArg2

public Arg getArg2()

Deprecated. Use getArg(2) instead.

Gets the default arg2 Arg object.

getArg2

public Arg getArg2(String key)

Deprecated. Use getArg(String, 2) instead.

Gets the arg2 Arg object based on the key passed in. If the key finds a null value then the default value will try to be retrieved.

getArg3

public Arg getArg3()

Deprecated. Use getArg(3) instead.

Gets the default arg3 Arg object.

getArg3

public Arg getArg3(String key)

Deprecated. Use getArg(String, 3) instead.

Gets the arg3 Arg object based on the key passed in. If the key finds a null value then the default value will try to be retrieved.

getArgs

public Arg[] getArgs(String key)
Retrieves the Args for the given validator name.
Parameters:
key - The validator's args to retrieve.
Returns:
An Arg[] sorted by the Args' positions (i.e. the Arg at index 0 has a position of 0).
Since:
Validator 1.1.1

getDependencies

public Collection getDependencies()

Deprecated. Use getDependencyList() instead.

Gets an unmodifiable Set of the dependencies.

getDependencyList

public List getDependencyList()
Gets an unmodifiable List of the dependencies in the same order they were defined in parameter passed to the setDepends() method.

getDepends

public String getDepends()
Gets the validation rules for this field as a comma separated list.

getFieldOrder

public int getFieldOrder()
Gets the position of the Field in the validation list.

getIndexedListProperty

public String getIndexedListProperty()
Gets the indexed property name of the field. This is the method name that will return an array or a Collection used to retrieve the list and then loop through the list performing the specified validations.

getIndexedProperty

public String getIndexedProperty()
Gets the indexed property name of the field. This is the method name that can take an int as a parameter for indexed property value retrieval.

getIndexedProperty

(package private)  Object[] getIndexedProperty(Object bean)
            throws ValidatorException
Returns an indexed property from the object we're validating.
Parameters:
bean - The bean to extract the indexed values from.
Throws:
ValidatorException - If there's an error looking up the property or, the property found is not indexed.

getKey

public String getKey()
Gets a unique key based on the property and indexedProperty fields.

getMessage

public Msg getMessage(String key)
Retrieve a message object.
Since:
Validator 1.1.4

getMessages

public Map getMessages()
The Field's messages are returned as an unmodifiable Map.
Since:
Validator 1.1.4

getMsg

public String getMsg(String key)
Retrieve a message value.

getPage

public int getPage()
Gets the page value that the Field is associated with for validation.

getProperty

public String getProperty()
Gets the property name of the field.

getVar

public Var getVar(String mainKey)
Retrieve a variable.
Parameters:
mainKey -

getVarValue

public String getVarValue(String mainKey)
Retrieve a variable's value.
Parameters:
mainKey -

getVars

public Map getVars()
The Field's variables are returned as an unmodifiable Map.

handleMissingAction

private void handleMissingAction(String name)
            throws ValidatorException
Called when a validator name is used in a depends clause but there is no know ValidatorAction configured for that name.
Parameters:
name - The name of the validator in the depends list.

internalProcessMessageComponents

private void internalProcessMessageComponents(String key,
                                              String replaceValue)
Replace the args key value with the key/value pairs passed in. TODO When processMessageComponents() is removed from the public API we should rename this private method to "processMessageComponents".

isDependency

public boolean isDependency(String validatorName)
Checks if the validator is listed as a dependency.

isIndexed

public boolean isIndexed()
If there is a value specified for the indexedProperty field then true will be returned. Otherwise it will be false.

process

public void process(Map globalConstants,
                    Map constants)

Deprecated. This method is called by the framework. It will be made protected in a future release. TODO

Replace constants with values in fields and process the depends field to create the dependency Map.

processArg

private void processArg(String key,
                        String replaceValue)
Replace the arg Collection key value with the key/value pairs passed in.

processMessageComponents

public void processMessageComponents(String key,
                                     String replaceValue)

Deprecated. This is an internal setup method that clients don't need to call.

Replace the args key value with the key/value pairs passed in.

processVars

private void processVars(String key,
                         String replaceValue)
Replace the vars value with the key/value pairs passed in.

runDependentValidators

private boolean runDependentValidators(ValidatorAction va,
                                       ValidatorResults results,
                                       Map actions,
                                       Map params,
                                       int pos)
            throws ValidatorException
Calls all of the validators that this validator depends on. TODO ValidatorAction should know how to run its own dependencies.
Parameters:
va - Run dependent validators for this action.
results -
actions -
pos -
Returns:
true if all of the dependent validations passed.

setDepends

public void setDepends(String depends)
Sets the validation rules for this field as a comma separated list.
Parameters:
depends - A comma separated list of validator names.

setFieldOrder

public void setFieldOrder(int fieldOrder)
Sets the position of the Field in the validation list.

setIndexedListProperty

public void setIndexedListProperty(String indexedListProperty)
Sets the indexed property name of the field.

setIndexedProperty

public void setIndexedProperty(String indexedProperty)
Sets the indexed property name of the field.

setKey

public void setKey(String key)
Sets a unique key for the field. This can be used to change the key temporarily to have a unique key for an indexed field.
Parameters:
key -

setPage

public void setPage(int page)
Sets the page value that the Field is associated with for validation.

setProperty

public void setProperty(String property)
Sets the property name of the field.

toString

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

validate

(package private)  ValidatorResults validate(Map params,
                                             Map actions)
            throws ValidatorException
Run the configured validations on this field. Run all validations in the depends clause over each item in turn, returning when the first one fails.
Parameters:
params - A Map of parameter class names to parameter values to pass into validation methods.
actions - A Map of validator names to ValidatorAction objects.
Returns:
A ValidatorResults object containing validation messages for this field.

validateForRule

private boolean validateForRule(ValidatorAction va,
                                ValidatorResults results,
                                Map actions,
                                Map params,
                                int pos)
            throws ValidatorException
Executes the given ValidatorAction and all ValidatorActions that it depends on.
Returns:
true if the validation succeeded.

Copyright (c) 2001-2004 Apache Software Foundation