Contains the information to dynamically create and run a validation
method. This is the class representation of a pluggable validator that can
be defined in an xml file with the <validator> element.
: The validation method is assumed to be thread safe.
| Method from org.apache.commons.validator.ValidatorAction Detail: |
boolean executeValidationMethod(Field field,
Map params,
ValidatorResults results,
int pos) throws ValidatorException {
params.put(Validator.VALIDATOR_ACTION_PARAM, this);
try {
if (this.validationMethod == null) {
synchronized(this) {
ClassLoader loader = this.getClassLoader(params);
this.loadValidationClass(loader);
this.loadParameterClasses(loader);
this.loadValidationMethod();
}
}
Object[] paramValues = this.getParameterValues(params);
if (field.isIndexed()) {
this.handleIndexedField(field, pos, paramValues);
}
Object result = null;
try {
result =
validationMethod.invoke(
getValidationClassInstance(),
paramValues);
} catch (IllegalArgumentException e) {
throw new ValidatorException(e.getMessage());
} catch (IllegalAccessException e) {
throw new ValidatorException(e.getMessage());
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof Exception) {
throw (Exception) e.getTargetException();
} else if (e.getTargetException() instanceof Error) {
throw (Error) e.getTargetException();
}
}
boolean valid = this.isValid(result);
if (!valid || (valid && !onlyReturnErrors(params))) {
results.add(field, this.name, valid, result);
}
if (!valid) {
return false;
}
// TODO This catch block remains for backward compatibility. Remove
// this for Validator 2.0 when exception scheme changes.
} catch (Exception e) {
if (e instanceof ValidatorException) {
throw (ValidatorException) e;
}
getLog().error(
"Unhandled exception thrown during validation: " + e.getMessage(),
e);
results.add(field, this.name, false);
return false;
}
return true;
}
Dynamically runs the validation method for this validator and returns
true if the data is valid. |
public String getClassname() {
return classname;
}
Gets the class of the validator action. |
public List getDependencyList() {
return Collections.unmodifiableList(this.dependencyList);
}
Returns the dependent validator names as an unmodifiable
List. |
public String getDepends() {
return this.depends;
}
Gets the dependencies of the validator action as a comma separated list
of validator names. |
public String getJavascript() {
return javascript;
}
Gets the Javascript equivalent of the java class and method
associated with this action. |
public String getJsFunctionName() {
return jsFunctionName;
}
Gets the Javascript function name. This is optional and can
be used instead of validator action name for the name of the
Javascript function/object. |
public String getMethod() {
return method;
}
Gets the name of method being called for the validator action. |
public String getMethodParams() {
return methodParams;
}
Gets the method parameters for the method. |
public String getMsg() {
return msg;
}
Gets the message associated with the validator action. |
public String getName() {
return name;
}
Gets the name of the validator action. |
protected void init() {
this.loadJavascriptFunction();
}
|
public boolean isDependency(String validatorName) {
return this.dependencyList.contains(validatorName);
}
Checks whether or not the value passed in is in the depends field. |
protected synchronized void loadJavascriptFunction() {
if (this.javascriptAlreadyLoaded()) {
return;
}
if (getLog().isTraceEnabled()) {
getLog().trace(" Loading function begun");
}
if (this.jsFunction == null) {
this.jsFunction = this.generateJsFunction();
}
String javascriptFileName = this.formatJavascriptFileName();
if (getLog().isTraceEnabled()) {
getLog().trace(" Loading js function '" + javascriptFileName + "'");
}
this.javascript = this.readJavascriptFile(javascriptFileName);
if (getLog().isTraceEnabled()) {
getLog().trace(" Loading javascript function completed");
}
}
Load the javascript function specified by the given path. For this
implementation, the jsFunction property should contain a
fully qualified package and script name, separated by periods, to be
loaded from the class loader that created this instance.
TODO if the path begins with a '/' the path will be intepreted as
absolute, and remain unchanged. If this fails then it will attempt to
treat the path as a file path. It is assumed the script ends with a
'.js'. |
public void setClassname(String classname) {
this.classname = classname;
}
Sets the class of the validator action. |
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 dependencies of the validator action. |
public void setJavascript(String javascript) {
if (jsFunction != null) {
throw new IllegalStateException("Cannot call setJavascript() after calling setJsFunction()");
}
this.javascript = javascript;
}
Sets the Javascript equivalent of the java class and method
associated with this action. |
public void setJsFunction(String jsFunction) {
if (javascript != null) {
throw new IllegalStateException("Cannot call setJsFunction() after calling setJavascript()");
}
this.jsFunction = jsFunction;
}
Sets the fully qualified class path of the Javascript function.
This is optional and can be used instead of the setJavascript().
Attempting to call both setJsFunction and setJavascript
will result in an IllegalStateException being thrown.
If neither setJsFunction or setJavascript is set then
validator will attempt to load the default javascript definition.
Examples
If in the validator.xml :
#1:
<validator name="tire"
jsFunction="com.yourcompany.project.tireFuncion">
Validator will attempt to load com.yourcompany.project.validateTireFunction.js from
its class path.
#2:
<validator name="tire">
Validator will use the name attribute to try and load
org.apache.commons.validator.javascript.validateTire.js
which is the default javascript definition.
|
public void setJsFunctionName(String jsFunctionName) {
this.jsFunctionName = jsFunctionName;
}
Sets the Javascript function name. This is optional and can
be used instead of validator action name for the name of the
Javascript function/object. |
public void setMethod(String method) {
this.method = method;
}
Sets the name of method being called for the validator action. |
public void setMethodParams(String methodParams) {
this.methodParams = methodParams;
this.methodParameterList.clear();
StringTokenizer st = new StringTokenizer(methodParams, ",");
while (st.hasMoreTokens()) {
String value = st.nextToken().trim();
if (value != null && value.length() > 0) {
this.methodParameterList.add(value);
}
}
}
Sets the method parameters for the method. |
public void setMsg(String msg) {
this.msg = msg;
}
Sets the message associated with the validator action. |
public void setName(String name) {
this.name = name;
}
Sets the name of the validator action. |
public String toString() {
StringBuffer results = new StringBuffer("ValidatorAction: ");
results.append(name);
results.append("\n");
return results.toString();
}
Returns a string representation of the object. |