1 55 56 package org.apache.bsf.util; 57 58 import java.io.*; 59 import java.util.*; 60 import java.net.URL ; 61 import java.net.MalformedURLException ; 62 import java.beans.Introspector ; 63 64 69 public class StringUtils 70 { 71 public static final String lineSeparator = 72 System.getProperty("line.separator", "\n"); 73 public static final String lineSeparatorStr = cleanString(lineSeparator); 74 75 public static String classNameToVarName(String className) 76 { 77 int arrayDim = 0; 79 80 while (className.endsWith("[]")) 81 { 82 className = className.substring(0, className.length() - 2); 83 arrayDim++; 84 } 85 86 int iLastPeriod = className.lastIndexOf('.'); 87 String varName = Introspector.decapitalize( 88 iLastPeriod != -1 89 ? className.substring(iLastPeriod + 1) 90 : className); 91 92 if (arrayDim > 0) 93 { 94 varName += "_" + arrayDim + "D"; 95 } 96 97 return getValidIdentifierName(varName); 98 } 99 public static String cleanString(String str) 101 { 102 if (str == null) 103 return null; 104 else 105 { 106 char[] charArray = str.toCharArray(); 107 StringBuffer sBuf = new StringBuffer (); 108 109 for (int i = 0; i < charArray.length; i++) 110 switch (charArray[i]) 111 { 112 case '\"' : sBuf.append("\\\""); 113 break; 114 case '\\' : sBuf.append("\\\\"); 115 break; 116 case '\n' : sBuf.append("\\n"); 117 break; 118 case '\r' : sBuf.append("\\r"); 119 break; 120 default : sBuf.append(charArray[i]); 121 break; 122 } 123 124 return sBuf.toString(); 125 } 126 } 127 132 public static String getChars(int numberOfChars, char theChar) 133 { 134 if (numberOfChars <= 0) 135 return ""; 136 137 StringBuffer sRet = new StringBuffer (numberOfChars); 138 139 for (int i = 0; i < numberOfChars; i++) 140 sRet.append(theChar); 141 142 return sRet.toString(); 143 } 144 155 public static String getClassName(Class targetClass) 156 { 157 String className = targetClass.getName(); 158 159 return targetClass.isArray() ? parseDescriptor(className) : className; 160 } 161 public static String getCommaListFromVector(Vector sourceVector) 162 { 163 StringBuffer strBuf = new StringBuffer (); 164 165 for (int i = 0; i < sourceVector.size(); i++) 166 { 167 strBuf.append((i > 0 ? ", " : "") + 168 sourceVector.elementAt(i)); 169 } 170 171 return strBuf.toString(); 172 } 173 177 public static Reader getContentAsReader(URL url) throws SecurityException , 178 IllegalArgumentException , 179 IOException 180 { 181 if (url == null) 182 { 183 throw new IllegalArgumentException ("URL cannot be null."); 184 } 185 186 try 187 { 188 Object content = url.getContent(); 189 190 if (content == null) 191 { 192 throw new IllegalArgumentException ("No content."); 193 } 194 195 if (content instanceof InputStream) 196 { 197 Reader in = new InputStreamReader((InputStream)content); 198 199 if (in.ready()) 200 { 201 return in; 202 } 203 else 204 { 205 throw new FileNotFoundException(); 206 } 207 } 208 else 209 { 210 throw new IllegalArgumentException ((content instanceof String ) 211 ? (String )content 212 : "This URL points to a: " + 213 StringUtils.getClassName(content.getClass())); 214 } 215 } 216 catch (SecurityException e) 217 { 218 throw new SecurityException ("Your JVM's SecurityManager has disallowed this."); 219 } 220 catch (FileNotFoundException e) 221 { 222 throw new FileNotFoundException("This file was not found: " + url); 223 } 224 } 225 228 public static String getContentAsString(URL url) throws SecurityException , 229 IllegalArgumentException , 230 IOException 231 { 232 return IOUtils.getStringFromReader(getContentAsReader(url)); 233 } 234 public static String getSafeString(String scriptStr) 236 { 237 BufferedReader in = new BufferedReader(new StringReader(scriptStr)); 238 StringBuffer strBuf = new StringBuffer (); 239 String tempLine, 240 previousLine = null; 241 242 try 243 { 244 while ((tempLine = in.readLine()) != null) 245 { 246 if (previousLine != null) 247 { 248 strBuf.append("\"" + previousLine + lineSeparatorStr + "\" +" + 249 lineSeparator); 250 } 251 252 previousLine = cleanString(tempLine); 253 } 254 } 255 catch (IOException e) 256 { 257 } 258 259 strBuf.append("\"" + (previousLine != null ? previousLine : "") + "\"" + 260 lineSeparator); 261 262 return strBuf.toString(); 263 } 264 266 public static URL getURL(URL contextURL, String spec) throws MalformedURLException 267 { 268 return getURL(contextURL, spec, 1); 269 } 270 274 private static URL getURL(URL contextURL, String spec, int recursiveDepth) 275 throws MalformedURLException 276 { 277 URL url = null; 278 279 try 280 { 281 url = new URL (contextURL, spec); 282 283 try 284 { 285 url.openStream(); 286 } 287 catch (IOException ioe1) 288 { 289 throw new MalformedURLException ("This file was not found: " + url); 290 } 291 } 292 catch (MalformedURLException e1) 293 { 294 url = new URL ("file", "", spec); 295 296 try 297 { 298 url.openStream(); 299 } 300 catch (IOException ioe2) 301 { 302 if (contextURL != null) 303 { 304 String contextFileName = contextURL.getFile(); 305 String parentName = new File(contextFileName).getParent(); 306 307 if (parentName != null && recursiveDepth < 3) 308 { 309 return getURL(new URL ("file", "", parentName + '/'), 310 spec, 311 recursiveDepth + 1); 312 } 313 } 314 315 throw new MalformedURLException ("This file was not found: " + url); 316 } 317 } 318 319 return url; 320 } 321 public static String getValidIdentifierName(String identifierName) 322 { 323 if (identifierName == null || identifierName.length() == 0) 324 return null; 325 326 StringBuffer strBuf = new StringBuffer (); 327 328 char[] chars = identifierName.toCharArray(); 329 330 strBuf.append(Character.isJavaIdentifierStart(chars[0]) 331 ? chars[0] 332 : '_' 333 ); 334 335 for (int i = 1; i < chars.length; i++) 336 { 337 strBuf.append(Character.isJavaIdentifierPart(chars[i]) 338 ? chars[i] 339 : '_' 340 ); 341 } 342 343 return strBuf.toString(); 344 } 345 public static boolean isValidIdentifierName(String identifierName) 346 { 347 if (identifierName == null || identifierName.length() == 0) 348 return false; 349 350 char[] chars = identifierName.toCharArray(); 351 352 if (!Character.isJavaIdentifierStart(chars[0])) 353 return false; 354 355 for (int i = 1; i < chars.length; i++) 356 if (!Character.isJavaIdentifierPart(chars[i])) 357 return false; 358 359 return true; 360 } 361 public static boolean isValidPackageName(String packageName) 362 { 363 if (packageName == null) 364 return false; 365 else if (packageName.length() == 0) 366 return true; 368 369 StringTokenizer strTok = new StringTokenizer(packageName, ".", true); 370 371 if (strTok.countTokens() % 2 != 1) 373 return false; 374 375 if (!isValidIdentifierName(strTok.nextToken())) 377 return false; 378 379 while (strTok.hasMoreTokens()) 381 { 382 if (!strTok.nextToken().equals(".")) 384 return false; 385 386 if (strTok.hasMoreTokens()) 388 { 389 if (!isValidIdentifierName(strTok.nextToken())) 390 return false; 391 } 392 else 393 return false; 394 } 395 396 return true; 397 } 398 401 private static String parseDescriptor(String className) 402 { 403 char[] classNameChars = className.toCharArray(); 404 int arrayDim = 0; 405 int i = 0; 406 407 while (classNameChars[i] == '[') 408 { 409 arrayDim++; 410 i++; 411 } 412 413 StringBuffer classNameBuf = new StringBuffer (); 414 415 switch (classNameChars[i++]) 416 { 417 case 'B' : classNameBuf.append("byte"); 418 break; 419 case 'C' : classNameBuf.append("char"); 420 break; 421 case 'D' : classNameBuf.append("double"); 422 break; 423 case 'F' : classNameBuf.append("float"); 424 break; 425 case 'I' : classNameBuf.append("int"); 426 break; 427 case 'J' : classNameBuf.append("long"); 428 break; 429 case 'S' : classNameBuf.append("short"); 430 break; 431 case 'Z' : classNameBuf.append("boolean"); 432 break; 433 case 'L' : classNameBuf.append(classNameChars, 434 i, classNameChars.length - i - 1); 435 break; 436 } 437 438 for (i = 0; i < arrayDim; i++) 439 classNameBuf.append("[]"); 440 441 return classNameBuf.toString(); 442 } 443 } 444 | Popular Tags |