Ejemplo n.º 1
0
  private String encodeParentAndChildrenAsString(FacesContext fc, UIComponent uic) {
    StringBuffer str = new StringBuffer();
    if (uic instanceof CommandSortHeader) {
      if (uic.getChildCount() > 0) {
        Iterator iter = uic.getChildren().iterator();
        while (iter.hasNext()) {
          UIComponent child = (UIComponent) iter.next();
          str.append(encodeParentAndChildrenAsString(fc, child));
        }
      }
    }
    Object value = uic.getAttributes().get("value");
    if (value == null) {
      ValueBinding vb = uic.getValueBinding("value");
      if (vb != null) {
        value = vb.getValue(fc);
      }
    }
    if (value == null) {
      return str.toString();
    }
    Converter converter = null;
    if (uic instanceof ValueHolder) {
      converter = ((ValueHolder) uic).getConverter();
    }
    if (converter == null) {
      converter =
          FacesContext.getCurrentInstance().getApplication().createConverter(value.getClass());
    }
    if (converter != null) {
      str.append(converter.getAsString(FacesContext.getCurrentInstance(), uic, value));
    } else {
      str.append(value);
    }

    // don't process selectItems or f:param for uiCommand)
    if (uic instanceof UISelectBoolean
        || uic instanceof UISelectMany
        || uic instanceof UISelectOne
        || uic instanceof UICommand) {
      return str.toString();
    }

    if (uic.getChildCount() > 0) {
      Iterator iter = uic.getChildren().iterator();
      while (iter.hasNext()) {
        UIComponent child = (UIComponent) iter.next();
        str.append(encodeParentAndChildrenAsString(fc, child));
      }
    }
    return str.toString();
  }
  private void adjustIndexOfDynamicChildren(FacesContext context, UIComponent parent) {
    StateContext stateContext = StateContext.getStateContext(context);
    if (!stateContext.hasOneOrMoreDynamicChild(parent)) {
      return;
    }

    List<UIComponent> children = parent.getChildren();
    List<UIComponent> dynamicChildren = Collections.emptyList();

    for (UIComponent cur : children) {
      if (stateContext.componentAddedDynamically(cur)) {
        if (dynamicChildren.isEmpty()) {
          dynamicChildren = new ArrayList<UIComponent>(children.size());
        }
        dynamicChildren.add(cur);
      }
    }
    for (UIComponent cur : dynamicChildren) {
      int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur);
      if (-1 != i) {
        children.remove(cur);
        children.add(i, cur);
      }
    }
  }
Ejemplo n.º 3
0
  public static List<SelectItem> createSelectItems(UIComponent component) {
    List<SelectItem> items = new ArrayList<SelectItem>();
    Iterator<UIComponent> children = component.getChildren().iterator();

    while (children.hasNext()) {
      UIComponent child = children.next();

      if (child instanceof UISelectItem) {
        UISelectItem selectItem = (UISelectItem) child;

        items.add(new SelectItem(selectItem.getItemValue(), selectItem.getItemLabel()));
      } else if (child instanceof UISelectItems) {
        Object selectItems = ((UISelectItems) child).getValue();

        if (selectItems instanceof SelectItem[]) {
          SelectItem[] itemsArray = (SelectItem[]) selectItems;

          for (SelectItem item : itemsArray)
            items.add(new SelectItem(item.getValue(), item.getLabel()));

        } else if (selectItems instanceof Collection) {
          Collection<SelectItem> collection = (Collection<SelectItem>) selectItems;

          for (SelectItem item : collection)
            items.add(new SelectItem(item.getValue(), item.getLabel()));
        }
      }
    }

    return items;
  }
  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;
  }
  private static void addChild(UIComponent parent, String prevId, UIComponent child) {
    if (parent != null) {
      List<UIComponent> children = parent.getChildren();
      int size = children.size();
      boolean hasPrev = prevId == null;

      int i = 0;
      for (; i < size; i++) {
        UIComponent oldChild = children.get(i);

        if (hasPrev && oldChild.getId() != null) {
          children.add(i, child);

          return;
        } else if (prevId != null && prevId.equals(oldChild.getId())) hasPrev = true;
      }

      parent.getChildren().add(child);
    }
  }
  public static UIComponent addVerbatim(UIComponent parent, String text) throws Exception {
    HtmlOutputText verbatim = new HtmlOutputText();

    verbatim.setTransient(true);
    verbatim.setValue(text);
    verbatim.setEscape(false);

    parent.getChildren().add(verbatim);

    return verbatim;
  }
Ejemplo n.º 8
0
 protected List getRenderedChildColumnsList(UIComponent component) {
   List results = new ArrayList();
   Iterator kids = component.getChildren().iterator();
   while (kids.hasNext()) {
     UIComponent kid = (UIComponent) kids.next();
     if ((kid instanceof UIColumn) && kid.isRendered()) {
       results.add(kid);
     } else if (kid instanceof UIColumns) {
       results.add(kid);
     }
   }
   return results;
 }
