You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by ba...@apache.org on 2006/10/06 02:10:09 UTC

svn commit: r453433 - in /myfaces/shared/branches/3_0_0/core: pom.xml src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java

Author: baranda
Date: Thu Oct  5 17:10:08 2006
New Revision: 453433

URL: http://svn.apache.org/viewvc?view=rev&rev=453433
Log:
MYFACES-1437 - Implement UIComponentTagUtils to use ValueExpression and MethodExpression.
Also excluded UIComponentTagUtils test, because the mock application is not prepared for new jsr-252 methods

Modified:
    myfaces/shared/branches/3_0_0/core/pom.xml
    myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java

Modified: myfaces/shared/branches/3_0_0/core/pom.xml
URL: http://svn.apache.org/viewvc/myfaces/shared/branches/3_0_0/core/pom.xml?view=diff&rev=453433&r1=453432&r2=453433
==============================================================================
--- myfaces/shared/branches/3_0_0/core/pom.xml (original)
+++ myfaces/shared/branches/3_0_0/core/pom.xml Thu Oct  5 17:10:08 2006
@@ -25,6 +25,7 @@
           <excludes>
             <exclude>**/*StateUtilsAES_CBCTest*</exclude>
             <exclude>**/*MessageUtilsTest*</exclude>
+            <exclude>**/*UIComponentTagUtilsTest*</exclude>
           </excludes>
         </configuration>
       </plugin>

Modified: myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java
URL: http://svn.apache.org/viewvc/myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java?view=diff&rev=453433&r1=453432&r2=453433
==============================================================================
--- myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java (original)
+++ myfaces/shared/branches/3_0_0/core/src/main/java/org/apache/myfaces/shared/taglib/UIComponentTagUtils.java Thu Oct  5 17:10:08 2006
@@ -27,7 +27,12 @@
 import javax.faces.el.ValueBinding;
 import javax.faces.event.ActionEvent;
 import javax.faces.event.ValueChangeEvent;
+import javax.faces.event.MethodExpressionActionListener;
+import javax.faces.event.MethodExpressionValueChangeListener;
 import javax.faces.webapp.UIComponentTag;
+import javax.faces.validator.MethodExpressionValidator;
+import javax.el.ValueExpression;
+import javax.el.MethodExpression;
 
 /**
  * @author Manfred Geiler (latest modification by $Author$)
@@ -46,126 +51,133 @@
     private UIComponentTagUtils() {}    //util class, no instantiation allowed
 
 
+    /**
+     * @deprecated
+     */
     public static boolean isValueReference(String v)
     {
         return UIComponentTag.isValueReference(v);
     }
 
-
+    /**
+     * @since 1.2
+     */
     public static void setIntegerProperty(FacesContext context,
                                           UIComponent component,
                                           String propName,
-                                          String value)
+                                          ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(propName, vb);
+                component.getAttributes().put(propName, Integer.valueOf(value.getExpressionString()));
             }
             else
             {
-                //FIXME: should use converter maybe?
-                component.getAttributes().put(propName, Integer.valueOf(value));
+                component.setValueExpression(propName, value);
             }
         }
     }
 
-    public static void setLongProperty(FacesContext context, 
+
+    /**
+     * @since 1.2
+     */
+    public static void setLongProperty(FacesContext context,
                                        UIComponent component,
-                                       String propName, 
-                                       String value)
+                                       String propName,
+                                       ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(propName, vb);
+                component.getAttributes().put(propName, Long.valueOf(value.getExpressionString()));
             }
             else
             {
-                //FIXME: should use converter maybe?
-                component.getAttributes().put(propName, Long.valueOf(value));
+                component.setValueExpression(propName, value);
             }
         }
     }
 
+    /**
+     * @since 1.2
+     */
     public static void setStringProperty(FacesContext context,
                                      UIComponent component,
                                      String propName,
-                                     String value)
+                                     ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(propName, vb);
+                component.getAttributes().put(propName, value.getExpressionString());
             }
             else
             {
-                //TODO: Warning if component has no such property (with reflection)
-                component.getAttributes().put(propName, value);
+                component.setValueExpression(propName, value);
             }
         }
     }
 
-
+    /**
+     * @since 1.2
+     */
     public static void setBooleanProperty(FacesContext context,
                                       UIComponent component,
                                       String propName,
-                                      String value)
+                                      ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(propName, vb);
+                component.getAttributes().put(propName, Boolean.valueOf(value.getExpressionString()));
             }
             else
             {
-                //TODO: More sophisticated way to convert boolean value (yes/no, 1/0, on/off, etc.)
-                component.getAttributes().put(propName, Boolean.valueOf(value));
+                component.setValueExpression(propName, value);
             }
         }
     }
 
