KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > aspectwerkz > util > Strings


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.aspectwerkz.util;
5
6 import java.util.List JavaDoc;
7 import java.util.ArrayList JavaDoc;
8
9 /**
10  * Utility methods for strings.
11  *
12  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
13  */

14 public class Strings {
15   /**
16    * Private constructor to prevent instantiability.
17    */

18   private Strings() {
19   }
20
21   /**
22    * Removes newline, carriage return and tab characters from a string.
23    *
24    * @param toBeEscaped string to escape
25    * @return the escaped string
26    */

27   public static String JavaDoc removeFormattingCharacters(final String JavaDoc toBeEscaped) {
28     StringBuffer JavaDoc escapedBuffer = new StringBuffer JavaDoc();
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 JavaDoc s = escapedBuffer.toString();
35     return s;//
36
// Strings.replaceSubString(s, "\"", "")
37
}
38
39   /**
40    * Replaces all occurences of a substring inside a string.
41    *
42    * @param str the string to search and replace in
43    * @param oldToken the string to search for
44    * @param newToken the string to replace newToken
45    * @return the new string
46    */

47   public static String JavaDoc replaceSubString(final String JavaDoc str, final String JavaDoc oldToken, final String JavaDoc newToken) {
48     return replaceSubString(str, oldToken, newToken, -1);
49   }
50
51   /**
52    * Replaces all occurences of a substring inside a string.
53    *
54    * @param str the string to search and replace in
55    * @param oldToken the string to search for
56    * @param newToken the string to replace newToken
57    * @param max maximum number of values to replace (-1 => no maximum)
58    * @return the new string
59    */

60   public static String JavaDoc replaceSubString(final String JavaDoc str, final String JavaDoc oldToken, final String JavaDoc newToken, int max) {
61     if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) {
62       return str;
63     }
64     StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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   /**
79    * String split on multicharacter delimiter. <p/>Written by Tim Quinn (tim.quinn@honeywell.com)
80    *
81    * @param stringToSplit
82    * @param delimiter
83    * @return
84    */

85   public static final String JavaDoc[] splitString(String JavaDoc stringToSplit, String JavaDoc delimiter) {
86     String JavaDoc[] aRet;
87     int iLast;
88     int iFrom;
89     int iFound;
90     int iRecords;
91
92     // return Blank Array if stringToSplit == "")
93
if (stringToSplit.equals("")) {
94       return new String JavaDoc[0];
95     }
96
97     // count Field Entries
98
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     // populate aRet[]
111
aRet = new String JavaDoc[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) { // at End
121
aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length());
122         } else if (iFound == 0) { // at Beginning
123
aRet[i] = "";
124         } else { // somewhere in middle
125
aRet[i] = stringToSplit.substring(iFrom, iFound);
126         }
127         iLast = iFound;
128         iFrom = iFound + delimiter.length();
129       }
130     }
131     return aRet;
132   }
133
134   /**
135    * Parse a method signature or method call signature.
136    * <br/>Given a call signature like "method(Type t)", extract the method name
137    * and param type and parameter name: [method, Type, t]
138    * <br/>Given a signature like "method(X x, Y)", extract the method name
139    * and param name / param type - but leaving empty String if
140    * the information is not available: [method, X, x, Y, ""]
141    *
142    * @param methodCallSignature
143    * @return each element (2xp+1 sized) (see doc)
144    */

145   public static String JavaDoc[] extractMethodSignature(String JavaDoc methodCallSignature) {
146     List JavaDoc extracted = new ArrayList JavaDoc();
147     String JavaDoc methodName = methodCallSignature;
148     String JavaDoc 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 JavaDoc[] parameters = Strings.splitString(methodCallDesc, ",");
157       for (int i = 0; i < parameters.length; i++) {
158         String JavaDoc[] 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 JavaDoc[]) extracted.toArray(new String JavaDoc[]{});
170   }
171
172   public static boolean isNullOrEmpty(String JavaDoc s) {
173     return (s == null) ? true : (s.length() <= 0);
174   }
175 }
Popular Tags