KickJava   Java API By Example, From Geeks To Geeks.

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


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

18
19 import java.util.NoSuchElementException JavaDoc;
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.apache.commons.lang.exception.ExceptionUtils;
23
24 /**
25  * This is where common String manipulation routines should go.
26  *
27  * @author <a HREF="mailto:epugh@upstate.com">Eric Pugh</a>
28  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
29  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
30  * @author <a HREF="mailto:gcoladonato@yahoo.com">Greg Coladonato</a>
31  * @author <a HREF="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
32  * @version $Id: StringUtils.java,v 1.7.2.2 2004/05/20 03:16:38 seade Exp $
33  * @deprecated This class will be removed after the 2.3 release. Please
34  * use the <a HREF="http://jakarta.apache.org/commons/">commons-lang</a> component.
35  */

36 public class StringUtils
37 {
38     /**
39      * Deal with null strings converting them to "" instead. It also
40      * invokes String.trim() on the output.
41      *
42      * @param foo A String.
43      * @return A String.
44      * @deprecated Use org.apache.commons.lang.StringUtils.defaultString()
45      */

46     public static final String JavaDoc makeString(String JavaDoc foo)
47     {
48         return org.apache.commons.lang.StringUtils.defaultString(foo);
49     }
50
51     /**
52      * Validates that the supplied string is neither <code>null</code>
53      * nor the empty string.
54      *
55      * @param foo The text to check.
56      * @return Whether valid.
57      * @deprecated Use org.apache.commons.lang.StringUtils.isNotEmpty()
58      */

59     public static final boolean isValid(String JavaDoc foo)
60     {
61         return org.apache.commons.lang.StringUtils.isNotEmpty(foo);
62     }
63
64     /**
65      * Determine whether a (trimmed) string is empty
66      *
67      * @param foo The text to check.
68      * @return Whether empty.
69      * @deprecated use org.apache.commons.lang.StringUtils.isEmpty() instead
70      */

71     public static final boolean isEmpty(String JavaDoc foo)
72     {
73         return org.apache.commons.lang.StringUtils.isEmpty(foo);
74     }
75
76     /**
77      * Returns the output of printStackTrace as a String.
78      *
79      * @param e A Throwable.
80      * @return A String.
81      * @deprecated use org.apache.commons.lang.ExceptionUtils.getStackTrace() instead
82      */

83     public static final String JavaDoc stackTrace(Throwable JavaDoc e)
84     {
85         return ExceptionUtils.getStackTrace(e);
86     }
87
88     /**
89      * Returns the output of printStackTrace as a String.
90      *
91      * @param e A Throwable.
92      * @param addPre a boolean to add HTML <pre> tags around the stacktrace
93      * @return A String.
94      */

95     public static final String JavaDoc stackTrace(Throwable JavaDoc e, boolean addPre)
96     {
97         if (addPre)
98         {
99             return "<pre>" + ExceptionUtils.getStackTrace(e) + "</pre>";
100         }
101         else
102         {
103             return ExceptionUtils.getStackTrace(e);
104         }
105     }
106
107     /**
108      * Compares two Strings, returns true if their values are the
109      * same.
110      *
111      * @param s1 The first string.
112      * @param s2 The second string.
113      * @return True if the values of both strings are the same.
114      * @deprecated use org.apache.commons.lang.StringUtils.equals() instead
115      */

116     public static boolean equals(String JavaDoc s1, String JavaDoc s2)
117     {
118         return org.apache.commons.lang.StringUtils.equals(s1, s2);
119     }
120
121     public static final int PPKEY_CLASSNAME = 0;
122     public static final int PPKEY_ID = 1;
123     public static final int PPKEY_PROPERTY = 2;
124
125     /**
126      * Takes a String of the form substring[substring]subtring and
127      * returns the 3 substrings
128      *
129      * @return a three element String array
130      */

131     public static String JavaDoc[] parseObjectKey(String JavaDoc s)
132     {
133         String JavaDoc[] p = new String JavaDoc[3];
134         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, "[]");
135         int count = st.countTokens();
136         if (count > 1)
137         {
138             p[0] = st.nextToken();
139             p[1] = st.nextToken();
140             if (count == 3)
141             {
142                 p[2] = st.nextToken();
143             }
144         }
145         return p;
146     }
147
148     /**
149      * Remove Underscores from a string and replaces first
150      * Letters with Capitals. foo_bar becomes FooBar
151      */

152     public static String JavaDoc removeUnderScores(String JavaDoc data)
153     {
154
155         String JavaDoc temp = null;
156         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
157         temp = data;
158
159         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(temp, "_");
160         while (st.hasMoreTokens())
161         {
162             String JavaDoc element = (String JavaDoc) st.nextElement();
163             out.append(org.apache.commons.lang.StringUtils.capitalise(element));
164         }
165         return out.toString();
166     }
167
168     /**
169      * Makes the first letter caps and leaves the rest as is.
170      *
171      * @deprecated use org.apache.commons.lang.StringUtils.capitalise() instead
172      */

