001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.aspectwerkz.util;
005:
006: import java.util.List;
007: import java.util.ArrayList;
008:
009: /**
010: * Utility methods for strings.
011: *
012: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
013: */
014: public class Strings {
015: /**
016: * Private constructor to prevent instantiability.
017: */
018: private Strings() {
019: }
020:
021: /**
022: * Removes newline, carriage return and tab characters from a string.
023: *
024: * @param toBeEscaped string to escape
025: * @return the escaped string
026: */
027: public static String removeFormattingCharacters(
028: final String toBeEscaped) {
029: StringBuffer escapedBuffer = new StringBuffer();
030: for (int i = 0; i < toBeEscaped.length(); i++) {
031: if ((toBeEscaped.charAt(i) != '\n')
032: && (toBeEscaped.charAt(i) != '\r')
033: && (toBeEscaped.charAt(i) != '\t')) {
034: escapedBuffer.append(toBeEscaped.charAt(i));
035: }
036: }
037: String s = escapedBuffer.toString();
038: return s;//
039: // Strings.replaceSubString(s, "\"", "")
040: }
041:
042: /**
043: * Replaces all occurences of a substring inside a string.
044: *
045: * @param str the string to search and replace in
046: * @param oldToken the string to search for
047: * @param newToken the string to replace newToken
048: * @return the new string
049: */
050: public static String replaceSubString(final String str,
051: final String oldToken, final String newToken) {
052: return replaceSubString(str, oldToken, newToken, -1);
053: }
054:
055: /**
056: * Replaces all occurences of a substring inside a string.
057: *
058: * @param str the string to search and replace in
059: * @param oldToken the string to search for
060: * @param newToken the string to replace newToken
061: * @param max maximum number of values to replace (-1 => no maximum)
062: * @return the new string
063: */
064: public static String replaceSubString(final String str,
065: final String oldToken, final String newToken, int max) {
066: if ((str == null) || (oldToken == null) || (newToken == null)
067: || (oldToken.length() == 0)) {
068: return str;
069: }
070: StringBuffer buf = new StringBuffer(str.length());
071: int start = 0;
072: int end = 0;
073: while ((end = str.indexOf(oldToken, start)) != -1) {
074: buf.append(str.substring(start, end)).append(newToken);
075: start = end + oldToken.length();
076: if (--max == 0) {
077: break;
078: }
079: }
080: buf.append(str.substring(start));
081: return buf.toString();
082: }
083:
084: /**
085: * String split on multicharacter delimiter. <p/>Written by Tim Quinn (tim.quinn@honeywell.com)
086: *
087: * @param stringToSplit
088: * @param delimiter
089: * @return
090: */
091: public static final String[] splitString(String stringToSplit,
092: String delimiter) {
093: String[] aRet;
094: int iLast;
095: int iFrom;
096: int iFound;
097: int iRecords;
098:
099: // return Blank Array if stringToSplit == "")
100: if (stringToSplit.equals("")) {
101: return new String[0];
102: }
103:
104: // count Field Entries
105: iFrom = 0;
106: iRecords = 0;
107: while (true) {
108: iFound = stringToSplit.indexOf(delimiter, iFrom);
109: if (iFound == -1) {
110: break;
111: }
112: iRecords++;
113: iFrom = iFound + delimiter.length();
114: }
115: iRecords = iRecords + 1;
116:
117: // populate aRet[]
118: aRet = new String[iRecords];
119: if (iRecords == 1) {
120: aRet[0] = stringToSplit;
121: } else {
122: iLast = 0;
123: iFrom = 0;
124: iFound = 0;
125: for (int i = 0; i < iRecords; i++) {
126: iFound = stringToSplit.indexOf(delimiter, iFrom);
127: if (iFound == -1) { // at End
128: aRet[i] = stringToSplit.substring(iLast
129: + delimiter.length(), stringToSplit
130: .length());
131: } else if (iFound == 0) { // at Beginning
132: aRet[i] = "";
133: } else { // somewhere in middle
134: aRet[i] = stringToSplit.substring(iFrom, iFound);
135: }
136: iLast = iFound;
137: iFrom = iFound + delimiter.length();
138: }
139: }
140: return aRet;
141: }
142:
143: /**
144: * Parse a method signature or method call signature.
145: * <br/>Given a call signature like "method(Type t)", extract the method name
146: * and param type and parameter name: [method, Type, t]
147: * <br/>Given a signature like "method(X x, Y)", extract the method name
148: * and param name / param type - but leaving empty String if
149: * the information is not available: [method, X, x, Y, ""]
150: *
151: * @param methodCallSignature
152: * @return each element (2xp+1 sized) (see doc)
153: */
154: public static String[] extractMethodSignature(
155: String methodCallSignature) {
156: List extracted = new ArrayList();
157: String methodName = methodCallSignature;
158: String methodCallDesc = null;
159: if (methodCallSignature.indexOf("(") > 0) {
160: methodName = methodName.substring(0, methodCallSignature
161: .indexOf("("));
162: methodCallDesc = methodCallSignature.substring(
163: methodCallSignature.indexOf("(") + 1,
164: methodCallSignature.lastIndexOf(")"));
165: }
166: extracted.add(methodName);
167: if (methodCallDesc != null) {
168: String[] parameters = Strings.splitString(methodCallDesc,
169: ",");
170: for (int i = 0; i < parameters.length; i++) {
171: String[] parameterInfo = Strings.splitString(Strings
172: .replaceSubString(parameters[i].trim(), " ",
173: " "), " ");
174: extracted.add(parameterInfo[0]);
175: extracted
176: .add((parameterInfo.length > 1) ? parameterInfo[1]
177: : "");
178: }
179: }
180: return (String[]) extracted.toArray(new String[] {});
181: }
182:
183: public static boolean isNullOrEmpty(String s) {
184: return (s == null) ? true : (s.length() <= 0);
185: }
186: }
|