7 Replies Latest reply on Aug 16, 2007 1:35 PM by mgrouch

    EL functions library

      Here are few additional EL functions I couldn't live without.
      (Might be they could be included into Seam code base)

      package org.el.func;
      
      import java.lang.reflect.InvocationTargetException;
      import java.text.SimpleDateFormat;
      import java.util.Collection;
      import java.util.Date;
      
      import org.apache.commons.beanutils.PropertyUtils;
      import org.jboss.seam.Entity;
      import org.jboss.seam.core.Expressions;
      
      public class SeamFunc {
      
       public static Object getId(Object bean) {
       Class<?> clazz = bean.getClass();
       if (!clazz.isAnnotationPresent(javax.persistence.Entity.class)) {
       // this better be instrumented proxy class
       clazz = clazz.getSuperclass();
       }
       return Entity.forClass(clazz).getIdentifier(bean);
       }
      
       public static String toString(Object obj) {
       return "" + obj;
       }
      
       public static Object[] toArray(Collection<Object> collect) {
       if (collect == null) {
       return null;
       } else {
       return collect.toArray();
       }
       }
      
       public static int size(Collection<Object> collect) {
       if (collect == null) {
       return 0;
       } else {
       return collect.size();
       }
       }
      
       public static boolean isBlank(String str) {
       if (str == null) {
       return true;
       } else {
       return "".equals(str.trim());
       }
       }
      
       public static String trunc(String str, int len) {
       if (str == null) {
       return null;
       } else {
       return str.substring(0, len);
       }
       }
      
       public static String concat(String... strings) {
       StringBuilder buff = new StringBuilder();
       if (strings != null) {
       for (String str : strings) {
       buff.append(str);
       }
       }
       return buff.toString();
       }
      
       public static boolean matches(String str, String regex) {
       if (str == null) {
       return false;
       } else {
       return str.matches(regex);
       }
       }
      
       public static String formatDate(Date date, String pattern) {
       if (date == null) {
       return null;
       }
       else {
       return new SimpleDateFormat(pattern).format(date);
       }
       }
      
       public static String getProperty(String propName) {
       return System.getProperty(propName);
       }
      
       public static String getEnv(String varName) {
       return System.getenv(varName);
       }
      
       public static Object getBeanProperty(Object bean, String propName) {
       Object obj = null;
       try {
       obj = PropertyUtils.getProperty(bean, propName);
       } catch (IllegalAccessException ex) {
       throw new RuntimeException(ex);
       } catch (InvocationTargetException ex) {
       throw new RuntimeException(ex);
       } catch (NoSuchMethodException ex) {
       throw new RuntimeException(ex);
       }
       return obj;
       }
      
       public static Object evalEl(String expression) {
       String framedExpr = "#{" + expression + "}";
       Object value = Expressions.instance().createValueBinding(framedExpr).getValue();
       return value;
       }
      }
      



      /**
       * Licensed under the Common Development and Distribution License,
       * you may not use this file except in compliance with the License.
       * You may obtain a copy of the License at
       *
       * http://www.sun.com/cddl/
       *
       * Unless required by applicable law or agreed to in writing, software
       * distributed under the License is distributed on an "AS IS" BASIS,
       * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
       * implied. See the License for the specific language governing
       * permissions and limitations under the License.
       */
      
      package org.el.func;
      
      import java.lang.reflect.Method;
      import java.lang.reflect.Modifier;
      import java.util.HashMap;
      import java.util.Map;
      
      import javax.faces.FacesException;
      
      import com.sun.facelets.tag.TagConfig;
      import com.sun.facelets.tag.TagHandler;
      import com.sun.facelets.tag.TagLibrary;
      
      public class FnLibrary implements TagLibrary {
      
       public final static String Namespace = "http://org.el.func/func";
      
       private final Map<String, Method> fns = new HashMap<String, Method>();
      
       public FnLibrary() {
       super();
       try {
       Method[] methods = SeamFunc.class.getMethods();
       putMethods(methods);
       } catch (Exception e) {
       throw new RuntimeException(e);
       }
       }
      
       private void putMethods(Method[] methods) {
       for (int i = 0; i < methods.length; i++) {
       if (Modifier.isStatic(methods.getModifiers())) {
       fns.put(methods.getName(), methods);
       }
       }
       }
      
       public boolean containsNamespace(String ns) {
       return Namespace.equals(ns);
       }
      
       public boolean containsTagHandler(String ns, String localName) {
       return false;
       }
      
       public TagHandler createTagHandler(String ns, String localName,
       TagConfig tag) throws FacesException {
       return null;
       }
      
       public boolean containsFunction(String ns, String name) {
       if (Namespace.equals(ns)) {
       return this.fns.containsKey(name);
       }
       return false;
       }
      
       public Method createFunction(String ns, String name) {
       if (Namespace.equals(ns)) {
       return (Method) this.fns.get(name);
       }
       return null;
       }
      
       public static void main(String[] argv) {
       FnLibrary lib = new FnLibrary();
       System.out.println(lib.containsFunction(FnLibrary.Namespace, "isBlank"));
       }
       }
      


      META-INF/elfunc.taglib.xml
      <facelet-taglib>
       <library-class>org.el.func.FnLibrary</library-class>
      </facelet-taglib>
      



        • 1. Re: EL functions library

          Here is sample of their usage

          <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:e="http://org.el.func/func">
           <div>#{e:evalEl('true')}</div>
           <div>#{e:isBlank(' ')}</div>
           <div>#{e:getBeanProperty(identity, 'username')}</div>
           <div>#{e:getEnv('PATH')}</div>
           <div>#{e:toString(identity)}</div>
           <div>#{e:trunc('hhhhhhhh', 2)}</div>
          </ui:composition>
          


          • 2. Re: EL functions library
            pmuir

            Looks useful, if you can create a JIRA issue, and provide as a patch against Seam UI in CVS head I can probably get them in very soon.

            • 3. Re: EL functions library

              One more function

              public static String getBeanName(Object bean) {
               return Component.getComponentName(bean.getClass());
               }


              • 4. Re: EL functions library
                • 5. Re: EL functions library
                  stephen.friedrich

                  Hm,
                  1) I don't like all these "null safe" functions. There's always the tendency to just hide bugs that better be fixed.
                  2) Seam is already quite complex, not so much in principle concepts, but in sheer number of diverse features. If such an EL library is to be included it should better be discussed broadly and designed with the best possible common requirements.

                  No offence intended, just my personal opinion.

                  • 6. Re: EL functions library
                    pmuir

                    Stephen, please add any comments to the JIRA issue as well.

                    • 7. Re: EL functions library

                      The likelyhood that if you do not make them null safe you will
                      end up writting checks using EL 'empty' keyword all over your EL in xhtml
                      pages which will make it not that readable.