| Method from org.apache.commons.validator.Field Detail: |
public void addArg(Arg arg) {
// 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;
}
determineArgPosition(arg);
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);
}
}
Add an Arg to the replacement argument list. |
public void addMsg(Msg msg) {
hMsgs.put(msg.getName(), msg);
}
|
public void addVar(Var v) {
this.hVars.put(v.getName(), v);
}
|
public void addVar(String name,
String value,
String jsType) {
this.addVar(new Var(name, value, jsType));
}
Add a Var, based on the values passed in, to the
Field. |
public Object clone() {
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;
}
Creates and returns a copy of this object. |
public void generateKey() {
if (this.isIndexed()) {
this.key = this.indexedListProperty + TOKEN_INDEXED + "." + this.property;
} else {
this.key = this.property;
}
}
Generate correct key value. |
public Arg getArg(int position) {
return this.getArg(DEFAULT_ARG, position);
}
Gets the default Arg object at the given position. |
public Arg getArg(String key,
int position) {
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;
}
Gets the Arg object at the given position. If the key
finds a null value then the default value will be
retrieved. |
public Arg[] getArgs(String key) {
Arg[] args = new Arg[this.args.length];
for (int i = 0; i < this.args.length; i++) {
args[i] = this.getArg(key, i);
}
return args;
}
Retrieves the Args for the given validator name. |
public List getDependencyList() {
return Collections.unmodifiableList(this.dependencyList);
}
Gets an unmodifiable List of the dependencies in the same
order they were defined in parameter passed to the setDepends() method. |
public String getDepends() {
return this.depends;
}
Gets the validation rules for this field as a comma separated list. |
public int getFieldOrder() {
return this.fieldOrder;
}
Gets the position of the Field in the validation list. |
public String getIndexedListProperty() {
return this.indexedListProperty;
}
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. |
public String getIndexedProperty() {
return this.indexedProperty;
}
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. |
Object[] getIndexedProperty(Object bean) throws ValidatorException {
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");
}
}
Returns an indexed property from the object we're validating. |
public String getKey() {
if (this.key == null) {
this.generateKey();
}
return this.key;
}
Gets a unique key based on the property and indexedProperty fields. |
public Msg getMessage(String key) {
return (Msg) hMsgs.get(key);
}
Retrieve a message object. |
public Map getMessages() {
return Collections.unmodifiableMap(hMsgs);
}
The Field's messages are returned as an
unmodifiable Map. |
public String getMsg(String key) {
Msg msg = getMessage(key);
return (msg == null) ? null : msg.getKey();
}
Retrieve a message value. |
protected Map getMsgMap() {
return hMsgs;
}
Returns a Map of String Msg names to Msg objects. |
public int getPage() {
return this.page;
}
Gets the page value that the Field is associated with for
validation. |
public String getProperty() {
return this.property;
}
Gets the property name of the field. |
public Var getVar(String mainKey) {
return (Var) hVars.get(mainKey);
}
|
protected Map getVarMap() {
return hVars;
}
Returns a Map of String Var names to Var objects. |
public String getVarValue(String mainKey) {
String value = null;
Object o = hVars.get(mainKey);
if (o != null && o instanceof Var) {
Var v = (Var) o;
value = v.getValue();
}
return value;
}
Retrieve a variable's value. |
public Map getVars() {
return Collections.unmodifiableMap(hVars);
}
The Field's variables are returned as an
unmodifiable Map. |
public boolean isDependency(String validatorName) {
return this.dependencyList.contains(validatorName);
}
Checks if the validator is listed as a dependency. |
public boolean isIndexed() {
return ((indexedListProperty != null && indexedListProperty.length() > 0));
}
If there is a value specified for the indexedProperty field then
true will be returned. Otherwise it will be
false. |
void process(Map globalConstants,
Map constants) {
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);
}
Replace constants with values in fields and process the depends field
to create the dependency Map. |
public void setDepends(String depends) {
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);
}
}
}
Sets the validation rules for this field as a comma separated list. |
public void setFieldOrder(int fieldOrder) {
this.fieldOrder = fieldOrder;
}
Sets the position of the Field in the validation list. |
public void setIndexedListProperty(String indexedListProperty) {
this.indexedListProperty = indexedListProperty;
}
Sets the indexed property name of the field. |
public void setIndexedProperty(String indexedProperty) {
this.indexedProperty = indexedProperty;
}
Sets the indexed property name of the field. |
public void setKey(String key) {
this.key = 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. |
public void setPage(int page) {
this.page = page;
}
Sets the page value that the Field is associated with for
validation. |
public void setProperty(String property) {
this.property = property;
}
Sets the property name of the field. |
public String toString() {
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();
}
Returns a string representation of the object. |
public ValidatorResults validate(Map params,
Map actions) throws ValidatorException {
if (this.getDepends() == null) {
return new ValidatorResults();
}
ValidatorResults allResults = new ValidatorResults();
Object bean = params.get(Validator.BEAN_PARAM);
int numberOfFieldsToValidate =
this.isIndexed() ? this.getIndexedPropertySize(bean) : 1;
for (int fieldNumber = 0; fieldNumber < numberOfFieldsToValidate; fieldNumber++) {
Iterator dependencies = this.dependencyList.iterator();
ValidatorResults results = new ValidatorResults();
while (dependencies.hasNext()) {
String depend = (String) dependencies.next();
ValidatorAction action = (ValidatorAction) actions.get(depend);
if (action == null) {
this.handleMissingAction(depend);
}
boolean good =
validateForRule(action, results, actions, params, fieldNumber);
if (!good) {
allResults.merge(results);
return allResults;
}
}
allResults.merge(results);
}
return allResults;
}
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. |