一、前言
关于jsf-facelets(1.1.9)源码包中com.sun.facelets.tag.jstl.fn.JstlFunction标签库函数类,实现指定字符串搜索(contains/containsIgnoreCase/endsWith/indexOf/startsWith等)、对象长度计算(length)、字符串替换(replace)、字符串分割( split)、字符串截取(substring/substringAfter/substringBefore)、字符串转大小写(toLowerCase/toUpperCase)等,详情参见源码说明。
二、源码说明
package com.sun.facelets.tag.jstl.fn;@b@@b@import java.lang.reflect.Array;@b@import java.util.Collection;@b@import java.util.Map;@b@@b@public final class JstlFunction@b@{@b@ public static boolean contains(String name, String searchString)@b@ {@b@ if ((name == null) || (searchString == null)) {@b@ return false;@b@ }@b@@b@ return (-1 != name.indexOf(searchString));@b@ }@b@@b@ public static boolean containsIgnoreCase(String name, String searchString) {@b@ if ((name == null) || (searchString == null))@b@ return false;@b@@b@ return (-1 != name.toUpperCase().indexOf(searchString.toUpperCase()));@b@ }@b@@b@ public static boolean endsWith(String name, String searchString) {@b@ if ((name == null) || (searchString == null))@b@ return false;@b@@b@ return name.endsWith(searchString);@b@ }@b@@b@ public static String escapeXml(String value) {@b@ if (value == null)@b@ return null;@b@@b@ return value.replaceAll("<", "<");@b@ }@b@@b@ public static int indexOf(String name, String searchString) {@b@ if ((name == null) || (searchString == null))@b@ return -1;@b@@b@ return name.indexOf(searchString);@b@ }@b@@b@ public static String join(String[] a, String delim) {@b@ if ((a == null) || (delim == null))@b@ return null;@b@@b@ if (a.length == 0)@b@ return "";@b@@b@ StringBuffer sb = new StringBuffer(a.length * (a[0].length() + delim.length()));@b@@b@ for (int i = 0; i < a.length; ++i)@b@ sb.append(a[i]).append(delim);@b@@b@ return sb.toString();@b@ }@b@@b@ public static int length(Object obj) {@b@ if (obj == null)@b@ return 0;@b@@b@ if (obj instanceof Collection)@b@ return ((Collection)obj).size();@b@@b@ if (obj.getClass().isArray())@b@ return Array.getLength(obj);@b@@b@ if (obj instanceof String)@b@ return ((String)obj).length();@b@@b@ if (obj instanceof Map)@b@ return ((Map)obj).size();@b@@b@ throw new IllegalArgumentException("Object type not supported: " + obj.getClass().getName());@b@ }@b@@b@ public static String replace(String value, String a, String b)@b@ {@b@ if ((value == null) || (a == null) || (b == null))@b@ return null;@b@@b@ return value.replaceAll(a, b);@b@ }@b@@b@ public static String[] split(String value, String d) {@b@ if ((value == null) || (d == null))@b@ return null;@b@@b@ return value.split(d);@b@ }@b@@b@ public static boolean startsWith(String value, String p) {@b@ if ((value == null) || (p == null))@b@ return false;@b@@b@ return value.startsWith(p);@b@ }@b@@b@ public static String substring(String v, int s, int e) {@b@ if (v == null)@b@ return null;@b@@b@ return v.substring(s, e);@b@ }@b@@b@ public static String substringAfter(String v, String p) {@b@ if (v == null)@b@ return null;@b@@b@ int i = v.indexOf(p);@b@ if (i >= 0)@b@ return v.substring(i + p.length());@b@@b@ return null;@b@ }@b@@b@ public static String substringBefore(String v, String s) {@b@ if (v == null)@b@ return null;@b@@b@ int i = v.indexOf(s);@b@ if (i > 0)@b@ return v.substring(0, i - 1);@b@@b@ return null;@b@ }@b@@b@ public static String toLowerCase(String v) {@b@ if (v == null)@b@ return null;@b@@b@ return v.toLowerCase();@b@ }@b@@b@ public static String toUpperCase(String v) {@b@ if (v == null)@b@ return null;@b@@b@ return v.toUpperCase();@b@ }@b@@b@ public static String trim(String v) {@b@ if (v == null)@b@ return null;@b@@b@ return v.trim();@b@ }@b@}