1 28 29 package com.caucho.jstl; 30 31 import com.caucho.util.CharBuffer; 32 import com.caucho.util.L10N; 33 34 import java.util.ArrayList ; 35 import java.util.Collection ; 36 import java.util.Map ; 37 import java.util.StringTokenizer ; 38 39 42 public class Functions { 43 private static L10N L = new L10N(Functions.class); 44 45 48 public static int length(Object obj) 49 { 50 if (obj == null) 51 return 0; 52 53 else if (obj instanceof CharSequence ) 54 return ((CharSequence ) obj).length(); 55 56 else if (obj instanceof Collection ) 57 return ((Collection ) obj).size(); 58 59 else if (obj instanceof Map ) 60 return ((Map ) obj).size(); 61 62 else if (obj.getClass().isArray()) 63 return ((Object []) obj).length; 64 65 else 66 return 0; 67 } 68 69 72 public static boolean contains(String whole, String part) 73 { 74 if (whole == null) 75 whole = ""; 76 77 if (part == null) 78 part = ""; 79 80 return whole.indexOf(part) >= 0; 81 } 82 83 86 public static boolean containsIgnoreCase(String whole, String part) 87 { 88 if (whole == null) 89 whole = ""; 90 91 if (part == null) 92 part = ""; 93 94 whole = whole.toUpperCase(); 95 part = part.toUpperCase(); 96 97 return whole.indexOf(part) >= 0; 98 } 99 100 103 public static boolean endsWith(String whole, String part) 104 { 105 if (whole == null) 106 whole = ""; 107 108 if (part == null) 109 part = ""; 110 111 return whole.endsWith(part); 112 } 113 114 117 public static String escapeXml(String string) 118 { 119 if (string == null) 120 return ""; 121 122 CharBuffer cb = CharBuffer.allocate(); 123 124 for (int i = 0; i < string.length(); i++) { 125 int ch = string.charAt(i); 126 127 switch (ch) { 128 case '<': 129 cb.append("<"); 130 break; 131 case '>': 132 cb.append(">"); 133 break; 134 case '&': 135 cb.append("&"); 136 break; 137 case '\'': 138 cb.append("'"); 139 break; 140 case '"': 141 cb.append("""); 142 break; 143 default: 144 cb.append((char) ch); 145 break; 146 } 147 } 148 149 return cb.close(); 150 } 151 152 155 public static int indexOf(String whole, String part) 156 { 157 if (whole == null) 158 whole = ""; 159 160 if (part == null) 161 part = ""; 162 163 return whole.indexOf(part); 164 } 165 166 169 public static String join(String []array, String sep) 170 { 171 if (array == null) 172 return ""; 173 174 if (sep == null) 175 sep = ""; 176 177 CharBuffer result = CharBuffer.allocate(); 178 179 for (int i = 0; i < array.length; i++) { 180 if (i != 0) 181 result.append(sep); 182 183 result.append(array[i]); 184 } 185 186 return result.close(); 187 } 188 189 192 public static String replace(String input, String before, String after) 193 { 194 if (input == null) 195 return ""; 196 197 if (before == null || before.equals("")) 198 return input; 199 200 if (after == null) 201 after = ""; 202 203 CharBuffer result = CharBuffer.allocate(); 204 205 int head = 0; 206 int next; 207 while ((next = input.indexOf(before, head)) >= 0) { 208 result.append(input.substring(head, next)); 209 210 result.append(after); 211 212 head = next + before.length(); 213 } 214 215 result.append(input.substring(head)); 216 217 return result.close(); 218 } 219 220 223 public static String []split(String input, String delimiters) 224 { 225 if (input == null || input.equals("")) { 226 return new String [] {""}; 227 } 228 229 if (delimiters == null || delimiters.equals("")) { 230 return new String [] { input }; 231 } 232 233 StringTokenizer tokenizer = new StringTokenizer (input, delimiters); 234 ArrayList <String > values = new ArrayList <String >(); 235 236 while (tokenizer.hasMoreTokens()) 237 values.add(tokenizer.nextToken()); 238 239 String []array = new String [values.size()]; 240 241 return values.toArray(array); 242 } 243 244 247 public static boolean startsWith(String whole, String part) 248 { 249 if (whole == null) 250 whole = ""; 251 252 if (part == null) 253 part = ""; 254 255 return whole.startsWith(part); 256 } 257 258 261 public static String substring(String string, int begin, int end) 262 { 263 if (string == null || string.equals("")) 264 return ""; 265 266 int length = string.length(); 267 268 if (begin < 0) 269 begin = 0; 270 271 if (length <= begin) 272 return ""; 273 274 if (end < 0 || length < end) 275 end = length; 276 277 if (end <= begin) 278 return ""; 279 280 return string.substring(begin, end); 281 } 282 283 286 public static String substringAfter(String whole, String part) 287 { 288 if (whole == null || whole.equals("")) 289 return ""; 290 291 if (part == null || part.equals("")) 292 return whole; 293 294 int p = whole.indexOf(part); 295 296 if (p < 0) 297 return ""; 298 else 299 return whole.substring(p + part.length()); 300 } 301 302 305 public static String substringBefore(String whole, String part) 306 { 307 if (whole == null || whole.equals("")) 308 return ""; 309 310 if (part == null || part.equals("")) 311 return ""; 312 313 int p = whole.indexOf(part); 314 315 if (p < 0) 316 return ""; 317 else 318 return whole.substring(0, p); 319 } 320 321 324 public static String toLowerCase(String string) 325 { 326 if (string == null || string.equals("")) 327 return ""; 328 329 return string.toLowerCase(); 330 } 331 332 335 public static String toUpperCase(String string) 336 { 337 if (string == null || string.equals("")) 338 return ""; 339 340 return string.toUpperCase(); 341 } 342 343 346 public static String trim(String string) 347 { 348 if (string == null || string.equals("")) 349 return ""; 350 351 return string.trim(); 352 } 353 } 354 | Popular Tags |