1 4 5 9 10 package org.openlaszlo.utils; 11 import java.util.*; 12 13 18 public abstract class StringUtils { 19 34 public static String [] split(String str, String delim) 35 { 36 List lines = new Vector(); 37 int startPos = 0; 38 while (true) { 39 int endPos = indexOf(str, delim, startPos); 40 if (endPos == -1) { 41 if (startPos > 0 || startPos < str.length()) { 42 lines.add(str.substring(startPos)); 43 } 44 break; 45 } 46 lines.add(str.substring(startPos, endPos)); 47 startPos = endPos + delim.length(); 48 } 49 { 50 String [] result = new String [lines.size()]; 51 int i = 0; 52 for (Iterator e = lines.iterator(); e.hasNext(); ) { 53 result[i] = (String ) e.next(); 54 i++; 55 } 56 return result; 57 } 58 } 59 60 72 public static String join(String [] lines, String delim) { 73 StringBuffer buffer = new StringBuffer (); 74 for (int i = 0; i < lines.length; i++) { 75 if (i > 0) { 76 buffer.append(delim); 77 } 78 buffer.append(lines[i]); 79 } 80 return buffer.toString(); 81 } 82 83 84 public static String join(List words, String delim) { 85 StringBuffer buffer = new StringBuffer (); 86 for (Iterator iter = words.iterator(); iter.hasNext(); ) { 87 buffer.append(iter.next()); 88 if (iter.hasNext()) { 89 buffer.append(delim); 90 } 91 } 92 return buffer.toString(); 93 } 94 95 102 public static int indexOf(String str, String value, int offset) { 103 if (value.length() == 0) { 104 throw new IllegalArgumentException (); 105 } 106 while (offset < str.length()) { 107 int pos = str.indexOf(value.charAt(0), offset); 108 if (pos == -1) { 109 return pos; 110 } 111 for (int i = 1; ; i++) { 112 if (i >= value.length()) { 113 return pos; 114 } 115 if (pos + i >= str.length() || 116 str.charAt(pos + i) != value.charAt(i)) { 117 break; 118 } 119 } 120 offset = pos + 1; 121 } 122 return -1; 123 } 124 125 134 public static String normalizeLineEndings(String src) { 135 if (src.indexOf('\r') >= 0) { 136 return StringUtils.join(StringUtils.split(src, "\r"), "\n"); 137 } 138 return src; 139 } 140 141 147 public static int parseInt(String src) { 148 if (src.length() > 1 && src.charAt(0) == '#') { 149 return Integer.parseInt(src.substring(1), 16); 150 } 151 else if (src.length() > 2 && src.charAt(0) == '0' 152 && src.charAt(1) == 'x') { 153 return Integer.parseInt(src.substring(2), 16); 154 } else { 155 return Integer.parseInt(src); 156 } 157 } 158 159 160 168 public static String replace(String str, char oldChar, String newStr) 169 { 170 int os = newStr.length()-1; 172 int bufOS = 0; 173 StringBuffer buf = new StringBuffer (str); 174 int i = str.indexOf(oldChar); 175 while (i != -1) { 176 buf.replace(i+bufOS, i+bufOS+1, newStr); 179 bufOS += os; 180 i = str.indexOf(oldChar, i+1); 181 } 182 return buf.toString(); 183 } 184 185 192 public static String setCharAt(String str, int x, char c) 193 throws IndexOutOfBoundsException { 194 195 StringBuffer buf = new StringBuffer (str); 196 buf.setCharAt(x, c); 197 return buf.toString(); 198 199 } 200 201 209 public static String replace(String str, String oldStr, String newStr) 210 { 211 StringBuffer buf = new StringBuffer (); 212 int curIndex = 0; 213 int len = oldStr.length(); 214 215 while(true) { 216 int x = str.indexOf(oldStr, curIndex); 217 if (x != -1) { 218 buf.append(str.substring(curIndex, x)); 219 buf.append(newStr); 220 curIndex = x + len; 221 } else { 222 buf.append(str.substring(curIndex)); 223 break; 224 } 225 } 226 227 return buf.toString(); 228 } 229 230 231 240 public static String expandPropertyValues(String str) 241 throws Exception 242 { 243 StringBuffer buf = new StringBuffer (); 244 245 int i0, i1; 246 int pos = 0; 247 String propName; 248 String propValue; 249 while (pos < str.length()) { 250 i0 = str.indexOf("${", pos); 251 if (i0 == -1) { 252 buf.append(str.substring(pos)); 253 break; 254 } 255 i1 = str.indexOf('}', i0); 256 if (i1 == -1) 257 throw new Exception ("missing close bracket: '}'"); 258 259 if (i0 > pos) 261 buf.append(str.substring(pos, i0)); 262 263 propName = str.substring(i0+2, i1); 264 try { 265 propValue = System.getProperty(propName); 266 if (propValue == null) 267 throw new Exception ("System property " + propName + 268 " does not exist"); 269 } catch (SecurityException e) { 270 propValue = ""; 272 } 273 274 buf.append(propValue); 275 276 pos = i1+1; 277 } 278 return buf.toString(); 279 } 280 } 281 | Popular Tags |