KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > utils > StringUtils


1 /*
2  * Copyright 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.axis.utils;
18
19 import org.apache.axis.InternalException;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.List JavaDoc;
23 import java.io.Writer JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.io.StringWriter JavaDoc;
26
27 public class StringUtils {
28     private StringUtils() {
29     }
30
31     /**
32      * An empty immutable <code>String</code> array.
33      */

34     public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
35     
36     /**
37      * Tests if this string starts with the specified prefix (Ignoring whitespaces)
38      * @param prefix
39      * @param string
40      * @return boolean
41      */

42     public static boolean startsWithIgnoreWhitespaces(String JavaDoc prefix, String JavaDoc string) {
43         int index1 = 0;
44         int index2 = 0;
45         int length1 = prefix.length();
46         int length2 = string.length();
47         char ch1 = ' ';
48         char ch2 = ' ';
49         while (index1 < length1 && index2 < length2) {
50             while (index1 < length1 && Character.isWhitespace(ch1 = prefix.charAt(index1))) {
51                 index1++;
52             }
53             while (index2 < length2 && Character.isWhitespace(ch2 = string.charAt(index2))) {
54                 index2++;
55             }
56             if (index1 == length1 && index2 == length2) {
57                 return true;
58             }
59             if (ch1 != ch2) {
60                 return false;
61             }
62             index1++;
63             index2++;
64         }
65         if(index1 < length1 && index2 >= length2)
66             return false;
67         return true;
68     }
69
70     /**
71      * <p>Splits the provided text into an array, separator specified.
72      * This is an alternative to using StringTokenizer.</p>
73      *
74      * <p>The separator is not included in the returned String array.
75      * Adjacent separators are treated as one separator.</p>
76      *
77      * <p>A <code>null</code> input String returns <code>null</code>.</p>
78      *
79      * <pre>
80      * StringUtils.split(null, *) = null
81      * StringUtils.split("", *) = []
82      * StringUtils.split("a.b.c", '.') = ["a", "b", "c"]
83      * StringUtils.split("a..b.c", '.') = ["a", "b", "c"]
84      * StringUtils.split("a:b:c", '.') = ["a:b:c"]
85      * StringUtils.split("a\tb\nc", null) = ["a", "b", "c"]
86      * StringUtils.split("a b c", ' ') = ["a", "b", "c"]
87      * </pre>
88      *
89      * @param str the String to parse, may be null
90      * @param separatorChar the character used as the delimiter,
91      * <code>null</code> splits on whitespace
92      * @return an array of parsed Strings, <code>null</code> if null String input
93      */

94     public static String JavaDoc[] split(String JavaDoc str, char separatorChar) {
95         if (str == null) {
96             return null;
97         }
98         int len = str.length();
99         if (len == 0) {
100             return EMPTY_STRING_ARRAY;
101         }
102         List JavaDoc list = new ArrayList JavaDoc();
103         int i = 0, start = 0;
104         boolean match = false;
105         while (i < len) {
106             if (str.charAt(i) == separatorChar) {
107                 if (match) {
108                     list.add(str.substring(start, i));
109                     match = false;
110                 }
111                 start = ++i;
112                 continue;
113             }
114             match = true;
115             i++;
116         }
117         if (match) {
118             list.add(str.substring(start, i));
119         }
120         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
121     }
122
123     // Empty checks
124
//-----------------------------------------------------------------------
125
/**
126      * <p>Checks if a String is empty ("") or null.</p>
127      *
128      * <pre>
129      * StringUtils.isEmpty(null) = true
130      * StringUtils.isEmpty("") = true
131      * StringUtils.isEmpty(" ") = false
132      * StringUtils.isEmpty("bob") = false
133      * StringUtils.isEmpty(" bob ") = false
134      * </pre>
135      *
136      * <p>NOTE: This method changed in Lang version 2.0.
137      * It no longer trims the String.
138      * That functionality is available in isBlank().</p>
139      *
140      * @param str the String to check, may be null
141      * @return <code>true</code> if the String is empty or null
142      */

