/**
   * Recursively searches for {@link NamingContainer}s from the given start point looking for the
   * component with the <code>id</code> specified by <code>forComponent</code>.
   *
   * @param startPoint - the starting point in which to begin the search
   * @param forComponent - the component to search for
   * @return the component with the the <code>id</code that matches
   *         <code>
   *     forComponent</code> otheriwse null if no match is found.
   */
  private static UIComponent findUIComponentBelow(UIComponent startPoint, String forComponent) {

    UIComponent retComp = null;
    if (startPoint.getChildCount() > 0) {
      List<UIComponent> children = startPoint.getChildren();
      for (int i = 0, size = children.size(); i < size; i++) {
        UIComponent comp = children.get(i);

        if (comp instanceof NamingContainer) {
          try {
            retComp = comp.findComponent(forComponent);
          } catch (IllegalArgumentException iae) {
            continue;
          }
        }

        if (retComp == null) {
          if (comp.getChildCount() > 0) {
            retComp = findUIComponentBelow(comp, forComponent);
          }
        }

        if (retComp != null) {
          break;
        }
      }
    }
    return retComp;
  }
Example #2
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();
  }
  public UIComponent resolve(UIComponent source, UIComponent last, String expression) {
    UIComponent parent = last.getParent();

    if (parent.getChildCount() > 1) {
      List<UIComponent> children = parent.getChildren();
      int index = children.indexOf(last);

      if (index < parent.getChildCount() - 1) {
        return children.get(index + 1);
      }
    }

    return null;
  }
  /**
   * Collections parameters for use with Behavior script rendering. Similar to getParamList(), but
   * returns a collection of ClientBehaviorContext.Parameter instances.
   *
   * @param command the command which may have parameters
   * @return a collection of ClientBehaviorContext.Parameter instances.
   */
  protected Collection<ClientBehaviorContext.Parameter> getBehaviorParameters(UIComponent command) {

    ArrayList<ClientBehaviorContext.Parameter> params = null;
    int childCount = command.getChildCount();

    if (childCount > 0) {

      for (UIComponent kid : command.getChildren()) {
        if (kid instanceof UIParameter) {
          UIParameter uiParam = (UIParameter) kid;
          String name = uiParam.getName();
          Object value = uiParam.getValue();

          if ((name != null) && (name.length() > 0)) {

            if (params == null) {
              params = new ArrayList<>(childCount);
            }

            params.add(new ClientBehaviorContext.Parameter(name, value));
          }
        }
      }
    }

    return (params == null) ? Collections.<ClientBehaviorContext.Parameter>emptyList() : params;
  }
  /* (non-Javadoc)
   * @see javax.faces.render.Renderer#encodeChildren(javax.faces.context.FacesContext, javax.faces.component.UIComponent)
   */
  public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
    //
    UIAjaxOutputPanel panel = (UIAjaxOutputPanel) component;
    if ("none".equals(panel.getLayout())) {
      if (component.getChildCount() > 0) {
        AjaxContext ajaxContext = AjaxContext.getCurrentInstance(context);
        boolean ajaxRequest = ajaxContext.isAjaxRequest();
        Set<String> ajaxRenderedAreas = ajaxContext.getAjaxRenderedAreas();
        for (UIComponent child : component.getChildren()) {
          String childId = child.getClientId(context);
          if (child.isRendered()) {
            renderChild(context, child);
          } else {
            // Render "dummy" span.
            ResponseWriter out = context.getResponseWriter();
            out.startElement(HTML.SPAN_ELEM, child);
            out.writeAttribute(HTML.id_ATTRIBUTE, childId, HTML.id_ATTRIBUTE);
            out.writeAttribute(HTML.style_ATTRIBUTE, "display: none;", "style");
            out.endElement(HTML.SPAN_ELEM);
          }
          // register child as rendered
          if (ajaxRequest && null != ajaxRenderedAreas) {
            ajaxRenderedAreas.add(childId);
          }
        }
      }

    } else {
      renderChildren(context, component);
    }
  }
