Source for My Favorite EL Helper Class

Send me a mail
 Dive into Oracle ADF   Click to see the XML version of this web page.   (Updated: 4/16/2008; 11:49:15 AM.)
Tips and tricks from Steve Muench on Oracle ADF Framework and JDeveloper IDE

Source for My Favorite EL Helper Class

I'll keep this page updated if I make any further improvements to this EL helper class that I like to use in my ADF/JSF applications. See this blog post to understand why JDeveloper doesn't offer code insight for two-letter class names like "EL" below, and how you can fix that. Of course, you can always name the class something like ELHelper instead and not have the issue to deal with.

Version for Use with JDeveloper 10.1.3 and JSF 1.1

package example.view.util;
import javax.faces.context.FacesContext;
import javax.faces.el.MethodBinding;
import javax.faces.el.ValueBinding;
/**
* Helper class for Using EL in JSF 1.1 (JDeveloper 10.1.3)
*/
public class EL {
  public static boolean test(String booleanExpr) {
    return Boolean.TRUE.equals(get(booleanExpr));
  }
  public static String getAsStringWithDefault(String expr,
                                              String defaultExpr) {
    return (String)getWithDefault(expr, defaultExpr);
  }
  public static String getAsString(String expr) {
    return (String)get(expr);
  }
  public static Integer getAsInteger(String expr) {
      return (Integer)get(expr);
  }
  public static Object get(String expr) {
    FacesContext fc = FacesContext.getCurrentInstance();
    ValueBinding vb = fc.getApplication().createValueBinding(expr);
    return vb.getValue(fc);
  }
  public static Object getWithDefault(String expr, String defaultExpr) {
    Object exprVal = get(expr);
    return exprVal != null ? exprVal : get(defaultExpr);
  }
  public static void set(String expr, String value) {
    Object valToSet = value;
    if (isELExpr(value)) {
      valToSet = get(value);
    }
    set(expr, valToSet);
  }
  public static void set(String expr, Object value) {
    FacesContext fc = FacesContext.getCurrentInstance();
    ValueBinding vb = fc.getApplication().createValueBinding(expr);
    vb.setValue(fc, value);
  }
  private static boolean isELExpr(Object o) {
    if (o instanceof String) {
      String str = (String)o;
      str.trim();
      return str.startsWith("#{") && str.endsWith("}");
    }
    return false;
  }
  public static Object invokeMethod(String expr, Class[] paramTypes, Object[] params) {
      FacesContext fc = FacesContext.getCurrentInstance();
      MethodBinding mb = fc.getApplication().createMethodBinding(expr,paramTypes);
      return mb.invoke(fc,params);
  }
  public static Object invokeMethod(String expr, Class paramType, Object param) {
      return invokeMethod(expr,new Class[]{paramType},new Object[]{param});
  } 
}

Version for Use with JDeveloper 11g and JSF 1.2

package example.view.util;
import javax.el.ELContext;
import javax.el.ExpressionFactory;
import javax.el.MethodExpression;
import javax.el.ValueExpression;
import javax.faces.context.FacesContext;
/**
 * Helper class for Using EL in JSF 1.2 (JDeveloper 11g)
 */
public class EL {
    public static boolean test(String booleanExpr) {
        return Boolean.TRUE.equals(get(booleanExpr));
    }
    public static String getAsStringWithDefault(String expr, 
                                                String defaultExpr) {
        return (String)getWithDefault(expr, defaultExpr);
    }
    public static String getAsString(String expr) {
        return (String)get(expr);
    }
    public static Integer getAsInteger(String expr) {
        return (Integer)get(expr);
    }
    public static Object get(String expr) {
        FacesContext fc = FacesContext.getCurrentInstance();
        return fc.getApplication().evaluateExpressionGet(fc,expr,Object.class);
    }
    public static Object getWithDefault(String expr, String defaultExpr) {
        Object exprVal = get(expr);
        return exprVal != null ? exprVal : get(defaultExpr);
    }
    public static void set(String expr, String value) {
        Object valToSet = value;
        if (isELExpr(value)) {
            valToSet = get(value);
        }
        set(expr, valToSet);
    }
    public static void set(String expr, Object value) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elc = fc.getELContext();
        ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        ValueExpression ve = ef.createValueExpression(elc, expr, Object.class);
        ve.setValue(elc, value);
    }
    private static boolean isELExpr(Object o) {
        if (o instanceof String) {
            String str = (String)o;
            str.trim();
            return str.startsWith("#{") && str.endsWith("}");
        }
        return false;
    }
    public static Object invokeMethod(String expr, Class[] paramTypes, 
                                      Object[] params) {
        FacesContext fc = FacesContext.getCurrentInstance();
        ELContext elc = fc.getELContext();
        ExpressionFactory ef = fc.getApplication().getExpressionFactory();
        MethodExpression me =
            ef.createMethodExpression(elc, expr, Object.class, paramTypes);
        return me.invoke(elc, params);
    }
    public static Object invokeMethod(String expr, Class paramType, 
                                      Object param) {
        return invokeMethod(expr, new Class[] { paramType },
                            new Object[] { param });
    }
}



© Copyright 2008 Steve Muench. Click here to send an email to the editor of this weblog.
Last update: 4/16/2008; 11:49:15 AM.