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