Example #6
0
    /**
     * Return an Iterator over the <code>UIColumn</code> children of the specified <code>UIData
     * </code> that have a <code>rendered</code> property of <code>true</code>.
     *
     * @param table the table from which to extract children
     * @return the List of all UIColumn children
     */
    private static List<UIColumn> getColumns(UIComponent table) {

      if (table instanceof UIData) {
        int childCount = table.getChildCount();
        if (childCount > 0) {
          List<UIColumn> results = new ArrayList<UIColumn>(childCount);
          for (UIComponent kid : table.getChildren()) {
            if ((kid instanceof UIColumn) && kid.isRendered()) {
              results.add((UIColumn) kid);
            }
          }
          return results;
        } else {
          return Collections.emptyList();
        }
      } else {
        int count;
        Object value = table.getAttributes().get("columns");
        if ((value != null) && (value instanceof Integer)) {
          count = ((Integer) value);
        } else {
          count = 2;
        }
        if (count < 1) {
          count = 1;
        }
        List<UIColumn> result = new ArrayList<UIColumn>(count);
        for (int i = 0; i < count; i++) {
          result.add(PLACE_HOLDER_COLUMN);
        }
        return result;
      }
    }
  /**
   * @param component <code>UIComponent</code> for which to extract children
   * @return an Iterator over the children of the specified component, selecting only those that
   *     have a <code>rendered</code> property of <code>true</code>.
   */
  protected Iterator<UIComponent> getChildren(UIComponent component) {

    int childCount = component.getChildCount();
    if (childCount > 0) {
      return component.getChildren().iterator();
    } else {
      return Collections.<UIComponent>emptyList().iterator();
    }
  }
Example #8
0
 public static void renderChildren(FacesContext context, UIComponent component)
     throws IOException {
   AssertionUtil.assertNotNull("context", context);
   AssertionUtil.assertNotNull("child", component);
   if (component.getChildCount() > 0) {
     for (Iterator it = component.getChildren().iterator(); it.hasNext(); ) {
       UIComponent child = (UIComponent) it.next();
       renderChild(context, child);
     }
   }
 }
 /**
  * Visits each component in the component tree, detects if it has "required=true" setting, and
  * adds the styleclass STYLECLASS_REQUIRED to it. Also, if it has a faces-message associated with
  * it (higher than INFO), then adds STYLECLASS_INVALID as well.
  */
 private static void visitComponent(FacesContext facesContext, UIComponent component) {
   if (component instanceof UIInput && ((UIInput) component).isRequired()) {
     addStyleClass(component, STYLECLASS_REQUIRED);
   }
   if (isInvalid(facesContext, component.getClientId(facesContext))) {
     addStyleClass(component, STYLECLASS_INVALID);
   }
   if (component.getChildCount() > 0) {
     for (UIComponent child : (List<UIComponent>) component.getChildren()) {
       visitComponent(facesContext, child);
     }
   }
 }
Example #10
0
 public void cleanSubmittedValues(UIComponent component) {
   if (component instanceof EditableValueHolder) {
     EditableValueHolder evh = (EditableValueHolder) component;
     evh.setSubmittedValue(null);
     evh.setValue(null);
     evh.setLocalValueSet(false);
     evh.setValid(true);
   }
   if (component.getChildCount() > 0) {
     for (UIComponent child : component.getChildren()) {
       cleanSubmittedValues(child);
     }
   }
 }
  protected String encodeParamsAsObject(FacesContext context, UIComponent component) {
    StringBuilder paramObj = new StringBuilder();
    for (int i = 0; i < component.getChildCount(); i++) {
      if (component.getChildren().get(i) instanceof UIParameter) {
        UIParameter param = (UIParameter) component.getChildren().get(i);
        Assert.hasText(
            param.getName(),
            "UIParameter requires a name when used as a child of a UICommand component");

        paramObj.append(", " + param.getName() + " : '" + param.getValue() + "'");
      }
    }
    return paramObj.toString();
  }
