Package org.apache.commons.validator
Class Field
- java.lang.Object
-
- org.apache.commons.validator.Field
-
- All Implemented Interfaces:
java.io.Serializable,java.lang.Cloneable
public class Field extends java.lang.Object implements java.lang.Cloneable, java.io.SerializableThis 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.
- Version:
- $Revision: 1713331 $
- See Also:
Form, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.Map<java.lang.String,Arg>[]argsHolds Maps of arguments.protected booleanclientValidationThe flag that indicates whether scripting should be generated by the client for client-side validation.private static java.lang.StringDEFAULT_ARGThis is the value that will be used as a key if theArgname field has no value.private java.util.List<java.lang.String>dependencyListInternal representation of this.depends String as a List.protected java.lang.StringdependsA comma separated list of validator's this field depends on.protected intfieldOrderThe order of the Field in the Form.protected org.apache.commons.collections.FastHashMaphMsgsDeprecated.Subclasses should use getMsgMap() instead.protected org.apache.commons.collections.FastHashMaphVarsDeprecated.Subclasses should use getVarMap() instead.protected java.lang.StringindexedListPropertyThe Field's indexed list property name.protected java.lang.StringindexedPropertyThe Field's indexed property name.protected java.lang.StringkeyThe Field's unique key.protected intpageThe Page Numberprotected java.lang.StringpropertyThe Field's property name.private static longserialVersionUIDprotected static java.lang.StringTOKEN_ENDThe end of a token.static java.lang.StringTOKEN_INDEXEDThis indicates an indexed property is being referenced.protected static java.lang.StringTOKEN_STARTThe start of a token.protected static java.lang.StringTOKEN_VARA Vriable token.
-
Constructor Summary
Constructors Constructor Description Field()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddArg(Arg arg)Add anArgto the replacement argument list.voidaddMsg(Msg msg)Add aMsgto theField.voidaddVar(java.lang.String name, java.lang.String value, java.lang.String jsType)Add aVar, based on the values passed in, to theField.voidaddVar(Var v)Add aVarto theField.java.lang.Objectclone()Creates and returns a copy of this object.private voiddetermineArgPosition(Arg arg)Calculate the position of the Argprivate voidensureArgsCapacity(Arg arg)Ensures that the args array can hold the given arg.voidgenerateKey()Generate correctkeyvalue.ArggetArg(int position)Gets the defaultArgobject at the given position.ArggetArg(java.lang.String key, int position)Gets theArgobject at the given position.Arg[]getArgs(java.lang.String key)Retrieves the Args for the given validator name.java.util.List<java.lang.String>getDependencyList()Gets an unmodifiableListof the dependencies in the same order they were defined in parameter passed to the setDepends() method.java.lang.StringgetDepends()Gets the validation rules for this field as a comma separated list.intgetFieldOrder()Gets the position of theFieldin the validation list.java.lang.StringgetIndexedListProperty()Gets the indexed property name of the field.java.lang.StringgetIndexedProperty()Gets the indexed property name of the field.(package private) java.lang.Object[]getIndexedProperty(java.lang.Object bean)Returns an indexed property from the object we're validating.private intgetIndexedPropertySize(java.lang.Object bean)Returns the size of an indexed property from the object we're validating.java.lang.StringgetKey()Gets a unique key based on the property and indexedProperty fields.MsggetMessage(java.lang.String key)Retrieve a message object.java.util.Map<java.lang.String,Msg>getMessages()TheField's messages are returned as an unmodifiableMap.java.lang.StringgetMsg(java.lang.String key)Retrieve a message value.protected java.util.Map<java.lang.String,Msg>getMsgMap()Returns a Map of String Msg names to Msg objects.intgetPage()Gets the page value that the Field is associated with for validation.java.lang.StringgetProperty()Gets the property name of the field.VargetVar(java.lang.String mainKey)Retrieve a variable.protected java.util.Map<java.lang.String,Var>getVarMap()Returns a Map of String Var names to Var objects.java.util.Map<java.lang.String,Var>getVars()TheField's variables are returned as an unmodifiableMap.java.lang.StringgetVarValue(java.lang.String mainKey)Retrieve a variable's value.private voidhandleMissingAction(java.lang.String name)Called when a validator name is used in a depends clause but there is no know ValidatorAction configured for that name.booleanisClientValidation()Determines whether client-side scripting should be generated for this field.booleanisDependency(java.lang.String validatorName)Checks if the validator is listed as a dependency.booleanisIndexed()If there is a value specified for the indexedProperty field thentruewill be returned.(package private) voidprocess(java.util.Map<java.lang.String,java.lang.String> globalConstants, java.util.Map<java.lang.String,java.lang.String> constants)Replace constants with values in fields and process the depends field to create the dependencyMap.private voidprocessArg(java.lang.String key, java.lang.String replaceValue)Replace the argCollectionkey value with the key/value pairs passed in.private voidprocessMessageComponents(java.lang.String key, java.lang.String replaceValue)Replace the args key value with the key/value pairs passed in.private voidprocessVars(java.lang.String key, java.lang.String replaceValue)Replace the vars value with the key/value pairs passed in.private booleanrunDependentValidators(ValidatorAction va, ValidatorResults results, java.util.Map<java.lang.String,ValidatorAction> actions, java.util.Map<java.lang.String,java.lang.Object> params, int pos)Calls all of the validators that this validator depends on.voidsetClientValidation(boolean clientValidation)Sets the flag that determines whether client-side scripting should be generated for this field.voidsetDepends(java.lang.String depends)Sets the validation rules for this field as a comma separated list.voidsetFieldOrder(int fieldOrder)Sets the position of theFieldin the validation list.voidsetIndexedListProperty(java.lang.String indexedListProperty)Sets the indexed property name of the field.voidsetIndexedProperty(java.lang.String indexedProperty)Sets the indexed property name of the field.voidsetKey(java.lang.String key)Sets a unique key for the field.voidsetPage(int page)Sets the page value that the Field is associated with for validation.voidsetProperty(java.lang.String property)Sets the property name of the field.java.lang.StringtoString()Returns a string representation of the object.ValidatorResultsvalidate(java.util.Map<java.lang.String,java.lang.Object> params, java.util.Map<java.lang.String,ValidatorAction> actions)Run the configured validations on this field.private booleanvalidateForRule(ValidatorAction va, ValidatorResults results, java.util.Map<java.lang.String,ValidatorAction> actions, java.util.Map<java.lang.String,java.lang.Object> params, int pos)Executes the given ValidatorAction and all ValidatorActions that it depends on.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
DEFAULT_ARG
private static final java.lang.String DEFAULT_ARG
This is the value that will be used as a key if theArgname field has no value.- See Also:
- Constant Field Values
-
TOKEN_INDEXED
public static final java.lang.String TOKEN_INDEXED
This indicates an indexed property is being referenced.- See Also:
- Constant Field Values
-
TOKEN_START
protected static final java.lang.String TOKEN_START
The start of a token.- See Also:
- Constant Field Values
-
TOKEN_END
protected static final java.lang.String TOKEN_END
The end of a token.- See Also:
- Constant Field Values
-
TOKEN_VAR
protected static final java.lang.String TOKEN_VAR
A Vriable token.- See Also:
- Constant Field Values
-
property
protected java.lang.String property
The Field's property name.
-
indexedProperty
protected java.lang.String indexedProperty
The Field's indexed property name.
-
indexedListProperty
protected java.lang.String indexedListProperty
The Field's indexed list property name.
-
key
protected java.lang.String key
The Field's unique key.
-
depends
protected java.lang.String depends
A comma separated list of validator's this field depends on.
-
page
protected int page
The Page Number
-
clientValidation
protected boolean clientValidation
The flag that indicates whether scripting should be generated by the client for client-side validation.- Since:
- Validator 1.4
-
fieldOrder
protected int fieldOrder
The order of the Field in the Form.
-
dependencyList
private final java.util.List<java.lang.String> 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().
-
hVars
protected org.apache.commons.collections.FastHashMap hVars
Deprecated.Subclasses should use getVarMap() instead.
-
hMsgs
protected org.apache.commons.collections.FastHashMap hMsgs
Deprecated.Subclasses should use getMsgMap() instead.
-
args
protected java.util.Map<java.lang.String,Arg>[] 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
-
-
Method Detail
-
getPage
public int getPage()
Gets the page value that the Field is associated with for validation.- Returns:
- The page number.
-
setPage
public void setPage(int page)
Sets the page value that the Field is associated with for validation.- Parameters:
page- The page number.
-
getFieldOrder
public int getFieldOrder()
Gets the position of theFieldin the validation list.- Returns:
- The field position.
-
setFieldOrder
public void setFieldOrder(int fieldOrder)
Sets the position of theFieldin the validation list.- Parameters:
fieldOrder- The field position.
-
getProperty
public java.lang.String getProperty()
Gets the property name of the field.- Returns:
- The field's property name.
-
setProperty
public void setProperty(java.lang.String property)
Sets the property name of the field.- Parameters:
property- The field's property name.
-
getIndexedProperty
public java.lang.String getIndexedProperty()
Gets the indexed property name of the field. This is the method name that can take anintas a parameter for indexed property value retrieval.- Returns:
- The field's indexed property name.
-
setIndexedProperty
public void setIndexedProperty(java.lang.String indexedProperty)
Sets the indexed property name of the field.- Parameters:
indexedProperty- The field's indexed property name.
-
getIndexedListProperty
public java.lang.String getIndexedListProperty()
Gets the indexed property name of the field. This is the method name that will return an array or aCollectionused to retrieve the list and then loop through the list performing the specified validations.- Returns:
- The field's indexed List property name.
-
setIndexedListProperty
public void setIndexedListProperty(java.lang.String indexedListProperty)
Sets the indexed property name of the field.- Parameters:
indexedListProperty- The field's indexed List property name.
-
getDepends
public java.lang.String getDepends()
Gets the validation rules for this field as a comma separated list.- Returns:
- A comma separated list of validator names.
-
setDepends
public void setDepends(java.lang.String depends)
Sets the validation rules for this field as a comma separated list.- Parameters:
depends- A comma separated list of validator names.
-
addMsg
public void addMsg(Msg msg)
Add aMsgto theField.- Parameters:
msg- A validation message.
-
getMsg
public java.lang.String getMsg(java.lang.String key)
Retrieve a message value.- Parameters:
key- Validation key.- Returns:
- A validation message for a specified validator.
-
getMessage
public Msg getMessage(java.lang.String key)
Retrieve a message object.- Parameters:
key- Validation key.- Returns:
- A validation message for a specified validator.
- Since:
- Validator 1.1.4
-
getMessages
public java.util.Map<java.lang.String,Msg> getMessages()
TheField's messages are returned as an unmodifiableMap.- Returns:
- Map of validation messages for the field.
- Since:
- Validator 1.1.4
-
isClientValidation
public boolean isClientValidation()
Determines whether client-side scripting should be generated for this field. The default istrue- Returns:
truefor scripting; otherwise false- Since:
- Validator 1.4
- See Also:
setClientValidation(boolean)
-
setClientValidation
public void setClientValidation(boolean clientValidation)
Sets the flag that determines whether client-side scripting should be generated for this field.- Parameters:
clientValidation- the scripting flag- Since:
- Validator 1.4
- See Also:
isClientValidation()
-
addArg
public void addArg(Arg arg)
Add anArgto the replacement argument list.- Parameters:
arg- Validation message's argument.- Since:
- Validator 1.1
-
determineArgPosition
private void determineArgPosition(Arg arg)
Calculate the position of the Arg
-
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.
-
getArg
public Arg getArg(int position)
Gets the defaultArgobject at the given position.- Parameters:
position- Validation message argument's position.- Returns:
- The default Arg or null if not found.
- Since:
- Validator 1.1
-
getArg
public Arg getArg(java.lang.String key, int position)
Gets theArgobject at the given position. If the key finds anullvalue 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
-
getArgs
public Arg[] getArgs(java.lang.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
-
addVar
public void addVar(Var v)
Add aVarto theField.- Parameters:
v- The Validator Argument.
-
addVar
public void addVar(java.lang.String name, java.lang.String value, java.lang.String jsType)Add aVar, based on the values passed in, to theField.- Parameters:
name- Name of the validation.value- The Argument's value.jsType- The Javascript type.
-
getVar
public Var getVar(java.lang.String mainKey)
Retrieve a variable.- Parameters:
mainKey- The Variable's key- Returns:
- the Variable
-
getVarValue
public java.lang.String getVarValue(java.lang.String mainKey)
Retrieve a variable's value.- Parameters:
mainKey- The Variable's key- Returns:
- the Variable's value
-
getVars
public java.util.Map<java.lang.String,Var> getVars()
TheField's variables are returned as an unmodifiableMap.- Returns:
- the Map of Variable's for a Field.
-
getKey
public java.lang.String getKey()
Gets a unique key based on the property and indexedProperty fields.- Returns:
- a unique key for the field.
-
setKey
public void setKey(java.lang.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- a unique key for the field
-
isIndexed
public boolean isIndexed()
If there is a value specified for the indexedProperty field thentruewill be returned. Otherwise it will befalse.- Returns:
- Whether the Field is indexed.
-
generateKey
public void generateKey()
Generate correctkeyvalue.
-
process
void process(java.util.Map<java.lang.String,java.lang.String> globalConstants, java.util.Map<java.lang.String,java.lang.String> constants)Replace constants with values in fields and process the depends field to create the dependencyMap.
-
processVars
private void processVars(java.lang.String key, java.lang.String replaceValue)Replace the vars value with the key/value pairs passed in.
-
processMessageComponents
private void processMessageComponents(java.lang.String key, java.lang.String replaceValue)Replace the args key value with the key/value pairs passed in.
-
processArg
private void processArg(java.lang.String key, java.lang.String replaceValue)Replace the argCollectionkey value with the key/value pairs passed in.
-
isDependency
public boolean isDependency(java.lang.String validatorName)
Checks if the validator is listed as a dependency.- Parameters:
validatorName- Name of the validator to check.- Returns:
- Whether the field is dependant on a validator.
-
getDependencyList
public java.util.List<java.lang.String> getDependencyList()
Gets an unmodifiableListof the dependencies in the same order they were defined in parameter passed to the setDepends() method.- Returns:
- A list of the Field's dependancies.
-
clone
public java.lang.Object clone()
Creates and returns a copy of this object.- Overrides:
clonein classjava.lang.Object- Returns:
- A copy of the Field.
-
toString
public java.lang.String toString()
Returns a string representation of the object.- Overrides:
toStringin classjava.lang.Object- Returns:
- A string representation of the object.
-
getIndexedProperty
java.lang.Object[] getIndexedProperty(java.lang.Object bean) throws ValidatorExceptionReturns 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.
-
getIndexedPropertySize
private int getIndexedPropertySize(java.lang.Object bean) throws ValidatorExceptionReturns the size of 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.
-
validateForRule
private boolean validateForRule(ValidatorAction va, ValidatorResults results, java.util.Map<java.lang.String,ValidatorAction> actions, java.util.Map<java.lang.String,java.lang.Object> params, int pos) throws ValidatorException
Executes the given ValidatorAction and all ValidatorActions that it depends on.- Returns:
- true if the validation succeeded.
- Throws:
ValidatorException
-
runDependentValidators
private boolean runDependentValidators(ValidatorAction va, ValidatorResults results, java.util.Map<java.lang.String,ValidatorAction> actions, java.util.Map<java.lang.String,java.lang.Object> 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.
- Throws:
ValidatorException- If there's an error running a validator
-
validate
public ValidatorResults validate(java.util.Map<java.lang.String,java.lang.Object> params, java.util.Map<java.lang.String,ValidatorAction> 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.
- Throws:
ValidatorException- If an error occurs during validation.
-
handleMissingAction
private void handleMissingAction(java.lang.String name) throws ValidatorExceptionCalled 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.- Throws:
ValidatorException
-
getMsgMap
protected java.util.Map<java.lang.String,Msg> getMsgMap()
Returns a Map of String Msg names to Msg objects.- Returns:
- A Map of the Field's messages.
- Since:
- Validator 1.2.0
-
getVarMap
protected java.util.Map<java.lang.String,Var> getVarMap()
Returns a Map of String Var names to Var objects.- Returns:
- A Map of the Field's variables.
- Since:
- Validator 1.2.0
-
-