@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);
        }
      }
    }
  }
예제 #2
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());
  }
예제 #3
0
  protected Converter findImplicitConverter(FacesContext context, UIComponent component) {
    ValueExpression ve = component.getValueExpression("value");

    if (ve != null) {
      Class<?> valueType = ve.getType(context.getELContext());

      if (valueType != null) return context.getApplication().createConverter(valueType);
    }

    return null;
  }
예제 #4
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);
  }
예제 #5
0
  /**
   * Algorithm works as follows; - If it's an input component, submitted value is checked first
   * since it'd be the value to be used in case validation errors terminates jsf lifecycle - Finally
   * the value of the component is retrieved from backing bean and if there's a converter, converted
   * value is returned
   *
   * <p>- If the component is not a value holder, toString of component is used to support Facelets
   * UIInstructions.
   *
   * @param facesContext FacesContext instance
   * @param component UIComponent instance whose value will be returned
   * @return End text
   */
  public static String getStringValueToRender(FacesContext facesContext, UIComponent component) {
    if (component instanceof ValueHolder) {

      if (component instanceof EditableValueHolder) {
        Object submittedValue = ((EditableValueHolder) component).getSubmittedValue();
        if (submittedValue != null) {
          return submittedValue.toString();
        }
      }

      ValueHolder valueHolder = (ValueHolder) component;
      Object value = valueHolder.getValue();
      if (value == null) return "";

      // first ask the converter
      if (valueHolder.getConverter() != null) {
        return valueHolder.getConverter().getAsString(facesContext, component, value);
      }
      // Try to guess
      else {
        ValueExpression expr = component.getValueExpression("value");
        if (expr != null) {
          Class<?> valueType = expr.getType(facesContext.getELContext());
          if (valueType != null) {
            Converter converterForType = facesContext.getApplication().createConverter(valueType);

            if (converterForType != null)
              return converterForType.getAsString(facesContext, component, value);
          }
        }
      }

      // No converter found just return the value as string
      return value.toString();
    } else {
      // This would get the plain texts on UIInstructions when using Facelets
      String value = component.toString();

      if (value != null) return value.trim();
      else return "";
    }
  }
예제 #6
0
  /**
   * Resolves the end text to render by using a specified value
   *
   * @param facesContext FacesContext instance
   * @param component UIComponent instance whose value will be returned
   * @return End text
   */
  public static String getStringValueToRender(
      FacesContext facesContext, UIComponent component, Object value) {
    if (value == null) return null;

    ValueHolder valueHolder = (ValueHolder) component;

    Converter converter = valueHolder.getConverter();
    if (converter != null) {
      return converter.getAsString(facesContext, component, value);
    } else {
      ValueExpression expr = component.getValueExpression("value");
      if (expr != null) {
        Class<?> valueType = expr.getType(facesContext.getELContext());
        Converter converterForType = facesContext.getApplication().createConverter(valueType);

        if (converterForType != null)
          return converterForType.getAsString(facesContext, component, value);
      }
    }

    return value.toString();
  }
예제 #7
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>");
  }