1 16 17 package org.apache.taglibs.standard.functions; 18 19 import java.lang.reflect.Array ; 20 import java.util.Collection ; 21 import java.util.Enumeration ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.StringTokenizer ; 25 26 import javax.servlet.jsp.JspTagException ; 27 28 import org.apache.taglibs.standard.resources.Resources; 29 import org.apache.taglibs.standard.tag.common.core.Util; 30 31 36 37 public class Functions { 38 39 42 45 public static String toUpperCase(String input) { 46 return input.toUpperCase(); 47 } 48 49 52 public static String toLowerCase(String input) { 53 return input.toLowerCase(); 54 } 55 56 59 public static int indexOf(String input, String substring) { 60 if (input == null) input = ""; 61 if (substring == null) substring = ""; 62 return input.indexOf(substring); 63 } 64 65 public static boolean contains(String input, String substring) { 66 return indexOf(input, substring) != -1; 67 } 68 69 public static boolean containsIgnoreCase(String input, String substring) { 70 if (input == null) input = ""; 71 if (substring == null) substring = ""; 72 String inputUC = input.toUpperCase(); 73 String substringUC = substring.toUpperCase(); 74 return indexOf(inputUC, substringUC) != -1; 75 } 76 77 public static boolean startsWith(String input, String substring) { 78 if (input == null) input = ""; 79 if (substring == null) substring = ""; 80 return input.startsWith(substring); 81 } 82 83 public static boolean endsWith(String input, String substring) { 84 if (input == null) input = ""; 85 if (substring == null) substring = ""; 86 int index = input.lastIndexOf(substring); 87 if (index == -1) return false; 88 if (index == 0 && substring.length() == 0) return true; 89 return (index == input.length() - substring.length()); 90 } 91 92 public static String substring(String input, int beginIndex, int endIndex) { 93 if (input == null) input = ""; 94 if (beginIndex >= input.length()) return ""; 95 if (beginIndex < 0) beginIndex = 0; 96 if (endIndex < 0 || endIndex > input.length()) endIndex = input.length(); 97 if (endIndex < beginIndex) return ""; 98 return input.substring(beginIndex, endIndex); 99 } 100 101 public static String substringAfter(String input, String substring) { 102 if (input == null) input = ""; 103 if (input.length() == 0) return ""; 104 if (substring == null) substring = ""; 105 if (substring.length() == 0) return input; 106 107 int index = input.indexOf(substring); 108 if (index == -1) { 109 return ""; 110 } else { 111 return input.substring(index+substring.length()); 112 } 113 } 114 115 public static String substringBefore(String input, String substring) { 116 if (input == null) input = ""; 117 if (input.length() == 0) return ""; 118 if (substring == null) substring = ""; 119 if (substring.length() == 0) return ""; 120 121 int index = input.indexOf(substring); 122 if (index == -1) { 123 return ""; 124 } else { 125 return input.substring(0, index); 126 } 127 } 128 129 132 public static String escapeXml(String input) { 133 if (input == null) return ""; 134 return Util.escapeXml(input); 135 } 136 137 public static String trim(String input) { 138 if (input == null) return ""; 139 return input.trim(); 140 } 141 142 public static String replace( 143 String input, 144 String substringBefore, 145 String substringAfter) 146 { 147 if (input == null) input = ""; 148 if (input.length() == 0) return ""; 149 if (substringBefore == null) substringBefore = ""; 150 if (substringBefore.length() == 0) return input; 151 152 StringBuffer buf = new StringBuffer (input.length()); 153 int startIndex = 0; 154 int index; 155 while ((index = input.indexOf(substringBefore, startIndex)) != -1) { 156 buf.append(input.substring(startIndex, index)).append(substringAfter); 157 startIndex = index + substringBefore.length(); 158 } 159 return buf.append(input.substring(startIndex)).toString(); 160 } 161 162 public static String [] split( 163 String input, 164 String delimiters) 165 { 166 String [] array; 167 if (input == null) input = ""; 168 if (input.length() == 0) { 169 array = new String [1]; 170 array[0] = ""; 171 return array; 172 } 173 174 if (delimiters == null) delimiters = ""; 175 176 StringTokenizer tok = new StringTokenizer (input, delimiters); 177 int count = tok.countTokens(); 178 array = new String [count]; 179 int i = 0; 180 while (tok.hasMoreTokens()) { 181 array[i++] = tok.nextToken(); 182 } 183 return array; 184 } 185 186 189 public static int length(Object obj) throws JspTagException { 190 if (obj == null) return 0; 191 192 if (obj instanceof String ) return ((String )obj).length(); 193 if (obj instanceof Collection ) return ((Collection )obj).size(); 194 if (obj instanceof Map ) return ((Map )obj).size(); 195 196 int count = 0; 197 if (obj instanceof Iterator ) { 198 Iterator iter = (Iterator )obj; 199 count = 0; 200 while (iter.hasNext()) { 201 count++; 202 iter.next(); 203 } 204 return count; 205 } 206 if (obj instanceof Enumeration ) { 207 Enumeration enum_ = (Enumeration )obj; 208 count = 0; 209 while (enum_.hasMoreElements()) { 210 count++; 211 enum_.nextElement(); 212 } 213 return count; 214 } 215 try { 216 count = Array.getLength(obj); 217 return count; 218 } catch (IllegalArgumentException ex) {} 219 throw new JspTagException (Resources.getMessage("PARAM_BAD_VALUE")); 220 } 221 222 public static String join(String [] array, String separator) { 223 if (array == null) return ""; 224 if (separator == null) separator = ""; 225 226 StringBuffer buf = new StringBuffer (); 227 for (int i=0; i<array.length; i++) { 228 buf.append(array[i]); 229 if (i < array.length-1) buf.append(separator); 230 } 231 232 return buf.toString(); 233 } 234 } 235 | Popular Tags |