173     public static String JavaDoc firstLetterCaps(String JavaDoc data)
174     {
175         return org.apache.commons.lang.StringUtils.capitalise(data);
176     }
177
178     /**
179      * Splits the provided CSV text into a list.
180      *
181      * @param text The CSV list of values to split apart.
182      * @param separator The separator character.
183      * @return The list of values.
184      * @deprecated use org.apache.commons.lang.StringUtils.split() instead
185      */

186     public static String JavaDoc[] split(String JavaDoc text, String JavaDoc separator)
187     {
188         return org.apache.commons.lang.StringUtils.split(text, separator);
189     }
190
191     /**
192      * Joins the elements of the provided array into a single string
193      * containing a list of CSV elements.
194      *
195      * @param list The list of values to join together.
196      * @param separator The separator character.
197      * @return The CSV text.
198      * @deprecated Use org.apache.commons.lang.StringUtils.join()
199      */

200     public static String JavaDoc join(String JavaDoc[] list, String JavaDoc separator)
201     {
202         return org.apache.commons.lang.StringUtils.join(list, separator);
203     }
204
205     /**
206      * Takes a block of text which might have long lines in it and wraps
207      * the long lines based on the supplied wrapColumn parameter. It was
208      * initially implemented for use by VelocityEmail. If there are tabs
209      * in inString, you are going to get results that are a bit strange,
210      * since tabs are a single character but are displayed as 4 or 8
211      * spaces. Remove the tabs.
212      *
213      * @param inString Text which is in need of word-wrapping.
214      * @param newline The characters that define a newline.
215      * @param wrapColumn The column to wrap the words at.
216      * @return The text with all the long lines word-wrapped.
217      */

218
219     public static String JavaDoc wrapText(String JavaDoc inString, String JavaDoc newline,
220                                   int wrapColumn)
221     {
222         StringTokenizer JavaDoc lineTokenizer = new StringTokenizer JavaDoc(
223                 inString, newline, true);
224         StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
225
226         while (lineTokenizer.hasMoreTokens())
227         {
228             try
229             {
230                 String JavaDoc nextLine = lineTokenizer.nextToken();
231
232                 if (nextLine.length() > wrapColumn)
233                 {
234                     // This line is long enough to be wrapped.
235
nextLine = wrapLine(nextLine, newline, wrapColumn);
236                 }
237
238                 stringBuffer.append(nextLine);
239             }
240             catch (NoSuchElementException JavaDoc nsee)
241             {
242                 // thrown by nextToken(), but I don't know why it would
243
break;
244             }
245         }
246
247         return (stringBuffer.toString());
248     }
249
250     /**
251      * Wraps a single line of text. Called by wrapText(). I can't
252      * think of any good reason for exposing this to the public,
253      * since wrapText should always be used AFAIK.
254      *
255      * @param line A line which is in need of word-wrapping.
256      * @param newline The characters that define a newline.
257      * @param wrapColumn The column to wrap the words at.
258      * @return A line with newlines inserted.
259      */

260
261     protected static String JavaDoc wrapLine(String JavaDoc line, String JavaDoc newline,
262                                      int wrapColumn)
263     {
264         StringBuffer JavaDoc wrappedLine = new StringBuffer JavaDoc();
265
266         while (line.length() > wrapColumn)
267         {
268             int spaceToWrapAt = line.lastIndexOf(' ', wrapColumn);
269
270             if (spaceToWrapAt >= 0)
271             {
272                 wrappedLine.append(line.substring(0, spaceToWrapAt));
273                 wrappedLine.append(newline);
274                 line = line.substring(spaceToWrapAt + 1);
275             }
276
277             // This must be a really long word or URL. Pass it
278
// through unchanged even though it's longer than the
279
// wrapColumn would allow. This behavior could be
280
// dependent on a parameter for those situations when
281
// someone wants long words broken at line length.
282
else
283             {
284                 spaceToWrapAt = line.indexOf(' ', wrapColumn);
285
286                 if (spaceToWrapAt >= 0)
287                 {
288                     wrappedLine.append(line.substring(0, spaceToWrapAt));
289                     wrappedLine.append(newline);
290                     line = line.substring(spaceToWrapAt + 1);
291                 }
292                 else
293                 {
294                     wrappedLine.append(line);
295                     line = "";
296                 }
297             }
298         }
299
300         // Whatever is left in line is short enough to just pass through,
301
// just like a small small kidney stone
302
wrappedLine.append(line);
303
304         return wrappedLine.toString();
305     }
306 }
307
Popular Tags