Example usage for org.apache.commons.validator Form getFields

List of usage examples for org.apache.commons.validator Form getFields

Introduction

In this page you can find the example usage for org.apache.commons.validator Form getFields.

Prototype

public List<Field> getFields() 

Source Link

Document

A List of Fields is returned as an unmodifiable List.

Usage

From source file:net.jawr.web.resource.bundle.generator.validator.CommonsValidatorGenerator.java

@SuppressWarnings("unchecked")
private List<ValidatorAction> createActionList(ValidatorResources resources, Form form) {
    List<String> actionMethods = new ArrayList<String>();

    Iterator<Field> itFields = form.getFields().iterator();

    while (itFields.hasNext()) {
        Field field = itFields.next();

        for (Iterator<String> x = field.getDependencyList().iterator(); x.hasNext();) {
            String o = x.next();//from  www  .  j a va 2s.  com

            if ((o != null) && !actionMethods.contains(o)) {
                actionMethods.add(o);
            }
        }
    }

    List<ValidatorAction> actions = new ArrayList<ValidatorAction>();

    // Create list of ValidatorActions based on actionMethods
    Iterator<String> iterator = actionMethods.iterator();

    while (iterator.hasNext()) {
        String depends = iterator.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.");
        }

        if ((va.getJavascript() != null) && (va.getJavascript().length() > 0)) {
            actions.add(va);
        } else {
            iterator.remove();
        }
    }

    Collections.sort(actions, ACTION_COMPARATOR);

    return actions;
}

From source file:net.jawr.web.resource.bundle.generator.validator.CommonsValidatorGenerator.java

@SuppressWarnings("unchecked")
private StringBuffer createDynamicJavascript(ValidatorResources resources, Form form, String messageNS,
        boolean stopOnErrors) {
    StringBuffer results = new StringBuffer();

    List<ValidatorAction> actions = this.createActionList(resources, form);

    final String methods = this.createMethods(actions, stopOnErrors);

    String jsFormName = form.getName();

    results.append(this.getJavascriptBegin(jsFormName, methods));

    for (Iterator<ValidatorAction> i = actions.iterator(); i.hasNext();) {
        ValidatorAction va = i.next();/* www  .j  a  va2s. c  om*/
        int jscriptVar = 0;
        String functionName = null;

        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }

        results.append("    function " + jsFormName + "_" + functionName + " () { \n");

        for (Iterator<Field> x = form.getFields().iterator(); x.hasNext();) {
            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.isDependency(va.getName())) {
                continue;
            }

            String message = null;
            Msg msg = field.getMessage(va.getName());
            if ((msg != null) && !msg.isResource()) {
                message = JavascriptStringUtil.quote(msg.toString());
            } else {
                if (msg == null) {
                    message = va.getMsg();
                } else {
                    message = msg.getKey();
                }
                Arg[] args = field.getArgs(va.getName());

                message = messageNS + "." + message + "(";
                for (int a = 0; a < args.length; a++) {
                    if (args[a] != null) {
                        if (args[a].isResource()) {
                            message += messageNS + "." + args[a].getKey() + "()";
                        } else {
                            message += "\"" + args[a].getKey() + "\"";
                        }
                        message += ",";
                    }
                }
                message += "null)";
            }

            message = (message != null) ? message : "";

            // prefix variable with 'a' to make it a legal identifier
            results.append(
                    "     this.a" + jscriptVar++ + " = [\"" + field.getKey() + "\", " + (message) + ", ");

            results.append("function(varName){");

            Map<String, Var> vars = field.getVars();

            // Loop through the field's variables.
            Iterator<Entry<String, Var>> varsIterator = vars.entrySet().iterator();

            while (varsIterator.hasNext()) {

                Entry<String, Var> varEntry = varsIterator.next();
                String varName = varEntry.getKey();
                Var var = varEntry.getValue();
                String varValue = var.getValue();

                // Non-resource variable
                if (var.isResource()) {
                    varValue = messageNS + "." + varValue + "()";
                } else
                    varValue = "'" + varValue + "'";
                String jsType = var.getJsType();

                // skip requiredif variables field, fieldIndexed, fieldTest,
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }

                String varValueEscaped = JavascriptStringUtil.escape(varValue);

                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=+" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=eval('/'+" + varValueEscaped + "+'/'); ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");

                    // So everyone using the latest format doesn't need to
                    // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=eval('/'+" + varValueEscaped + "+'/'); ");
                } else {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                }
            }

            results.append(" return this[varName];}];\n");
        }

        results.append("    } \n\n");
    }
    return results;
}

