Example #1
0
  public static void decorateAttribute(UIComponent component, String attribute, String value) {
    String attributeValue = (String) component.getAttributes().get(attribute);

    if (attributeValue != null) {
      if (attributeValue.indexOf(value) == -1) {
        String decoratedValue = attributeValue + ";" + value;

        component.getAttributes().put(attribute, decoratedValue);
      } else {
        component.getAttributes().put(attribute, attributeValue);
      }
    } else {
      component.getAttributes().put(attribute, value);
    }
  }
  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]);
      }
    }
  }
  @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);
        }
      }
    }
  }
Example #4
0
 @SuppressWarnings({"UnusedDeclaration"})
 protected UIComponent findReparentedComponent(
     FaceletContext ctx, UIComponent parent, String tagId) {
   UIComponent facet = parent.getFacets().get(UIComponent.COMPOSITE_FACET_NAME);
   if (facet != null) {
     UIComponent newParent = facet.findComponent((String) parent.getAttributes().get(tagId));
     if (newParent != null) return ComponentSupport.findChildByTagId(newParent, tagId);
   }
   return null;
 }
Example #5
0
  protected void doNewComponentActions(FaceletContext ctx, String id, UIComponent c) {

    if (log.isLoggable(Level.FINE)) {
      log.fine(owner.getTag() + " Component[" + id + "] Created: " + c.getClass().getName());
    }
    // If this is NOT a composite component...
    if (null == createCompositeComponentDelegate) {
      // set the attributes and properties into the UIComponent instance.
      owner.setAttributes(ctx, c);
    }
    // otherwise, allow the composite component code to do it.

    // mark it owned by a facelet instance
    c.getAttributes().put(ComponentSupport.MARK_CREATED, id);

    if (ctx.getFacesContext().isProjectStage(ProjectStage.Development)) {
      // inject the location into the component
      c.getAttributes().put(UIComponent.VIEW_LOCATION_KEY, owner.getTag().getLocation());
    }
  }
Example #6
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 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);
        }
      }
    }
  }
  @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);
  }
  /** 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);
    }
  }
Example #11
0
  public static boolean shouldDecode(UIComponent component) {
    boolean disabled = Boolean.valueOf(String.valueOf(component.getAttributes().get("disabled")));
    boolean readonly = Boolean.valueOf(String.valueOf(component.getAttributes().get("readonly")));

    return !disabled && !readonly;
  }
  // Test attribute-property transparency
  public boolean doTestAttributesTransparency(ValueHolder vh, UIComponent newComp) {
    assertEquals(vh.getValue(), (String) newComp.getAttributes().get("value"));
    vh.setValue("foo");
    assertEquals("foo", (String) newComp.getAttributes().get("value"));
    vh.setValue(null);
    assertNull((String) newComp.getAttributes().get("value"));
    newComp.getAttributes().put("value", "bar");
    assertEquals("bar", vh.getValue());
    newComp.getAttributes().put("value", null);
    assertNull(vh.getValue());

    assertEquals(vh.getConverter(), (String) newComp.getAttributes().get("converter"));
    vh.setConverter(new LongConverter());
    assertNotNull((Converter) newComp.getAttributes().get("converter"));
    assertTrue(newComp.getAttributes().get("converter") instanceof LongConverter);
    vh.setConverter(null);
    assertNull(newComp.getAttributes().get("converter"));
    newComp.getAttributes().put("converter", new ShortConverter());
    assertNotNull(vh.getConverter());
    assertTrue(vh.getConverter() instanceof ShortConverter);
    newComp.getAttributes().put("converter", null);
    assertNull(vh.getConverter());

    return true;
  }
Example #13
0
 /** @return the Facet name we are scoped in, otherwise null */
 private String getFacetName(UIComponent parent) {
   return (String) parent.getAttributes().get(FacetHandler.KEY);
 }
