1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.kernel.util;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  
28  import java.io.BufferedReader;
29  import java.io.IOException;
30  import java.io.InputStream;
31  import java.io.InputStreamReader;
32  import java.io.StringReader;
33  
34  import java.net.URL;
35  
36  import java.util.ArrayList;
37  import java.util.Collection;
38  import java.util.Enumeration;
39  import java.util.List;
40  import java.util.Map;
41  import java.util.StringTokenizer;
42  
43  /**
44   * <a href="StringUtil.java.html"><b><i>View Source</i></b></a>
45   *
46   * @author Brian Wing Shun Chan
47   * @author Sandeep Soni
48   * @author Ganesh Ram
49   *
50   */
51  public class StringUtil {
52  
53      public static String add(String s, String add) {
54          return add(s, add, StringPool.COMMA);
55      }
56  
57      public static String add(String s, String add, String delimiter) {
58          return add(s, add, delimiter, false);
59      }
60  
61      public static String add(
62          String s, String add, String delimiter, boolean allowDuplicates) {
63  
64          if ((add == null) || (delimiter == null)) {
65              return null;
66          }
67  
68          if (s == null) {
69              s = StringPool.BLANK;
70          }
71  
72          if (allowDuplicates || !contains(s, add, delimiter)) {
73              StringBuilder sb = new StringBuilder();
74  
75              sb.append(s);
76  
77              if (Validator.isNull(s) || s.endsWith(delimiter)) {
78                  sb.append(add);
79                  sb.append(delimiter);
80              }
81              else {
82                  sb.append(delimiter);
83                  sb.append(add);
84                  sb.append(delimiter);
85              }
86  
87              s = sb.toString();
88          }
89  
90          return s;
91      }
92  
93      public static String bytesToHexString(byte[] bytes) {
94          StringBuilder sb = new StringBuilder(bytes.length * 2);
95  
96          for (int i = 0; i < bytes.length; i++) {
97              String hex = Integer.toHexString(
98                  0x0100 + (bytes[i] & 0x00FF)).substring(1);
99  
100             if (hex.length() < 2) {
101                 sb.append("0");
102             }
103 
104             sb.append(hex);
105         }
106 
107         return sb.toString();
108     }
109 
110     public static boolean contains(String s, String text) {
111         return contains(s, text, StringPool.COMMA);
112     }
113 
114     public static boolean contains(String s, String text, String delimiter) {
115         if ((s == null) || (text == null) || (delimiter == null)) {
116             return false;
117         }
118 
119         StringBuilder sb = null;
120 
121         if (!s.endsWith(delimiter)) {
122             sb = new StringBuilder();
123 
124             sb.append(s);
125             sb.append(delimiter);
126 
127             s = sb.toString();
128         }
129 
130         sb = new StringBuilder();
131 
132         sb.append(delimiter);
133         sb.append(text);
134         sb.append(delimiter);
135 
136         String dtd = sb.toString();
137 
138         int pos = s.indexOf(dtd);
139 
140         if (pos == -1) {
141             sb = new StringBuilder();
142 
143             sb.append(text);
144             sb.append(delimiter);
145 
146             String td = sb.toString();
147 
148             if (s.startsWith(td)) {
149                 return true;
150             }
151 
152             return false;
153         }
154 
155         return true;
156     }
157 
158     public static int count(String s, String text) {
159         if ((s == null) || (text == null)) {
160             return 0;
161         }
162 
163         int count = 0;
164 
165         int pos = s.indexOf(text);
166 
167         while (pos != -1) {
168             pos = s.indexOf(text, pos + text.length());
169 
170             count++;
171         }
172 
173         return count;
174     }
175 
176     public static boolean endsWith(String s, char end) {
177         return endsWith(s, (new Character(end)).toString());
178     }
179 
180     public static boolean endsWith(String s, String end) {
181         if ((s == null) || (end == null)) {
182             return false;
183         }
184 
185         if (end.length() > s.length()) {
186             return false;
187         }
188 
189         String temp = s.substring(s.length() - end.length(), s.length());
190 
191         if (temp.equalsIgnoreCase(end)) {
192             return true;
193         }
194         else {
195             return false;
196         }
197     }
198 
199     public static String extractChars(String s) {
200         if (s == null) {
201             return StringPool.BLANK;
202         }
203 
204         StringBuilder sb = new StringBuilder();
205 
206         char[] c = s.toCharArray();
207 
208         for (int i = 0; i < c.length; i++) {
209             if (Validator.isChar(c[i])) {
210                 sb.append(c[i]);
211             }
212         }
213 
214         return sb.toString();
215     }
216 
217     public static String extractDigits(String s) {
218         if (s == null) {
219             return StringPool.BLANK;
220         }
221 
222         StringBuilder sb = new StringBuilder();
223 
224         char[] c = s.toCharArray();
225 
226         for (int i = 0; i < c.length; i++) {
227             if (Validator.isDigit(c[i])) {
228                 sb.append(c[i]);
229             }
230         }
231 
232         return sb.toString();
233     }
234 
235     public static String extractFirst(String s, String delimiter) {
236         if (s == null) {
237             return null;
238         }
239         else {
240             String[] array = split(s, delimiter);
241 
242             if (array.length > 0) {
243                 return array[0];
244             }
245             else {
246                 return null;
247             }
248         }
249     }
250 
251     public static String extractLast(String s, String delimiter) {
252         if (s == null) {
253             return null;
254         }
255         else {
256             String[] array = split(s, delimiter);
257 
258             if (array.length > 0) {
259                 return array[array.length - 1];
260             }
261             else {
262                 return null;
263             }
264         }
265     }
266 
267     public static String highlight(String s, String keywords) {
268         return highlight(s, keywords, "<span class=\"highlight\">", "</span>");
269     }
270 
271     public static String highlight(
272         String s, String keywords, String highlight1, String highlight2) {
273 
274         if (s == null) {
275             return null;
276         }
277 
278         // The problem with using a regexp is that it searches the text in a
279         // case insenstive manner but doens't replace the text in a case
280         // insenstive manner. So the search results actually get messed up. The
281         // best way is to actually parse the results.
282 
283         //return s.replaceAll(
284         //  "(?i)" + keywords, highlight1 + keywords + highlight2);
285 
286         StringBuilder sb = new StringBuilder(StringPool.SPACE);
287 
288         StringTokenizer st = new StringTokenizer(s);
289 
290         while (st.hasMoreTokens()) {
291             String token = st.nextToken();
292 
293             if (token.equalsIgnoreCase(keywords)) {
294                 sb.append(highlight1);
295                 sb.append(token);
296                 sb.append(highlight2);
297             }
298             else {
299                 sb.append(token);
300             }
301 
302             if (st.hasMoreTokens()) {
303                 sb.append(StringPool.SPACE);
304             }
305         }
306 
307         return sb.toString();
308     }
309 
310     public static String insert(String s, String insert, int offset) {
311         if (s == null) {
312             return null;
313         }
314 
315         if (insert == null) {
316             return s;
317         }
318 
319         if (offset > s.length()) {
320             offset = s.length();
321         }
322 
323         StringBuilder sb = new StringBuilder(s);
324 
325         sb.insert(offset, insert);
326 
327         return sb.toString();
328     }
329 
330     public static String lowerCase(String s) {
331         if (s == null) {
332             return null;
333         }
334         else {
335             return s.toLowerCase();
336         }
337     }
338 
339     public static boolean matches(String s, String pattern) {
340         String[] array = pattern.split("\\*");
341 
342         for (int i = 0; i < array.length; i++) {
343             int pos = s.indexOf(array[i]);
344 
345             if (pos == -1) {
346                 return false;
347             }
348 
349             s = s.substring(pos + array[i].length());
350         }
351 
352         return true;
353     }
354 
355     public static String merge(boolean[] array) {
356         return merge(array, StringPool.COMMA);
357     }
358 
359     public static String merge(boolean[] array, String delimiter) {
360         if (array == null) {
361             return null;
362         }
363 
364         StringBuilder sb = new StringBuilder();
365 
366         for (int i = 0; i < array.length; i++) {
367             sb.append(String.valueOf(array[i]).trim());
368 
369             if ((i + 1) != array.length) {
370                 sb.append(delimiter);
371             }
372         }
373 
374         return sb.toString();
375     }
376 
377     public static String merge(double[] array) {
378         return merge(array, StringPool.COMMA);
379     }
380 
381     public static String merge(double[] array, String delimiter) {
382         if (array == null) {
383             return null;
384         }
385 
386         StringBuilder sb = new StringBuilder();
387 
388         for (int i = 0; i < array.length; i++) {
389             sb.append(String.valueOf(array[i]).trim());
390 
391             if ((i + 1) != array.length) {
392                 sb.append(delimiter);
393             }
394         }
395 
396         return sb.toString();
397     }
398 
399     public static String merge(float[] array) {
400         return merge(array, StringPool.COMMA);
401     }
402 
403     public static String merge(float[] array, String delimiter) {
404         if (array == null) {
405             return null;
406         }
407 
408         StringBuilder sb = new StringBuilder();
409 
410         for (int i = 0; i < array.length; i++) {
411             sb.append(String.valueOf(array[i]).trim());
412 
413             if ((i + 1) != array.length) {
414                 sb.append(delimiter);
415             }
416         }
417 
418         return sb.toString();
419     }
420 
421     public static String merge(int[] array) {
422         return merge(array, StringPool.COMMA);
423     }
424 
425     public static String merge(int[] array, String delimiter) {
426         if (array == null) {
427             return null;
428         }
429 
430         StringBuilder sb = new StringBuilder();
431 
432         for (int i = 0; i < array.length; i++) {
433             sb.append(String.valueOf(array[i]).trim());
434 
435             if ((i + 1) != array.length) {
436                 sb.append(delimiter);
437             }
438         }
439 
440         return sb.toString();
441     }
442 
443     public static String merge(long[] array) {
444         return merge(array, StringPool.COMMA);
445     }
446 
447     public static String merge(long[] array, String delimiter) {
448         if (array == null) {
449             return null;
450         }
451 
452         StringBuilder sb = new StringBuilder();
453 
454         for (int i = 0; i < array.length; i++) {
455             sb.append(String.valueOf(array[i]).trim());
456 
457             if ((i + 1) != array.length) {
458                 sb.append(delimiter);
459             }
460         }
461 
462         return sb.toString();
463     }
464 
465     public static String merge(short[] array) {
466         return merge(array, StringPool.COMMA);
467     }
468 
469     public static String merge(short[] array, String delimiter) {
470         if (array == null) {
471             return null;
472         }
473 
474         StringBuilder sb = new StringBuilder();
475 
476         for (int i = 0; i < array.length; i++) {
477             sb.append(String.valueOf(array[i]).trim());
478 
479             if ((i + 1) != array.length) {
480                 sb.append(delimiter);
481             }
482         }
483 
484         return sb.toString();
485     }
486 
487     public static String merge(Collection<?> col) {
488         return merge(col, StringPool.COMMA);
489     }
490 
491     public static String merge(Collection<?> col, String delimiter) {
492         if (col == null) {
493             return null;
494         }
495 
496         return merge(col.toArray(new Object[col.size()]), delimiter);
497     }
498 
499     public static String merge(Object[] array) {
500         return merge(array, StringPool.COMMA);
501     }
502 
503     public static String merge(Object[] array, String delimiter) {
504         if (array == null) {
505             return null;
506         }
507 
508         StringBuilder sb = new StringBuilder();
509 
510         for (int i = 0; i < array.length; i++) {
511             sb.append(String.valueOf(array[i]).trim());
512 
513             if ((i + 1) != array.length) {
514                 sb.append(delimiter);
515             }
516         }
517 
518         return sb.toString();
519     }
520 
521     public static String randomize(String s) {
522         return Randomizer.getInstance().randomize(s);
523     }
524 
525     public static String read(ClassLoader classLoader, String name)
526         throws IOException {
527 
528         return read(classLoader, name, false);
529     }
530 
531     public static String read(ClassLoader classLoader, String name, boolean all)
532         throws IOException {
533 
534         if (all) {
535             StringBuilder sb = new StringBuilder();
536 
537             Enumeration<URL> enu = classLoader.getResources(name);
538 
539             while (enu.hasMoreElements()) {
540                 URL url = enu.nextElement();
541 
542                 InputStream is = url.openStream();
543 
544                 if (is == null) {
545                     throw new IOException(
546                         "Unable to open resource at " + url.toString());
547                 }
548 
549                 String s = read(is);
550 
551                 if (s != null) {
552                     sb.append(s);
553                     sb.append(StringPool.NEW_LINE);
554                 }
555 
556                 is.close();
557             }
558 
559             return sb.toString().trim();
560         }
561         else {
562             InputStream is = classLoader.getResourceAsStream(name);
563 
564             if (is == null) {
565                 throw new IOException(
566                     "Unable to open resource in class loader " + name);
567             }
568 
569             String s = read(is);
570 
571             is.close();
572 
573             return s;
574         }
575     }
576 
577     public static String read(InputStream is) throws IOException {
578         StringBuilder sb = new StringBuilder();
579 
580         BufferedReader br = new BufferedReader(new InputStreamReader(is));
581 
582         String line = null;
583 
584         while ((line = br.readLine()) != null) {
585             sb.append(line).append('\n');
586         }
587 
588         br.close();
589 
590         return sb.toString().trim();
591     }
592 
593     public static String remove(String s, String remove) {
594         return remove(s, remove, StringPool.COMMA);
595     }
596 
597     public static String remove(String s, String remove, String delimiter) {
598         if ((s == null) || (remove == null) || (delimiter == null)) {
599             return null;
600         }
601 
602         if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
603             s += delimiter;
604         }
605 
606         StringBuilder sb = new StringBuilder();
607 
608         sb.append(delimiter);
609         sb.append(remove);
610         sb.append(delimiter);
611 
612         String drd = sb.toString();
613 
614         sb = new StringBuilder();
615 
616         sb.append(remove);
617         sb.append(delimiter);
618 
619         String rd = sb.toString();
620 
621         while (contains(s, remove, delimiter)) {
622             int pos = s.indexOf(drd);
623 
624             if (pos == -1) {
625                 if (s.startsWith(rd)) {
626                     int x = remove.length() + delimiter.length();
627                     int y = s.length();
628 
629                     s = s.substring(x, y);
630                 }
631             }
632             else {
633                 int x = pos + remove.length() + delimiter.length();
634                 int y = s.length();
635 
636                 sb = new StringBuilder();
637 
638                 sb.append(s.substring(0, pos));
639                 sb.append(s.substring(x, y));
640 
641                 s = sb.toString();
642             }
643         }
644 
645         return s;
646     }
647 
648     public static String replace(String s, char oldSub, char newSub) {
649         if (s == null) {
650             return null;
651         }
652 
653         return s.replace(oldSub, newSub);
654     }
655 
656     public static String replace(String s, char oldSub, String newSub) {
657         if ((s == null) || (newSub == null)) {
658             return null;
659         }
660 
661         // The number 5 is arbitrary and is used as extra padding to reduce
662         // buffer expansion
663 
664         StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length());
665 
666         char[] charArray = s.toCharArray();
667 
668         for (char c : charArray) {
669             if (c == oldSub) {
670                 sb.append(newSub);
671             }
672             else {
673                 sb.append(c);
674             }
675         }
676 
677         return sb.toString();
678     }
679 
680     public static String replace(String s, String oldSub, String newSub) {
681         if ((s == null) || (oldSub == null) || (newSub == null)) {
682             return null;
683         }
684 
685         int y = s.indexOf(oldSub);
686 
687         if (y >= 0) {
688 
689             // The number 5 is arbitrary and is used as extra padding to reduce
690             // buffer expansion
691 
692             StringBuilder sb = new StringBuilder(
693                 s.length() + 5 * newSub.length());
694 
695             int length = oldSub.length();
696             int x = 0;
697 
698             while (x <= y) {
699                 sb.append(s.substring(x, y));
700                 sb.append(newSub);
701 
702                 x = y + length;
703                 y = s.indexOf(oldSub, x);
704             }
705 
706             sb.append(s.substring(x));
707 
708             return sb.toString();
709         }
710         else {
711             return s;
712         }
713     }
714 
715     public static String replace(String s, String[] oldSubs, String[] newSubs) {
716         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
717             return null;
718         }
719 
720         if (oldSubs.length != newSubs.length) {
721             return s;
722         }
723 
724         for (int i = 0; i < oldSubs.length; i++) {
725             s = replace(s, oldSubs[i], newSubs[i]);
726         }
727 
728         return s;
729     }
730 
731     public static String replace(
732         String s, String[] oldSubs, String[] newSubs, boolean exactMatch) {
733 
734         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
735             return null;
736         }
737 
738         if (oldSubs.length != newSubs.length) {
739             return s;
740         }
741 
742         if (!exactMatch) {
743             replace(s, oldSubs, newSubs);
744         }
745         else {
746             for (int i = 0; i < oldSubs.length; i++) {
747                 s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]);
748             }
749         }
750 
751         return s;
752     }
753 
754     public static String replaceFirst(String s, char oldSub, char newSub) {
755         if (s == null) {
756             return null;
757         }
758 
759         return s.replaceFirst(String.valueOf(oldSub), String.valueOf(newSub));
760     }
761 
762     public static String replaceFirst(String s, char oldSub, String newSub) {
763         if ((s == null) || (newSub == null)) {
764             return null;
765         }
766 
767         return s.replaceFirst(String.valueOf(oldSub), newSub);
768     }
769 
770     public static String replaceFirst(String s, String oldSub, String newSub) {
771         if ((s == null) || (oldSub == null) || (newSub == null)) {
772             return null;
773         }
774 
775         return s.replaceFirst(oldSub, newSub);
776     }
777 
778     public static String replaceFirst(
779         String s, String[] oldSubs, String[] newSubs) {
780 
781         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
782             return null;
783         }
784 
785         if (oldSubs.length != newSubs.length) {
786             return s;
787         }
788 
789         for (int i = 0; i < oldSubs.length; i++) {
790             s = replaceFirst(s, oldSubs[i], newSubs[i]);
791         }
792 
793         return s;
794     }
795 
796     /**
797      * Returns a string with replaced values. This method will replace all text
798      * in the given string, between the beginning and ending delimiter, with new
799      * values found in the given map. For example, if the string contained the
800      * text <code>[$HELLO$]</code>, and the beginning delimiter was
801      * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and the
802      * values map had a key of <code>HELLO</code> that mapped to
803      * <code>WORLD</code>, then the replaced string will contain the text
804      * <code>[$WORLD$]</code>.
805      *
806      * @param       s the original string
807      * @param       begin the beginning delimiter
808      * @param       end the ending delimiter
809      * @param       values a map of old and new values
810      * @return      a string with replaced values
811      */
812     public static String replaceValues(
813         String s, String begin, String end, Map<String, String> values) {
814 
815         if ((s == null) || (begin == null) || (end == null) ||
816             (values == null) || (values.size() == 0)) {
817 
818             return s;
819         }
820 
821         StringBuilder sb = new StringBuilder(s.length());
822 
823         int pos = 0;
824 
825         while (true) {
826             int x = s.indexOf(begin, pos);
827             int y = s.indexOf(end, x + begin.length());
828 
829             if ((x == -1) || (y == -1)) {
830                 sb.append(s.substring(pos, s.length()));
831 
832                 break;
833             }
834             else {
835                 sb.append(s.substring(pos, x + begin.length()));
836 
837                 String oldValue = s.substring(x + begin.length(), y);
838 
839                 String newValue = values.get(oldValue);
840 
841                 if (newValue == null) {
842                     newValue = oldValue;
843                 }
844 
845                 sb.append(newValue);
846 
847                 pos = y;
848             }
849         }
850 
851         return sb.toString();
852     }
853 
854     public static String reverse(String s) {
855         if (s == null) {
856             return null;
857         }
858 
859         char[] c = s.toCharArray();
860         char[] reverse = new char[c.length];
861 
862         for (int i = 0; i < c.length; i++) {
863             reverse[i] = c[c.length - i - 1];
864         }
865 
866         return new String(reverse);
867     }
868 
869     public static String safePath(String path) {
870         return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
871     }
872 
873     public static String shorten(String s) {
874         return shorten(s, 20);
875     }
876 
877     public static String shorten(String s, int length) {
878         return shorten(s, length, "...");
879     }
880 
881     public static String shorten(String s, String suffix) {
882         return shorten(s, 20, suffix);
883     }
884 
885     public static String shorten(String s, int length, String suffix) {
886         if ((s == null) || (suffix == null)) {
887             return null;
888         }
889 
890         if (s.length() > length) {
891             for (int j = length; j >= 0; j--) {
892                 if (Character.isWhitespace(s.charAt(j))) {
893                     length = j;
894 
895                     break;
896                 }
897             }
898 
899             StringBuilder sb = new StringBuilder();
900 
901             sb.append(s.substring(0, length));
902             sb.append(suffix);
903 
904             s =  sb.toString();
905         }
906 
907         return s;
908     }
909 
910     public static String[] split(String s) {
911         return split(s, StringPool.COMMA);
912     }
913 
914     public static String[] split(String s, String delimiter) {
915         if ((Validator.isNull(s)) || (delimiter == null) ||
916             (delimiter.equals(StringPool.BLANK))) {
917 
918             return new String[0];
919         }
920 
921         s = s.trim();
922 
923         if (!s.endsWith(delimiter)) {
924             StringBuilder sb = new StringBuilder();
925 
926             sb.append(s);
927             sb.append(delimiter);
928 
929             s = sb.toString();
930         }
931 
932         if (s.equals(delimiter)) {
933             return new String[0];
934         }
935 
936         List<String> nodeValues = new ArrayList<String>();
937 
938         if (delimiter.equals(StringPool.NEW_LINE) ||
939             delimiter.equals(StringPool.RETURN)) {
940 
941             try {
942                 BufferedReader br = new BufferedReader(new StringReader(s));
943 
944                 String line = null;
945 
946                 while ((line = br.readLine()) != null) {
947                     nodeValues.add(line);
948                 }
949 
950                 br.close();
951             }
952             catch (IOException ioe) {
953                 _log.error(ioe.getMessage());
954             }
955         }
956         else {
957             int offset = 0;
958             int pos = s.indexOf(delimiter, offset);
959 
960             while (pos != -1) {
961                 nodeValues.add(new String(s.substring(offset, pos)));
962 
963                 offset = pos + delimiter.length();
964                 pos = s.indexOf(delimiter, offset);
965             }
966         }
967 
968         return nodeValues.toArray(new String[nodeValues.size()]);
969     }
970 
971     public static boolean[] split(String s, boolean x) {
972         return split(s, StringPool.COMMA, x);
973     }
974 
975     public static boolean[] split(String s, String delimiter, boolean x) {
976         String[] array = split(s, delimiter);
977         boolean[] newArray = new boolean[array.length];
978 
979         for (int i = 0; i < array.length; i++) {
980             boolean value = x;
981 
982             try {
983                 value = Boolean.valueOf(array[i]).booleanValue();
984             }
985             catch (Exception e) {
986             }
987 
988             newArray[i] = value;
989         }
990 
991         return newArray;
992     }
993 
994     public static double[] split(String s, double x) {
995         return split(s, StringPool.COMMA, x);
996     }
997 
998     public static double[] split(String s, String delimiter, double x) {
999         String[] array = split(s, delimiter);
1000        double[] newArray = new double[array.length];
1001
1002        for (int i = 0; i < array.length; i++) {
1003            double value = x;
1004
1005            try {
1006                value = Double.parseDouble(array[i]);
1007            }
1008            catch (Exception e) {
1009            }
1010
1011            newArray[i] = value;
1012        }
1013
1014        return newArray;
1015    }
1016
1017    public static float[] split(String s, float x) {
1018        return split(s, StringPool.COMMA, x);
1019    }
1020
1021    public static float[] split(String s, String delimiter, float x) {
1022        String[] array = split(s, delimiter);
1023        float[] newArray = new float[array.length];
1024
1025        for (int i = 0; i < array.length; i++) {
1026            float value = x;
1027
1028            try {
1029                value = Float.parseFloat(array[i]);
1030            }
1031            catch (Exception e) {
1032            }
1033
1034            newArray[i] = value;
1035        }
1036
1037        return newArray;
1038    }
1039
1040    public static int[] split(String s, int x) {
1041        return split(s, StringPool.COMMA, x);
1042    }
1043
1044    public static int[] split(String s, String delimiter, int x) {
1045        String[] array = split(s, delimiter);
1046        int[] newArray = new int[array.length];
1047
1048        for (int i = 0; i < array.length; i++) {
1049            int value = x;
1050
1051            try {
1052                value = Integer.parseInt(array[i]);
1053            }
1054            catch (Exception e) {
1055            }
1056
1057            newArray[i] = value;
1058        }
1059
1060        return newArray;
1061    }
1062
1063    public static long[] split(String s, long x) {
1064        return split(s, StringPool.COMMA, x);
1065    }
1066
1067    public static long[] split(String s, String delimiter, long x) {
1068        String[] array = split(s, delimiter);
1069        long[] newArray = new long[array.length];
1070
1071        for (int i = 0; i < array.length; i++) {
1072            long value = x;
1073
1074            try {
1075                value = Long.parseLong(array[i]);
1076            }
1077            catch (Exception e) {
1078            }
1079
1080            newArray[i] = value;
1081        }
1082
1083        return newArray;
1084    }
1085
1086    public static short[] split(String s, short x) {
1087        return split(s, StringPool.COMMA, x);
1088    }
1089
1090    public static short[] split(String s, String delimiter, short x) {
1091        String[] array = split(s, delimiter);
1092        short[] newArray = new short[array.length];
1093
1094        for (int i = 0; i < array.length; i++) {
1095            short value = x;
1096
1097            try {
1098                value = Short.parseShort(array[i]);
1099            }
1100            catch (Exception e) {
1101            }
1102
1103            newArray[i] = value;
1104        }
1105
1106        return newArray;
1107    }
1108
1109    public static boolean startsWith(String s, char begin) {
1110        return startsWith(s, (new Character(begin)).toString());
1111    }
1112
1113    public static boolean startsWith(String s, String start) {
1114        if ((s == null) || (start == null)) {
1115            return false;
1116        }
1117
1118        if (start.length() > s.length()) {
1119            return false;
1120        }
1121
1122        String temp = s.substring(0, start.length());
1123
1124        if (temp.equalsIgnoreCase(start)) {
1125            return true;
1126        }
1127        else {
1128            return false;
1129        }
1130    }
1131
1132    /**
1133     * Return the number of starting letters that s1 and s2 have in common
1134     * before they deviate.
1135     *
1136     * @param       s1 the first string
1137     * @param       s2 the second string
1138     *
1139     * @return      the number of starting letters that s1 and s2 have in common
1140     *              before they deviate
1141     */
1142    public static int startsWithWeight(String s1, String s2) {
1143        if ((s1 == null) || (s2 == null)) {
1144            return 0;
1145        }
1146
1147        char[] charArray1 = s1.toCharArray();
1148        char[] charArray2 = s2.toCharArray();
1149
1150        int i = 0;
1151
1152        for (; (i < charArray1.length) && (i < charArray2.length); i++) {
1153            if (charArray1[i] != charArray2[i]) {
1154                break;
1155            }
1156        }
1157
1158        return i;
1159    }
1160
1161    public static String stripBetween(String s, String begin, String end) {
1162        if ((s == null) || (begin == null) || (end == null)) {
1163            return s;
1164        }
1165
1166        StringBuilder sb = new StringBuilder(s.length());
1167
1168        int pos = 0;
1169
1170        while (true) {
1171            int x = s.indexOf(begin, pos);
1172            int y = s.indexOf(end, x + begin.length());
1173
1174            if ((x == -1) || (y == -1)) {
1175                sb.append(s.substring(pos, s.length()));
1176
1177                break;
1178            }
1179            else {
1180                sb.append(s.substring(pos, x));
1181
1182                pos = y + end.length();
1183            }
1184        }
1185
1186        return sb.toString();
1187    }
1188
1189    public static String trim(String s) {
1190        return trim(s, null);
1191    }
1192
1193    public static String trim(String s, char c) {
1194        return trim(s, new char[] {c});
1195    }
1196
1197    public static String trim(String s, char[] exceptions) {
1198        if (s == null) {
1199            return null;
1200        }
1201
1202        char[] charArray = s.toCharArray();
1203
1204        int len = charArray.length;
1205
1206        int x = 0;
1207        int y = charArray.length;
1208
1209        for (int i = 0; i < len; i++) {
1210            char c = charArray[i];
1211
1212            if (_isTrimable(c, exceptions)) {
1213                x = i + 1;
1214            }
1215            else {
1216                break;
1217            }
1218        }
1219
1220        for (int i = len - 1; i >= 0; i--) {
1221            char c = charArray[i];
1222
1223            if (_isTrimable(c, exceptions)) {
1224                y = i;
1225            }
1226            else {
1227                break;
1228            }
1229        }
1230
1231        if ((x != 0) || (y != len)) {
1232            return s.substring(x, y);
1233        }
1234        else {
1235            return s;
1236        }
1237    }
1238
1239    public static String trimLeading(String s) {
1240        return trimLeading(s, null);
1241    }
1242
1243    public static String trimLeading(String s, char c) {
1244        return trimLeading(s, new char[] {c});
1245    }
1246
1247    public static String trimLeading(String s, char[] exceptions) {
1248        if (s == null) {
1249            return null;
1250        }
1251
1252        char[] charArray = s.toCharArray();
1253
1254        int len = charArray.length;
1255
1256        int x = 0;
1257        int y = charArray.length;
1258
1259        for (int i = 0; i < len; i++) {
1260            char c = charArray[i];
1261
1262            if (_isTrimable(c, exceptions)) {
1263                x = i + 1;
1264            }
1265            else {
1266                break;
1267            }
1268        }
1269
1270        if ((x != 0) || (y != len)) {
1271            return s.substring(x, y);
1272        }
1273        else {
1274            return s;
1275        }
1276    }
1277
1278    public static String trimTrailing(String s) {
1279        return trimTrailing(s, null);
1280    }
1281
1282    public static String trimTrailing(String s, char c) {
1283        return trimTrailing(s, new char[] {c});
1284    }
1285
1286    public static String trimTrailing(String s, char[] exceptions) {
1287        if (s == null) {
1288            return null;
1289        }
1290
1291        char[] charArray = s.toCharArray();
1292
1293        int len = charArray.length;
1294
1295        int x = 0;
1296        int y = charArray.length;
1297
1298        for (int i = len - 1; i >= 0; i--) {
1299            char c = charArray[i];
1300
1301            if (_isTrimable(c, exceptions)) {
1302                y = i;
1303            }
1304            else {
1305                break;
1306            }
1307        }
1308
1309        if ((x != 0) || (y != len)) {
1310            return s.substring(x, y);
1311        }
1312        else {
1313            return s;
1314        }
1315    }
1316
1317    public static String upperCase(String s) {
1318        if (s == null) {
1319            return null;
1320        }
1321        else {
1322            return s.toUpperCase();
1323        }
1324    }
1325
1326    public static String upperCaseFirstLetter(String s) {
1327        char[] chars = s.toCharArray();
1328
1329        if ((chars[0] >= 97) && (chars[0] <= 122)) {
1330            chars[0] = (char)(chars[0] - 32);
1331        }
1332
1333        return new String(chars);
1334    }
1335
1336    public static String valueOf(Object obj) {
1337        return String.valueOf(obj);
1338    }
1339
1340    public static String wrap(String text) {
1341        return wrap(text, 80, StringPool.NEW_LINE);
1342    }
1343
1344    public static String wrap(String text, int width, String lineSeparator) {
1345        if (text == null) {
1346            return null;
1347        }
1348
1349        StringBuilder sb = new StringBuilder();
1350
1351        try {
1352            BufferedReader br = new BufferedReader(new StringReader(text));
1353
1354            String s = StringPool.BLANK;
1355
1356            while ((s = br.readLine()) != null) {
1357                if (s.length() == 0) {
1358                    sb.append(lineSeparator);
1359                }
1360                else {
1361                    String[] tokens = s.split(StringPool.SPACE);
1362                    boolean firstWord = true;
1363                    int curLineLength = 0;
1364
1365                    for (int i = 0; i < tokens.length; i++) {
1366                        if (!firstWord) {
1367                            sb.append(StringPool.SPACE);
1368                            curLineLength++;
1369                        }
1370
1371                        if (firstWord) {
1372                            sb.append(lineSeparator);
1373                        }
1374
1375                        sb.append(tokens[i]);
1376
1377                        curLineLength += tokens[i].length();
1378
1379                        if (curLineLength >= width) {
1380                            firstWord = true;
1381                            curLineLength = 0;
1382                        }
1383                        else {
1384                            firstWord = false;
1385                        }
1386                    }
1387                }
1388            }
1389        }
1390        catch (IOException ioe) {
1391            _log.error(ioe.getMessage());
1392        }
1393
1394        return sb.toString();
1395    }
1396
1397    private static boolean _isTrimable(char c, char[] exceptions) {
1398        if ((exceptions != null) && (exceptions.length > 0)) {
1399            for (int i = 0; i < exceptions.length; i++) {
1400                if (c == exceptions[i]) {
1401                    return false;
1402                }
1403            }
1404        }
1405
1406        return Character.isWhitespace(c);
1407    }
1408
1409    private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1410
1411}