From source file:org.apache.struts.faces.taglib.JavascriptValidatorTag.java

/**
 * Render the JavaScript for to perform validations based on the form name.
 *
 * @exception JspException if a JSP exception has occurred
 *//*w ww  .j  av  a2  s  .c  o m*/
public int doStartTag() throws JspException {
    StringBuffer results = new StringBuffer();

    ModuleConfig config = RequestUtils.getModuleConfig(pageContext);
    ValidatorResources resources = (ValidatorResources) pageContext
            .getAttribute(ValidatorPlugIn.VALIDATOR_KEY + config.getPrefix(), PageContext.APPLICATION_SCOPE);

    Locale locale = RequestUtils.retrieveUserLocale(this.pageContext, null);

    Form form = resources.get(locale, formName);
    if (form != null) {
        if ("true".equalsIgnoreCase(dynamicJavascript)) {
            MessageResources messages = (MessageResources) pageContext.getAttribute(bundle + config.getPrefix(),
                    PageContext.APPLICATION_SCOPE);

            List lActions = new ArrayList();
            List lActionMethods = new ArrayList();

            // Get List of actions for this Form
            for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                Field field = (Field) i.next();

                for (Iterator x = field.getDependencies().iterator(); x.hasNext();) {
                    Object o = x.next();

                    if (o != null && !lActionMethods.contains(o)) {
                        lActionMethods.add(o);
                    }
                }

            }

            // 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();
                }

                if (isStruts11()) {
                    results.append("    function " + functionName + " () { \n");
                } else {
                    results.append("    function " + formName + "_" + 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())) {

                        continue;
                    }

                    String message = Resources.getMessage(messages, locale, va, field);

                    message = (message != null) ? message : "";

                    jscriptVar = this.getNextVar(jscriptVar);

                    results.append("     this." + jscriptVar + " = new Array(\"" + getFormClientId() + ":"
                            + field.getKey() + "\", \"" + message + "\", ");

                    results.append("new Function (\"varName\", \"");

                    Map vars = field.getVars();
                    // Loop through the field's variables.
                    Iterator varsIterator = vars.keySet().iterator();
                    while (varsIterator.hasNext()) {
                        String varName = (String) varsIterator.next();
                        Var var = (Var) vars.get(varName);
                        String varValue = var.getValue();
                        String jsType = var.getJsType();

                        // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                        if (varName.startsWith("field")) {
                            continue;
                        }

                        if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "="
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "; ");
                        } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                            // So everyone using the latest format doesn't need to change their xml files immediately.
                        } else if ("mask".equalsIgnoreCase(varName)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                        }
                    }

                    results.append(" return this[varName];\"));\n");
                }
                results.append("    } \n\n");
            }
        } else if ("true".equalsIgnoreCase(staticJavascript)) {
            results.append(this.getStartElement());
            if ("true".equalsIgnoreCase(htmlComment)) {
                results.append(htmlBeginComment);
            }
        }
    }

    if ("true".equalsIgnoreCase(staticJavascript)) {
        results.append(getJavascriptStaticMethods(resources));
    }

    if (form != null
            && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) {

        results.append(getJavascriptEnd());
    }

    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e.getMessage());
    }

    return (EVAL_BODY_TAG);

}

From source file:org.apache.struts.taglib.html.JavascriptValidatorTag.java

/**
 * Generates the dynamic JavaScript for the form.
 *
 * @param config//from w  w w. ja v a2  s  .  com
 * @param resources
 * @param locale
 * @param form
 */
