[jboss-user] [JBoss Seam] - EL functions library

mgrouch do-not-reply at jboss.com
Tue Aug 14 23:41:06 EDT 2007


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>
  | 


View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4074260#4074260

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4074260



More information about the jboss-user mailing list