KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jetspeed > util > StringUtils


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.jetspeed.util;
18
19 import java.util.StringTokenizer JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * This class provides static util methods for String manaipulation that
24  * aren't part of the default JDK functionalities.
25  *
26  * @author <a HREF="mailto:raphael@apache.org">Raphaël Luta</a>
27  * @author <a HREF="mailto:morciuch@apache.org">Mark Orciuch</a>
28  * @version $Id: StringUtils.java,v 1.9 2004/02/23 03:23:42 jford Exp $
29  */

30 public class StringUtils
31 {
32     /**
33      * Replaces all the occurences of a substring found within a string by a
34      * replacement string
35      *
36      * @deprecated (Generates too much garbage)
37      * @param original the string in where the replace will take place
38      * @param find the substring to find and replace
39      * @param replacement the replacement string for all occurences of find
40      * @return the original string where all the occurences of find are replaced by
41      * replacement
42      */

43     public static String JavaDoc replaceAll(String JavaDoc original, String JavaDoc find, String JavaDoc replacement)
44     {
45         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(original);
46
47         int idx = original.length();
48         int offset = find.length();
49         
50         while( ( idx=original.lastIndexOf(find, idx-1) ) > -1 )
51         {
52             buffer.replace(idx,idx+offset, replacement);
53         }
54
55         return buffer.toString();
56     }
57     /**
58      * Replaces all the occurences of a substring found
59      * within a StringBuffer by a
60      * replacement string
61      *
62      * @param buffer the StringBuffer in where the replace will take place
63      * @param find the substring to find and replace
64      * @param replacement the replacement string for all occurences of find
65      * @return the original StringBuffer where all the
66      * occurences of find are replaced by replacement
67      */

68     public static StringBuffer JavaDoc replaceAll(StringBuffer JavaDoc buffer, String JavaDoc find, String JavaDoc replacement)
69     {
70
71         int bufidx = buffer.length() - 1;
72         int offset = find.length();
73         while( bufidx > -1 ) {
74             int findidx = offset -1;
75             while( findidx > -1 ) {
76                 if( bufidx == -1 ) {
77                     //Done
78
return buffer;
79                 }
80                 if( buffer.charAt( bufidx ) == find.charAt( findidx ) ) {
81                     findidx--; //Look for next char
82
bufidx--;
83                 } else {
84                     findidx = offset - 1; //Start looking again
85
bufidx--;
86                     if( bufidx == -1 ) {
87                         //Done
88
return buffer;
89                     }
90                     continue;
91                 }
92             }
93             //Found
94
//System.out.println( "replacing from " + (bufidx + 1) +
95
// " to " + (bufidx + 1 + offset ) +
96
// " with '" + replacement + "'" );
97
buffer.replace( bufidx+1,
98                             bufidx+1+offset,
99                             replacement);
100             //start looking again
101
}
102         //No more matches
103
return buffer;
104             
105     }
106
107    /**
108      * Takes an array of tokens and converts into separator-separated string.
109      *
110      * @param String[] The array of strings input.
111      * @param String The string separator.
112      * @return String A string containing tokens separated by seperator.
113      */

114     public static final String JavaDoc arrayToString(String JavaDoc[] array, String JavaDoc separators)
115     {
116         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("");
117         String JavaDoc empty = "";
118         
119         if (array == null)
120             return empty;
121
122         if (separators == null)
123             separators = ",";
124
125         for (int ix=0; ix < array.length; ix++)
126         {
127             if (array[ix] != null && !array[ix].equals(""))
128             {
129                 sb.append(array[ix] + separators);
130             }
131         }
132         String JavaDoc str = sb.toString();
133         if (!str.equals(""))
134         {
135             str = str.substring(0, (str.length() - separators.length()));
136         }
137         return str;
138     }
139
140     /**
141       * Converts a delimited string into an array of string tokens.
142       *
143       * @param String[] The 'separator' separated string.
144       * @param String The string separator.
145       * @return String A string array of the original tokens.
146       */

147     public static final String JavaDoc[] stringToArray(String JavaDoc str, String JavaDoc separators)
148     {
149         StringTokenizer JavaDoc tokenizer;
150         String JavaDoc[] array = null;
151         int count = 0;
152
153         if (str == null)
154             return array;
155
156         if (separators == null)
157             separators = ",";
158
159         tokenizer = new StringTokenizer JavaDoc(str, separators);
160         if ((count = tokenizer.countTokens()) <= 0) {
161             return array;
162         }
163         
164         array = new String JavaDoc[count];
165         
166         int ix = 0;
167         while (tokenizer.hasMoreTokens())
168         {
169             array[ix] = tokenizer.nextToken();
170             ix++;
171         }
172
173         return array;
174     }
175
176     /**
177      * Remove a given set of characters from a String.
178      *
179      * @param String The input string to be cleansed of 'removeChars'.
180      * @param String The characters to be removed.
181      * @return String The new string cleansed of 'removeChars'.
182      */

183     public static String JavaDoc removeChars (String JavaDoc data, String JavaDoc removeChars)
184     {
185         String JavaDoc temp = null;
186         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
187         temp = data;
188
189         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(temp, removeChars);
190         while (st.hasMoreTokens())
191         {
192             String JavaDoc element = (String JavaDoc) st.nextElement();
193             out.append(element);
194         }
195         return out.toString();
196     }
197
198     /*
199      * Given a filename, strips the .extension
200      *
201      */

202     public static String JavaDoc stripExtension(String JavaDoc filename)
203     {
204         int index = filename.lastIndexOf('.');
205         if (index > -1)
206         {
207             return filename.substring(0, index);
208         }
209         return filename;
210     }
211
212     /**
213      * Performs variable substitution for a string. String is scanned for ${variable_name} and if one is found,
214      * it is replaced with corresponding value from the vars hashtable.
215      *
216      * @param origString unmodified string
217      * @param vars Hashtable of replacement values
218      * @return modified string
219      * @exception Exception
220      */

221     public static String JavaDoc replaceVars(String JavaDoc origString, Map JavaDoc vars)
222     {
223
224         StringBuffer JavaDoc finalString = new StringBuffer JavaDoc();
225         int index = 0;
226         int i = 0;
227         String JavaDoc key = null;
228         String JavaDoc value = null;
229         while ((index = origString.indexOf("${", i)) > -1)
230         {
231             key = origString.substring(index + 2, origString.indexOf("}", index+3));
232             value = (String JavaDoc) vars.get(key);
233             finalString.append(origString.substring(i, index));
234             if (value != null)
235             {
236                 finalString.append(value);
237             }
238             else
239             {
240                 finalString.append("${"+key+"}");
241             }
242             i = index + 3 + key.length();
243         }
244         finalString.append (origString.substring(i));
245
246         return finalString.toString();
247     }
248
249 }
250
Popular Tags