private String createDynamicJavascript(ModuleConfig config, ValidatorResources resources, Locale locale,
        Form form) throws JspException {
    StringBuffer results = new StringBuffer();

    MessageResources messages = TagUtils.getInstance().retrieveMessageResources(pageContext, bundle, true);

    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();

    List actions = this.createActionList(resources, form);

    final String methods = this.createMethods(actions, this.stopOnError(config));

    String formName = form.getName();

    jsFormName = formName;

    if (jsFormName.charAt(0) == '/') {
        String mappingName = TagUtils.getInstance().getActionMappingName(jsFormName);
        ActionMapping mapping = (ActionMapping) config.findActionConfig(mappingName);

        if (mapping == null) {
            JspException e = new JspException(messages.getMessage("formTag.mapping", mappingName));

            pageContext.setAttribute(Globals.EXCEPTION_KEY, e, PageContext.REQUEST_SCOPE);
            throw e;
        }

        jsFormName = mapping.getAttribute();
    }

    results.append(this.getJavascriptBegin(methods));

    for (Iterator i = actions.iterator(); i.hasNext();) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;

        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }

        results.append("    function " + jsFormName + "_" + 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())) {
                continue;
            }

            String message = Resources.getMessage(application, request, messages, locale, va, field);

            message = (message != null) ? message : "";

            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \""
                    + escapeQuotes(message) + "\", ");

            results.append("new Function (\"varName\", \"");

            Map vars = field.getVars();

            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();

            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = Resources.getVarValue(var, application, request, false);
                String jsType = var.getJsType();

                // skip requiredif variables field, fieldIndexed, fieldTest,
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }

                String varValueEscaped = escapeJavascript(varValue);

                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");

                    // So everyone using the latest format doesn't need to
                    // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }

            results.append(" return this[varName];\"));\n");
        }

        results.append("    } \n\n");
    }

    return results.toString();
}

From source file:org.apache.struts.taglib.html.JavascriptValidatorTag.java

/**
 * Get List of actions for the given Form.
 *
 * @param resources/*from   w ww  . j  a v  a 2s.  c om*/
 * @param form
 * @return A sorted List of ValidatorAction objects.
 */
private List createActionList(ValidatorResources resources, Form form) {
    List actionMethods = new ArrayList();

    Iterator iterator = form.getFields().iterator();

    while (iterator.hasNext()) {
        Field field = (Field) iterator.next();

        for (Iterator x = field.getDependencyList().iterator(); x.hasNext();) {
            Object o = x.next();

            if ((o != null) && !actionMethods.contains(o)) {
                actionMethods.add(o);
            }
        }
    }

    List actions = new ArrayList();

    // Create list of ValidatorActions based on actionMethods
    iterator = actionMethods.iterator();

    while (iterator.hasNext()) {
        String depends = (String) iterator.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.");
        }

        if ((va.getJavascript() != null) && (va.getJavascript().length() > 0)) {
            actions.add(va);
        } else {
            iterator.remove();
        }
    }

    Collections.sort(actions, actionComparator);

    return actions;
}

From source file:org.apache.velocity.tools.struts.ValidatorTool.java

/**
 * Generates the dynamic JavaScript for the form.
 *
 * @param resources the validator resources
 * @param locale the locale for the current request
 * @param form the form to generate javascript for
 * @return the dynamic javascript//from   w ww . j  a v a2 s.  co m
 */