143     public static boolean isEmpty(String JavaDoc str) {
144         return (str == null || str.length() == 0);
145     }
146     
147     // Stripping
148
//-----------------------------------------------------------------------
149
/**
150      * <p>Strips whitespace from the start and end of a String.</p>
151      *
152      * <p>This removes whitespace. Whitespace is defined by
153      * {@link Character#isWhitespace(char)}.</p>
154      *
155      * <p>A <code>null</code> input String returns <code>null</code>.</p>
156      *
157      * <pre>
158      * StringUtils.strip(null) = null
159      * StringUtils.strip("") = ""
160      * StringUtils.strip(" ") = ""
161      * StringUtils.strip("abc") = "abc"
162      * StringUtils.strip(" abc") = "abc"
163      * StringUtils.strip("abc ") = "abc"
164      * StringUtils.strip(" abc ") = "abc"
165      * StringUtils.strip(" ab c ") = "ab c"
166      * </pre>
167      *
168      * @param str the String to remove whitespace from, may be null
169      * @return the stripped String, <code>null</code> if null String input
170      */

171     public static String JavaDoc strip(String JavaDoc str) {
172         return strip(str, null);
173     }
174     
175     /**
176      * <p>Strips any of a set of characters from the start and end of a String.
177      * This is similar to {@link String#trim()} but allows the characters
178      * to be stripped to be controlled.</p>
179      *
180      * <p>A <code>null</code> input String returns <code>null</code>.
181      * An empty string ("") input returns the empty string.</p>
182      *
183      * <p>If the stripChars String is <code>null</code>, whitespace is
184      * stripped as defined by {@link Character#isWhitespace(char)}.
185      * Alternatively use {@link #strip(String)}.</p>
186      *
187      * <pre>
188      * StringUtils.strip(null, *) = null
189      * StringUtils.strip("", *) = ""
190      * StringUtils.strip("abc", null) = "abc"
191      * StringUtils.strip(" abc", null) = "abc"
192      * StringUtils.strip("abc ", null) = "abc"
193      * StringUtils.strip(" abc ", null) = "abc"
194      * StringUtils.strip(" abcyx", "xyz") = " abc"
195      * </pre>
196      *
197      * @param str the String to remove characters from, may be null
198      * @param stripChars the characters to remove, null treated as whitespace
199      * @return the stripped String, <code>null</code> if null String input
200      */

201     public static String JavaDoc strip(String JavaDoc str, String JavaDoc stripChars) {
202         if (str == null) {
203             return str;
204         }
205         int len = str.length();
206         if (len == 0) {
207             return str;
208         }
209         int start = getStripStart(str, stripChars);
210         if (start == len) {
211             return "";
212         }
213         int end = getStripEnd(str, stripChars);
214         return (start == 0 && end == len) ? str : str.substring(start, end);
215     }
216
217     /**
218      * <p>Strips any of a set of characters from the start of a String.</p>
219      *
220      * <p>A <code>null</code> input String returns <code>null</code>.
221      * An empty string ("") input returns the empty string.</p>
222      *
223      * <p>If the stripChars String is <code>null</code>, whitespace is
224      * stripped as defined by {@link Character#isWhitespace(char)}.</p>
225      *
226      * <pre>
227      * StringUtils.stripStart(null, *) = null
228      * StringUtils.stripStart("", *) = ""
229      * StringUtils.stripStart("abc", "") = "abc"
230      * StringUtils.stripStart("abc", null) = "abc"
231      * StringUtils.stripStart(" abc", null) = "abc"
232      * StringUtils.stripStart("abc ", null) = "abc "
233      * StringUtils.stripStart(" abc ", null) = "abc "
234      * StringUtils.stripStart("yxabc ", "xyz") = "abc "
235      * </pre>
236      *
237      * @param str the String to remove characters from, may be null
238      * @param stripChars the characters to remove, null treated as whitespace
239      * @return the stripped String, <code>null</code> if null String input
240      */

