private static void renderPassThruAttributesOptimized(
      FacesContext context,
      ResponseWriter writer,
      UIComponent component,
      Attribute[] knownAttributes,
      List<String> setAttributes,
      Map<String, List<ClientBehavior>> behaviors)
      throws IOException {

    String behaviorEventName = getSingleBehaviorEventName(behaviors);
    boolean renderedBehavior = false;

    Collections.sort(setAttributes);
    boolean isXhtml = XHTML_CONTENT_TYPE.equals(writer.getContentType());
    Map<String, Object> attrMap = component.getAttributes();
    for (String name : setAttributes) {

      int index = Arrays.binarySearch(knownAttributes, Attribute.attr(name));
      if (index >= 0) {
        Object value = attrMap.get(name);
        if (value != null && shouldRenderAttribute(value)) {

          Attribute attr = knownAttributes[index];

          if (isBehaviorEventAttribute(attr, behaviorEventName)) {
            renderHandler(context, component, null, name, value, behaviorEventName);

            renderedBehavior = true;
          } else {
            writer.writeAttribute(prefixAttribute(name, isXhtml), value, name);
          }
        }
      }
    }

    if ((behaviorEventName != null) && !renderedBehavior) {

      for (int i = 0; i < knownAttributes.length; i++) {
        Attribute attr = knownAttributes[i];
        String[] events = attr.getEvents();
        if ((events != null) && (events.length > 0) && (behaviorEventName.equals(events[0]))) {

          renderHandler(context, component, null, attr.getName(), null, behaviorEventName);
        }
      }
    }
  }
  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);
  }
  @SuppressWarnings("unchecked")
  public static void renderPassThruAttributes(
      FacesContext context,
      ResponseWriter writer,
      UIComponent component,
      Attribute[] attributes,
      Map<String, List<ClientBehavior>> behaviors)
      throws IOException {

    if (behaviors == null) {
      behaviors = Collections.emptyMap();
    }

    if ((behaviors.size() < 2)) {
      List<String> setAttributes =
          (List<String>) component.getAttributes().get(ATTRIBUTES_THAT_ARE_SET_KEY);
      if (setAttributes != null) {
        renderPassThruAttributesOptimized(
            context, writer, component, attributes, setAttributes, behaviors);
      }
    } else {
      renderPassThruAttributesUnoptimized(context, writer, component, attributes, behaviors);
    }

    List<String> html5PassThru =
        (List<String>) component.getAttributes().get(HTML5DataRule.HTML5DATAATTRIBUTES);
    if (html5PassThru != null) {
      for (String attribute : html5PassThru) {
        String value;
        if (component.getValueExpression(attribute) != null) {
          Object valueObject =
              component.getValueExpression(attribute).getValue(context.getELContext());
          value = valueObject != null ? valueObject.toString() : null;
        } else {
          value = (String) component.getAttributes().get(attribute);
        }

        if (value != null) {
          writer.writeAttribute(attribute, value, null);
        }
      }
    }
  }
  @SuppressWarnings("rawtypes")
  public static void renderSelectOnclick(FacesContext context, UIComponent component)
      throws IOException {

    final String handlerName = "onclick";
    final Object userHandler = component.getAttributes().get(handlerName);
    String behaviorEventName = "valueChange";
    if (component instanceof ClientBehaviorHolder) {
      Map behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
      if (null != behaviors && behaviors.containsKey("click")) {
        behaviorEventName = "click";
      }
    }

    renderHandler(
        context,
        component,
        Collections.<Parameter>emptyList(),
        handlerName,
        userHandler,
        behaviorEventName);
  }