Example usage for org.apache.commons.validator Var isResource

List of usage examples for org.apache.commons.validator Var isResource

Introduction

In this page you can find the example usage for org.apache.commons.validator Var isResource.

Prototype

public boolean isResource() 

Source Link

Document

Tests whether or not the value is a resource key or literal value.

Usage

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();//  ww w.j a  v a2 s  . c  o  m
        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.validator.Resources.java

/**
 * Get the value of a variable.//from   w  ww  . j a  v a 2s.  c  om
 *
 * @param var         the validator variable
 * @param application The ServletContext
 * @param request     the servlet request
 * @param required    Whether the variable is mandatory
 * @return The variables values
 */
public static String getVarValue(Var var, ServletContext application, HttpServletRequest request,
        boolean required) {
    String varName = var.getName();
    String varValue = var.getValue();

    // Non-resource variable
    if (!var.isResource()) {
        return varValue;
    }

    // Get the message resources
    String bundle = var.getBundle();
    MessageResources messages = getMessageResources(application, request, bundle);

    // Retrieve variable's value from message resources
    Locale locale = RequestUtils.getUserLocale(request, null);
    String value = messages.getMessage(locale, varValue, null);

    // Not found in message resources
    if ((value == null) && required) {
        throw new IllegalArgumentException(
                sysmsgs.getMessage("variable.resource.notfound", varName, varValue, bundle));
    }

    if (log.isDebugEnabled()) {
        log.debug("Var=[" + varName + "], " + "bundle=[" + bundle + "], " + "key=[" + varValue + "], "
                + "value=[" + value + "]");
    }

    return value;
}