241     public static String JavaDoc stripStart(String JavaDoc str, String JavaDoc stripChars) {
242         int start = getStripStart(str, stripChars);
243         return (start <= 0) ? str : str.substring(start);
244     }
245
246     private static int getStripStart(String JavaDoc str, String JavaDoc stripChars) {
247         int strLen;
248         if (str == null || (strLen = str.length()) == 0) {
249             return -1;
250         }
251         int start = 0;
252         if (stripChars == null) {
253             while ((start != strLen) && Character.isWhitespace(str.charAt(start))) {
254                 start++;
255             }
256         } else if (stripChars.length() == 0) {
257             return start;
258         } else {
259             while ((start != strLen) && (stripChars.indexOf(str.charAt(start)) != -1)) {
260                 start++;
261             }
262         }
263         return start;
264     }
265
266     /**
267      * <p>Strips any of a set of characters from the end of a String.</p>
268      *
269      * <p>A <code>null</code> input String returns <code>null</code>.
270      * An empty string ("") input returns the empty string.</p>
271      *
272      * <p>If the stripChars String is <code>null</code>, whitespace is
273      * stripped as defined by {@link Character#isWhitespace(char)}.</p>
274      *
275      * <pre>
276      * StringUtils.stripEnd(null, *) = null
277      * StringUtils.stripEnd("", *) = ""
278      * StringUtils.stripEnd("abc", "") = "abc"
279      * StringUtils.stripEnd("abc", null) = "abc"
280      * StringUtils.stripEnd(" abc", null) = " abc"
281      * StringUtils.stripEnd("abc ", null) = "abc"
282      * StringUtils.stripEnd(" abc ", null) = " abc"
283      * StringUtils.stripEnd(" abcyx", "xyz") = " abc"
284      * </pre>
285      *
286      * @param str the String to remove characters from, may be null
287      * @param stripChars the characters to remove, null treated as whitespace
288      * @return the stripped String, <code>null</code> if null String input
289      */

290     public static String JavaDoc stripEnd(String JavaDoc str, String JavaDoc stripChars) {
291         int end = getStripEnd(str, stripChars);
292         return (end < 0) ? str : str.substring(0, end);
293     }
294
295     private static int getStripEnd(String JavaDoc str, String JavaDoc stripChars) {
296         int end;
297         if (str == null || (end = str.length()) == 0) {
298             return -1;
299         }
300         if (stripChars == null) {
301             while ((end != 0) && Character.isWhitespace(str.charAt(end - 1))) {
302                 end--;
303             }
304         } else if (stripChars.length() == 0) {
305             return end;
306         } else {
307             while ((end != 0) && (stripChars.indexOf(str.charAt(end - 1)) != -1)) {
308                 end--;
309             }
310         }
311         return end;
312     }
313
314     /**
315      * write the escaped version of a given string
316      *
317      * @param str string to be encoded
318      * @return a new escaped <code>String</code>, <code>null</code> if null string input
319      */

320     public static String JavaDoc escapeNumericChar(String JavaDoc str) {
321         if (str == null) {
322             return null;
323         }
324         try {
325             StringWriter JavaDoc writer = new StringWriter JavaDoc(str.length());
326             escapeNumericChar(writer, str);
327             return writer.toString();
328         } catch (IOException JavaDoc ioe) {
329             // this should never ever happen while writing to a StringWriter
330
ioe.printStackTrace();
331             return null;
332         }
333     }
334
335     /**
336      * write the escaped version of a given string
337      *
338      * @param out writer to write this string to
339      * @param str string to be encoded
340      */

