KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > WordUtils


1 /*
2  * Copyright 2002-2005 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 package org.apache.commons.lang;
17
18 /**
19  * <p>Operations on Strings that contain words.</p>
20  *
21  * <p>This class tries to handle <code>null</code> input gracefully.
22  * An exception will not be thrown for a <code>null</code> input.
23  * Each method documents its behaviour in more detail.</p>
24  *
25  * @author Apache Jakarta Velocity
26  * @author Henri Yandell
27  * @author Stephen Colebourne
28  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
29  * @author Gary Gregory
30  * @since 2.0
31  * @version $Id: WordUtils.java 161678 2005-04-17 18:42:40Z mvdb $
32  */

33 public class WordUtils {
34
35     /**
36      * <p><code>WordUtils</code> instances should NOT be constructed in
37      * standard programming. Instead, the class should be used as
38      * <code>WordUtils.wrap("foo bar", 20);</code>.</p>
39      *
40      * <p>This constructor is public to permit tools that require a JavaBean
41      * instance to operate.</p>
42      */

43     public WordUtils() {
44     }
45
46     // Wrapping
47
//--------------------------------------------------------------------------
48
// /**
49
// * <p>Wraps a block of text to a specified line length using '\n' as
50
// * a newline.</p>
51
// *
52
// * <p>This method takes a block of text, which might have long lines in it
53
// * and wraps the long lines based on the supplied lineLength parameter.</p>
54
// *
55
// * <p>If a single word is longer than the line length (eg. a URL), it will
56
// * not be broken, and will display beyond the expected width.</p>
57
// *
58
// * <p>If there are tabs in inString, you are going to get results that are
59
// * a bit strange. Tabs are a single character but are displayed as 4 or 8
60
// * spaces. Remove the tabs.</p>
61
// *
62
// * @param str text which is in need of word-wrapping, may be null
63
// * @param lineLength the column to wrap the words at
64
// * @return the text with all the long lines word-wrapped
65
// * <code>null</code> if null string input
66
// */
67
// public static String wrapText(String str, int lineLength) {
68
// return wrap(str, null, lineLength);
69
// }
70

71 // /**
72
// * <p>Wraps a block of text to a specified line length.</p>
73
// *
74
// * <p>This method takes a block of text, which might have long lines in it
75
// * and wraps the long lines based on the supplied lineLength parameter.</p>
76
// *
77
// * <p>If a single word is longer than the wrapColumn (eg. a URL), it will
78
// * not be broken, and will display beyond the expected width.</p>
79
// *
80
// * <p>If there are tabs in inString, you are going to get results that are
81
// * a bit strange. Tabs are a single character but are displayed as 4 or 8
82
// * spaces. Remove the tabs.</p>
83
// *
84
// * @param str text which is in need of word-wrapping, may be null
85
// * @param newLineChars the characters that define a newline, null treated as \n
86
// * @param lineLength the column to wrap the words at
87
// * @return the text with all the long lines word-wrapped
88
// * <code>null</code> if null string input
89
// */
90
// public static String wrapText(String str, String newLineChars, int lineLength) {
91
// if (str == null) {
92
// return null;
93
// }
94
// if (newLineChars == null) {
95
// newLineChars = "\n";
96
// }
97
// StringTokenizer lineTokenizer = new StringTokenizer(str, newLineChars, true);
98
// StringBuffer stringBuffer = new StringBuffer();
99
//
100
// while (lineTokenizer.hasMoreTokens()) {
101
// try {
102
// String nextLine = lineTokenizer.nextToken();
103
//
104
// if (nextLine.length() > lineLength) {
105
// // This line is long enough to be wrapped.
106
// nextLine = wrapLine(nextLine, null, lineLength, false);
107
// }
108
//
109
// stringBuffer.append(nextLine);
110
//
111
// } catch (NoSuchElementException nsee) {
112
// // thrown by nextToken(), but I don't know why it would
113
// break;
114
// }
115
// }
116
//
117
// return stringBuffer.toString();
118
// }
119

120     // Wrapping
121
//-----------------------------------------------------------------------
122
/**
123      * <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
124      *
125      * <p>New lines will be separated by the system property line separator.
126      * Very long words, such as URLs will <i>not</i> be wrapped.</p>
127      *
128      * <p>Leading spaces on a new line are stripped.
129      * Trailing spaces are not stripped.</p>
130      *
131      * <pre>
132      * WordUtils.wrap(null, *) = null
133      * WordUtils.wrap("", *) = ""
134      * </pre>
135      *
136      * @param str the String to be word wrapped, may be null
137      * @param wrapLength the column to wrap the words at, less than 1 is treated as 1
138      * @return a line with newlines inserted, <code>null</code> if null input
139      */

140     public static String JavaDoc wrap(String JavaDoc str, int wrapLength) {
141         return wrap(str, wrapLength, null, false);
142     }
143     
144     /**
145      * <p>Wraps a single line of text, identifying words by <code>' '</code>.</p>
146      *
147      * <p>Leading spaces on a new line are stripped.
148      * Trailing spaces are not stripped.</p>
149      *
150      * <pre>
151      * WordUtils.wrap(null, *, *, *) = null
152      * WordUtils.wrap("", *, *, *) = ""
153      * </pre>
154      *
155      * @param str the String to be word wrapped, may be null
156      * @param wrapLength the column to wrap the words at, less than 1 is treated as 1
157      * @param newLineStr the string to insert for a new line,
158      * <code>null</code> uses the system property line separator
159      * @param wrapLongWords true if long words (such as URLs) should be wrapped
160      * @return a line with newlines inserted, <code>null</code> if null input
161      */

162     public static String JavaDoc wrap(String JavaDoc str, int wrapLength, String JavaDoc newLineStr, boolean wrapLongWords) {
163         if (str == null) {
164             return null;
165         }
166         if (newLineStr == null) {
167             newLineStr = SystemUtils.LINE_SEPARATOR;
168         }
169         if (wrapLength < 1) {
170             wrapLength = 1;
171         }
172         int inputLineLength = str.length();
173         int offset = 0;
174         StringBuffer JavaDoc wrappedLine = new StringBuffer JavaDoc(inputLineLength + 32);
175         
176         while ((inputLineLength - offset) > wrapLength) {
177             if (str.charAt(offset) == ' ') {
178                 offset++;
179                 continue;
180             }
181             int spaceToWrapAt = str.lastIndexOf(' ', wrapLength + offset);
182
183             if (spaceToWrapAt >= offset) {
184                 // normal case
185
wrappedLine.append(str.substring(offset, spaceToWrapAt));
186                 wrappedLine.append(newLineStr);
187                 offset = spaceToWrapAt + 1;
188                 
189             } else {
190                 // really long word or URL
191
if (wrapLongWords) {
192                     // wrap really long word one line at a time
193
wrappedLine.append(str.substring(offset, wrapLength + offset));
194                     wrappedLine.append(newLineStr);
195                     offset += wrapLength;
196                 } else {
197                     // do not wrap really long word, just extend beyond limit
198
spaceToWrapAt = str.indexOf(' ', wrapLength + offset);
199                     if (spaceToWrapAt >= 0) {
200                         wrappedLine.append(str.substring(offset, spaceToWrapAt));
201                         wrappedLine.append(newLineStr);
202                         offset = spaceToWrapAt + 1;
203                     } else {
204                         wrappedLine.append(str.substring(offset));
205                         offset = inputLineLength;
206                     }
207                 }
208             }
209         }
210
211         // Whatever is left in line is short enough to just pass through
212
wrappedLine.append(str.substring(offset));
213
214         return wrappedLine.toString();
215     }
216
217     // Capitalizing
218
//-----------------------------------------------------------------------
219
/**
220      * <p>Capitalizes all the whitespace separated words in a String.
221      * Only the first letter of each word is changed. To convert the
222      * rest of each word to lowercase at the same time,
223      * use {@link #capitalizeFully(String)}.</p>
224      *
225      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
226      * A <code>null</code> input String returns <code>null</code>.
227      * Capitalization uses the unicode title case, normally equivalent to
228      * upper case.</p>
229      *
230      * <pre>
231      * WordUtils.capitalize(null) = null
232      * WordUtils.capitalize("") = ""
233      * WordUtils.capitalize("i am FINE") = "I Am FINE"
234      * </pre>
235      *
236      * @param str the String to capitalize, may be null
237      * @return capitalized String, <code>null</code> if null String input
238      * @see #uncapitalize(String)
239      * @see #capitalizeFully(String)
240      */

241     public static String JavaDoc capitalize(String JavaDoc str) {
242         return capitalize(str, null);
243     }
244
245     /**
246      * <p>Capitalizes all the delimiter separated words in a String.
247      * Only the first letter of each word is changed. To convert the
248      * rest of each word to lowercase at the same time,
249      * use {@link #capitalizeFully(String, char[])}.</p>
250      *
251      * <p>The delimiters represent a set of characters understood to separate words.
252      * The first string character and the first non-delimiter character after a
253      * delimiter will be capitalized. </p>
254      *
255      * <p>A <code>null</code> input String returns <code>null</code>.
256      * Capitalization uses the unicode title case, normally equivalent to
257      * upper case.</p>
258      *
259      * <pre>
260      * WordUtils.capitalize(null, *) = null
261      * WordUtils.capitalize("", *) = ""
262      * WordUtils.capitalize(*, new char[0]) = *
263      * WordUtils.capitalize("i am fine", null) = "I Am Fine"
264      * WordUtils.capitalize("i aM.fine", {'.'}) = "I aM.Fine"
265      * </pre>
266      *
267      * @param str the String to capitalize, may be null
268      * @param delimiters set of characters to determine capitalization, null means whitespace
269      * @return capitalized String, <code>null</code> if null String input
270      * @see #uncapitalize(String)
271      * @see #capitalizeFully(String)
272      * @since 2.1
273      */

274     public static String JavaDoc capitalize(String JavaDoc str, char[] delimiters) {
275         if (str == null || str.length() == 0) {
276             return str;
277         }
278         int strLen = str.length();
279         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(strLen);
280
281         int delimitersLen = 0;
282         if(delimiters != null) {
283             delimitersLen = delimiters.length;
284         }
285
286         boolean capitalizeNext = true;
287         for (int i = 0; i < strLen; i++) {
288             char ch = str.charAt(i);
289
290             boolean isDelimiter = false;
291             if(delimiters == null) {
292                 isDelimiter = Character.isWhitespace(ch);
293             } else {
294                 for(int j=0; j < delimitersLen; j++) {
295                     if(ch == delimiters[j]) {
296                         isDelimiter = true;
297                         break;
298                     }
299                 }
300             }
301
302             if (isDelimiter) {
303                 buffer.append(ch);
304                 capitalizeNext = true;
305             } else if (capitalizeNext) {
306                 buffer.append(Character.toTitleCase(ch));
307                 capitalizeNext = false;
308             } else {
309                 buffer.append(ch);
310             }
311         }
312         return buffer.toString();
313     }
314
315     /**
316      * <p>Converts all the whitespace separated words in a String into capitalized words,
317      * that is each word is made up of a titlecase character and then a series of
318      * lowercase characters. </p>
319      *
320      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
321      * A <code>null</code> input String returns <code>null</code>.
322      * Capitalization uses the unicode title case, normally equivalent to
323      * upper case.</p>
324      *
325      * <pre>
326      * WordUtils.capitalizeFully(null) = null
327      * WordUtils.capitalizeFully("") = ""
328      * WordUtils.capitalizeFully("i am FINE") = "I Am Fine"
329      * </pre>
330      *
331      * @param str the String to capitalize, may be null
332      * @return capitalized String, <code>null</code> if null String input
333      */

334     public static String JavaDoc capitalizeFully(String JavaDoc str) {
335         return capitalizeFully(str, null);
336     }
337
338     /**
339      * <p>Converts all the delimiter separated words in a String into capitalized words,
340      * that is each word is made up of a titlecase character and then a series of
341      * lowercase characters. </p>
342      *
343      * <p>The delimiters represent a set of characters understood to separate words.
344      * The first string character and the first non-delimiter character after a
345      * delimiter will be capitalized. </p>
346      *
347      * <p>A <code>null</code> input String returns <code>null</code>.
348      * Capitalization uses the unicode title case, normally equivalent to
349      * upper case.</p>
350      *
351      * <pre>
352      * WordUtils.capitalizeFully(null, *) = null
353      * WordUtils.capitalizeFully("", *) = ""
354      * WordUtils.capitalizeFully(*, null) = *
355      * WordUtils.capitalizeFully(*, new char[0]) = *
356      * WordUtils.capitalizeFully("i aM.fine", {'.'}) = "I am.Fine"
357      * </pre>
358      *
359      * @param str the String to capitalize, may be null
360      * @param delimiters set of characters to determine capitalization, null means whitespace
361      * @return capitalized String, <code>null</code> if null String input
362      * @since 2.1
363      */

364     public static String JavaDoc capitalizeFully(String JavaDoc str, char[] delimiters) {
365         if (str == null || str.length() == 0) {
366             return str;
367         }
368         str = str.toLowerCase();
369         return capitalize(str, delimiters);
370     }
371
372     /**
373      * <p>Uncapitalizes all the whitespace separated words in a String.
374      * Only the first letter of each word is changed.</p>
375      *
376      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
377      * A <code>null</code> input String returns <code>null</code>.</p>
378      *
379      * <pre>
380      * WordUtils.uncapitalize(null) = null
381      * WordUtils.uncapitalize("") = ""
382      * WordUtils.uncapitalize("I Am FINE") = "i am fINE"
383      * </pre>
384      *
385      * @param str the String to uncapitalize, may be null
386      * @return uncapitalized String, <code>null</code> if null String input
387      * @see #capitalize(String)
388      */

389     public static String JavaDoc uncapitalize(String JavaDoc str) {
390         return uncapitalize(str, null);
391     }
392
393     /**
394      * <p>Uncapitalizes all the whitespace separated words in a String.
395      * Only the first letter of each word is changed.</p>
396      *
397      * <p>The delimiters represent a set of characters understood to separate words.
398      * The first string character and the first non-delimiter character after a
399      * delimiter will be uncapitalized. </p>
400      *
401      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
402      * A <code>null</code> input String returns <code>null</code>.</p>
403      *
404      * <pre>
405      * WordUtils.uncapitalize(null, *) = null
406      * WordUtils.uncapitalize("", *) = ""
407      * WordUtils.uncapitalize(*, null) = *
408      * WordUtils.uncapitalize(*, new char[0]) = *
409      * WordUtils.uncapitalize("I AM.FINE", {'.'}) = "i AM.fINE"
410      * </pre>
411      *
412      * @param str the String to uncapitalize, may be null
413      * @param delimiters set of characters to determine uncapitalization, null means whitespace
414      * @return uncapitalized String, <code>null</code> if null String input
415      * @see #capitalize(String)
416      * @since 2.1
417      */

418     public static String JavaDoc uncapitalize(String JavaDoc str, char[] delimiters) {
419         if (str == null || str.length() == 0) {
420             return str;
421         }
422         int strLen = str.length();
423
424         int delimitersLen = 0;
425         if(delimiters != null) {
426             delimitersLen = delimiters.length;
427         }
428
429         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(strLen);
430         boolean uncapitalizeNext = true;
431         for (int i = 0; i < strLen; i++) {
432             char ch = str.charAt(i);
433
434             boolean isDelimiter = false;
435             if(delimiters == null) {
436                 isDelimiter = Character.isWhitespace(ch);
437             } else {
438                 for(int j=0; j < delimitersLen; j++) {
439                     if(ch == delimiters[j]) {
440                         isDelimiter = true;
441                         break;
442                     }
443                 }
444             }
445
446             if (isDelimiter) {
447                 buffer.append(ch);
448                 uncapitalizeNext = true;
449             } else if (uncapitalizeNext) {
450                 buffer.append(Character.toLowerCase(ch));
451                 uncapitalizeNext = false;
452             } else {
453                 buffer.append(ch);
454             }
455         }
456         return buffer.toString();
457     }
458
459     /**
460      * <p>Swaps the case of a String using a word based algorithm.</p>
461      *
462      * <ul>
463      * <li>Upper case character converts to Lower case</li>
464      * <li>Title case character converts to Lower case</li>
465      * <li>Lower case character after Whitespace or at start converts to Title case</li>
466      * <li>Other Lower case character converts to Upper case</li>
467      * </ul>
468      *
469      * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.
470      * A <code>null</code> input String returns <code>null</code>.</p>
471      *
472      * <pre>
473      * StringUtils.swapCase(null) = null
474      * StringUtils.swapCase("") = ""
475      * StringUtils.swapCase("The dog has a BONE") = "tHE DOG HAS A bone"
476      * </pre>
477      *
478      * @param str the String to swap case, may be null
479      * @return the changed String, <code>null</code> if null String input
480      */

481     public static String JavaDoc swapCase(String JavaDoc str) {
482         int strLen;
483         if (str == null || (strLen = str.length()) == 0) {
484             return str;
485         }
486         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(strLen);
487
488         boolean whitespace = true;
489         char ch = 0;
490         char tmp = 0;
491
492         for (int i = 0; i < strLen; i++) {
493             ch = str.charAt(i);
494             if (Character.isUpperCase(ch)) {
495                 tmp = Character.toLowerCase(ch);
496             } else if (Character.isTitleCase(ch)) {
497                 tmp = Character.toLowerCase(ch);
498             } else if (Character.isLowerCase(ch)) {
499                 if (whitespace) {
500                     tmp = Character.toTitleCase(ch);
501                 } else {
502                     tmp = Character.toUpperCase(ch);
503                 }
504             } else {
505                 tmp = ch;
506             }
507             buffer.append(tmp);
508             whitespace = Character.isWhitespace(ch);
509         }
510         return buffer.toString();
511     }
512     
513 }
514
Popular Tags