protected String getDynamicJavascript(ValidatorResources resources, Locale locale, Form form) {
    StringBuffer results = new StringBuffer();

    MessageResources messages = StrutsUtils.getMessageResources(request, app);

    List actions = createActionList(resources, form);

    final String methods = createMethods(actions);

    String formName = form.getName();

    jsFormName = formName;
    if (jsFormName.charAt(0) == '/') {
        String mappingName = StrutsUtils.getActionMappingName(jsFormName);
        ModuleConfig mconfig = ModuleUtils.getInstance().getModuleConfig(request, app);

        ActionConfig mapping = (ActionConfig) mconfig.findActionConfig(mappingName);
        if (mapping == null) {
            throw new NullPointerException("Cannot retrieve mapping for action " + mappingName);
        }
        jsFormName = mapping.getAttribute();
    }

    results.append(getJavascriptBegin(methods));

    for (Iterator i = actions.iterator(); i.hasNext();) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;

        if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }

        results.append("    function ");
        results.append(jsFormName);
        results.append("_");
        results.append(functionName);
        results.append(" () { \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())) {
                continue;
            }

            String message = Resources.getMessage(messages, locale, va, field);

            message = (message != null) ? message : "";

            //jscriptVar = this.getNextVar(jscriptVar);

            results.append("     this.a");
            results.append(jscriptVar++);
            results.append(" = new Array(\"");
            results.append(field.getKey()); // TODO: escape?
            results.append("\", \"");
            results.append(escapeJavascript(message));
            results.append("\", ");
            results.append("new Function (\"varName\", \"");

            Map vars = field.getVars();
            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();
            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next(); // TODO: escape?
                Var var = (Var) vars.get(varName);
                String varValue = var.getValue();
                String jsType = var.getJsType();

                // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }

                // these are appended no matter what jsType is
                results.append("this.");
                results.append(varName);

                String escapedVarValue = escapeJavascript(varValue);

                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("=");
                    results.append(escapedVarValue);
                    results.append("; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("=/");
                    results.append(escapedVarValue);
                    results.append("/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("='");
                    results.append(escapedVarValue);
                    results.append("'; ");
                }
                // So everyone using the latest format
                // doesn't need to change their xml files immediately.
                else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("=/");
                    results.append(escapedVarValue);
                    results.append("/; ");
                } else {
                    results.append("='");
                    results.append(escapedVarValue);
                    results.append("'; ");
                }
            }
            results.append(" return this[varName];\"));\n");
        }
        results.append("    } \n\n");
    }
    return results.toString();
}

From source file:org.apache.velocity.tools.struts.ValidatorTool.java

/**
 * Get List of actions for the given Form.
 *
 * @param resources the validator resources
 * @param form the form for which the actions are requested
 * @return A sorted List of ValidatorAction objects.
 *///w w  w . ja  v  a2  s. c o m
protected List createActionList(ValidatorResources resources, Form form) {
    List actionMethods = new ArrayList();
    // Get List of actions for this Form
    for (Iterator i = form.getFields().iterator(); i.hasNext();) {
        Field field = (Field) i.next();
        for (Iterator x = field.getDependencyList().iterator(); x.hasNext();) {
            Object o = x.next();
            if (o != null && !actionMethods.contains(o)) {
                actionMethods.add(o);
            }
        }
    }

    List actions = new ArrayList();

    // Create list of ValidatorActions based on actionMethods
    for (Iterator i = actionMethods.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) {
            actions.add(va);
        } else {
            i.remove();
        }
    }

    Collections.sort(actions, actionComparator);
    return actions;
}

From source file:org.caofei.taglib.html.JavascriptValidatorTag.java

/**
 * Generates the dynamic JavaScript for the form.
 *
 * @param config/*from   w w  w  .  ja v  a2 s. co m*/
 * @param resources
 * @param locale
 * @param form
 */
