001: /*
002: * Copyright 2001 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.proxylet.util;
006:
007: import java.io.PrintWriter;
008: import java.io.StringWriter;
009: import java.util.ArrayList;
010: import java.util.Arrays;
011: import java.util.Collection;
012: import java.util.Iterator;
013: import java.util.StringTokenizer;
014:
015: /**
016: * Utility class which provide few commonly used methods to manipulate strings
017: *
018: */
019: public final class StringHelper {
020: public static String searchAndReplace(final String aSource,
021: final String[][] aFindReplaceMap) {
022: if (aSource == null || aFindReplaceMap == null) {
023: return aSource;
024: }
025:
026: String lResult = aSource;
027: for (int i = 0; i < aFindReplaceMap.length; i++) {
028: lResult = searchAndReplace(lResult, aFindReplaceMap[i][0],
029: aFindReplaceMap[i][1]);
030: }
031: return lResult;
032: }//searchAndReplace()
033:
034: public static String[] searchAndReplace(final String[] aSource,
035: final String aFind, final String aReplace) {
036: if (aSource == null || aSource.length == 0) {
037: return aSource;
038: }
039:
040: String[] lResult = new String[aSource.length];
041: for (int i = 0; i < aSource.length; i++) {
042: lResult[i] = searchAndReplace(aSource[i], aFind, aReplace);
043: }
044:
045: return lResult;
046: }//searchAndReplace()
047:
048: public static String searchAndReplace(final String aSource,
049: final String aFind, String aReplace) {
050: int lFindLength;
051: // the next statement has the side effect of throwing a null pointer
052: // exception if s is null.
053: if (aSource == null || aFind == null
054: || (lFindLength = aFind.length()) == 0) {
055: // If there is nothing to find, we won't try and find it.
056: return aSource;
057: }
058:
059: if (aReplace == null) {
060: // a null string and an empty string are the same
061: // for replacement purposes.
062: aReplace = "";
063: }
064:
065: int lSourceLength = aSource.length();
066: int lReplaceLength = aReplace.length();
067:
068: // We need to figure out how long our resulting string will be.
069: // This is required because without it, the possible resizing
070: // and copying of memory structures could lead to an unacceptable runtime.
071: // In the worst case it would have to be resized n times with each
072: // resize having a O(n) copy leading to an O(n^2) algorithm.
073: int length;
074: if (lFindLength == lReplaceLength) {
075: // special case in which we don't need to count the replacements
076: // because the count falls out of the length formula.
077: length = lSourceLength;
078: } else {
079: int count;
080: int start;
081: int end;
082:
083: // Scan s and count the number of times we find our target.
084: count = 0;
085: start = 0;
086: while ((end = aSource.indexOf(aFind, start)) != -1) {
087: count++;
088: start = end + lFindLength;
089: }
090:
091: if (count == 0) {
092: // special case in which on first pass, we find there is nothing
093: // to be replaced. No need to do a second pass or create a string buffer.
094: return aSource;
095: }
096:
097: length = lSourceLength
098: - (count * (lFindLength - lReplaceLength));
099: }
100:
101: int start = 0;
102: int end = aSource.indexOf(aFind, start);
103: if (end == -1) {
104: // nothing was found in the string to replace.
105: // we can get this if the find and replace strings
106: // are the same length because we didn't check before.
107: // in this case, we will return the original string
108: return aSource;
109: }
110: // it looks like we actually have something to replace
111: // *sigh* allocate memory for it.
112: StringBuffer sb = new StringBuffer(length);
113:
114: // Scan s and do the replacements
115: while (end != -1) {
116: sb.append(aSource.substring(start, end));
117: sb.append(aReplace);
118: start = end + lFindLength;
119: end = aSource.indexOf(aFind, start);
120: }
121: end = lSourceLength;
122: sb.append(aSource.substring(start, end));
123:
124: return (sb.toString());
125: }//searchAndReplace()
126:
127: public static String normalize(final String aSource) {
128: return (aSource != null) ? aSource.trim() : "";
129: }//normalize()
130:
131: public static String normalize(final String aSource,
132: final String aDefault) {
133: final String result = normalize(aSource);
134: return (result.length() == 0) ? aDefault : result;
135: }//normalize()
136:
137: public static String exceptionStack2String(final Throwable t) {
138: if (t != null) {
139: StringWriter sw = new StringWriter();
140: t.printStackTrace(new PrintWriter(sw));
141: return sw.toString();
142: }
143:
144: return null;
145: }//exceptionStack2String()
146:
147: public static String findMatchByIgnoringCase(
148: final Collection aCollection, final String aString) {
149: final Iterator iterator = aCollection.iterator();
150: String s;
151: while (iterator.hasNext()) {
152: s = (String) iterator.next();
153: if (s.equalsIgnoreCase(aString)) {
154: return s;
155: }
156: }//while loop
157:
158: return null;
159: }//findMatch()
160:
161: public static final String[] tokenize(final String aString,
162: final String aTokens) {
163: return tokenize(aString, aTokens, false);
164: }//tokenize()
165:
166: public static String[] tokenize(final String aSource,
167: final String aTokenList, final boolean aIncludeEmpty,
168: final int aEnsureLength) {
169: final String[] result = tokenize(aSource, aTokenList,
170: aIncludeEmpty);
171: return ensureLength(result, aEnsureLength);
172: }//splitString()
173:
174: public static final String[] tokenize(final String aString,
175: final String aTokens, final boolean aIncludeEmpty) {
176: if (aTokens.length() == 1) {
177: return tokenize(aString, aTokens.charAt(0), aIncludeEmpty);
178: } else {
179: return tokenizeNonPerfWay(aString, aTokens, aIncludeEmpty);
180: }
181: }//tokenize()
182:
183: private static final String[] tokenize(final String aString,
184: final char aToken, final boolean aIncludeEmpty) {
185: final ArrayList lList = new ArrayList();
186:
187: int i = 0;
188: int j = aString.indexOf(aToken); // First substring
189:
190: String sub;
191: while (j >= 0) {
192: if (j != 0
193: && aString.charAt(j - 1) == Constants.ESCAPE_FORWARD_SLASH_CHAR) {
194: j = aString.indexOf(aToken, j + 1); //skip this token
195: continue;
196: }
197:
198: sub = aString.substring(i, j);
199: i = j + 1;
200: j = aString.indexOf(aToken, i); // Rest of substrings
201: if (aIncludeEmpty || sub.length() > 0) {
202: lList.add(sub);
203: }
204: }
205:
206: sub = aString.substring(i); //Last substring
207: if (aString.length() > 0 && (aIncludeEmpty || sub.length() > 0)) {
208: lList.add(sub);
209: }
210:
211: return (String[]) lList.toArray(Constants.EMPTY_STRING_ARRAY);
212: }//tokenize()
213:
214: private static String[] tokenizeNonPerfWay(
215: final String aSourceString, final String aDelimList,
216: final boolean aBool) {
217: final StringTokenizer st = new StringTokenizer(aSourceString,
218: aDelimList, aBool);
219: final ArrayList lList = new ArrayList();
220:
221: boolean wasPreviousOneADelim = false;
222: for (int i = 0; st.hasMoreTokens(); i++) {
223: String bToken = st.nextToken();
224: boolean isPresentOneADelim = (aDelimList.indexOf(bToken) != -1) ? true
225: : false;
226:
227: if (!isPresentOneADelim) {
228: lList.add(bToken);
229: } else {
230: if (i == 0) {
231: lList.add("");
232: } else if (wasPreviousOneADelim) {
233: lList.add("");
234: }
235: }
236:
237: wasPreviousOneADelim = isPresentOneADelim;
238: }//for loop
239:
240: if (wasPreviousOneADelim) {
241: lList.add("");
242: }
243:
244: return (String[]) lList.toArray(Constants.EMPTY_STRING_ARRAY);
245: }//parse()
246:
247: private static String[] ensureLength(String[] result,
248: final int aLength) {
249: //insert the dummy ones only if the lenght is less than the passed one
250: if (aLength > result.length) {
251: String[] temp = new String[aLength];
252: Arrays.fill(temp, Constants.EMPTY_STRING);
253: System.arraycopy(result, 0, temp, 0, result.length);
254: result = temp;
255: }
256:
257: return result;
258: }//ensureLength()
259:
260: /**
261: * Not implemented.. would be implemented when jdk1.4 is used
262: */
263: public static String[] regExpSplit(final String aOrgURL,
264: final String aRegExp) {
265: if (aOrgURL == null) {
266: return Constants.EMPTY_STRING_ARRAY;
267: }
268:
269: String urlPart = aOrgURL.trim();
270:
271: //remove the starting and ending ', " ,\t, \s chars
272: while (urlPart.startsWith(Constants.DOUBLE_QUOTES)
273: || urlPart.startsWith(Constants.SINGLE_QUOTES)
274: || urlPart.startsWith(Constants.ESCAPE_SINGLE_QUOTES)
275: || urlPart.startsWith(Constants.ESCAPE_DOUBLE_QUOTES)) {
276: urlPart = urlPart.substring(1).trim();
277: }
278:
279: //BugNo:4784407
280: while (urlPart.startsWith("&#")) {
281: int index = urlPart.indexOf(";");
282: if (index == -1) {
283: break;
284: }
285:
286: urlPart = urlPart.substring(index + 1).trim();
287: }
288:
289: int firstIndex = aOrgURL.indexOf(urlPart);
290:
291: while (urlPart.endsWith(Constants.ESCAPE_SINGLE_QUOTES)
292: || urlPart.endsWith(Constants.ESCAPE_DOUBLE_QUOTES)
293: || urlPart.endsWith(Constants.DOUBLE_QUOTES)
294: || urlPart.endsWith(Constants.SINGLE_QUOTES)) {
295: urlPart = urlPart.substring(0, urlPart.length() - 1).trim();
296: if (urlPart.endsWith("\\")) {
297: urlPart = urlPart.substring(0, urlPart.length() - 1)
298: .trim();
299: }
300: }
301:
302: return new String[] { aOrgURL.substring(0, firstIndex),
303: urlPart,
304: aOrgURL.substring(firstIndex + urlPart.length()) };
305: }//regExpSplit()
306:
307: public static final String trimQuotes(String aString) {
308: String lValue = aString;
309: String[] splitted = StringHelper.regExpSplit(lValue,
310: Constants.REG_EX_FOR_TRIMING_QUOTES);
311:
312: if (splitted.length == 3) {
313: lValue = splitted[1];
314: }
315:
316: return lValue;
317: }//trimQuotes()
318:
319: public static boolean startsWithIgnoreCase(String aSRC,
320: String aMache) {
321: return aSRC.regionMatches(true, 0, aMache, 0, aMache.length());
322: }//startsWithIgnoreCase()
323:
324: public static void main(String[] args) {
325: final String[][] lData = {
326: { "", ":" },
327: { ":::", ":" },
328:
329: {
330: "jini://raja.sun. com ;mahesh.sun.com;jini://srinu.sil.co.in",
331: ";" },
332:
333: { ",y,,", "," },
334:
335: { "test", " ", },
336:
337: { ",y,", "," },
338:
339: { ",y", "," },
340:
341: { " ,y", "," },
342:
343: };
344: for (int i = 0; i < lData.length; i++) {
345: System.out.println(Arrays.asList(tokenize(lData[i][0],
346: lData[i][1], true)));
347: }
348: }//main()
349: }//class StringHelper
|