Example #14
0
  /**
   * Method handles UIComponent tree creation in accordance with the JSF 1.2 spec.
   *
   * <ol>
   *   <li>First determines this UIComponent's id by calling {@link
   *       javax.faces.view.facelets.ComponentHandler#getTagId()}.
   *   <li>Search the parent for an existing UIComponent of the id we just grabbed
   *   <li>If found, {@link
   *       com.sun.faces.facelets.tag.jsf.ComponentSupport#markForDeletion(javax.faces.component.UIComponent)
   *       mark} its children for deletion.
   *   <li>If <i>not</i> found, call {@link #createComponent(FaceletContext) createComponent}.
   *       <ol>
   *         <li>Only here do we apply {@link
   *             com.sun.faces.facelets.tag.MetaTagHandlerImpl#setAttributes(FaceletContext,
   *             Object)}
   *         <li>Set the UIComponent's id
   *         <li>Set the RendererType of this instance
   *       </ol>
   *   <li>Now apply the nextHandler, passing the UIComponent we've created/found.
   *   <li>Now add the UIComponent to the passed parent
   *   <li>Lastly, if the UIComponent already existed (found), then {@link
   *       ComponentSupport#finalizeForDeletion(UIComponent) finalize} for deletion.
   * </ol>
   *
   * @throws TagException if the UIComponent parent is null
   */
  @Override
  public void apply(FaceletContext ctx, UIComponent parent) throws IOException {
    FacesContext context = ctx.getFacesContext();

    // make sure our parent is not null
    if (parent == null) {
      throw new TagException(owner.getTag(), "Parent UIComponent was null");
    }

    // our id
    String id = ctx.generateUniqueId(owner.getTagId());

    // grab our component
    UIComponent c = findChild(ctx, parent, id);
    if (null == c
        && context.isPostback()
        && UIComponent.isCompositeComponent(parent)
        && parent.getAttributes().get(id) != null) {
      c = findReparentedComponent(ctx, parent, id);
    } else {
      /**
       * If we found a child that is dynamic, the actual parent might have changed, so we need to
       * remove it from the actual parent. The reapplyDynamicActions will then replay the actions
       * and will make sure it ends up in the correct order.
       */
      if (c != null
          && c.getParent() != parent
          && c.getAttributes().containsKey(DYNAMIC_COMPONENT)) {
        c.getParent().getChildren().remove(c);
      }
    }

    boolean componentFound = false;
    if (c != null) {
      componentFound = true;
      doExistingComponentActions(ctx, id, c);
    } else {
      c = this.createComponent(ctx);

      doNewComponentActions(ctx, id, c);
      assignUniqueId(ctx, parent, id, c);

      // hook method
      owner.onComponentCreated(ctx, c, parent);
    }

    CompositeComponentStackManager ccStackManager =
        CompositeComponentStackManager.getManager(context);
    boolean compcompPushed = pushComponentToEL(ctx, c, ccStackManager);

    if (ProjectStage.Development == context.getApplication().getProjectStage()) {
      ComponentSupport.setTagForComponent(context, c, this.owner.getTag());
    }

    // If this this a naming container, stop generating unique Ids
    // for the repeated tags
    boolean setUniqueIds = false;
    boolean oldUnique = false;
    if (c instanceof NamingContainer) {
      oldUnique = ComponentSupport.setNeedUniqueIds(ctx, false);
      setUniqueIds = true;
    }
    try {
      // first allow c to get populated
      owner.applyNextHandler(ctx, c);
    } finally {
      if (setUniqueIds) ComponentSupport.setNeedUniqueIds(ctx, oldUnique);
    }

    // finish cleaning up orphaned children
    if (componentFound) {
      doOrphanedChildCleanup(ctx, parent, c);
    }

    this.privateOnComponentPopulated(ctx, c);
    owner.onComponentPopulated(ctx, c, parent);
    // add to the tree afterwards
    // this allows children to determine if it's
    // been part of the tree or not yet
    addComponentToView(ctx, parent, c, componentFound);
    adjustIndexOfDynamicChildren(context, c);
    popComponentFromEL(ctx, c, ccStackManager, compcompPushed);
  }
  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);
  }