Example #12
0
 protected List<SelectItem> getSelectItems(UIComponent component) {
   List<SelectItem> items = new ArrayList<>();
   int childCount = component.getChildCount();
   if (childCount == 0) {
     return items;
   }
   List<UIComponent> children = component.getChildren();
   for (UIComponent child : children) {
     if (child instanceof UISelectItem) {
       this.addSelectItem((UISelectItem) child, items);
     } else if (child instanceof UISelectItems) {
       this.addSelectItems((UISelectItems) child, items);
     }
   }
   return items;
 }
  @Override
  public void encodeChildren(FacesContext context, UIComponent component) throws IOException {
    if (component.getChildCount() > 0) {

      if (component.getRendersChildren()) {
        Iterator<UIComponent> iterator = component.getChildren().iterator();
        while (iterator.hasNext()) {
          UIComponent uiComponent = (UIComponent) iterator.next();
          // if (uiComponent instanceof UIInputTipsy)
          // {
          uiComponent.encodeAll(context);
          // }
        }
      }
    }
  }
Example #14
0
  public void apply(FaceletContext ctx, UIComponent parent) throws IOException {

    UIComponent compositeParent = UIComponent.getCurrentCompositeComponent(ctx.getFacesContext());
    if (compositeParent == null) {
      return;
    }

    boolean required = ((this.required != null) && this.required.getBoolean(ctx));

    if (compositeParent.getChildCount() == 0 && required) {
      throwRequiredException(ctx, compositeParent);
    }

    List<UIComponent> compositeChildren = compositeParent.getChildren();
    List<UIComponent> parentChildren = parent.getChildren();
    parentChildren.addAll(compositeChildren);
  }
Example #15
0
 /**
  * finds a child component with the given id that is located below the given parent component
  *
  * @param parent the parent
  * @param componentId the component id
  * @return the component or null if no component was found
  */
 public static UIComponent findChildComponent(UIComponent parent, String componentId) {
   UIComponent component = null;
   if (parent.getChildCount() == 0) return null;
   Iterator<UIComponent> children = parent.getChildren().iterator();
   while (children.hasNext()) {
     UIComponent nextChild = children.next();
     if (nextChild instanceof NamingContainer) {
       component = nextChild.findComponent(componentId);
     }
     if (component == null) {
       component = findChildComponent(nextChild, componentId);
     }
     if (component != null) {
       break;
     }
   }
   return component;
 }
 private static UIComponent findLabelFor(UIComponent root, String clientId) {
   for (UIComponent c : (List<UIComponent>) root.getChildren()) {
     if ("javax.faces.Label".equals(c.getRendererType())) {
       String labelFor = (String) c.getAttributes().get("for");
       if (clientId.equals(labelFor) || clientId.endsWith(":" + labelFor)) {
         return c;
       }
     }
     if (c.getChildCount() > 0) {
       // look for the right label recursively
       UIComponent label = findLabelFor(c, clientId);
       if (label != null) {
         return label;
       }
     }
   }
   return null;
 }
  /**
   * @param command the command which may have parameters
   * @return an array of parameters
   */
  protected Param[] getParamList(UIComponent command) {

    if (command.getChildCount() > 0) {
      ArrayList<Param> parameterList = new ArrayList<>();

      for (UIComponent kid : command.getChildren()) {
        if (kid instanceof UIParameter) {
          UIParameter uiParam = (UIParameter) kid;
          if (!uiParam.isDisable()) {
            Object value = uiParam.getValue();
            Param param = new Param(uiParam.getName(), (value == null ? null : value.toString()));
            parameterList.add(param);
          }
        }
      }
      return parameterList.toArray(new Param[parameterList.size()]);
    } else {
      return EMPTY_PARAMS;
    }
  }
  /**
   * This helper fetches the pConfig'name' panels where the UIComponents for creating a metric
   * config are rendered, and removes all child elements on them
   */
  private void removeAllInputHelpersForMetricConfig() {
    List<UIComponent> lPanels = new ArrayList<UIComponent>();
    lPanels.add(this.getComponent("pConfigVeryGood"));
    lPanels.add(this.getComponent("pConfigGood"));
    lPanels.add(this.getComponent("pConfigBad"));
    lPanels.add(this.getComponent("pConfigVeryBad"));

    // iterate over all these panels and remove their child elements
    for (UIComponent uiPanel : lPanels) {
      if ((uiPanel != null) && (uiPanel.getChildCount() > 0)) {
        List<UIComponent> lToRemove = new ArrayList<UIComponent>();
        for (UIComponent comp : ((List<UIComponent>) uiPanel.getChildren())) {
          lToRemove.add(comp);
        }
        for (UIComponent comp : lToRemove) {
          uiPanel.getChildren().remove(comp);
        }
      }
    }
  }
  /**
   * {@inheritDoc} In case children were to be removed between the time when this Change was added,
   * and the time when it was applied, maybe due to application of a RemoveChildrenChange, such
   * children are not re-instated. In case children were to be added between the time when this
   * Change was added, and the time when it was applied, maybe due to application of an
   * AddChildChange, such children are appended to the end of the list in preserving the order in
   * which they were added (that is they appear at the end).
   */
  @SuppressWarnings("unchecked")
  @Override
  public void changeComponent(UIComponent uiComponent) {
    int childCount = uiComponent.getChildCount();
    if (childCount == 0) return;

    // build order map of of current Nodes, keyed by id
    Map<String, UIComponent> childrenMap = new LinkedHashMap<String, UIComponent>();

    List<UIComponent> children = uiComponent.getChildren();

    int fakeIndex = 0;
    for (UIComponent child : children) {
      String attrValue = (String) child.getAttributes().get(_identifier);

      // create a dummy key to maintain order of children whose identifier
      // does not exist
      if (attrValue == null) {
        attrValue = Integer.valueOf(fakeIndex++).toString();
      }
      childrenMap.put(attrValue, child);
    }

    // remove the children so that we can add them back in
    children.clear();

    //
    // put children back in, in order
    //
    for (String currReorderID : _childIds) {
      UIComponent currChild = childrenMap.remove(currReorderID);

      if (currChild != null) {
        children.add(currChild);
      }
    }

    // add in all of the rest of the children in
    // relative order they originally appeared
    children.addAll(childrenMap.values());
  }