Ejemplo n.º 9
0
  protected void doOrphanedChildCleanup(FaceletContext ctx, UIComponent parent, UIComponent c) {

    ComponentSupport.finalizeForDeletion(c);
    if (getFacetName(parent) == null) {
      FacesContext context = ctx.getFacesContext();
      boolean suppressEvents = ComponentSupport.suppressViewModificationEvents(context);

      if (suppressEvents) {
        // if the component has already been found, it will be removed
        // and added back to the view.  We don't want to publish events
        // for this case.
        context.setProcessingEvents(false);
      }
      // suppress the remove event for this case since it will be re-added
      parent.getChildren().remove(c);
      if (suppressEvents) {
        // re-enable event processing
        context.setProcessingEvents(true);
      }
    }
  }
Ejemplo n.º 10
0
  private void adjustIndexOfDynamicChildren(FacesContext context, UIComponent parent) {
    StateContext stateContext = StateContext.getStateContext(context);
    if (!stateContext.hasOneOrMoreDynamicChild(parent)) {
      return;
    }

    List<UIComponent> children = parent.getChildren();
    List<UIComponent> dynamicChildren = Collections.emptyList();

    for (UIComponent cur : children) {
      if (stateContext.componentAddedDynamically(cur)) {
        if (dynamicChildren.isEmpty()) {
          dynamicChildren = new ArrayList<UIComponent>(children.size());
        }
        dynamicChildren.add(cur);
      }
    }

    // First remove all the dynamic children, this puts the non-dynamic children at
    // their original position
    for (UIComponent cur : dynamicChildren) {
      int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur);
      if (-1 != i) {
        children.remove(cur);
      }
    }

    // Now that the non-dynamic children are in the correct position add the dynamic children
    // back in.
    for (UIComponent cur : dynamicChildren) {
      int i = stateContext.getIndexOfDynamicallyAddedChildInParent(cur);
      if (-1 != i) {
        if (i < children.size()) {
          children.add(i, cur);
        } else {
          children.add(cur);
        }
      }
    }
  }
Ejemplo n.º 11
0
  protected void processColumn(
      FacesContext fc,
      OutputTypeHandler outputHandler,
      UIComponent uiColumn,
      int colIndex,
      int countOfRowsDisplayed) {
    StringBuffer stringOutput = new StringBuffer();

    Iterator childrenOfThisColumn = uiColumn.getChildren().iterator();
    while (childrenOfThisColumn.hasNext()) {

      UIComponent nextChild = (UIComponent) childrenOfThisColumn.next();
      if (nextChild.isRendered() && !(nextChild instanceof RowSelector)) {
        stringOutput.append(encodeParentAndChildrenAsString(fc, nextChild));
        // a blank to separate
        if (childrenOfThisColumn.hasNext()) {
          stringOutput.append(' ');
        }
      }
    }
    outputHandler.writeCell(stringOutput.toString(), colIndex, countOfRowsDisplayed);
  }
Ejemplo n.º 12
0
  /** 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);
    }
  }
Ejemplo n.º 13
0
  private void printComponentTree(
      PrintWriter out, String errorId, FacesContext context, UIComponent comp, int depth) {
    for (int i = 0; i < depth; i++) out.print(' ');

    boolean isError = false;
    if (errorId != null && errorId.equals(comp.getClientId(context))) {
      isError = true;
      out.print("<span style='color:red'>");
    }

    out.print("&lt;" + comp.getClass().getSimpleName());
    if (comp.getId() != null) out.print(" id=\"" + comp.getId() + "\"");

    for (Method method : comp.getClass().getMethods()) {
      if (!method.getName().startsWith("get") && !method.getName().startsWith("is")) continue;
      else if (method.getParameterTypes().length != 0) continue;

      String name;

      if (method.getName().startsWith("get")) name = method.getName().substring(3);
      else if (method.getName().startsWith("is")) name = method.getName().substring(2);
      else continue;

      // XXX: getURL
      name = Character.toLowerCase(name.charAt(0)) + name.substring(1);

      ValueExpression expr = comp.getValueExpression(name);

      Class type = method.getReturnType();

      if (expr != null) {
        out.print(" " + name + "=\"" + expr.getExpressionString() + "\"");
      } else if (method.getDeclaringClass().equals(UIComponent.class)
          || method.getDeclaringClass().equals(UIComponentBase.class)) {
      } else if (name.equals("family")) {
      } else if (String.class.equals(type)) {
        try {
          Object value = method.invoke(comp);

          if (value != null) out.print(" " + name + "=\"" + value + "\"");
        } catch (Exception e) {
        }
      }
    }

    int facetCount = comp.getFacetCount();
    int childCount = comp.getChildCount();

    if (facetCount == 0 && childCount == 0) {
      out.print("/>");

      if (isError) out.print("</span>");

      out.println();
      return;
    }
    out.println(">");

    if (isError) out.print("</span>");

    for (int i = 0; i < childCount; i++) {
      printComponentTree(out, errorId, context, comp.getChildren().get(i), depth + 1);
    }

    for (int i = 0; i < depth; i++) out.print(' ');

    if (isError) out.print("<span style='color:red'>");

    out.println("&lt;/" + comp.getClass().getSimpleName() + ">");

    if (isError) out.print("</span>");
  }