1 8 package org.codehaus.aspectwerkz.util; 9 10 import java.util.List ; 11 import java.util.ArrayList ; 12 13 18 public class Strings { 19 22 private Strings() { 23 } 24 25 31 public static String removeFormattingCharacters(final String toBeEscaped) { 32 StringBuffer escapedBuffer = new StringBuffer (); 33 for (int i = 0; i < toBeEscaped.length(); i++) { 34 if ((toBeEscaped.charAt(i) != '\n') && (toBeEscaped.charAt(i) != '\r') && (toBeEscaped.charAt(i) != '\t')) { 35 escapedBuffer.append(toBeEscaped.charAt(i)); 36 } 37 } 38 String s = escapedBuffer.toString(); 39 return s; } 42 43 51 public static String replaceSubString(final String str, final String oldToken, final String newToken) { 52 return replaceSubString(str, oldToken, newToken, -1); 53 } 54 55 64 public static String replaceSubString(final String str, final String oldToken, final String newToken, int max) { 65 if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) { 66 return str; 67 } 68 StringBuffer buf = new StringBuffer (str.length()); 69 int start = 0; 70 int end = 0; 71 while ((end = str.indexOf(oldToken, start)) != -1) { 72 buf.append(str.substring(start, end)).append(newToken); 73 start = end + oldToken.length(); 74 if (--max == 0) { 75 break; 76 } 77 } 78 buf.append(str.substring(start)); 79 return buf.toString(); 80 } 81 82 89 public static final String [] splitString(String stringToSplit, String delimiter) { 90 String [] aRet; 91 int iLast; 92 int iFrom; 93 int iFound; 94 int iRecords; 95 96 if (stringToSplit.equals("")) { 98 return new String [0]; 99 } 100 101 iFrom = 0; 103 iRecords = 0; 104 while (true) { 105 iFound = stringToSplit.indexOf(delimiter, iFrom); 106 if (iFound == -1) { 107 break; 108 } 109 iRecords++; 110 iFrom = iFound + delimiter.length(); 111 } 112 iRecords = iRecords + 1; 113 114 aRet = new String [iRecords]; 116 if (iRecords == 1) { 117 aRet[0] = stringToSplit; 118 } else { 119 iLast = 0; 120 iFrom = 0; 121 iFound = 0; 122 for (int i = 0; i < iRecords; i++) { 123 iFound = stringToSplit.indexOf(delimiter, iFrom); 124 if (iFound == -1) { aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length()); 126 } else if (iFound == 0) { aRet[i] = ""; 128 } else { aRet[i] = stringToSplit.substring(iFrom, iFound); 130 } 131 iLast = iFound; 132 iFrom = iFound + delimiter.length(); 133 } 134 } 135 return aRet; 136 } 137 138 149 public static String [] extractMethodSignature(String methodCallSignature) { 150 List extracted = new ArrayList (); 151 String methodName = methodCallSignature; 152 String methodCallDesc = null; 153 if (methodCallSignature.indexOf("(") > 0) { 154 methodName = methodName.substring(0, methodCallSignature.indexOf("(")); 155 methodCallDesc = 156 methodCallSignature.substring(methodCallSignature.indexOf("(") + 1, methodCallSignature.lastIndexOf(")")); 157 } 158 extracted.add(methodName); 159 if (methodCallDesc != null) { 160 String [] parameters = Strings.splitString(methodCallDesc, ","); 161 for (int i = 0; i < parameters.length; i++) { 162 String [] parameterInfo = Strings.splitString( 163 Strings.replaceSubString( 164 parameters[i].trim(), 165 " ", 166 " " 167 ), " " 168 ); 169 extracted.add(parameterInfo[0]); 170 extracted.add((parameterInfo.length > 1) ? parameterInfo[1] : ""); 171 } 172 } 173 return (String []) extracted.toArray(new String []{}); 174 } 175 176 public static boolean isNullOrEmpty(String s) { 177 return (s == null) ? true : (s.length() <= 0); 178 } 179 } | Popular Tags |