341     public static void escapeNumericChar(Writer JavaDoc out, String JavaDoc str)
342             throws IOException JavaDoc {
343         if (str == null) {
344             return;
345         }
346         int length = str.length();
347         char character;
348         for (int i = 0; i < length; i++) {
349             character = str.charAt( i );
350             if (character > 0x7F) {
351                 out.write("&#x");
352                 out.write(Integer.toHexString(character).toUpperCase());
353                 out.write(";");
354             } else {
355                 out.write(character);
356             }
357         }
358     }
359
360     /**
361      * <p>Unescapes numeric character referencs found in the <code>String</code>.</p>
362      *
363      * <p>For example, it will return a unicode string which means the specified numeric
364      * character references looks like "&#x3088;&#x3046;&#x3053;&#x305d;".</p>
365      *
366      * @param str the <code>String</code> to unescape, may be null
367      * @return a new unescaped <code>String</code>, <code>null</code> if null string input
368      */

369     public static String JavaDoc unescapeNumericChar(String JavaDoc str) {
370         if (str == null) {
371             return null;
372         }
373         try {
374             StringWriter JavaDoc writer = new StringWriter JavaDoc(str.length());
375             unescapeNumericChar(writer, str);
376             return writer.toString();
377         } catch (IOException JavaDoc ioe) {
378             // this should never ever happen while writing to a StringWriter
379
ioe.printStackTrace();
380             return null;
381         }
382     }
383
384     /**
385      * <p>Unescapes numeric character references found in the <code>String</code> to a
386      * <code>Writer</code>.</p>
387      *
388      * <p>For example, it will return a unicode string which means the specified numeric
389      * character references looks like "&#x3088;&#x3046;&#x3053;&#x305d;".</p>
390      *
391      * <p>A <code>null</code> string input has no effect.</p>
392      *
393      * @param out the <code>Writer</code> used to output unescaped characters
394      * @param str the <code>String</code> to unescape, may be null
395      * @throws IllegalArgumentException if the Writer is <code>null</code>
396      * @throws java.io.IOException if error occurs on underlying Writer
397      */

398     public static void unescapeNumericChar(Writer JavaDoc out, String JavaDoc str) throws IOException JavaDoc {
399         if (out == null) {
400             throw new IllegalArgumentException JavaDoc("The Writer must not be null");
401         }
402         if (str == null) {
403             return;
404         }
405
406         int sz = str.length();
407         StringBuffer JavaDoc unicode = new StringBuffer JavaDoc(4);
408         StringBuffer JavaDoc escapes = new StringBuffer JavaDoc(3);
409         boolean inUnicode = false;
410
411         for (int i = 0; i < sz; i++) {
412             char ch = str.charAt(i);
413             if (inUnicode) {
414                 // if in unicode, then we're reading unicode
415
// values in somehow
416
unicode.append(ch);
417                 if (unicode.length() == 4) {
418                     // unicode now contains the four hex digits
419
// which represents our unicode character
420
try {
421                         int value = Integer.parseInt(unicode.toString(), 16);
422                         out.write((char) value);
423                         unicode.setLength(0);
424                         // need to skip the delimiter - ';'
425
i = i + 1;
426                         inUnicode = false;
427                     } catch (NumberFormatException JavaDoc nfe) {
428                         throw new InternalException(nfe);
429                     }
430                 }
431                 continue;
432             } else if (ch=='&') {
433                 // Start of the escape sequence ...
434
// At least, the numeric character references require 8 bytes to
435
// describe a Unicode character like as"&#xFFFF;"
436
if (i+7 <= sz) {
437                     escapes.append(ch);
438                     escapes.append(str.charAt(i+1));
439                     escapes.append(str.charAt(i+2));
440                     if (escapes.toString().equals("&#x") && str.charAt(i+7)==';') {
441                         inUnicode = true;
442                     } else {
443                         out.write(escapes.toString());
444                     }
445                     escapes.setLength(0);
446                     // need to skip the escaping chars - '&#x'
447
i = i + 2;
448                 } else {
449                     out.write(ch);
450                 }
451                 continue;
452             }
453             out.write(ch);
454         }
455     }
456 }
457
Popular Tags