}
// Create list of ValidatorActions based on lActionMethods
for (Iterator i = lActionMethods.iterator(); i.hasNext();) {
String depends = (String) i.next();
ValidatorAction va = resources.getValidatorAction(depends);
// throw nicer NPE for easier debugging
if (va == null) {
throw new NullPointerException(
"Depends string \""
+ depends
+ "\" was not found in validator-rules.xml.");
}
String javascript = va.getJavascript();
if (javascript != null && javascript.length() > 0) {
lActions.add(va);
} else {
i.remove();
}
}
Collections.sort(lActions, new Comparator() {
public int compare(Object o1, Object o2) {
ValidatorAction va1 = (ValidatorAction) o1;
ValidatorAction va2 = (ValidatorAction) o2;
if ((va1.getDepends() == null || va1.getDepends().length() == 0)
&& (va2.getDepends() == null || va2.getDepends().length() == 0)) {
return 0;
} else if (
(va1.getDepends() != null && va1.getDepends().length() > 0)
&& (va2.getDepends() == null || va2.getDepends().length() == 0)) {
return 1;
} else if (
(va1.getDepends() == null || va1.getDepends().length() == 0)
&& (va2.getDepends() != null && va2.getDepends().length() > 0)) {
return -1;
} else {
return va1.getDependencies().size() - va2.getDependencies().size();
}
}
});
String methods = null;
for (Iterator i = lActions.iterator(); i.hasNext();) {
ValidatorAction va = (ValidatorAction) i.next();
if (methods == null) {
methods = va.getMethod() + "(form)";
} else {
methods += " && " + va.getMethod() + "(form)";
}
}
results.append(getJavascriptBegin(methods));
for (Iterator i = lActions.iterator(); i.hasNext();) {
ValidatorAction va = (ValidatorAction) i.next();
String jscriptVar = null;
String functionName = null;
if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
functionName = va.getJsFunctionName();
} else {
functionName = va.getName();
}
results.append(" function " + functionName + " () { \n");
for (Iterator x = form.getFields().iterator(); x.hasNext();) {
Field field = (Field) x.next();
// Skip indexed fields for now until there is
// a good way to handle error messages (and the length of the list (could retrieve from scope?))
if (!field.isIndexed()
&& field.getPage() == page
&& field.isDependency(va.getName())) {
String message = Resources.getMessage(messages, locale, va, field);
message = (message != null ? message : "");
jscriptVar = getNextVar(jscriptVar);