private String createDynamicJavascript(ValidatorResources resources, Locale locale, Form form)
        throws JspException {
    StringBuffer results = new StringBuffer();
    /*
            MessageResources messages =
    TagUtils.getInstance().retrieveMessageResources(pageContext,
        bundle, true);
    */
    HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
    ServletContext application = pageContext.getServletContext();

    List actions = this.createActionList(resources, form);

    final String methods = this.createMethods(actions, this.stopOnError());

    String formName = form.getName();

    jsFormName = formName;

    /*
    if (jsFormName.charAt(0) == '/') {
    String mappingName =
        TagUtils.getInstance().getActionMappingName(jsFormName);
    ActionMapping mapping =
        (ActionMapping) config.findActionConfig(mappingName);
            
    if (mapping == null) {
        JspException e =
            new JspException(messages.getMessage("formTag.mapping",
                    mappingName));
            
        pageContext.setAttribute(Globals.EXCEPTION_KEY, e,
            PageContext.REQUEST_SCOPE);
        throw e;
    }
    jsFormName = mapping.getAttribute();
    }
     */

    results.append(this.getJavascriptBegin(methods));

    for (Iterator i = actions.iterator(); i.hasNext();) {
        ValidatorAction va = (ValidatorAction) i.next();
        int jscriptVar = 0;
        String functionName = null;

        if ((va.getJsFunctionName() != null) && (va.getJsFunctionName().length() > 0)) {
            functionName = va.getJsFunctionName();
        } else {
            functionName = va.getName();
        }

        results.append("    function " + jsFormName + "_" + 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())) {
                continue;
            }

            String message = null;
            /*
            Resources.getMessage(application, request, messages,
                locale, va, field);
            */
            message = (message != null) ? message : "";

            // prefix variable with 'a' to make it a legal identifier
            results.append("     this.a" + jscriptVar++ + " = new Array(\"" + field.getKey() + "\", \""
                    + escapeQuotes(message) + "\", ");

            results.append("new Function (\"varName\", \"");

            Map vars = field.getVars();

            // Loop through the field's variables.
            Iterator varsIterator = vars.keySet().iterator();

            while (varsIterator.hasNext()) {
                String varName = (String) varsIterator.next();
                Var var = (Var) vars.get(varName);
                String varValue = null;
                /*
                Resources.getVarValue(var, application, request, false);
                */
                String jsType = var.getJsType();

                // skip requiredif variables field, fieldIndexed, fieldTest,
                // fieldValue
                if (varName.startsWith("field")) {
                    continue;
                }

                String varValueEscaped = escapeJavascript(varValue);

                if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=" + varValueEscaped + "; ");
                } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");

                    // So everyone using the latest format doesn't need to
                    // change their xml files immediately.
                } else if ("mask".equalsIgnoreCase(varName)) {
                    results.append("this." + varName + "=/" + varValueEscaped + "/; ");
                } else {
                    results.append("this." + varName + "='" + varValueEscaped + "'; ");
                }
            }

            results.append(" return this[varName];\"));\n");
        }

        results.append("    } \n\n");
    }

    return results.toString();
}

From source file:org.springmodules.commons.validator.taglib.JavascriptValidatorTag.java

/**
 * Render the JavaScript for to perform validations based on the form name.
 *
 * @throws JspException if a JSP exception has occurred
 *///from ww  w .  ja v  a 2  s .  c o m
public int doStartTag() throws JspException {
    StringBuffer results = new StringBuffer();

    Locale locale = pageContext.getRequest().getLocale();

    ValidatorResources resources = getValidatorResources();

    Form form = resources.get(locale, formName);
    if (form != null) {
        if ("true".equalsIgnoreCase(dynamicJavascript)) {
            MessageSource messages = getMessageSource();

            List lActions = new ArrayList();
            List lActionMethods = new ArrayList();

            // Get List of actions for this Form
            for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                Field field = (Field) i.next();

                for (Iterator x = field.getDependencies().iterator(); x.hasNext();) {
                    Object o = x.next();

                    if (o != null && !lActionMethods.contains(o)) {
                        lActionMethods.add(o);
                    }
                }
            }

            // 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())) {

                        continue;
                    }

                    String message = MessageUtils.getMessage(messages, locale, va, field);

                    message = (message != null) ? message : "";

                    jscriptVar = this.getNextVar(jscriptVar);

                    results.append("     this." + jscriptVar + " = new Array(\"" + field.getKey() + "\", \""
                            + message + "\", ");

                    results.append("new Function (\"varName\", \"");

                    Map vars = field.getVars();
                    // Loop through the field's variables.
                    Iterator varsIterator = vars.keySet().iterator();
                    while (varsIterator.hasNext()) {
                        String varName = (String) varsIterator.next();
                        Var var = (Var) vars.get(varName);
                        String varValue = var.getValue();
                        String jsType = var.getJsType();

                        // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                        if (varName.startsWith("field")) {
                            continue;
                        }

                        if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "="
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "; ");
                        } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                            // So everyone using the latest format doesn't need to change their xml files immediately.
                        } else if ("mask".equalsIgnoreCase(varName)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "/; ");
                        } else {
                            results.append("this." + varName + "='"
                                    + ValidatorUtil.replace(varValue, "\\", "\\\\") + "'; ");
                        }
                    }

                    results.append(" return this[varName];\"));\n");
                }
                results.append("    } \n\n");
            }
        } else if ("true".equalsIgnoreCase(staticJavascript)) {
            results.append(this.getStartElement());
            if ("true".equalsIgnoreCase(htmlComment)) {
                results.append(htmlBeginComment);
            }
        }
    }

    if ("true".equalsIgnoreCase(staticJavascript)) {
        results.append(getJavascriptStaticMethods(resources));
    }

    if (form != null
            && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) {

        results.append(getJavascriptEnd());
    }

    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e.getMessage());
    }

    return (SKIP_BODY);

}

