Beispiel #1
0
  /**
   * 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.
   */
  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;
  }
Beispiel #2
0
  /**
   * 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
   */
  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();
  }