1 package jimm.util; 2 import java.awt.FontMetrics ; 3 import java.util.*; 4 5 10 public class StringUtils { 11 12 protected static final int DEFAULT_MAX_MESSAGE_WIDTH = 78; 13 14 28 public static List split(String str, String delim) { 29 if (str == null) 30 return null; 31 32 ArrayList list = new ArrayList(); 33 34 if (delim == null) { 35 list.add(str); 36 return list; 37 } 38 39 int subStart, afterDelim = 0; 40 int delimLength = delim.length(); 41 while ((subStart = str.indexOf(delim, afterDelim)) != -1) { 42 list.add(str.substring(afterDelim, subStart)); 43 afterDelim = subStart + delimLength; 44 } 45 if (afterDelim <= str.length()) 46 list.add(str.substring(afterDelim)); 47 48 return list; 49 } 50 51 59 public static String join(Collection c, String joinWith) { 60 if (c == null) 61 return ""; 62 63 StringBuffer buf = new StringBuffer (); 64 boolean first = true; 65 for (Iterator iter = c.iterator(); iter.hasNext(); ) { 66 if (first) first = false; 67 else if (joinWith != null) buf.append(joinWith); 68 buf.append(iter.next().toString()); 69 } 70 return buf.toString(); 71 } 72 73 83 public static List splitIntoLines(String str) { 84 ArrayList strings = new ArrayList(); 85 86 int len = str.length(); 87 if (len == 0) { 88 strings.add(""); 89 return strings; 90 } 91 92 int lineStart = 0; 93 94 for (int i = 0; i < len; ++i) { 95 char c = str.charAt(i); 96 if (c == '\r') { 97 int newlineLength = 1; 98 if ((i + 1) < len && str.charAt(i + 1) == '\n') 99 newlineLength = 2; 100 strings.add(str.substring(lineStart, i)); 101 lineStart = i + newlineLength; 102 if (newlineLength == 2) ++i; 104 } 105 else if (c == '\n') { 106 strings.add(str.substring(lineStart, i)); 107 lineStart = i + 1; 108 } 109 } 110 if (lineStart < len) 111 strings.add(str.substring(lineStart)); 112 113 return strings; 114 } 115 116 129 public static void splitUp(StringBuffer buf, String str) { 130 splitUp(buf, str, DEFAULT_MAX_MESSAGE_WIDTH); 131 } 132 133 147 public static void splitUp(StringBuffer buf, String str, int maxWidth) { 148 if (str == null) 149 return; 150 151 str = str.trim(); 152 while (str.length() >= maxWidth) { 153 int pos = str.lastIndexOf(' ', maxWidth); 154 if (pos == -1) { pos = str.indexOf(' ', maxWidth); 156 if (pos == -1) 157 break; 158 } 159 buf.append(str.substring(0, pos).trim()); 160 buf.append("\n"); 161 str = str.substring(pos + 1).trim(); 162 } 163 buf.append(str); 164 } 165 166 179 public static List wrap(String str, FontMetrics fm, int maxWidth) { 180 List lines = splitIntoLines(str); 181 if (lines.size() == 0) 182 return lines; 183 184 ArrayList strings = new ArrayList(); 185 for (Iterator iter = lines.iterator(); iter.hasNext(); ) 186 wrapLineInto((String )iter.next(), strings, fm, maxWidth); 187 return strings; 188 } 189 190 199 public static void wrapLineInto(String line, List list, FontMetrics fm, 200 int maxWidth) 201 { 202 int len = line.length(); 203 int width; 204 while (len > 0 && (width = fm.stringWidth(line)) > maxWidth) { 205 int guess = len * maxWidth / width; 208 String before = line.substring(0, guess).trim(); 209 210 width = fm.stringWidth(before); 211 int pos; 212 if (width > maxWidth) pos = findBreakBefore(line, guess); 214 else { pos = findBreakAfter(line, guess); 216 if (pos != -1) { before = line.substring(0, pos).trim(); 218 if (fm.stringWidth(before) > maxWidth) 219 pos = findBreakBefore(line, guess); 220 } 221 } 222 if (pos == -1) pos = guess; 224 list.add(line.substring(0, pos).trim()); 225 line = line.substring(pos).trim(); 226 len = line.length(); 227 } 228 if (len > 0) 229 list.add(line); 230 } 231 232 240 public static int findBreakBefore(String line, int start) { 241 for (int i = start; i >= 0; --i) { 242 char c = line.charAt(i); 243 if (Character.isWhitespace(c) || c == '-') 244 return i; 245 } 246 return -1; 247 } 248 249 257 public static int findBreakAfter(String line, int start) { 258 int len = line.length(); 259 for (int i = start; i < len; ++i) { 260 char c = line.charAt(i); 261 if (Character.isWhitespace(c) || c == '-') 262 return i; 263 } 264 return -1; 265 } 266 267 274 public static String escapeHTML(String str) { 275 if (str == null || str.length() == 0) 276 return ""; 277 278 StringBuffer buf = new StringBuffer (); 279 int len = str.length(); 280 for (int i = 0; i < len; ++i) { 281 char c = str.charAt(i); 282 switch (c) { 283 case '&': buf.append("&"); break; 284 case '<': buf.append("<"); break; 285 case '>': buf.append(">"); break; 286 case '"': buf.append("""); break; 287 case '\'': buf.append("'"); break; 288 default: buf.append(c); break; 289 } 290 } 291 return buf.toString(); 292 } 293 294 306 public static String newlinesToXHTMLBreaks(String str) { 307 if (str == null || str.length() == 0) 308 return ""; 309 310 StringBuffer buf = new StringBuffer (); 311 int len = str.length(); 312 for (int i = 0; i < len; ++i) { 313 char c = str.charAt(i); 314 switch (c) { 315 case '\n': buf.append("\n<br />"); break; 316 case '\r': 317 if (i + 1 < len && str.charAt(i + 1) == '\n') ++i; 319 buf.append("\n<br />"); 320 break; 321 default: 322 buf.append(c); break; 323 } 324 } 325 return buf.toString(); 326 } 327 328 335 public static String escapeXML(String str) { 336 return escapeHTML(str); 337 } 338 339 345 public static String unescapeXML(String str) { 346 if (str == null || str.length() == 0) 347 return ""; 348 349 StringBuffer buf = new StringBuffer (); 350 int len = str.length(); 351 for (int i = 0; i < len; ++i) { 352 char c = str.charAt(i); 353 if (c == '&') { 354 int pos = str.indexOf(";", i); 355 if (pos == -1) { buf.append('&'); 357 } 358 else if (str.charAt(i+1) == '#') { 359 int val = Integer.parseInt(str.substring(i+2, pos), 16); 360 buf.append((char)val); 361 i = pos; 362 } 363 else { 364 String substr = str.substring(i, pos + 1); 365 if (substr.equals("&")) 366 buf.append('&'); 367 else if (substr.equals("<")) 368 buf.append('<'); 369 else if (substr.equals(">")) 370 buf.append('>'); 371 else if (substr.equals(""")) 372 buf.append('"'); 373 else if (substr.equals("'")) 374 buf.append('\''); 375 else buf.append(substr); 377 i = pos; 378 } 379 } 380 else { 381 buf.append(c); 382 } 383 } 384 return buf.toString(); 385 } 386 387 404 public static String replaceDelimited(String start, String end, Replacer r, 405 String s) 406 { 407 return replaceDelimited(null, start, end, r, s); 408 } 409 410 430 public static String replaceDelimited(String exceptAfter, String start, 431 String end, Replacer r, String s) 432 { 433 if (s == null) 434 return null; 435 436 int startLength, endLength; 437 if (start == null || end == null || (startLength = start.length()) == 0 438 || (endLength = end.length()) == 0) 439 return s; 440 441 int exceptAfterLength = exceptAfter == null ? 0 : exceptAfter.length(); 442 443 String str = new String (s); int pos, pos2; 445 int searchFrom = 0; 446 while ((pos = str.indexOf(start, searchFrom)) != -1) { 447 if (exceptAfterLength > 0) { 449 int lookFrom = pos - exceptAfterLength; 450 if (lookFrom >= 0 451 && str.indexOf(exceptAfter, lookFrom) == lookFrom) 452 { 453 searchFrom = pos + 1; 454 continue; 455 } 456 } 457 458 pos2 = str.indexOf(end, pos + startLength); 459 if (pos2 != -1) { 460 Object val = r.replace(str.substring(pos + startLength, pos2)); 461 if (val == null) 462 return null; 463 String valAsString = val.toString(); 464 str = str.substring(0, pos) + valAsString 465 + str.substring(pos2 + endLength); 466 searchFrom = pos + valAsString.length(); 467 } 468 else break; 470 } 471 return str; 472 } 473 474 480 public static String nullOrTrimmed(String str) { 481 return str == null ? str : str.trim(); 482 } 483 484 } 485 | Popular Tags |