Example #20
0
 private UIData getUIDataFromUIForm(List children, String id) {
   UIData data = null;
   Iterator itr = children.iterator();
   while (itr.hasNext()) {
     UIComponent component = (UIComponent) itr.next();
     if (component instanceof UIData) {
       String dataId = component.getId();
       if (dataId.equals(id)) {
         data = (UIData) component;
         break;
       }
     } else {
       if (component.getChildCount() > 0) {
         data = getUIDataFromUIForm(component.getChildren(), id);
         if (data != null) {
           break;
         }
       }
     }
   }
   return data;
 }
 protected Param[] getParamList(UIComponent command) {
   String flavor = (String) command.getAttributes().get("flavor");
   if (StringUtils.isNotBlank(flavor) || command.getChildCount() > 0) {
     ArrayList<Param> parameterList = new ArrayList<Param>();
     if (StringUtils.isNotBlank(flavor)) {
       Param param = new Param("flavor", flavor);
       parameterList.add(param);
     }
     for (UIComponent kid : command.getChildren()) {
       if (kid instanceof UIParameter) {
         UIParameter uiParam = (UIParameter) kid;
         if (!uiParam.isDisable()) {
           Object value = uiParam.getValue();
           Param param = new Param(uiParam.getName(), (value == null ? null : value.toString()));
           parameterList.add(param);
         }
       }
     }
     return parameterList.toArray(new Param[parameterList.size()]);
   } else {
     return EMPTY_PARAMS;
   }
 }
Example #22
0
  @Override
  public void encodeChildren(FacesContext context, UIComponent component) throws IOException {

    boolean renderChildren =
        WebConfiguration.getInstance()
            .isOptionEnabled(WebConfiguration.BooleanWebContextInitParameter.AllowTextChildren);

    if (!renderChildren) {
      return;
    }

    rendererParamsNotNull(context, component);

    if (!shouldEncodeChildren(component)) {
      return;
    }

    if (component.getChildCount() > 0) {
      for (UIComponent kid : component.getChildren()) {
        encodeRecursive(context, kid);
      }
    }
  }
