Example #1
3
 private Object removeFromMap(Serializable key, String mapKey) {
   Object ret = null;
   if (component.initialStateMarked()) {
     Map<String, Object> dMap = (Map<String, Object>) deltaMap.get(key);
     if (dMap != null) {
       ret = dMap.remove(mapKey);
       if (dMap.isEmpty()) {
         deltaMap.remove(key);
       }
     }
   }
   Map<String, Object> map = (Map<String, Object>) get(key);
   if (map != null) {
     if (ret == null) {
       ret = map.remove(mapKey);
     } else {
       map.remove(mapKey);
     }
     if (map.isEmpty()) {
       defaultMap.remove(key);
     }
   }
   if (ret != null && !component.initialStateMarked()) {
     deltaMap.remove(key);
   }
   return ret;
 }
  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 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;
  }
Example #4
0
  private void doSetBindings(ELContext elContext, UIComponent component) {
    if (component == null) return;

    ValueExpression binding = component.getValueExpression("binding");

    if (binding != null) binding.setValue(elContext, component);

    Iterator<UIComponent> iter = component.getFacetsAndChildren();
    while (iter.hasNext()) doSetBindings(elContext, iter.next());
  }
Example #5
0
  /**
   * Docs
   *
   * @param key
   * @param defaultValue value to return if key is not present in main or delta-map
   * @return value - if null, defaultValue
   */
  public Object eval(Serializable key, Object defaultValue) {
    Object retVal = get(key);
    if (retVal == null) {
      ValueExpression ve = component.getValueExpression(key.toString());
      if (ve != null) {
        retVal = ve.getValue(component.getFacesContext().getELContext());
      }
    }

    return ((retVal != null) ? retVal : defaultValue);
  }
Example #6
0
 /**
  * One and only implementation of save-state - makes all other implementations unnecessary.
  *
  * @param context
  * @return the saved state
  */
 public Object saveState(FacesContext context) {
   if (component.initialStateMarked()) {
     return saveMap(deltaMap);
   } else {
     return saveMap(defaultMap);
   }
 }
  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;
  }
Example #8
0
 /**
  * One and only implementation of restore state. Makes all other implementations unnecessary.
  *
  * @param context FacesContext
  * @param state the state to be restored.
  */
 public void restoreState(FacesContext context, Object state) {
   FacesContext fc = FacesContext.getCurrentInstance();
   Object[] savedState = (Object[]) state;
   component.initialState = (Boolean) savedState[savedState.length - 1];
   int length = (savedState.length - 1) / 2;
   for (int i = 0; i < length; i++) {
     Object value = savedState[i * 2 + 1];
     if (Void.TYPE.equals(value)) {
       value = null;
     }
     Serializable serializable = (Serializable) savedState[i * 2];
     if (value != null) {
       if (value instanceof Collection) {
         value = restoreAttachedState(fc, value);
       } else if (value instanceof StateHolderSaver) {
         value = ((StateHolderSaver) value).restore(context);
       } else {
         value = (value instanceof Serializable ? value : restoreAttachedState(fc, value));
       }
     }
     if (value instanceof Map) {
       for (Map.Entry<String, Object> entry : ((Map<String, Object>) value).entrySet()) {
         this.put(serializable, entry.getKey(), entry.getValue());
       }
     } else if (value instanceof List) {
       for (Object o : ((List<Object>) value)) {
         this.add(serializable, o);
       }
     } else {
       put(serializable, value);
     }
   }
 }
  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;
  }
  private static void renderPassThruAttributesUnoptimized(
      FacesContext context,
      ResponseWriter writer,
      UIComponent component,
      Attribute[] knownAttributes,
      Map<String, List<ClientBehavior>> behaviors)
      throws IOException {

    boolean isXhtml = XHTML_CONTENT_TYPE.equals(writer.getContentType());

    Map<String, Object> attrMap = component.getAttributes();

    for (Attribute attribute : knownAttributes) {
      String attrName = attribute.getName();
      String[] events = attribute.getEvents();
      boolean hasBehavior =
          ((events != null) && (events.length > 0) && (behaviors.containsKey(events[0])));

      Object value = attrMap.get(attrName);

      if (value != null && shouldRenderAttribute(value) && !hasBehavior) {
        writer.writeAttribute(prefixAttribute(attrName, isXhtml), value, attrName);
      } else if (hasBehavior) {

        renderHandler(context, component, null, attrName, value, events[0]);
      }
    }
  }
  private static String getSubmitHandler(
      FacesContext context,
      UIComponent component,
      Collection<ClientBehaviorContext.Parameter> params,
      boolean preventDefault) {

    StringBuilder builder = new StringBuilder(256);

    String formClientId = getFormClientId(component, context);
    String componentClientId = component.getClientId(context);

    builder.append("qab.sf(document.getElementById('");
    builder.append(formClientId);
    builder.append("'),{");

    appendProperty(builder, componentClientId, componentClientId);

    if ((null != params) && (!params.isEmpty())) {
      for (ClientBehaviorContext.Parameter param : params) {
        appendProperty(builder, param.getName(), param.getValue());
      }
    }

    builder.append("})");

    if (preventDefault) {
      builder.append(";return false");
    }

    return builder.toString();
  }
  public static String getFormClientId(UIComponent component, FacesContext context) {

    UIComponent parent = component.getParent();
    while (parent != null) {
      if (parent instanceof UIForm) {
        break;
      }
      parent = parent.getParent();
    }

    UIForm form = (UIForm) parent;
    if (form != null) {
      return form.getClientId(context);
    }

    return 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);
        }
      }
    }
  }
  /** 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);
  }
  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;
  }
  public static String findClientIds(String ids, UIComponent component, FacesContext context)
      throws IOException {
    if (ids == null) return null;

    StringBuilder clientIds = new StringBuilder();
    String[] idlist = ids.split("[\\s]");
    for (String id : idlist) {
      if (!id.startsWith("@")) {
        UIComponent found = component.findComponent(id);
        if (found != null) {
          if (clientIds.length() > 0) clientIds.append(" ");
          clientIds.append(found.getClientId(context));
        } else {
          throw new IOException("Cannot find id " + id + " within components NamingContainer");
        }
      }
    }

    return clientIds.toString();
  }
Example #18
0
  /**
   * We need to remove from both maps, if we do remove an existing key.
   *
   * @param key
   * @return the removed object in the delta-map. if not present, the removed object from the main
   *     map
   */
  public Object remove(Serializable key) {
    if (component.initialStateMarked()) {
      Object retVal = deltaMap.remove(key);

      if (retVal == null) {
        return defaultMap.remove(key);
      } else {
        defaultMap.remove(key);
        return retVal;
      }
    } else {
      return defaultMap.remove(key);
    }
  }