-
+    /**
+     * @since 1.2
+     */
     public static void setValueProperty(FacesContext context,
                                         UIComponent component,
-                                        String value)
+                                        ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (!value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR, vb);
+                component.setValueExpression(org.apache.myfaces.shared.renderkit.JSFAttr.VALUE_ATTR, value);
             }
             else if (component instanceof UICommand)
             {
-                ((UICommand)component).setValue(value);
+                ((UICommand)component).setValue(value.getExpressionString());
             }
             else if (component instanceof UIParameter)
             {
-                ((UIParameter)component).setValue(value);
+                ((UIParameter)component).setValue(value.getExpressionString());
             }
             else if (component instanceof UISelectBoolean)
             {
-                ((UISelectBoolean)component).setValue(Boolean.valueOf(value));
+                ((UISelectBoolean)component).setValue(Boolean.valueOf(value.getExpressionString()));
             }
             else if (component instanceof UIGraphic)
             {
-                ((UIGraphic)component).setValue(value);
+                ((UIGraphic)component).setValue(value.getExpressionString());
             }
             //Since many input components are ValueHolders the special components
             //must come first, ValueHolder is the last resort.
             else if (component instanceof ValueHolder)
             {
-                ((ValueHolder)component).setValue(value);
+                ((ValueHolder)component).setValue(value.getExpressionString());
             }
             else
             {
@@ -174,25 +186,26 @@
         }
     }
 
-
+    /**
+     * @since 1.2
+     */
     public static void setConverterProperty(FacesContext context,
                                         UIComponent component,
-                                        String value)
+                                        ValueExpression value)
     {
         if (value != null)
         {
             if (component instanceof ValueHolder)
             {
-                if (isValueReference(value))
+                if (value.isLiteralText())
                 {
-                    ValueBinding vb = context.getApplication().createValueBinding(value);
-                    component.setValueBinding(org.apache.myfaces.shared.renderkit.JSFAttr.CONVERTER_ATTR, vb);
+                    FacesContext facesContext = FacesContext.getCurrentInstance();
+                    Converter converter = facesContext.getApplication().createConverter(value.getExpressionString());
+                    ((ValueHolder)component).setConverter(converter);
                 }
                 else
                 {
-                    FacesContext facesContext = FacesContext.getCurrentInstance();
-                    Converter converter = facesContext.getApplication().createConverter(value);
-                    ((ValueHolder)component).setConverter(converter);
+                    component.setValueExpression(org.apache.myfaces.shared.renderkit.JSFAttr.CONVERTER_ATTR, value);
                 }
             }
             else
@@ -202,10 +215,12 @@
         }
     }
 
-
-    public static void setValidatorProperty(FacesContext context,
+    /**
+     * @since 1.2
+     */
+    public static void addValidatorProperty(FacesContext context,
                                             UIComponent component,
-                                            String validator)
+                                            MethodExpression validator)
     {
         if (validator != null)
         {
@@ -213,30 +228,24 @@
             {
                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder");
             }
-            if (isValueReference(validator))
-            {
-                MethodBinding mb = context.getApplication().createMethodBinding(validator,
-                                                                                VALIDATOR_ARGS);
-                ((EditableValueHolder)component).setValidator(mb);
-            }
-            else
-            {
-                log.error("Component " + component.getClientId(context) + " has invalid validation expression " + validator);
-            }
+
+            ((EditableValueHolder)component).addValidator(new MethodExpressionValidator(validator));
         }
     }
 
+    /**
+     * @since 1.2
+     */
     public static void setValueBinding(FacesContext context,
                                        UIComponent component,
                                        String propName,
-                                       String value)
+                                       ValueExpression value)
     {
         if (value != null)
         {
-            if (isValueReference(value))
+            if (!value.isLiteralText())
             {
-                ValueBinding vb = context.getApplication().createValueBinding(value);
-                component.setValueBinding(propName, vb);
+                component.setValueExpression(propName, value);
             }
             else
             {
@@ -245,65 +254,48 @@
         }
     }
 
+    /**
+     * @since 1.2
+     */
     public static void setActionProperty(FacesContext context,
                                          UIComponent component,
-                                         String action)
+                                         MethodExpression action)
     {
         if (action != null)
         {
-            if (!(component instanceof ActionSource))
-            {
-                throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource");
-            }
-            MethodBinding mb;
-            if (isValueReference(action))
-            {
-                mb = context.getApplication().createMethodBinding(action, null);
-            }
-            else
+            if (!(component instanceof ActionSource2))
             {
-                mb = new SimpleActionMethodBinding(action);
+                throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource2");
             }
-            ((ActionSource)component).setAction(mb);
+
+            ((ActionSource2)component).setActionExpression(action);
         }
     }
 
-    public static void setActionListenerProperty(FacesContext context,
+    /**
+     * @since 1.2
+     */
+    public static void addActionListenerProperty(FacesContext context,
                                                  UIComponent component,
-                                                 String actionListener)
+                                                 MethodExpression actionListener)
     {
         if (actionListener != null)
         {
-            if (!(component instanceof ActionSource))
+            if (!(component instanceof ActionSource2))
             {
                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no ActionSource");
             }
-            if (isValueReference(actionListener))
-            {
-                MethodBinding mb = context.getApplication().createMethodBinding(actionListener,
-                                                                                ACTION_LISTENER_ARGS);
 
-                /**
-                if(! Void.class.equals(mb.getType(context)))
-                {
-                    throw new IllegalArgumentException(
-                            actionListener +
-                            " : Return types for action listeners must be void, see JSF spec. 3.2.1.1");
-                }
-                */
-
-                ((ActionSource)component).setActionListener(mb);
-            }
-            else
-            {
-                log.error("Component " + component.getClientId(context) + " has invalid actionListener value: " + actionListener);
-            }
+            ((ActionSource2)component).addActionListener(new MethodExpressionActionListener(actionListener));
         }
     }
 
