📄 field.java
字号:
Object o = hVars.get(mainKey);
if (o != null && o instanceof Var) {
Var v = (Var) o;
value = v.getValue();
}
return value;
}
/**
* The <code>Field</code>'s variables are returned as an
* unmodifiable <code>Map</code>.
* @return the Map of Variable's for a Field.
*/
public Map getVars() {
return Collections.unmodifiableMap(hVars);
}
/**
* Gets a unique key based on the property and indexedProperty fields.
* @return a unique key for the field.
*/
public String getKey() {
if (this.key == null) {
this.generateKey();
}
return this.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.
* @param key a unique key for the field
*/
public void setKey(String key) {
this.key = key;
}
/**
* If there is a value specified for the indexedProperty field then
* <code>true</code> will be returned. Otherwise it will be
* <code>false</code>.
* @return Whether the Field is indexed.
*/
public boolean isIndexed() {
return ((indexedListProperty != null && indexedListProperty.length() > 0));
}
/**
* Generate correct <code>key</code> value.
*/
public void generateKey() {
if (this.isIndexed()) {
this.key = this.indexedListProperty + TOKEN_INDEXED + "." + this.property;
} else {
this.key = this.property;
}
}
/**
* Replace constants with values in fields and process the depends field
* to create the dependency <code>Map</code>.
*/
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 the vars value with the key/value pairs passed in.
*/
private void processVars(String key, String replaceValue) {
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));
}
}
/**
* Replace the args key value with the key/value pairs passed in.
*/
private void processMessageComponents(String key, String replaceValue) {
String varKey = TOKEN_START + TOKEN_VAR;
// Process Messages
if (key != null && !key.startsWith(varKey)) {
for (Iterator i = hMsgs.values().iterator(); i.hasNext();) {
Msg msg = (Msg) i.next();
msg.setKey(ValidatorUtils.replace(msg.getKey(), key, replaceValue));
}
}
this.processArg(key, replaceValue);
}
/**
* Replace the arg <code>Collection</code> key value with the key/value
* pairs passed in.
*/
private void processArg(String key, String replaceValue) {
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));
}
}
}
}
/**
* Checks if the validator is listed as a dependency.
* @param validatorName Name of the validator to check.
* @return Whether the field is dependant on a validator.
*/
public boolean isDependency(String validatorName) {
return this.dependencyList.contains(validatorName);
}
/**
* Gets an unmodifiable <code>List</code> of the dependencies in the same
* order they were defined in parameter passed to the setDepends() method.
* @return A list of the Field's dependancies.
*/
public List getDependencyList() {
return Collections.unmodifiableList(this.dependencyList);
}
/**
* Creates and returns a copy of this object.
* @return A copy of 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;
}
/**
* Returns a string representation of the object.
* @return A string representation of the object.
*/
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 an indexed property from the object we're validating.
*
* @param 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.
*/
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 the size of an indexed property from the object we're validating.
*
* @param 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.
*/
private int getIndexedPropertySize(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 == null) {
return 0;
} else if (indexedProperty instanceof Collection) {
return ((Collection)indexedProperty).size();
} else if (indexedProperty.getClass().isArray()) {
return ((Object[])indexedProperty).length;
} else {
throw new ValidatorException(this.getKey() + " is not indexed");
}
}
/**
* Executes the given ValidatorAction and all ValidatorActions that it
* depends on.
* @return true if the validation succeeded.
*/
private boolean validateForRule(
ValidatorAction va,
ValidatorResults results,
Map actions,
Map params,
int pos)
throws ValidatorException {
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);
}
/**
* Calls all of the validators that this validator depends on.
* TODO ValidatorAction should know how to run its own dependencies.
* @param va Run dependent validators for this action.
* @param results
* @param actions
* @param pos
* @return true if all of the dependent validations passed.
* @throws ValidatorException If there's an error running a validator
*/
private boolean runDependentValidators(
ValidatorAction va,
ValidatorResults results,
Map actions,
Map params,
int pos)
throws ValidatorException {
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;
}
/**
* 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.
* @param params A Map of parameter class names to parameter values to pass
* into validation methods.
* @param actions A Map of validator names to ValidatorAction objects.
* @return A ValidatorResults object containing validation messages for
* this field.
* @throws ValidatorException If an error occurs during validation.
*/
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;
}
/**
* Called when a validator name is used in a depends clause but there is
* no know ValidatorAction configured for that name.
* @param name The name of the validator in the depends list.
* @throws ValidatorException
*/
private void handleMissingAction(String name) throws ValidatorException {
throw new ValidatorException("No ValidatorAction named " + name
+ " found for field " + this.getProperty());
}
/**
* Returns a Map of String Msg names to Msg objects.
* @since Validator 1.2.0
* @return A Map of the Field's messages.
*/
protected Map getMsgMap() {
return hMsgs;
}
/**
* Returns a Map of String Var names to Var objects.
* @since Validator 1.2.0
* @return A Map of the Field's variables.
*/
protected Map getVarMap() {
return hVars;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -