private static boolean appendBehaviorsToChain(
      StringBuilder builder,
      FacesContext context,
      UIComponent component,
      List<ClientBehavior> behaviors,
      String behaviorEventName,
      Collection<ClientBehaviorContext.Parameter> params) {

    if ((behaviors == null) || (behaviors.isEmpty())) {
      return false;
    }

    ClientBehaviorContext bContext =
        createClientBehaviorContext(context, component, behaviorEventName, params);

    boolean submitting = false;

    for (ClientBehavior behavior : behaviors) {
      String script = behavior.getScript(bContext);
      if ((script != null) && (script.length() > 0)) {
        appendScriptToChain(builder, script);

        if (isSubmitting(behavior)) {
          submitting = true;
        }
      }
    }

    return submitting;
  }
  private static void renderHandler(
      FacesContext context,
      UIComponent component,
      Collection<ClientBehaviorContext.Parameter> params,
      String handlerName,
      Object handlerValue,
      String behaviorEventName)
      throws IOException {

    ResponseWriter writer = context.getResponseWriter();
    String userHandler = getNonEmptyUserHandler(handlerValue);
    List<ClientBehavior> behaviors = getClientBehaviors(component, behaviorEventName);

    if ((null != behaviors) && (behaviors.size() > 0) && componentIsDisabled(component)) {
      behaviors = null;
    }

    if (params == null) {
      params = Collections.emptyList();
    }
    String handler = null;
    switch (getHandlerType(behaviors, params, userHandler)) {
      case USER_HANDLER_ONLY:
        handler = userHandler;
        break;

      case SINGLE_BEHAVIOR_ONLY:
        handler =
            getSingleBehaviorHandler(
                context, component, behaviors.get(0), params, behaviorEventName);
        break;

      case SUBMIT_ONLY:
        handler = getSubmitHandler(context, component, params, true);
        break;

      case CHAIN:
        handler =
            getChainedHandler(
                context, component, behaviors, params, behaviorEventName, userHandler);
        break;
      default:
        assert (false);
    }

    writer.writeAttribute(handlerName, handler, null);
  }
  private static HandlerType getHandlerType(
      List<ClientBehavior> behaviors,
      Collection<ClientBehaviorContext.Parameter> params,
      String userHandler) {

    if ((behaviors == null) || (behaviors.isEmpty())) {

      if (params.isEmpty()) return HandlerType.USER_HANDLER_ONLY;

      return (userHandler == null) ? HandlerType.SUBMIT_ONLY : HandlerType.CHAIN;
    }

    if ((behaviors.size() == 1) && (userHandler == null)) {
      ClientBehavior behavior = behaviors.get(0);

      if (isSubmitting(behavior) || params.isEmpty()) return HandlerType.SINGLE_BEHAVIOR_ONLY;
    }

    return HandlerType.CHAIN;
  }