View Javadoc

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          return escapedBuffer.toString();
39      }
40  
41      /***
42       * Replaces all occurences of a substring inside a string.
43       * 
44       * @param str the string to search and replace in
45       * @param oldToken the string to search for
46       * @param newToken the string to replace newToken
47       * @return the new string
48       */
49      public static String replaceSubString(final String str, final String oldToken, final String newToken) {
50          return replaceSubString(str, oldToken, newToken, -1);
51      }
52  
53      /***
54       * Replaces all occurences of a substring inside a string.
55       * 
56       * @param str the string to search and replace in
57       * @param oldToken the string to search for
58       * @param newToken the string to replace newToken
59       * @param max maximum number of values to replace (-1 => no maximum)
60       * @return the new string
61       */
62      public static String replaceSubString(final String str, final String oldToken, final String newToken, int max) {
63          if ((str == null) || (oldToken == null) || (newToken == null) || (oldToken.length() == 0)) {
64              return str;
65          }
66          StringBuffer buf = new StringBuffer(str.length());
67          int start = 0;
68          int end = 0;
69          while ((end = str.indexOf(oldToken, start)) != -1) {
70              buf.append(str.substring(start, end)).append(newToken);
71              start = end + oldToken.length();
72              if (--max == 0) {
73                  break;
74              }
75          }
76          buf.append(str.substring(start));
77          return buf.toString();
78      }
79  
80      /***
81       * String split on multicharacter delimiter. <p/>Written by Tim Quinn (tim.quinn@honeywell.com)
82       * 
83       * @param stringToSplit
84       * @param delimiter
85       * @return
86       */
87      public static final String[] splitString(String stringToSplit, String delimiter) {
88          String[] aRet;
89          int iLast;
90          int iFrom;
91          int iFound;
92          int iRecords;
93  
94          // return Blank Array if stringToSplit == "")
95          if (stringToSplit.equals("")) {
96              return new String[0];
97          }
98  
99          // count Field Entries
100         iFrom = 0;
101         iRecords = 0;
102         while (true) {
103             iFound = stringToSplit.indexOf(delimiter, iFrom);
104             if (iFound == -1) {
105                 break;
106             }
107             iRecords++;
108             iFrom = iFound + delimiter.length();
109         }
110         iRecords = iRecords + 1;
111 
112         // populate aRet[]
113         aRet = new String[iRecords];
114         if (iRecords == 1) {
115             aRet[0] = stringToSplit;
116         } else {
117             iLast = 0;
118             iFrom = 0;
119             iFound = 0;
120             for (int i = 0; i < iRecords; i++) {
121                 iFound = stringToSplit.indexOf(delimiter, iFrom);
122                 if (iFound == -1) { // at End
123                     aRet[i] = stringToSplit.substring(iLast + delimiter.length(), stringToSplit.length());
124                 } else if (iFound == 0) { // at Beginning
125                     aRet[i] = "";
126                 } else { // somewhere in middle
127                     aRet[i] = stringToSplit.substring(iFrom, iFound);
128                 }
129                 iLast = iFound;
130                 iFrom = iFound + delimiter.length();
131             }
132         }
133         return aRet;
134     }
135 
136     /***
137      * Parse a method signature or method call signature.
138      * <br/>Given a call signature like "method(Type t)", extract the method name
139      * and param type and parameter name: [method, Type, t]
140      * <br/>Given a signature like "method(X x, Y)", extract the method name
141      * and param name / param type - but leaving empty String if
142      * the information is not available: [method, X, x, Y, ""]
143      *
144      * @param methodCallSignature
145      * @return each element (2xp+1 sized) (see doc)
146      */
147     public static String[] extractMethodSignature(String methodCallSignature) {
148         List extracted = new ArrayList();
149         String methodName = methodCallSignature;
150         String methodCallDesc = null;
151         if (methodCallSignature.indexOf("(") > 0) {
152             methodName = methodName.substring(0, methodCallSignature.indexOf("("));
153             methodCallDesc =
154             methodCallSignature.substring(methodCallSignature.indexOf("(") + 1, methodCallSignature.lastIndexOf(")"));
155         }
156         extracted.add(methodName);
157         if (methodCallDesc != null) {
158             String[] parameters = Strings.splitString(methodCallDesc, ",");
159             for (int i = 0; i < parameters.length; i++) {
160                 String[] parameterInfo = Strings.splitString(
161                         Strings.replaceSubString(
162                                 parameters[i].trim(),
163                                 "  ",
164                                 " "
165                         ), " "
166                 );
167                 extracted.add(parameterInfo[0]);
168                 extracted.add((parameterInfo.length>1)?parameterInfo[1]:"");
169             }
170         }
171         return (String[]) extracted.toArray(new String[]{});
172     }
173 }