private static String getChainedHandler(
      FacesContext context,
      UIComponent component,
      List<ClientBehavior> behaviors,
      Collection<ClientBehaviorContext.Parameter> params,
      String behaviorEventName,
      String userHandler) {

    StringBuilder builder = new StringBuilder(100);
    builder.append("jsf.util.chain(this,event,");

    appendScriptToChain(builder, userHandler);

    boolean submitting =
        appendBehaviorsToChain(builder, context, component, behaviors, behaviorEventName, params);

    boolean hasParams = ((null != params) && !params.isEmpty());

    if (!submitting && hasParams) {
      String submitHandler = getSubmitHandler(context, component, params, false);

      appendScriptToChain(builder, submitHandler);

      submitting = true;
    }

    builder.append(")");

    if (submitting && ("action".equals(behaviorEventName) || "click".equals(behaviorEventName))) {
      builder.append(";return false");
    }

    return builder.toString();
  }
Ejemplo n.º 2
0
  /**
   * Adjust the viewID per the requirements of {@link #renderView}.
   *
   * @param context current {@link javax.faces.context.FacesContext}
   * @param viewId incoming view ID
   * @return the view ID with an altered suffix mapping (if necessary)
   */
  protected String convertViewId(FacesContext context, String viewId) {

    // if the viewId doesn't already use the above suffix,
    // replace or append.
    int extIdx = viewId.lastIndexOf('.');
    int length = viewId.length();
    StringBuilder buffer = new StringBuilder(length);

    for (String ext : configuredExtensions) {
      if (viewId.endsWith(ext)) {
        return viewId;
      }

      appendOrReplaceExtension(viewId, ext, length, extIdx, buffer);

      String convertedViewId = buffer.toString();
      try {
        if (context.getExternalContext().getResource(convertedViewId) != null) {
          // RELEASE_PENDING (rlubke,driscoll) cache the lookup
          return convertedViewId;
        }
      } catch (MalformedURLException e) {
        if (logger.isLoggable(Level.SEVERE)) {
          logger.log(Level.SEVERE, e.toString(), e);
        }
      }
    }

    // unable to find any resource match that the default ViewHandler
    // can deal with.  Fall back to legacy (JSF 1.2) id conversion.
    return legacyConvertViewId(viewId, length, extIdx, buffer);
  }
  private static void appendScriptToChain(StringBuilder builder, String script) {

    if ((script == null) || (script.length() == 0)) {
      return;
    }

    if (builder.charAt(builder.length() - 1) != ',') builder.append(',');

    appendQuotedValue(builder, script);
  }
Ejemplo n.º 4
0
  // Utility method used by viewId conversion.  Appends the extension
  // if no extension is present.  Otherwise, replaces the extension.
  private void appendOrReplaceExtension(
      String viewId, String ext, int length, int extIdx, StringBuilder buffer) {

    buffer.setLength(0);
    buffer.append(viewId);

    if (extIdx != -1) {
      buffer.replace(extIdx, length, ext);
    } else {
      // no extension in the provided viewId, append the suffix
      buffer.append(ext);
    }
  }
  private static String getSubmitHandler(
      FacesContext context,
      UIComponent component,
      Collection<ClientBehaviorContext.Parameter> params,
      boolean preventDefault) {

    StringBuilder builder = new StringBuilder(256);

    String formClientId = getFormClientId(component, context);
    String componentClientId = component.getClientId(context);

    builder.append("qab.sf(document.getElementById('");
    builder.append(formClientId);
    builder.append("'),{");

    appendProperty(builder, componentClientId, componentClientId);

    if ((null != params) && (!params.isEmpty())) {
      for (ClientBehaviorContext.Parameter param : params) {
        appendProperty(builder, param.getName(), param.getValue());
      }
    }

    builder.append("})");

    if (preventDefault) {
      builder.append(";return false");
    }

    return builder.toString();
  }
  private static void appendQuotedValue(StringBuilder builder, String script) {

    builder.append("'");

    int length = script.length();

    for (int i = 0; i < length; i++) {
      char c = script.charAt(i);

      if (c == '\'' || c == '\\') {
        builder.append('\\');
      }

      builder.append(c);
    }

    builder.append("'");
  }
  public static String findClientIds(String ids, UIComponent component, FacesContext context)
      throws IOException {
    if (ids == null) return null;

    StringBuilder clientIds = new StringBuilder();
    String[] idlist = ids.split("[\\s]");
    for (String id : idlist) {
      if (!id.startsWith("@")) {
        UIComponent found = component.findComponent(id);
        if (found != null) {
          if (clientIds.length() > 0) clientIds.append(" ");
          clientIds.append(found.getClientId(context));
        } else {
          throw new IOException("Cannot find id " + id + " within components NamingContainer");
        }
      }
    }

    return clientIds.toString();
  }
