1 /***************************************************************************************
2 * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3 * http://aspectwerkz.codehaus.org *
4 * ---------------------------------------------------------------------------------- *
5 * The software in this package is published under the terms of the LGPL license *
6 * a copy of which has been included with this distribution in the license.txt file. *
7 **************************************************************************************/
8 package org.codehaus.aspectwerkz.util;
9
10 import java.util.List;
11 import java.util.ArrayList;
12
13 /***
14 * Utility methods for strings.
15 *
16 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
17 */
18 public class Strings {
19 /***
20 * Private constructor to prevent instantiability.
21 */
22 private Strings() {
23 }
24
25 /***
26 * Removes newline, carriage return and tab characters from a string.
27 *
28 * @param toBeEscaped string to escape
29 * @return the escaped string
30 */
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;
40
41 }
42
43 /***
44 * Replaces all occurences of a substring inside a string.
45 *
46 * @param str the string to search and replace in
47 * @param oldToken the string to search for
48 * @param newToken the string to replace newToken
49 * @return the new string
50 */
51 public static String replaceSubString(final String str, final String oldToken, final String newToken) {
52 return replaceSubString(str, oldToken, newToken, -1);
53 }
54
55 /***
56 * Replaces all occurences of a substring inside a string.
57 *
58 * @param str the string to search and replace in
59 * @param oldToken the string to search for
60 * @param newToken the string to replace newToken
61 * @param max maximum number of values to replace (-1 => no maximum)
62 * @return the new string
63 */
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 /***
83 * String split on multicharacter delimiter. <p/>Written by Tim Quinn (tim.quinn@honeywell.com)
84 *
85 * @param stringToSplit
86 * @param delimiter
87 * @return
88 */
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
97 if (stringToSplit.equals("")) {
98 return new String[0];
99 }
100
101
102 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
115 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) {
125 aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length());
126 } else if (iFound == 0) {
127 aRet[i] = "";
128 } else {
129 aRet[i] = stringToSplit.substring(iFrom, iFound);
130 }
131 iLast = iFound;
132 iFrom = iFound + delimiter.length();
133 }
134 }
135 return aRet;
136 }
137
138 /***
139 * Parse a method signature or method call signature.
140 * <br/>Given a call signature like "method(Type t)", extract the method name
141 * and param type and parameter name: [method, Type, t]
142 * <br/>Given a signature like "method(X x, Y)", extract the method name
143 * and param name / param type - but leaving empty String if
144 * the information is not available: [method, X, x, Y, ""]
145 *
146 * @param methodCallSignature
147 * @return each element (2xp+1 sized) (see doc)
148 */
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 }