Search in sources :

Example 1 with Var

use of org.apache.commons.validator.Var 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 2 with Var

use of org.apache.commons.validator.Var in project sonar-java by SonarSource.

the class Resources method getVarValue.

/**
 * Get the value of a variable.
 *
 * @param varName   The variable name
 * @param field     the validator Field
 * @param validator The Validator
 * @param request   the servlet request
 * @param required  Whether the variable is mandatory
 * @return The variable's value
 */
public static String getVarValue(String varName, Field field, Validator validator, HttpServletRequest request, boolean required) {
    Var var = field.getVar(varName);
    if (var == null) {
        String msg = sysmsgs.getMessage("variable.missing", varName);
        if (required) {
            throw new IllegalArgumentException(msg);
        }
        if (log.isDebugEnabled()) {
            log.debug(field.getProperty() + ": " + msg);
        }
        return null;
    }
    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    return getVarValue(var, application, request, required);
}
Also used : Var(org.apache.commons.validator.Var) ServletContext(javax.servlet.ServletContext)

Example 3 with Var

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

the class Resources method getVarValue.

/**
     * Get the value of a variable.
     *
     * @param varName   The variable name
     * @param field     the validator Field
     * @param validator The Validator
     * @param request   the servlet request
     * @param required  Whether the variable is mandatory
     * @return The variable's value
     */
public static String getVarValue(String varName, Field field, Validator validator, HttpServletRequest request, boolean required) {
    Var var = field.getVar(varName);
    if (var == null) {
        String msg = sysmsgs.getMessage("variable.missing", varName);
        if (required) {
            throw new IllegalArgumentException(msg);
        }
        if (log.isDebugEnabled()) {
            log.debug(field.getProperty() + ": " + msg);
        }
        return null;
    }
    ServletContext application = (ServletContext) validator.getParameterValue(SERVLET_CONTEXT_PARAM);
    return getVarValue(var, application, request, required);
}
Also used : Var(org.apache.commons.validator.Var) ServletContext(javax.servlet.ServletContext)

Example 4 with Var

use of org.apache.commons.validator.Var in project sonar-java 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)

Aggregations

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