Example #19
0
  /**
   * Put the object in the main-map and/or the delta-map, if necessary.
   *
   * @param key
   * @param value
   * @return the original value in the delta-map, if not present, the old value in the main map
   */
  public Object put(Serializable key, Object value) {

    if (component.initialStateMarked() || value instanceof PartialStateHolder) {
      Object retVal = deltaMap.put(key, value);

      if (retVal == null) {
        return defaultMap.put(key, value);
      } else {
        defaultMap.put(key, value);
        return retVal;
      }
    } else {
      return defaultMap.put(key, value);
    }
  }
  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;
  }
  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);
        }
      }
    }
  }
Example #22
0
  public void add(Serializable key, Object value) {

    if (component.initialStateMarked() || value instanceof PartialStateHolder) {
      List<Object> deltaList = (List<Object>) deltaMap.get(key);
      if (deltaList == null) {
        deltaList = new ArrayList<Object>(4);
        deltaMap.put(key, deltaList);
      }
      deltaList.add(value);
    }
    List<Object> items = (List<Object>) get(key);
    if (items == null) {
      items = new ArrayList<Object>(4);
      defaultMap.put(key, items);
    }
    items.add(value);
  }
  @SuppressWarnings("rawtypes")
  public static void renderXHTMLStyleBooleanAttributes(ResponseWriter writer, UIComponent component)
      throws IOException {

    assert (writer != null);
    assert (component != null);

    Map attrMap = component.getAttributes();
    for (String attrName : BOOLEAN_ATTRIBUTES) {
      Object val = attrMap.get(attrName);
      if (val == null) {
        continue;
      }

      if (Boolean.valueOf(val.toString())) {
        writer.writeAttribute(attrName, true, attrName);
      }
    }
  }
  @SuppressWarnings("rawtypes")
  public static void renderOnclick(
      FacesContext context,
      UIComponent component,
      Collection<ClientBehaviorContext.Parameter> params)
      throws IOException {

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

    renderHandler(context, component, params, handlerName, userHandler, behaviorEventName);
  }
Example #25
0
  private Object saveMap(Map<Serializable, Object> map) {

    FacesContext fc = FacesContext.getCurrentInstance();

    Object[] savedState = new Object[map.size() * 2 + 1];

    int i = 0;

    for (Map.Entry<Serializable, Object> entry : map.entrySet()) {
      Object value = entry.getValue();
      if (value == null) {
        value = Void.TYPE;
      }
      savedState[i * 2] = entry.getKey();
      if (value instanceof Collection) {
        value = saveAttachedState(fc, value);
      }
      savedState[i * 2 + 1] = value instanceof Serializable ? value : saveAttachedState(fc, value);
      i++;
    }
    savedState[savedState.length - 1] = component.initialStateMarked();
    return savedState;
  }
Example #26
0
  public Object put(Serializable key, String mapKey, Object value) {

    Object ret = null;
    if (component.initialStateMarked() || value instanceof PartialStateHolder) {
      Map<String, Object> dMap = (Map<String, Object>) deltaMap.get(key);
      if (dMap == null) {
        dMap = new HashMap<String, Object>(5);
        deltaMap.put(key, dMap);
      }
      ret = dMap.put(mapKey, value);
    }
    Map<String, Object> map = (Map<String, Object>) get(key);
    if (map == null) {
      map = new HashMap<String, Object>(8);
      defaultMap.put(key, map);
    }
    if (ret == null) {
      return map.put(mapKey, value);
    } else {
      map.put(mapKey, value);
      return ret;
    }
  }
Example #27
0
 private Object removeFromList(Serializable key, Object value) {
   Object ret = null;
   if (component.initialStateMarked() || value instanceof PartialStateHolder) {
     Collection<Object> deltaList = (Collection<Object>) deltaMap.get(key);
     if (deltaList != null) {
       ret = deltaList.remove(value);
       if (deltaList.isEmpty()) {
         deltaMap.remove(key);
       }
     }
   }
   Collection<Object> list = (Collection<Object>) get(key);
   if (list != null) {
     if (ret == null) {
       ret = list.remove(value);
     } else {
       list.remove(value);
     }
     if (list.isEmpty()) {
       defaultMap.remove(key);
     }
   }
   return ret;
 }
  /** 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);
    }
  }
  public static String getLabel(FacesContext context, UIComponent component) {
    String label = (String) component.getAttributes().get("label");

    if (label != null && !"".equals(label)) return label;
    else return component.getClientId(context);
  }
Example #30
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>");
  }