public static UIComponent addTransient(
      FacesContext context, ServletRequest req, UIComponent parent, String prevId, Class childClass)
      throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    if (parent == null) {
      UIComponentClassicTagBase parentTag =
          (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent");

      parent = parentTag.getComponentInstance();

      BodyContent body = parentTag.getBodyContent();

      if (body != null) addVerbatim(parent, body);
    }

    UIComponent child = null;
    ;

    if (child == null) child = (UIComponent) childClass.newInstance();

    child.setTransient(true);

    addChild(parent, prevId, child);

    return child;
  }
  public static UIComponent addFacet(
      FacesContext context,
      ServletRequest req,
      UIComponent parent,
      String facetName,
      ValueExpression binding,
      Class childClass)
      throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    if (parent == null) {
      UIComponentClassicTagBase parentTag =
          (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent");

      parent = parentTag.getComponentInstance();
    }

    UIComponent child = null;

    if (binding != null) child = (UIComponent) binding.getValue(context.getELContext());

    if (child == null) child = (UIComponent) childClass.newInstance();

    if (parent != null) parent.getFacets().put(facetName, child);

    if (binding != null) binding.setValue(context.getELContext(), child);

    return child;
  }
  public static UIComponent addPersistent(
      FacesContext context,
      ServletRequest req,
      UIComponent parent,
      ValueExpression binding,
      Class childClass)
      throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    if (parent == null) {
      UIComponentClassicTagBase parentTag =
          (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent");

      parent = parentTag.getComponentInstance();

      BodyContent body = parentTag.getBodyContent();

      addVerbatim(parent, body);
    }

    UIComponent child = null;

    if (binding != null) child = (UIComponent) binding.getValue(context.getELContext());

    if (child == null) {
      child = (UIComponent) childClass.newInstance();

      // jsf/3251
      if (binding != null) binding.setValue(context.getELContext(), child);
    }

    if (parent != null) parent.getChildren().add(child);

    return child;
  }
  public static UIComponent findPersistent(
      FacesContext context, ServletRequest req, UIComponent parent, String id) throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    BodyContent body = null;

    if (parent == null) {
      UIComponentClassicTagBase parentTag =
          (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent");

      parent = parentTag.getComponentInstance();

      body = parentTag.getBodyContent();
    }

    if (parent != null) {
      List<UIComponent> children = parent.getChildren();
      int size = children.size();

      String prevId = null;
      for (int i = 0; i < size; i++) {
        UIComponent child = children.get(i);

        if (id.equals(child.getId())) {
          if (body != null) addVerbatim(parent, prevId, body);

          return child;
        }

        if (child.getId() != null) prevId = child.getId();
      }
    }

    return null;
  }
  /** Decodes the data from the form. */
  @Override
  public void decode(FacesContext context, UIComponent component) {
    String clientId = component.getClientId(context);

    ExternalContext ext = context.getExternalContext();
    Map<String, String> paramMap = ext.getRequestParameterMap();

    String value = paramMap.get(clientId);

    if (value != null) ((EditableValueHolder) component).setSubmittedValue(value);
  }
  public static UIViewRoot findRoot(FacesContext context, ServletRequest req, Object etag)
      throws Exception {
    if (context == null) context = FacesContext.getCurrentInstance();

    UIViewRoot root = context.getViewRoot();

    if (root == null)
      throw new NullPointerException(L.l("f:view can't find current in FacesContext"));

    Object oldETag = root.getAttributes().get("caucho.etag");

    if (oldETag != null && !oldETag.equals(etag)) {
      // clear view on JSP change

      root.getChildren().clear();
      root.getFacets().clear();
    }

    root.getAttributes().put("caucho.etag", etag);

    return root;
  }
  public static UIComponent findFacet(
      FacesContext context, ServletRequest req, UIComponent parent, String facetName)
      throws Exception {
    if (context == null) FacesContext.getCurrentInstance();

    if (parent == null) {
      UIComponentClassicTagBase parentTag =
          (UIComponentClassicTagBase) req.getAttribute("caucho.jsf.parent");

      parent = parentTag.getComponentInstance();
    }

    if (parent != null) return parent.getFacet(facetName);
    else return null;
  }
  /** Renders the open tag for the text. */
  @Override
  public void encodeBegin(FacesContext context, UIComponent component) throws IOException {
    ResponseWriter out = context.getResponseWriter();

    String id = component.getId();

    String accesskey;
    String dir;
    boolean disabled;
    String disabledClass;
    String enabledClass;
    String lang;

    String onblur;
    String onchange;
    String onclick;
    String ondblclick;
    String onfocus;

    String onkeydown;
    String onkeypress;
    String onkeyup;

    String onmousedown;
    String onmousemove;
    String onmouseout;
    String onmouseover;
    String onmouseup;

    String onselect;

    boolean readonly;
    String style;
    String styleClass;
    String tabindex;
    String title;
    Object value;

    if (component instanceof HtmlSelectOneMenu) {
      HtmlSelectOneMenu htmlComponent = (HtmlSelectOneMenu) component;

      accesskey = htmlComponent.getAccesskey();
      dir = htmlComponent.getDir();
      disabled = htmlComponent.isDisabled();
      disabledClass = htmlComponent.getDisabledClass();
      enabledClass = htmlComponent.getEnabledClass();
      lang = htmlComponent.getLang();

      onblur = htmlComponent.getOnblur();
      onchange = htmlComponent.getOnchange();
      onclick = htmlComponent.getOnclick();
      ondblclick = htmlComponent.getOndblclick();
      onfocus = htmlComponent.getOnfocus();

      onkeydown = htmlComponent.getOnkeydown();
      onkeypress = htmlComponent.getOnkeypress();
      onkeyup = htmlComponent.getOnkeyup();

      onmousedown = htmlComponent.getOnmousedown();
      onmousemove = htmlComponent.getOnmousemove();
      onmouseout = htmlComponent.getOnmouseout();
      onmouseover = htmlComponent.getOnmouseover();
      onmouseup = htmlComponent.getOnmouseup();

      onselect = htmlComponent.getOnselect();

      readonly = htmlComponent.isReadonly();
      style = htmlComponent.getStyle();
      styleClass = htmlComponent.getStyleClass();
      tabindex = htmlComponent.getTabindex();
      title = htmlComponent.getTitle();

      value = htmlComponent.getValue();
    } else {
      Map<String, Object> attrMap = component.getAttributes();

      accesskey = (String) attrMap.get("accesskey");
      dir = (String) attrMap.get("dir");
      disabled = Boolean.TRUE.equals(attrMap.get("disabled"));
      disabledClass = (String) attrMap.get("disabledClass");
      enabledClass = (String) attrMap.get("enabledClass");
      lang = (String) attrMap.get("lang");

      onblur = (String) attrMap.get("onblur");
      onchange = (String) attrMap.get("onchange");
      onclick = (String) attrMap.get("onclick");
      ondblclick = (String) attrMap.get("ondblclick");
      onfocus = (String) attrMap.get("onfocus");

      onkeydown = (String) attrMap.get("onkeydown");
      onkeypress = (String) attrMap.get("onkeypress");
      onkeyup = (String) attrMap.get("onkeyup");

      onmousedown = (String) attrMap.get("onmousedown");
      onmousemove = (String) attrMap.get("onmousemove");
      onmouseout = (String) attrMap.get("onmouseout");
      onmouseover = (String) attrMap.get("onmouseover");
      onmouseup = (String) attrMap.get("onmouseup");

      onselect = (String) attrMap.get("onselect");

      readonly = Boolean.TRUE.equals(attrMap.get("readonly"));
      style = (String) attrMap.get("style");
      styleClass = (String) attrMap.get("styleClass");
      tabindex = (String) attrMap.get("tabindex");
      title = (String) attrMap.get("title");

      value = attrMap.get("value");
    }

    UIViewRoot viewRoot = context.getViewRoot();

    out.startElement("select", component);

    if (style != null) out.writeAttribute("style", style, "style");

    if (styleClass != null) out.writeAttribute("class", styleClass, "class");

    String clientId = component.getClientId(context);
    out.writeAttribute("name", clientId, "name");

    if (id != null && !id.startsWith(UIViewRoot.UNIQUE_ID_PREFIX))
      out.writeAttribute("id", clientId, "id");

    if (disabled) out.writeAttribute("disabled", "disabled", "disabled");

    if (accesskey != null) out.writeAttribute("accesskey", accesskey, "accesskey");

    if (dir != null) out.writeAttribute("dir", dir, "dir");

    if (lang != null) out.writeAttribute("lang", lang, "lang");

    if (onblur != null) out.writeAttribute("onblur", onblur, "onblur");

    if (onchange != null) out.writeAttribute("onchange", onchange, "onchange");

    if (onclick != null) out.writeAttribute("onclick", onclick, "onclick");

    if (ondblclick != null) out.writeAttribute("ondblclick", ondblclick, "ondblclick");

    if (onfocus != null) out.writeAttribute("onfocus", onfocus, "onfocus");

    if (onkeydown != null) out.writeAttribute("onkeydown", onkeydown, "onkeydown");

    if (onkeypress != null) out.writeAttribute("onkeypress", onkeypress, "onkeypress");

    if (onkeyup != null) out.writeAttribute("onkeyup", onkeyup, "onkeyup");

    if (onmousedown != null) out.writeAttribute("onmousedown", onmousedown, "onmousedown");

    if (onmousemove != null) out.writeAttribute("onmousemove", onmousemove, "onmousemove");

    if (onmouseout != null) out.writeAttribute("onmouseout", onmouseout, "onmouseout");

    if (onmouseover != null) out.writeAttribute("onmouseover", onmouseover, "onmouseover");

    if (onmouseup != null) out.writeAttribute("onmouseup", onmouseup, "onmouseup");

    if (onselect != null) out.writeAttribute("onselect", onselect, "onselect");

    if (readonly) out.writeAttribute("readonly", "readonly", "readonly");

    if (tabindex != null) out.writeAttribute("tabindex", tabindex, "tabindex");

    if (title != null) out.writeAttribute("title", title, "title");

    out.writeAttribute("size", "1", "size");

    out.write("\n");

    encodeOneChildren(out, context, component, value, enabledClass, disabledClass);

    out.endElement("select");
    out.write("\n");

    for (UIComponent child : component.getChildren()) {
      if (child instanceof UIComponent) child.encodeAll(context);
    }
  }