Search in sources :

Example 1 with ValidatorAction

use of org.apache.commons.validator.ValidatorAction in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createMethods.

/**
     * Creates the JavaScript methods list from the given actions.
     *
     * @param actions     A List of ValidatorAction objects.
     * @param stopOnError If true, behaves like released version of struts 1.1
     *                    and stops after first error. If false, evaluates all
     *                    validations.
     * @return JavaScript methods.
     */
private String createMethods(List actions, boolean stopOnError) {
    StringBuffer methods = new StringBuffer();
    final String methodOperator = stopOnError ? " && " : " & ";
    Iterator iter = actions.iterator();
    while (iter.hasNext()) {
        ValidatorAction va = (ValidatorAction) iter.next();
        if (methods.length() > 0) {
            methods.append(methodOperator);
        }
        methods.append(va.getMethod()).append("(form)");
    }
    return methods.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) Iterator(java.util.Iterator)

Example 2 with ValidatorAction

use of org.apache.commons.validator.ValidatorAction in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createDynamicJavascript.

/**
     * Generates the dynamic JavaScript for the form.
     *
     * @param config
     * @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();
            // 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();
                // 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();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) MessageResources(org.apache.struts.util.MessageResources) Var(org.apache.commons.validator.Var) HttpServletRequest(javax.servlet.http.HttpServletRequest) JspException(javax.servlet.jsp.JspException) Field(org.apache.commons.validator.Field) ActionMapping(org.apache.struts.action.ActionMapping) Iterator(java.util.Iterator) ServletContext(javax.servlet.ServletContext) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 3 with ValidatorAction

use of org.apache.commons.validator.ValidatorAction in project sonarqube by SonarSource.

the class JavascriptValidatorTag method createActionList.

/**
     * Get List of actions for the given Form.
     *
     * @param resources
     * @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;
}
Also used : Field(org.apache.commons.validator.Field) ValidatorAction(org.apache.commons.validator.ValidatorAction) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with ValidatorAction

use of org.apache.commons.validator.ValidatorAction in project sonarqube by SonarSource.

the class JavascriptValidatorTag method getJavascriptStaticMethods.

protected String getJavascriptStaticMethods(ValidatorResources resources) {
    StringBuffer sb = new StringBuffer();
    sb.append("\n\n");
    Iterator actions = resources.getValidatorActions().values().iterator();
    while (actions.hasNext()) {
        ValidatorAction va = (ValidatorAction) actions.next();
        if (va != null) {
            String javascript = va.getJavascript();
            if ((javascript != null) && (javascript.length() > 0)) {
                sb.append(javascript + "\n");
            }
        }
    }
    return sb.toString();
}
Also used : ValidatorAction(org.apache.commons.validator.ValidatorAction) Iterator(java.util.Iterator)

Aggregations

Iterator (java.util.Iterator)4 ValidatorAction (org.apache.commons.validator.ValidatorAction)4 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Field (org.apache.commons.validator.Field)2 Map (java.util.Map)1 ServletContext (javax.servlet.ServletContext)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 JspException (javax.servlet.jsp.JspException)1 Var (org.apache.commons.validator.Var)1 ActionMapping (org.apache.struts.action.ActionMapping)1 MessageResources (org.apache.struts.util.MessageResources)1