Methods Summary |
---|
public void | addArg(Arg arg)Add an Arg to the replacement argument list.
// TODO this first if check can go away after arg0, etc. are removed from dtd
if (arg == null || arg.getKey() == null || arg.getKey().length() == 0) {
return;
}
this.ensureArgsCapacity(arg);
Map argMap = this.args[arg.getPosition()];
if (argMap == null) {
argMap = new HashMap();
this.args[arg.getPosition()] = argMap;
}
if (arg.getName() == null) {
argMap.put(DEFAULT_ARG, arg);
} else {
argMap.put(arg.getName(), arg);
}
|
public void | addMsg(Msg msg)Add a Msg to the Field .
hMsgs.put(msg.getName(), msg.getKey());
|
public void | addVar(Var v)Add a Var to the Field .
this.hVars.put(v.getName(), v);
|
public void | addVar(java.lang.String name, java.lang.String value, java.lang.String jsType)Add a Var , based on the values passed in, to the
Field .
this.addVar(new Var(name, value, jsType));
|
public java.lang.Object | clone()Creates and returns a copy of this object.
Field field = null;
try {
field = (Field) super.clone();
} catch(CloneNotSupportedException e) {
throw new RuntimeException(e.toString());
}
field.args = new Map[this.args.length];
for (int i = 0; i < this.args.length; i++) {
if (this.args[i] == null) {
continue;
}
Map argMap = new HashMap(this.args[i]);
Iterator iter = argMap.keySet().iterator();
while (iter.hasNext()) {
String validatorName = (String) iter.next();
Arg arg = (Arg) argMap.get(validatorName);
argMap.put(validatorName, arg.clone());
}
field.args[i] = argMap;
}
field.hVars = ValidatorUtils.copyFastHashMap(hVars);
field.hMsgs = ValidatorUtils.copyFastHashMap(hMsgs);
return field;
|
private void | ensureArgsCapacity(Arg arg)Ensures that the args array can hold the given arg. Resizes the array as
necessary.
if (arg.getPosition() >= this.args.length) {
Map[] newArgs = new Map[arg.getPosition() + 1];
System.arraycopy(this.args, 0, newArgs, 0, this.args.length);
this.args = newArgs;
}
|
public void | generateKey()Generate correct key value.
if (this.isIndexed()) {
this.key = this.indexedListProperty + TOKEN_INDEXED + "." + this.property;
} else {
this.key = this.property;
}
|
public Arg | getArg(int position)Gets the default Arg object at the given position.
return this.getArg(DEFAULT_ARG, position);
|
public Arg | getArg(java.lang.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.
if ((position >= this.args.length) || (this.args[position] == null)) {
return null;
}
Arg arg = (Arg) args[position].get(key);
// Didn't find default arg so exit, otherwise we would get into
// infinite recursion
if ((arg == null) && key.equals(DEFAULT_ARG)) {
return null;
}
return (arg == null) ? this.getArg(position) : arg;
|
public Arg[] | getArgs(java.lang.String key)Retrieves the Args for the given validator name.
Arg[] args = new Arg[this.args.length];
for (int i = 0; i < this.args.length; i++) {
args[i] = this.getArg(key, i);
}
return args;
|
public java.util.List | getDependencyList()Gets an unmodifiable List of the dependencies in the same
order they were defined in parameter passed to the setDepends() method.
return Collections.unmodifiableList(this.dependencyList);
|
public java.lang.String | getDepends()Gets the validation rules for this field as a comma separated list.
return this.depends;
|
public int | getFieldOrder()Gets the position of the Field in the validation list.
return this.fieldOrder;
|
public java.lang.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.
return this.indexedListProperty;
|
java.lang.Object[] | getIndexedProperty(java.lang.Object bean)Returns an indexed property from the object we're validating.
Object indexedProperty = null;
try {
indexedProperty =
PropertyUtils.getProperty(bean, this.getIndexedListProperty());
} catch(IllegalAccessException e) {
throw new ValidatorException(e.getMessage());
} catch(InvocationTargetException e) {
throw new ValidatorException(e.getMessage());
} catch(NoSuchMethodException e) {
throw new ValidatorException(e.getMessage());
}
if (indexedProperty instanceof Collection) {
return ((Collection) indexedProperty).toArray();
} else if (indexedProperty.getClass().isArray()) {
return (Object[]) indexedProperty;
} else {
throw new ValidatorException(this.getKey() + " is not indexed");
}
|
public java.lang.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.
return this.indexedProperty;
|
public java.lang.String | getKey()Gets a unique key based on the property and indexedProperty fields.
if (this.key == null) {
this.generateKey();
}
return this.key;
|
public java.lang.String | getMsg(java.lang.String key)Retrieve a message value.
return (String) hMsgs.get(key);
|
public int | getPage()Gets the page value that the Field is associated with for
validation.
return this.page;
|
public java.lang.String | getProperty()Gets the property name of the field.
return this.property;
|
public Var | getVar(java.lang.String mainKey)Retrieve a variable.
return (Var) hVars.get(mainKey);
|
public java.lang.String | getVarValue(java.lang.String mainKey)Retrieve a variable's value.
String value = null;
Object o = hVars.get(mainKey);
if (o != null && o instanceof Var) {
Var v = (Var) o;
value = v.getValue();
}
return value;
|
public java.util.Map | getVars()The Field 's variables are returned as an
unmodifiable Map .
return Collections.unmodifiableMap(hVars);
|
private void | handleMissingAction(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.
throw new ValidatorException(
"No ValidatorAction named "
+ name
+ " found for field "
+ this.getProperty());
|
public boolean | isDependency(java.lang.String validatorName)Checks if the validator is listed as a dependency.
return this.dependencyList.contains(validatorName);
|
public boolean | isIndexed()If there is a value specified for the indexedProperty field then
true will be returned. Otherwise it will be
false .
return ((indexedListProperty != null && indexedListProperty.length() > 0));
|
void | process(java.util.Map globalConstants, java.util.Map constants)Replace constants with values in fields and process the depends field
to create the dependency Map .
this.hMsgs.setFast(false);
this.hVars.setFast(true);
this.generateKey();
// Process FormSet Constants
for (Iterator i = constants.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String key2 = TOKEN_START + key + TOKEN_END;
String replaceValue = (String) constants.get(key);
property = ValidatorUtils.replace(property, key2, replaceValue);
processVars(key2, replaceValue);
this.processMessageComponents(key2, replaceValue);
}
// Process Global Constants
for (Iterator i = globalConstants.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String key2 = TOKEN_START + key + TOKEN_END;
String replaceValue = (String) globalConstants.get(key);
property = ValidatorUtils.replace(property, key2, replaceValue);
processVars(key2, replaceValue);
this.processMessageComponents(key2, replaceValue);
}
// Process Var Constant Replacement
for (Iterator i = hVars.keySet().iterator(); i.hasNext();) {
String key = (String) i.next();
String key2 = TOKEN_START + TOKEN_VAR + key + TOKEN_END;
Var var = this.getVar(key);
String replaceValue = var.getValue();
this.processMessageComponents(key2, replaceValue);
}
hMsgs.setFast(true);
|
private void | processArg(java.lang.String key, java.lang.String replaceValue)Replace the arg Collection key value with the key/value
pairs passed in.
for (int i = 0; i < this.args.length; i++) {
Map argMap = this.args[i];
if (argMap == null) {
continue;
}
Iterator iter = argMap.values().iterator();
while (iter.hasNext()) {
Arg arg = (Arg) iter.next();
if (arg != null) {
arg.setKey(
ValidatorUtils.replace(arg.getKey(), key, replaceValue));
}
}
}
|
private void | processMessageComponents(java.lang.String key, java.lang.String replaceValue)Replace the args key value with the key/value pairs passed in.
String varKey = TOKEN_START + TOKEN_VAR;
// Process Messages
if (key != null && !key.startsWith(varKey)) {
for (Iterator i = hMsgs.keySet().iterator(); i.hasNext();) {
String msgKey = (String) i.next();
String value = this.getMsg(msgKey);
hMsgs.put(msgKey, ValidatorUtils.replace(value, key, replaceValue));
}
}
this.processArg(key, replaceValue);
|
private void | processVars(java.lang.String key, java.lang.String replaceValue)Replace the vars value with the key/value pairs passed in.
Iterator i = this.hVars.keySet().iterator();
while (i.hasNext()) {
String varKey = (String) i.next();
Var var = this.getVar(varKey);
var.setValue(ValidatorUtils.replace(var.getValue(), key, replaceValue));
}
|
private boolean | runDependentValidators(ValidatorAction va, ValidatorResults results, java.util.Map actions, java.util.Map params, int pos)Calls all of the validators that this validator depends on.
TODO ValidatorAction should know how to run its own dependencies.
List dependentValidators = va.getDependencyList();
if (dependentValidators.isEmpty()) {
return true;
}
Iterator iter = dependentValidators.iterator();
while (iter.hasNext()) {
String depend = (String) iter.next();
ValidatorAction action = (ValidatorAction) actions.get(depend);
if (action == null) {
this.handleMissingAction(depend);
}
if (!this.validateForRule(action, results, actions, params, pos)) {
return false;
}
}
return true;
|
public void | setDepends(java.lang.String depends)Sets the validation rules for this field as a comma separated list.
this.depends = depends;
this.dependencyList.clear();
StringTokenizer st = new StringTokenizer(depends, ",");
while (st.hasMoreTokens()) {
String depend = st.nextToken().trim();
if (depend != null && depend.length() > 0) {
this.dependencyList.add(depend);
}
}
|
public void | setFieldOrder(int fieldOrder)Sets the position of the Field in the validation list.
this.fieldOrder = fieldOrder;
|
public void | setIndexedListProperty(java.lang.String indexedListProperty)Sets the indexed property name of the field.
this.indexedListProperty = indexedListProperty;
|
public void | setIndexedProperty(java.lang.String indexedProperty)Sets the indexed property name of the field.
this.indexedProperty = indexedProperty;
|
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.
this.key = key;
|
public void | setPage(int page)Sets the page value that the Field is associated with for
validation.
this.page = page;
|
public void | setProperty(java.lang.String property)Sets the property name of the field.
this.property = property;
|
public java.lang.String | toString()Returns a string representation of the object.
StringBuffer results = new StringBuffer();
results.append("\t\tkey = " + key + "\n");
results.append("\t\tproperty = " + property + "\n");
results.append("\t\tindexedProperty = " + indexedProperty + "\n");
results.append("\t\tindexedListProperty = " + indexedListProperty + "\n");
results.append("\t\tdepends = " + depends + "\n");
results.append("\t\tpage = " + page + "\n");
results.append("\t\tfieldOrder = " + fieldOrder + "\n");
if (hVars != null) {
results.append("\t\tVars:\n");
for (Iterator i = hVars.keySet().iterator(); i.hasNext();) {
Object key = i.next();
results.append("\t\t\t");
results.append(key);
results.append("=");
results.append(hVars.get(key));
results.append("\n");
}
}
return results.toString();
|
ValidatorResults | validate(java.util.Map params, java.util.Map actions)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.
if (this.getDepends() == null) {
return new ValidatorResults();
}
ValidatorResults allResults = new ValidatorResults();
Object bean = params.get(Validator.BEAN_PARAM);
int numberOfFieldsToValidate =
this.isIndexed() ? this.getIndexedProperty(bean).length : 1;
for (int fieldNumber = 0; fieldNumber < numberOfFieldsToValidate; fieldNumber++) {
Iterator dependencies = this.dependencyList.iterator();
while (dependencies.hasNext()) {
String depend = (String) dependencies.next();
ValidatorAction action = (ValidatorAction) actions.get(depend);
if (action == null) {
this.handleMissingAction(depend);
}
ValidatorResults results = new ValidatorResults();
boolean good =
validateForRule(action, results, actions, params, fieldNumber);
allResults.merge(results);
if (!good && numberOfFieldsToValidate <= 1) {
return allResults;
}
}
}
return allResults;
|
private boolean | validateForRule(ValidatorAction va, ValidatorResults results, java.util.Map actions, java.util.Map params, int pos)Executes the given ValidatorAction and all ValidatorActions that it
depends on.
ValidatorResult result = results.getValidatorResult(this.getKey());
if (result != null && result.containsAction(va.getName())) {
return result.isValid(va.getName());
}
if (!this.runDependentValidators(va, results, actions, params, pos)) {
return false;
}
return va.executeValidationMethod(this, params, results, pos);
|