1 4 package com.tc.aspectwerkz.util; 5 6 import java.util.List ; 7 import java.util.ArrayList ; 8 9 14 public class Strings { 15 18 private Strings() { 19 } 20 21 27 public static String removeFormattingCharacters(final String toBeEscaped) { 28 StringBuffer escapedBuffer = new StringBuffer (); 29 for (int i = 0; i < toBeEscaped.length(); i++) { 30 if ((toBeEscaped.charAt(i) != '\n') && (toBeEscaped.charAt(i) != '\r') && (toBeEscaped.charAt(i) != '\t')) { 31 escapedBuffer.append(toBeEscaped.charAt(i)); 32 } 33 } 34 String s = escapedBuffer.toString(); 35 return s; } 38 39 47 public static String replaceSubString(final String str, final String oldToken, final String newToken) { 48 return replaceSubString(str, oldToken, newToken, -1); 49 } 50 51 60 public static String replaceSubString(final String str, final String oldToken, final String newToken, int max) { 61 if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) { 62 return str; 63 } 64 StringBuffer buf = new StringBuffer (str.length()); 65 int start = 0; 66 int end = 0; 67 while ((end = str.indexOf(oldToken, start)) != -1) { 68 buf.append(str.substring(start, end)).append(newToken); 69 start = end + oldToken.length(); 70 if (--max == 0) { 71 break; 72 } 73 } 74 buf.append(str.substring(start)); 75 return buf.toString(); 76 } 77 78 85 public static final String [] splitString(String stringToSplit, String delimiter) { 86 String [] aRet; 87 int iLast; 88 int iFrom; 89 int iFound; 90 int iRecords; 91 92 if (stringToSplit.equals("")) { 94 return new String [0]; 95 } 96 97 iFrom = 0; 99 iRecords = 0; 100 while (true) { 101 iFound = stringToSplit.indexOf(delimiter, iFrom); 102 if (iFound == -1) { 103 break; 104 } 105 iRecords++; 106 iFrom = iFound + delimiter.length(); 107 } 108 iRecords = iRecords + 1; 109 110 aRet = new String [iRecords]; 112 if (iRecords == 1) { 113 aRet[0] = stringToSplit; 114 } else { 115 iLast = 0; 116 iFrom = 0; 117 iFound = 0; 118 for (int i = 0; i < iRecords; i++) { 119 iFound = stringToSplit.indexOf(delimiter, iFrom); 120 if (iFound == -1) { aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length()); 122 } else if (iFound == 0) { aRet[i] = ""; 124 } else { aRet[i] = stringToSplit.substring(iFrom, iFound); 126 } 127 iLast = iFound; 128 iFrom = iFound + delimiter.length(); 129 } 130 } 131 return aRet; 132 } 133 134 145 public static String [] extractMethodSignature(String methodCallSignature) { 146 List extracted = new ArrayList (); 147 String methodName = methodCallSignature; 148 String methodCallDesc = null; 149 if (methodCallSignature.indexOf("(") > 0) { 150 methodName = methodName.substring(0, methodCallSignature.indexOf("(")); 151 methodCallDesc = 152 methodCallSignature.substring(methodCallSignature.indexOf("(") + 1, methodCallSignature.lastIndexOf(")")); 153 } 154 extracted.add(methodName); 155 if (methodCallDesc != null) { 156 String [] parameters = Strings.splitString(methodCallDesc, ","); 157 for (int i = 0; i < parameters.length; i++) { 158 String [] parameterInfo = Strings.splitString( 159 Strings.replaceSubString( 160 parameters[i].trim(), 161 " ", 162 " " 163 ), " " 164 ); 165 extracted.add(parameterInfo[0]); 166 extracted.add((parameterInfo.length > 1) ? parameterInfo[1] : ""); 167 } 168 } 169 return (String []) extracted.toArray(new String []{}); 170 } 171 172 public static boolean isNullOrEmpty(String s) { 173 return (s == null) ? true : (s.length() <= 0); 174 } 175 } | Popular Tags |