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