KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > utility > StringHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * StringHelper.java
26  *
27  * Created on March 3, 2000
28  */

29
30 package com.sun.jdo.spi.persistence.utility;
31
32 import java.util.*;
33
34 /**
35  * NOTE: These utilities have been moved from another (more specific
36  * package's) utility class so that more classes can have access to them.
37  * There can be some refactoring work to combine these with some of the
38  * methods in the StringScanner class.
39  *
40  */

41
42 public class StringHelper
43 {
44     /**
45      * constants for escape
46      */

47     private static final char BACKSLASH = '\\';
48     private static final char QUOTE = '"';
49
50     /** Convert an array of objects into a separated string. This method
51      * assumes there is no instance of the separator in the strings of list.
52      * @param list The list of objects to be expanded.
53      * @param beginIndex The index of the first element in the list to be used.
54      * @param endIndex The index of the last element in the list to be used.
55      * @param separator The separator to be used between strings.
56      * @return a string representing the expanded list.
57      */

58     public static String JavaDoc arrayToSeparatedList (List list, int beginIndex,
59         int endIndex, String JavaDoc separator)
60     {
61         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
62
63         if (list != null)
64         {
65             int i, count = (endIndex + 1);
66
67             if ((count > beginIndex) && (list.size() >= count))
68                 result.append(list.get(beginIndex));
69
70             for (i = beginIndex + 1; i < count; i++)
71                 result.append(separator + list.get(i));
72         }
73
74         return result.toString();
75     }
76
77     /** Convert an array of objects into a separated string using the default
78      * separator. This method assumes there is no instance of the separator
79      * in the strings of list.
80      * @param list The list of objects to be expanded.
81      * @param beginIndex The index of the first element in the list to be used.
82      * @param endIndex The index of the last element in the list to be used.
83      * @return a string representing the expanded list.
84      */

85     public static String JavaDoc arrayToSeparatedList (List list, int beginIndex,
86         int endIndex)
87     {
88         return arrayToSeparatedList(list, beginIndex, endIndex, ","); // NOI18N
89
}
90
91     /** Convert an array of objects into a separated string using the specified
92      * separator and the entire array. This method assumes there is no
93      * instance of the separator in the strings of list.
94      * @param list The list of objects to be expanded.
95      * @param separator The separator to be used between strings.
96      * @return a string representing the expanded list.
97      */

98     public static String JavaDoc arrayToSeparatedList (List list, String JavaDoc separator)
99     {
100         return arrayToSeparatedList(list, 0,
101             ((list != null) ? (list.size() - 1) : 0), separator);
102     }
103
104     /** Convert an array of objects into a separated string using the default
105      * separator and the entire array. This method assumes there is no
106      * instance of the separator in the strings of list.
107      * @param list The list of objects to be expanded.
108      * @return a string representing the expanded list.
109      */

110     public static String JavaDoc arrayToSeparatedList (List list)
111     {
112         return arrayToSeparatedList(list, 0,
113             ((list != null) ? (list.size() - 1) : 0));
114     }
115
116     /** Convert a separated string to an array of strings
117      * @param list The string representing the list of objects.
118      * @param separator The separator to be used to tokenize strings.
119      * @return an array representing the tokenized list.
120      */

121     public static List separatedListToArray (String JavaDoc list, String JavaDoc separator)
122     {
123         ArrayList result = new ArrayList();
124
125         if (list != null)
126         {
127             StringTokenizer st = new StringTokenizer(list, separator);
128             int i, size = st.countTokens();
129
130             for (i = 0; i < size; i++)
131                 result.add(st.nextToken());
132         }
133
134         return result;
135     }
136
137     /** Convert a separated string to an array of strings using the default
138      * separator.
139      * @param list The string representing the list of objects.
140      * @return an array representing the tokenized list.
141      */

142     public static List separatedListToArray (String JavaDoc list)
143     {
144         return separatedListToArray(list, ","); // NOI18N
145
}
146
147     /** Convert an array of int values into a separated string.
148      * @param intArray The array of int values to be expanded.
149      * @param separator The separator to be used between strings.
150      * @return a string representing the expanded array.
151      */

152     public static String JavaDoc intArrayToSeparatedList(int[] intArray,
153         String JavaDoc separator)
154     {
155         return intArrayToSeparatedList(intArray, 0,
156             ((intArray != null) ? (intArray.length - 1) : 0), separator);
157     }
158     
159     /** Convert an array of int values into a separated string.
160      * @param intArray The array of int values to be expanded.
161      * @param beginIndex The index of the first element in the array to be used.
162      * @param endIndex The index of the last element in the array to be used.
163      * @param separator The separator to be used between strings.
164      * @return a string representing the expanded array.
165      */

166     public static String JavaDoc intArrayToSeparatedList(int[] intArray, int beginIndex,
167         int endIndex, String JavaDoc separator)
168     {
169         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
170
171         if (intArray != null)
172         {
173             int count = (endIndex + 1);
174             if ((count > beginIndex) && (intArray.length >= count))
175                 result.append(intArray[beginIndex]);
176
177             for (int i = beginIndex + 1; i < count; i++) {
178                 result.append(separator);
179                 result.append(intArray[i]);
180             }
181         }
182
183         return result.toString();
184     }
185     
186     /** Checks if a string is null or empty.
187      * @return <code>true</code> if the string is null or empty after trim,
188      * <code>false</code> otherwirse.
189      */

190     public static boolean isEmpty (String JavaDoc aString)
191     {
192         return ((aString == null) || (aString.trim().length() == 0));
193     }
194
195     /** Gets a version of the specified string with the first letter
196      * capitalized. This can be used to convert a field name to get and set
197      * method names.
198      * @param aString the string to be capitalized
199      * @return a capitalized for the specified string
200      */

201     public static String JavaDoc getCapitalizedString (String JavaDoc aString)
202     {
203         if (isEmpty(aString))
204             return aString;
205
206         return Character.toUpperCase(aString.charAt(0)) + aString.substring(1);
207     }
208
209     /** Replaces the first occurence of <code>oldString</code> in <code>string</code>
210      * with <code>newString</code>. The methods returns either a new string
211      * instance (in the case <code>oldString</code> is included in the string)
212      * or the origial string itself (in the case <code>oldString</code> is not
213      * included).
214      * @param string the original string.
215      * @param oldString the string to be replaced.
216      * @param newString the string the old value is replaced with.
217      * @return a string derived from the specified this string by replacing the
218      * first occurence oldString with newString.
219      */

220     public static String JavaDoc replaceFirst (String JavaDoc string, String JavaDoc oldString, String JavaDoc newString)
221     {
222         int index = string.indexOf(oldString);
223         if (index != -1) {
224             StringBuffer JavaDoc sb = new StringBuffer JavaDoc(string.length());
225             sb.append(string.substring(0, index));
226             sb.append(newString);
227             sb.append(string.substring(index + oldString.length()));
228             return sb.toString();
229         }
230         return string;
231     }
232
233     /** Replaces all occurences of <code>oldString</code> in <code>string</code>
234      * with <code>newString</code>. The methods returns either a new string
235      * instance (in the case <code>oldString</code> is included in the string)
236      * or the origial string itself (in the case <code>oldString</code> is not
237      * included).
238      * @param string the original string.
239      * @param oldString the string to be replaced.
240      * @param newString the string the old value is replaced with.
241      * @return a string derived from the specified this string by replacing
242      * every occurrence of oldString with newString.
243      */

244     public static String JavaDoc replace (String JavaDoc string, String JavaDoc oldString,
245         String JavaDoc newString)
246     {
247         StringBuffer JavaDoc sb = null;
248         final int l = oldString.length();
249         int beginIndex = 0;
250         int index;
251
252         while ((index = string.indexOf(oldString, beginIndex)) > -1)
253         {
254             // only create the StringBuffer if there's an occurence of oldString
255
if (sb == null)
256             {
257                 sb = new StringBuffer JavaDoc(string.length());
258             }
259             sb.append(string.substring(beginIndex, index));
260             sb.append(newString);
261             beginIndex = index + l;
262         }
263
264         // append the rest if the old value was found at least once
265
if (sb != null)
266         {
267             sb.append(string.substring(beginIndex));
268         }
269
270         return (sb != null ? sb.toString() : string);
271     }
272
273     /**
274      * Trims trailing spaces from input.
275      * @param input The input string.
276      * @return A new string with trailing spaces trimmed. If there are no
277      * trailing spaces, returns <CODE>input</CODE>.
278      */

279     public static String JavaDoc rtrim (String JavaDoc input)
280     {
281         String JavaDoc retVal = input;
282
283         if (input != null)
284         {
285             int lastCharIndex = input.length() - 1;
286             int originalLastCharIndex = lastCharIndex;
287
288             while ((lastCharIndex >= 0) &&
289                 Character.isSpaceChar(input.charAt(lastCharIndex)))
290             {
291                 lastCharIndex--;
292             }
293             if (lastCharIndex != originalLastCharIndex)
294             {
295                 //We have characters to trim.
296
retVal = input.substring(0,lastCharIndex + 1);
297             }
298         }
299
300         return retVal;
301     }
302
303     /**
304      * Escaping given string by " and \.
305      * @param str String to be escaped
306      * @return string escaped by " and \.
307      */

308     public static String JavaDoc escape(String JavaDoc str)
309     {
310         if (str == null)
311         {
312             return str;
313         }
314         else
315         {
316             int indS = str.indexOf(BACKSLASH);
317             int indQ = str.indexOf(QUOTE);
318             if (indS == -1 && indQ == -1)
319             {
320                 return str;
321             }
322             else
323             {
324                 StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
325                 char data[] = str.toCharArray();
326                 for (int i = 0; i < data.length; i++)
327                 {
328                     if (BACKSLASH == data[i] || QUOTE == data[i])
329                     {
330                         buf.append(BACKSLASH);
331                     }
332                     buf.append(data[i]);
333                 }
334                 return buf.toString();
335             }
336         }
337     }
338 }
339
Popular Tags