KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > codehaus > aspectwerkz > util > Strings


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 JavaDoc;
11 import java.util.ArrayList JavaDoc;
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 JavaDoc removeFormattingCharacters(final String JavaDoc toBeEscaped) {
32         StringBuffer JavaDoc escapedBuffer = new StringBuffer JavaDoc();
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 JavaDoc s = escapedBuffer.toString();
39         return s;//
40
// Strings.replaceSubString(s, "\"", "")
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 JavaDoc replaceSubString(final String JavaDoc str, final String JavaDoc oldToken, final String JavaDoc 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 JavaDoc replaceSubString(final String JavaDoc str, final String JavaDoc oldToken, final String JavaDoc newToken, int max) {
65         if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) {
66             return str;
67         }
68         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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 JavaDoc[] splitString(String JavaDoc stringToSplit, String JavaDoc delimiter) {
90         String JavaDoc[] aRet;
91         int iLast;
92         int iFrom;
93         int iFound;
94         int iRecords;
95
96         // return Blank Array if stringToSplit == "")
97
if (stringToSplit.equals("")) {
98             return new String JavaDoc[0];
99         }
100
101         // count Field Entries
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         // populate aRet[]
115
aRet = new String JavaDoc[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) { // at End
125
aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length());
126                 } else if (iFound == 0) { // at Beginning
127
aRet[i] = "";
128                 } else { // somewhere in middle
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 JavaDoc[] extractMethodSignature(String JavaDoc methodCallSignature) {
150         List JavaDoc extracted = new ArrayList JavaDoc();
151         String JavaDoc methodName = methodCallSignature;
152         String JavaDoc 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 JavaDoc[] parameters = Strings.splitString(methodCallDesc, ",");
161             for (int i = 0; i < parameters.length; i++) {
162                 String JavaDoc[] 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 JavaDoc[]) extracted.toArray(new String JavaDoc[]{});
174     }
175
176     public static boolean isNullOrEmpty(String JavaDoc s) {
177         return (s == null) ? true : (s.length() <= 0);
178     }
179 }
Popular Tags