1 30 package com.genimen.djeneric.util; 31 32 public class DjString 33 { 34 static public final String WHITESPACE = " \t\r\n"; 35 36 static public final String reverse(String p_string) 37 { 38 StringBuffer arg = new StringBuffer (p_string); 39 return reverse(arg).toString(); 40 } 41 42 static public final StringBuffer reverse(StringBuffer p_string) 43 { 44 return p_string.reverse(); 45 } 46 47 static public final String left(String p_string, int p_len) 48 { 49 return p_string.length() > p_len ? p_string.substring(0, p_len) : p_string; 50 } 51 52 static public final String right(String p_string, int p_len) 53 { 54 return p_string.length() > p_len ? p_string.substring(p_string.length() - p_len) : p_string; 55 } 56 57 static public final String strip(String p_stringToStrip, String p_sepChars) 58 { 59 StringBuffer arg = new StringBuffer (p_stringToStrip); 60 strip(arg, p_sepChars); 61 return arg.toString(); 62 } 63 64 static public final String strip(String p_toStrip) 65 { 66 return (strip(p_toStrip, WHITESPACE)); 67 } 68 69 static public final String stripLeft(String p_stringToStrip, String p_sepChars) 70 { 71 StringBuffer arg = new StringBuffer (p_stringToStrip); 72 stripLeft(arg, p_sepChars); 73 return arg.toString(); 74 } 75 76 static public final String stripLeft(String p_toStrip) 77 { 78 return (stripLeft(p_toStrip, WHITESPACE)); 79 } 80 81 static public final String stripRight(String p_stringToStrip, String p_sepChars) 82 { 83 StringBuffer arg = new StringBuffer (p_stringToStrip); 84 stripRight(arg, p_sepChars); 85 return arg.toString(); 86 } 87 88 static public final String stripRight(String p_toStrip) 89 { 90 return (stripRight(p_toStrip, WHITESPACE)); 91 } 92 93 static public final String appendToLength(String p_original, String p_toAppend, int len) 94 { 95 if (p_toAppend == null || p_toAppend.length() == 0) throw new RuntimeException ("append string is empty"); 96 StringBuffer arg = new StringBuffer (p_original); 97 appendToLength(arg, p_toAppend, len); 98 return arg.toString(); 99 } 100 101 static public final StringBuffer strip(StringBuffer p_toStrip, String p_sepChars) 102 { 103 stripLeft(p_toStrip, p_sepChars); 104 stripRight(p_toStrip, p_sepChars); 105 return (p_toStrip); 106 } 107 108 static public final StringBuffer stripLeft(StringBuffer p_toStrip, String p_sepChars) 109 { 110 int i = 0; 111 while (i < p_toStrip.length() && p_sepChars.indexOf(p_toStrip.charAt(i)) != -1) 112 { 113 i++; 114 } 115 if (i > 0) 116 { 117 String sres = new String (p_toStrip.toString().substring(i)); 118 p_toStrip.setLength(0); 119 p_toStrip.append(sres); 120 } 121 return (p_toStrip); 122 } 123 124 static public final StringBuffer stripRight(StringBuffer p_toStrip, String p_sepChars) 125 { 126 p_toStrip.reverse(); 127 stripLeft(p_toStrip, p_sepChars); 128 p_toStrip.reverse(); 129 return (p_toStrip); 130 } 131 132 static public final StringBuffer stripRight(StringBuffer p_toStrip) 133 { 134 return (stripRight(p_toStrip, WHITESPACE)); 135 } 136 137 static public final StringBuffer stripLeft(StringBuffer p_toStrip) 138 { 139 return (stripLeft(p_toStrip, WHITESPACE)); 140 } 141 142 static public final StringBuffer appendToLength(StringBuffer p_original, String p_toAppend, int len) 143 { while (p_original.length() < len) 145 { 146 p_original.append(p_toAppend); 147 } 148 p_original.setLength(len); 149 return (p_original); 150 } 151 152 static public final StringBuffer left(StringBuffer p_string, int p_len) 153 { 154 if (p_string.length() > p_len) p_string.setLength(p_len); 155 return (p_string); 156 } 157 158 static public final StringBuffer right(StringBuffer p_string, int p_len) 159 { 160 if (p_string.length() > p_len) 161 { 162 p_string.reverse(); 163 p_string.setLength(p_len); 164 p_string.reverse(); 165 } 166 return (p_string); 167 } 168 169 static public final String initCapNoLowerCase(String src) 170 { 171 if ((src == null) || (src.length() == 0)) return src; 172 StringBuffer sb = new StringBuffer (src); 173 sb.setCharAt(0, sb.substring(0, 1).toUpperCase().charAt(0)); 174 for (int i = 0; i < sb.length(); i++) 175 { 176 if (i > 0) 177 { 178 if ((sb.charAt(i - 1) == ' ') || (sb.charAt(i - 1) == '_')) 179 { 180 sb.setCharAt(i, sb.substring(i, i + 1).toUpperCase().charAt(0)); 181 } 182 } 183 } 184 return new String (sb); 185 } 186 187 static public final String initCap(String src) 188 { 189 return initCapNoLowerCase(src.toLowerCase()); 190 } 191 192 static public final String getNextLine(StringBuffer srcBuf) 193 { 194 if ((srcBuf == null) || (srcBuf.length() == 0)) return (null); 195 String src = srcBuf.toString(); 196 String result = null; 197 int offset = src.indexOf("\n"); 198 if (offset >= 0) 199 { 200 result = srcBuf.substring(0, offset + 1); 201 srcBuf.delete(0, offset + 1); 202 } 203 else 204 { 205 result = srcBuf.toString(); 206 srcBuf.setLength(0); 207 } 208 return (result); 209 } 210 211 public static final String getNextParam(StringBuffer srcBuf, String separator, char quoteChar, char escapeChar) 212 { 213 if ((srcBuf == null) || (srcBuf.length() == 0)) return (null); 214 String src = srcBuf.toString(); 215 src = wipeConstantText(src, quoteChar, escapeChar, ' '); 216 String result = null; 217 int offset = src.indexOf(separator); 218 if (offset >= 0) 219 { 220 result = srcBuf.substring(0, offset); 221 srcBuf.delete(0, offset + separator.length()); 222 } 223 else 224 { 225 result = srcBuf.toString(); 226 srcBuf.setLength(0); 227 } 228 DjStringReplacer sr = new DjStringReplacer(result); 229 result = sr.replace("" + escapeChar + "" + quoteChar, "" + quoteChar, true); result = sr.replace("" + escapeChar + "" + escapeChar, "" + escapeChar, true); return (result); 236 } 237 238 public static final String replace(String srcBuf, String fromStr, String toStr) 239 { 240 DjStringReplacer sr = new DjStringReplacer(srcBuf); 241 return sr.replace(fromStr, toStr); 242 } 243 244 public static final String replace(String srcBuf, String fromStr, String toStr, boolean caseSensitive) 245 { 246 DjStringReplacer sr = new DjStringReplacer(srcBuf); 247 return sr.replace(fromStr, toStr, caseSensitive); 248 } 249 250 public static final String makeTitle(String srcBuf) 251 { 252 return replace(initCap(srcBuf), "_", " "); 253 } 254 255 static private final String _valids = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890_"; 256 257 public static final String makeClassName(String srcBuf) 258 { 259 StringBuffer sb = new StringBuffer (srcBuf); 260 for (int i = 0; i < sb.length(); i++) 261 { 262 if (_valids.indexOf(sb.charAt(i)) == -1) sb.replace(i, i + 1, "_"); 263 } 264 return replace(replace(initCapNoLowerCase(sb.toString()), "_", ""), " ", ""); 265 } 266 267 public static String stripPackagePrefixFromClassName(String cls) 268 { 269 String result = cls; 270 cls = DjString.reverse(cls); 271 int offs = cls.indexOf("."); 272 if (offs != -1) 273 { 274 result = DjString.reverse(cls.substring(0, offs)); 275 } 276 return result; 277 } 278 279 public static String stripFromBegin(String str, String what) 280 { 281 if (str.trim().toUpperCase().startsWith(what.toUpperCase())) 282 { 283 str = str.trim().substring(what.length()).trim(); 284 } 285 return str; 286 } 287 288 public static boolean isEmpty(String str) 290 { 291 if (str == null) return true; 292 if (str.trim().length() == 0) return true; 293 return false; 294 } 295 296 public static String wipeConstantText(String src, char textMarker, char escapeChar, char wipeChar) 297 { 298 int offset = 0; 299 int len = src.length(); 300 boolean inText = false; 301 int inc; 302 char[] ca = (src + " ").toCharArray(); 305 309 while (offset < len) 310 { 311 inc = 1; 312 if (inText) 313 { 314 if (ca[offset] == escapeChar && ca[offset + 1] == textMarker) 315 { 317 inc = 2; 318 } 319 else if (ca[offset] == textMarker) 320 { 321 ca[offset] = ' '; 322 inText = false; 323 } 324 } 325 else 326 { 328 if (ca[offset] == textMarker) inText = true; 329 } 330 if (inText) 331 { 332 ca[offset] = ' '; 333 if (inc != 1) ca[offset + 1] = ' '; 334 } 335 if (!inText) 336 { 337 offset = src.indexOf(textMarker, offset + inc); 338 if (offset == -1) offset = len; 339 } 340 else offset += inc; 341 } 342 return new String (ca); 343 } 344 345 public static boolean isValidInteger(String str) 346 { 347 try 348 { 349 Integer.parseInt(str); 350 return true; 351 } 352 catch (Exception x) 353 { 354 DjLogger.log(x); 355 } 356 return false; } 358 359 public static int safeCompare(Object one, Object two) 360 { 361 if (one == null && two == null) return 0; 362 if (one == null) return -1; 363 if (two == null) return 1; 364 return one.toString().toLowerCase().compareTo(two.toString().toLowerCase()); 365 } 366 367 public static String indentString(String val, int ind) 368 { 369 if (ind == 0) return val; 370 371 StringBuffer result = new StringBuffer (val); 372 373 if (ind > 0) 374 { 375 for (int x = 0; x < ind; x++) 376 result.insert(0, ' '); 377 int i = 0; 378 while (i < result.length()) 379 { 380 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 381 { 382 for (int x = 0; x < ind; x++) 383 result.insert(i + 1, ' '); 384 i += ind; 385 } 386 i++; 387 } 388 } 389 if (ind < 0) 390 { 391 ind = -ind; 392 for (int d = 0; d < ind; d++) 393 if (result.charAt(0) == ' ') result.deleteCharAt(0); 394 int i = 0; 395 while (i < result.length()) 396 { 397 if (i < result.length() - 1 && result.charAt(i) == '\n' && result.charAt(i + 1) != '\n') 398 { 399 int end = i + ind; 400 if (end > result.length()) end = result.length(); 401 for (int d = i; d < end; d++) 402 if (result.charAt(i + 1) == ' ') result.deleteCharAt(i + 1); 403 } 404 i++; 405 } 406 } 407 return result.toString(); 408 } 409 } | Popular Tags |