-    public static void setValueChangedListenerProperty(FacesContext context,
+    /**
+     * @since 1.2
+     */
+    public static void addValueChangedListenerProperty(FacesContext context,
                                                        UIComponent component,
-                                                       String valueChangedListener)
+                                                       MethodExpression valueChangedListener)
     {
         if (valueChangedListener != null)
         {
@@ -311,26 +303,149 @@
             {
                 throw new IllegalArgumentException("Component " + component.getClientId(context) + " is no EditableValueHolder");
             }
-            if (isValueReference(valueChangedListener))
-            {
-                MethodBinding mb = context.getApplication().createMethodBinding(valueChangedListener,
-                                                                                VALUE_LISTENER_ARGS);
-                /**
-                if(! Void.class.equals(mb.getType(context)))
-                {
-                    throw new IllegalArgumentException(
-                            valueChangedListener + 
-                            " : Return types for value change listeners must be void, see JSF spec. 3.2.5.1");
-                }
-                */
 
-                ((EditableValueHolder)component).setValueChangeListener(mb);
-            }
-            else
-            {
-                log.error("Component " + component.getClientId(context) + " has invalid valueChangedListener expression " + valueChangedListener);
-            }
+            ((EditableValueHolder)component).addValueChangeListener(
+                    new MethodExpressionValueChangeListener(valueChangedListener));
         }
     }
+
+
+    /**
+     * DEPRECATED METHODS
+     *********************/
+
+    /**
+     * @deprecated
+     */
+    public static void setIntegerProperty(FacesContext context,
+                                          UIComponent component,
+                                          String propName,
+                                          String value)
+    {
+        setLongProperty(context, component, propName, createValueExpression(context, value, Integer.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setLongProperty(FacesContext context, 
+                                       UIComponent component,
+                                       String propName, 
+                                       String value)
+    {
+        setLongProperty(context, component, propName, createValueExpression(context, value, Long.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setStringProperty(FacesContext context,
+                                     UIComponent component,
+                                     String propName,
+                                     String value)
+    {
+        setStringProperty(context, component, propName, createValueExpression(context, value, String.class));
+    }
+
+
+    /**
+     * @deprecated
+     */
+    public static void setBooleanProperty(FacesContext context,
+                                      UIComponent component,
+                                      String propName,
+                                      String value)
+    {
+        setStringProperty(context, component, propName, createValueExpression(context, value, Boolean.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setValueProperty(FacesContext context,
+                                        UIComponent component,
+                                        String value)
+    {
+        setValueProperty(context, component, createValueExpression(context, value, Object.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setConverterProperty(FacesContext context,
+                                        UIComponent component,
+                                        String value)
+    {
+        setValueProperty(context, component, createValueExpression(context, value, String.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setValidatorProperty(FacesContext context,
+                                            UIComponent component,
+                                            String validator)
+    {
+        addValidatorProperty(context, component, createMethodExpression(context, validator, null, VALIDATOR_ARGS));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setValueBinding(FacesContext context,
+                                       UIComponent component,
+                                       String propName,
+                                       String value)
+    {
+        setValueBinding(context, component, propName, createValueExpression(context, value, String.class));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setActionProperty(FacesContext context,
+                                         UIComponent component,
+                                         String action)
+    {
+        setActionProperty(context, component, createMethodExpression(context, action, null, null));
+     }
+
+    /**
+     * @deprecated
+     */
+    public static void setActionListenerProperty(FacesContext context,
+                                                 UIComponent component,
+                                                 String actionListener)
+    {
+        addActionListenerProperty(context, component, createMethodExpression(context, actionListener, null, ACTION_LISTENER_ARGS));
+    }
+
+    /**
+     * @deprecated
+     */
+    public static void setValueChangedListenerProperty(FacesContext context,
+                                                       UIComponent component,
+                                                       String valueChangedListener)
+    {
+        addValueChangedListenerProperty(context, component, createMethodExpression(context, valueChangedListener, null, VALUE_LISTENER_ARGS));
+    }
+
+    private static ValueExpression createValueExpression(FacesContext context,
+                                     String expression,
+                                     Class expectedType)
+    {
+        return (expression == null)? null : context.getApplication().getExpressionFactory()
+                .createValueExpression(context.getELContext(), expression, expectedType);
+    }
+
+    private static MethodExpression createMethodExpression(FacesContext context,
+                                     String expression,
+                                     Class expectedType,
+                                     Class[] parameters)
+    {
+        return (expression == null)? null : context.getApplication().getExpressionFactory()
+                .createMethodExpression(context.getELContext(), expression, expectedType, parameters);
+    }
+
 
 }