Ejemplo n.º 8
0
  /**
   * if the specified mapping is a prefix mapping, and the provided request URI (usually the value
   * from <code>ExternalContext.getRequestServletPath()</code>) starts with <code>mapping + '/'
   * </code>, prune the mapping from the URI and return it, otherwise, return the original URI.
   *
   * @param uri the servlet request path
   * @param mapping the FacesServlet mapping used for this request
   * @return the URI without additional FacesServlet mappings
   * @since 1.2
   */
  protected String normalizeRequestURI(String uri, String mapping) {

    if (mapping == null || !Util.isPrefixMapped(mapping)) {
      return uri;
    } else {
      int length = mapping.length() + 1;
      StringBuilder builder = new StringBuilder(length);
      builder.append(mapping).append('/');
      String mappingMod = builder.toString();
      boolean logged = false;
      while (uri.startsWith(mappingMod)) {
        if (!logged && logger.isLoggable(Level.WARNING)) {
          logged = true;
          logger.log(
              Level.WARNING, "jsf.viewhandler.requestpath.recursion", new Object[] {uri, mapping});
        }
        uri = uri.substring(length - 1);
      }
      return uri;
    }
  }
Ejemplo n.º 9
0
  private String legacyConvertViewId(String viewId, int length, int extIdx, StringBuilder buffer) {

    // In 1.2, the viewId was converted by replacing the extension
    // with the single extension specified by javax.faces.DEFAULT_SUFFIX,
    // which defaulted to ".jsp".  In 2.0, javax.faces.DEFAULT_SUFFIX
    // may specify multiple extensions.  If javax.faces.DEFAULT_SUFFIX is
    // explicitly set, we honor it and pick off the first specified
    // extension.  If javax.faces.DEFAULT_SUFFIX is not explicitly set,
    // we honor the default 1.2 behavior and use ".jsp" as the suffix.

    String ext =
        (extensionsSet && !(configuredExtensions.length == 0)) ? configuredExtensions[0] : ".jsp";

    if (viewId.endsWith(ext)) {
      return viewId;
    }

    appendOrReplaceExtension(viewId, ext, length, extIdx, buffer);

    return buffer.toString();
  }
  public static void appendProperty(
      StringBuilder builder, String name, Object value, boolean quoteValue) {

    if ((null == name) || (name.length() == 0)) throw new IllegalArgumentException();

    char lastChar = builder.charAt(builder.length() - 1);
    if ((lastChar != ',') && (lastChar != '{')) builder.append(',');

    appendQuotedValue(builder, name);
    builder.append(":");

    if (value == null) {
      builder.append("''");
    } else if (quoteValue) {
      appendQuotedValue(builder, value.toString());
    } else {
      builder.append(value.toString());
    }
  }
Ejemplo n.º 11
0
  public static String findClientIds(FacesContext context, UIComponent component, String list) {
    if (list == null) return "@none";

    // System.out.println("ComponentUtils.findClientIds()  component.clientId: " +
    // component.getClientId(context) + "  list: " + list);

    String[] ids = list.split("[,\\s]+");
    StringBuilder buffer = new StringBuilder();

    for (int i = 0; i < ids.length; i++) {
      if (i != 0) buffer.append(" ");
      String id = ids[i].trim();
      // System.out.println("ComponentUtils.findClientIds()    ["+i+"]  id: " + id);

      if (id.equals("@all") || id.equals("@none")) {
        // System.out.println("ComponentUtils.findClientIds()    ["+i+"]  " + id);
        buffer.append(id);
      } else if (id.equals("@this")) {
        // System.out.println("ComponentUtils.findClientIds()    ["+i+"]  @this  : " +
        // component.getClientId(context));
        buffer.append(component.getClientId(context));
      } else if (id.equals("@parent")) {
        // System.out.println("ComponentUtils.findClientIds()    ["+i+"]  @parent: " +
        // component.getParent().getClientId(context));
        buffer.append(component.getParent().getClientId(context));
      } else if (id.equals("@form")) {
        UIComponent form = ComponentUtils.findParentForm(context, component);
        if (form == null)
          throw new FacesException(
              "Component " + component.getClientId(context) + " needs to be enclosed in a form");

        buffer.append(form.getClientId(context));
      } else {
        UIComponent comp = component.findComponent(id);

        // For portlets, if the standard search doesn't work, it may be necessary to do an absolute
        // search
        // which requires including the portlet's namespace. So the resulting encoded id looks
        // something
        // like portletNamespace:container:componentId.  We make the search absolute by pre-pending
        // a leading colon (:).
        if (comp == null) {
          String encodedId = encodeNameSpace(context, id);
          if (!encodedId.startsWith(":")) {
            encodedId = ":" + encodedId;
          }
          comp = component.findComponent(encodedId);
          //                    System.out.println("ComponentUtils.findClientIds()   ["+i+"]  comp
          // : " + (comp == null ? "null" : comp.getClientId(context)) + "  id: " + encodedId);
        }

        if (comp != null) {
          buffer.append(comp.getClientId(context));
        } else {
          if (context.getApplication().getProjectStage().equals(ProjectStage.Development)) {
            logger.log(Level.INFO, "Cannot find component with identifier \"{0}\" in view.", id);
          }
          buffer.append(id);
        }
      }
    }

    return buffer.toString();
  }