From source file:org.springmodules.validation.commons.taglib.JavascriptValidatorTag.java

/**
 * Render the JavaScript for to perform validations based on the form name.
 *
 * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
 *//*  w w w  . j  ava  2s.c  o  m*/
public int doStartTag() throws JspException {
    StringBuffer results = new StringBuffer();

    Locale locale = RequestContextUtils.getLocale((HttpServletRequest) pageContext.getRequest());

    ValidatorResources resources = getValidatorResources();

    Form form = resources.getForm(locale, formName);
    if (form != null) {
        if ("true".equalsIgnoreCase(dynamicJavascript)) {
            MessageSource messages = getMessageSource();

            List lActions = new ArrayList();
            List lActionMethods = new ArrayList();

            // Get List of actions for this Form
            for (Iterator i = form.getFields().iterator(); i.hasNext();) {
                Field field = (Field) i.next();

                for (Iterator x = field.getDependencyList().iterator(); x.hasNext();) {
                    Object o = x.next();

                    if (o != null && !lActionMethods.contains(o)) {
                        lActionMethods.add(o);
                    }
                }
            }

            // 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.getDependencyList().size() - va2.getDependencyList().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())) {

                        continue;
                    }

                    String message = MessageUtils.getMessage(messages, locale, va, field);

                    message = (message != null) ? message : "";

                    jscriptVar = this.getNextVar(jscriptVar);

                    results.append("     this." + jscriptVar + " = new Array(\"" + field.getKey() + "\", \""
                            + message + "\", ");

                    results.append("new Function (\"varName\", \"");

                    Map vars = field.getVars();
                    // Loop through the field's variables.
                    Iterator varsIterator = vars.keySet().iterator();
                    while (varsIterator.hasNext()) {
                        String varName = (String) varsIterator.next();
                        Var var = (Var) vars.get(varName);
                        String varValue = var.getValue();
                        String jsType = var.getJsType();

                        // skip requiredif variables field, fieldIndexed, fieldTest, fieldValue
                        if (varName.startsWith("field")) {
                            continue;
                        }

                        if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "="
                                    + ValidatorUtils.replace(varValue, "\\", "\\\\") + "; ");
                        } else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtils.replace(varValue, "\\", "\\\\") + "/; ");
                        } else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) {
                            results.append("this." + varName + "='"
                                    + ValidatorUtils.replace(varValue, "\\", "\\\\") + "'; ");
                            // So everyone using the latest format doesn't need to change their xml files immediately.
                        } else if ("mask".equalsIgnoreCase(varName)) {
                            results.append("this." + varName + "=/"
                                    + ValidatorUtils.replace(varValue, "\\", "\\\\") + "/; ");
                        } else {
                            results.append("this." + varName + "='"
                                    + ValidatorUtils.replace(varValue, "\\", "\\\\") + "'; ");
                        }
                    }

                    results.append(" return this[varName];\"));\n");
                }
                results.append("    } \n\n");
            }
        } else if ("true".equalsIgnoreCase(staticJavascript)) {
            results.append(this.getStartElement());
            if ("true".equalsIgnoreCase(htmlComment)) {
                results.append(htmlBeginComment);
            }
        }
    }

    if ("true".equalsIgnoreCase(staticJavascript)) {
        results.append(getJavascriptStaticMethods(resources));
    }

    if (form != null
            && ("true".equalsIgnoreCase(dynamicJavascript) || "true".equalsIgnoreCase(staticJavascript))) {

        results.append(getJavascriptEnd());
    }

    JspWriter writer = pageContext.getOut();
    try {
        writer.print(results.toString());
    } catch (IOException e) {
        throw new JspException(e.getMessage());
    }

    return (SKIP_BODY);

}