Example #23
0
 private UIData findUIData(List children, String id) {
   Iterator itr = children.iterator();
   UIData data = null;
   UIComponent component = null;
   while (itr.hasNext()) {
     component = (UIComponent) itr.next();
     if (component instanceof UIForm) {
       data = getUIDataFromUIForm(component.getChildren(), id);
       if (data != null) {
         this.form = (UIForm) component; // cache the form
         break;
       }
     } else {
       if (component.getChildCount() > 0) {
         // Search through this component's children too
         data = findUIData(component.getChildren(), id);
         if (data != null) {
           break;
         }
       }
     }
   }
   return data;
 }
  public boolean hasActiveItem(UIComponent component, String activeItem) {
    if (activeItem == null) {
      return false;
    }
    if (component instanceof AbstractPanelMenuItem) {
      AbstractPanelMenuItem item = (AbstractPanelMenuItem) component;
      if (activeItem.equals(item.getName())) {
        return true;
      }
    }

    if (component instanceof AbstractPanelMenuGroup) {
      AbstractPanelMenuGroup group = (AbstractPanelMenuGroup) component;
      if (!group.getPanelMenu().isBubbleSelection()) {
        return false;
      }
    }

    if (component.getChildCount() > 0) {
      for (UIComponent child : component.getChildren()) {
        if (!child.isRendered()) {
          continue;
        }

        if (!(child instanceof AbstractPanelMenuItem)) {
          continue;
        }

        if (hasActiveItem(child, activeItem)) {
          return true;
        }
      }
    }

    return false;
  }
Example #25
0
 @Override
 public int getNodeCount() {
   return currentParent.getChildCount();
 }
Example #26
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>");
  }
Example #27
0
  @Override
  public void encodeEnd(FacesContext context, UIComponent component) throws IOException {

    rendererParamsNotNull(context, component);

    if (!shouldEncode(component)) {
      return;
    }

    Object currentObj = ((ValueHolder) component).getValue();
    String currentValue;
    if (currentObj != null) {
      currentValue = currentObj.toString();
    } else {
      // if the value is null, do not output anything.
      return;
    }

    int childCount = component.getChildCount();
    List<Object> parameterList;

    if (childCount > 0) {
      parameterList = new ArrayList<Object>(childCount);
      // get UIParameter children...

      for (UIComponent kid : component.getChildren()) {
        // PENDING(rogerk) ignore if child is not UIParameter?
        if (!(kid instanceof UIParameter)) {
          continue;
        }

        parameterList.add(((UIParameter) kid).getValue());
      }
    } else {
      parameterList = Collections.emptyList();
    }

    // If at least one substitution parameter was specified,
    // use the string as a MessageFormat instance.
    String message;
    if (parameterList.size() > 0) {
      MessageFormat fmt = new MessageFormat(currentValue, context.getViewRoot().getLocale());
      StringBuffer buf = new StringBuffer(currentValue.length() * 2);
      fmt.format(parameterList.toArray(new Object[parameterList.size()]), buf, null);
      message = buf.toString();
    } else {
      message = currentValue;
    }

    ResponseWriter writer = context.getResponseWriter();
    assert (writer != null);

    String style = (String) component.getAttributes().get("style");
    String styleClass = (String) component.getAttributes().get("styleClass");
    String lang = (String) component.getAttributes().get("lang");
    String dir = (String) component.getAttributes().get("dir");
    String title = (String) component.getAttributes().get("title");
    boolean wroteSpan = false;
    if (styleClass != null
        || style != null
        || dir != null
        || lang != null
        || title != null
        || shouldWriteIdAttribute(component)) {
      writer.startElement("span", component);
      writeIdAttributeIfNecessary(context, writer, component);
      wroteSpan = true;

      if (style != null) {
        writer.writeAttribute("style", style, "style");
      }
      if (null != styleClass) {
        writer.writeAttribute("class", styleClass, "styleClass");
      }
      if (dir != null) {
        writer.writeAttribute("dir", dir, "dir");
      }
      if (lang != null) {
        writer.writeAttribute(RenderKitUtils.prefixAttribute("lang", writer), lang, "lang");
      }
      if (title != null) {
        writer.writeAttribute("title", title, "title");
      }
    }

    Object val = component.getAttributes().get("escape");
    boolean escape = (val != null) && Boolean.valueOf(val.toString());

    if (escape) {
      writer.writeText(message, component, "value");
    } else {
      writer.write(message);
    }
    if (wroteSpan) {
      writer.endElement("span");
    }
  }