1 package org.ejen.ext; 22 23 import org.ejen.util.XSLUtil; 24 import org.apache.xalan.extensions.ExpressionContext; 25 import java.text.SimpleDateFormat ; 26 import java.util.Date ; 27 28 66 public class StringUtil { 67 public static final String LINE_SEPARATOR = System.getProperty("line.separator", 68 "\n"); 69 70 73 protected StringUtil() {} 74 75 93 public static String dateFormat(ExpressionContext context, String format) { 94 return new SimpleDateFormat (XSLUtil.evaluate(context, format)).format(new Date ()); 95 } 96 97 117 public static boolean equals(ExpressionContext context, String s1, String s2) { 118 return XSLUtil.evaluate(context, s1).equals(XSLUtil.evaluate(context, s2)); 119 } 120 121 141 public static boolean equalsIgnoreCase(ExpressionContext context, String s1, String s2) { 142 return XSLUtil.evaluate(context, s1).equalsIgnoreCase(XSLUtil.evaluate(context, 143 s2)); 144 } 145 146 163 public static String toLowerCase(ExpressionContext context, String s) { 164 return XSLUtil.evaluate(context, s).toLowerCase(); 165 } 166 167 184 public static String toUpperCase(ExpressionContext context, String s) { 185 return XSLUtil.evaluate(context, s).toUpperCase(); 186 } 187 188 205 public static String toULowerCase(ExpressionContext context, String s) { 206 s = XSLUtil.evaluate(context, s); 207 if (s.length() == 0) { 208 return s; 209 } 210 if (s.length() == 1) { 211 return s.toUpperCase(); 212 } else { 213 return s.substring(0, 1).toUpperCase() 214 + s.substring(1).toLowerCase(); 215 } 216 } 217 218 238 public static String indent(ExpressionContext context, int newLines, int tabs) { 239 StringBuffer sb = new StringBuffer (); 240 241 for (int i = 0; i < newLines; i++) { 242 sb.append('\n'); 243 } 244 for (int i = 0; i < tabs; i++) { 245 sb.append('\t'); 246 } 247 return sb.toString(); 248 } 249 250 275 public static String replace(ExpressionContext context, String s1, String s2, String s3) { 276 s2 = unescape(s2); 277 s3 = unescape(s3); 278 int iS2inS1 = s1.indexOf(s2); 279 280 if (iS2inS1 == -1) { 281 return s1; 282 } 283 StringBuffer sb = new StringBuffer (s1.substring(0, iS2inS1)).append(s3); 284 int oldIS2inS1 = iS2inS1; 285 int s1Length = s1.length(); 286 int s2Length = s2.length(); 287 288 while (true) { 289 iS2inS1 = s1.indexOf(s2, iS2inS1 + s2Length); 290 if (iS2inS1 == -1) { 291 if (oldIS2inS1 + s2Length < s1Length) { 292 sb.append(s1.substring(oldIS2inS1 + s2Length)); 293 } 294 break; 295 } 296 sb.append(s1.substring(oldIS2inS1 + s2Length, iS2inS1)).append(s3); 297 oldIS2inS1 = iS2inS1; 298 } 299 return sb.toString(); 300 } 301 302 312 protected static String unescape(String s) { 313 int ibs = s.indexOf('\\'); 314 315 if (ibs == -1) { 316 return s; 317 } 318 StringBuffer sb = new StringBuffer (); 319 320 int lastChar = s.length() - 1; 321 int from = 0; 322 323 while (true) { 324 if (from < ibs) { 325 sb.append(s.substring(from, ibs)); 326 } 327 if (ibs >= lastChar) { 328 break; 329 } 330 char c = s.charAt(ibs + 1); 331 332 switch (c) { 333 case 't': 334 sb.append('\t'); 335 break; 336 337 case 'n': 338 sb.append('\n'); 339 break; 340 341 case 'r': 342 sb.append('\r'); 343 break; 344 345 case 'f': 346 sb.append('\f'); 347 break; 348 349 default: 350 sb.append(c); 351 break; } 353 from = ibs + 2; 354 ibs = s.indexOf('\\', from); 355 if (ibs == -1) { 356 if (from < s.length()) { 357 sb.append(s.substring(from, s.length())); 358 } 359 break; 360 } 361 } 362 return sb.toString(); 363 } 364 } 365
| Popular Tags
|