Beispiel #1
0
  /**
   * Initializes this tool.
   *
   * @param obj the current ViewContext
   * @throws IllegalArgumentException if the param is not a ViewContext
   */
  public void init(Object obj) {
    if (!(obj instanceof ViewContext)) {
      throw new IllegalArgumentException("Tool can only be initialized with a ViewContext");
    }

    this.context = (ViewContext) obj;
    this.request = context.getRequest();
    this.session = request.getSession(false);
    this.app = context.getServletContext();

    Boolean b = (Boolean) context.getAttribute(ViewContext.XHTML);
    if (b != null) {
      this.xhtml = b.booleanValue();
    }

    /* Is there a mapping associated with this request? */
    ActionConfig config = (ActionConfig) request.getAttribute(Globals.MAPPING_KEY);
    if (config != null) {
      /* Is there a form bean associated with this mapping? */
      this.formName = config.getAttribute();
    }

    ModuleConfig mconfig = ModuleUtils.getInstance().getModuleConfig(request, app);
    this.resources =
        (ValidatorResources) app.getAttribute(ValidatorPlugIn.VALIDATOR_KEY + mconfig.getPrefix());
  }
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();
  }