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 (Validator.isNull(s) || Validator.isNull(keywords)) {
282             return s;
283         }
284 
285         Pattern pattern = Pattern.compile(
286             Pattern.quote(keywords), Pattern.CASE_INSENSITIVE);
287 
288         return _highlight(s, pattern, highlight1, highlight2);
289     }
290 
291     public static String highlight(String s, String[] queryTerms) {
292         return highlight(
293             s, queryTerms, "<span class=\"highlight\">", "</span>");
294     }
295 
296     public static String highlight(
297         String s, String[] queryTerms, String highlight1, String highlight2) {
298 
299         if (Validator.isNull(s) || Validator.isNull(queryTerms)) {
300             return s;
301         }
302 
303         StringBuilder sb = new StringBuilder();
304 
305         for (int i = 0; i < queryTerms.length; i++) {
306             sb.append(Pattern.quote(queryTerms[i].trim()));
307 
308             if ((i + 1) < queryTerms.length) {
309                 sb.append(StringPool.PIPE);
310             }
311         }
312 
313         Pattern pattern = Pattern.compile(
314             sb.toString(), Pattern.CANON_EQ | Pattern.UNICODE_CASE);
315 
316         return _highlight(s, pattern, highlight1, highlight2);
317     }
318 
319     public static String insert(String s, String insert, int offset) {
320         if (s == null) {
321             return null;
322         }
323 
324         if (insert == null) {
325             return s;
326         }
327 
328         if (offset > s.length()) {
329             offset = s.length();
330         }
331 
332         StringBuilder sb = new StringBuilder(s);
333 
334         sb.insert(offset, insert);
335 
336         return sb.toString();
337     }
338 
339     public static String lowerCase(String s) {
340         if (s == null) {
341             return null;
342         }
343         else {
344             return s.toLowerCase();
345         }
346     }
347 
348     public static boolean matches(String s, String pattern) {
349         String[] array = pattern.split("\\*");
350 
351         for (int i = 0; i < array.length; i++) {
352             int pos = s.indexOf(array[i]);
353 
354             if (pos == -1) {
355                 return false;
356             }
357 
358             s = s.substring(pos + array[i].length());
359         }
360 
361         return true;
362     }
363 
364     public static String merge(boolean[] array) {
365         return merge(array, StringPool.COMMA);
366     }
367 
368     public static String merge(boolean[] array, String delimiter) {
369         if (array == null) {
370             return null;
371         }
372 
373         StringBuilder sb = new StringBuilder();
374 
375         for (int i = 0; i < array.length; i++) {
376             sb.append(String.valueOf(array[i]).trim());
377 
378             if ((i + 1) != array.length) {
379                 sb.append(delimiter);
380             }
381         }
382 
383         return sb.toString();
384     }
385 
386     public static String merge(double[] array) {
387         return merge(array, StringPool.COMMA);
388     }
389 
390     public static String merge(double[] array, String delimiter) {
391         if (array == null) {
392             return null;
393         }
394 
395         StringBuilder sb = new StringBuilder();
396 
397         for (int i = 0; i < array.length; i++) {
398             sb.append(String.valueOf(array[i]).trim());
399 
400             if ((i + 1) != array.length) {
401                 sb.append(delimiter);
402             }
403         }
404 
405         return sb.toString();
406     }
407 
408     public static String merge(float[] array) {
409         return merge(array, StringPool.COMMA);
410     }
411 
412     public static String merge(float[] array, String delimiter) {
413         if (array == null) {
414             return null;
415         }
416 
417         StringBuilder sb = new StringBuilder();
418 
419         for (int i = 0; i < array.length; i++) {
420             sb.append(String.valueOf(array[i]).trim());
421 
422             if ((i + 1) != array.length) {
423                 sb.append(delimiter);
424             }
425         }
426 
427         return sb.toString();
428     }
429 
430     public static String merge(int[] array) {
431         return merge(array, StringPool.COMMA);
432     }
433 
434     public static String merge(int[] array, String delimiter) {
435         if (array == null) {
436             return null;
437         }
438 
439         StringBuilder sb = new StringBuilder();
440 
441         for (int i = 0; i < array.length; i++) {
442             sb.append(String.valueOf(array[i]).trim());
443 
444             if ((i + 1) != array.length) {
445                 sb.append(delimiter);
446             }
447         }
448 
449         return sb.toString();
450     }
451 
452     public static String merge(long[] array) {
453         return merge(array, StringPool.COMMA);
454     }
455 
456     public static String merge(long[] array, String delimiter) {
457         if (array == null) {
458             return null;
459         }
460 
461         StringBuilder sb = new StringBuilder();
462 
463         for (int i = 0; i < array.length; i++) {
464             sb.append(String.valueOf(array[i]).trim());
465 
466             if ((i + 1) != array.length) {
467                 sb.append(delimiter);
468             }
469         }
470 
471         return sb.toString();
472     }
473 
474     public static String merge(short[] array) {
475         return merge(array, StringPool.COMMA);
476     }
477 
478     public static String merge(short[] array, String delimiter) {
479         if (array == null) {
480             return null;
481         }
482 
483         StringBuilder sb = new StringBuilder();
484 
485         for (int i = 0; i < array.length; i++) {
486             sb.append(String.valueOf(array[i]).trim());
487 
488             if ((i + 1) != array.length) {
489                 sb.append(delimiter);
490             }
491         }
492 
493         return sb.toString();
494     }
495 
496     public static String merge(Collection<?> col) {
497         return merge(col, StringPool.COMMA);
498     }
499 
500     public static String merge(Collection<?> col, String delimiter) {
501         if (col == null) {
502             return null;
503         }
504 
505         return merge(col.toArray(new Object[col.size()]), delimiter);
506     }
507 
508     public static String merge(Object[] array) {
509         return merge(array, StringPool.COMMA);
510     }
511 
512     public static String merge(Object[] array, String delimiter) {
513         if (array == null) {
514             return null;
515         }
516 
517         StringBuilder sb = new StringBuilder();
518 
519         for (int i = 0; i < array.length; i++) {
520             sb.append(String.valueOf(array[i]).trim());
521 
522             if ((i + 1) != array.length) {
523                 sb.append(delimiter);
524             }
525         }
526 
527         return sb.toString();
528     }
529 
530     public static String randomize(String s) {
531         return Randomizer.getInstance().randomize(s);
532     }
533 
534     public static String read(ClassLoader classLoader, String name)
535         throws IOException {
536 
537         return read(classLoader, name, false);
538     }
539 
540     public static String read(ClassLoader classLoader, String name, boolean all)
541         throws IOException {
542 
543         if (all) {
544             StringBuilder sb = new StringBuilder();
545 
546             Enumeration<URL> enu = classLoader.getResources(name);
547 
548             while (enu.hasMoreElements()) {
549                 URL url = enu.nextElement();
550 
551                 InputStream is = url.openStream();
552 
553                 if (is == null) {
554                     throw new IOException(
555                         "Unable to open resource at " + url.toString());
556                 }
557 
558                 String s = read(is);
559 
560                 if (s != null) {
561                     sb.append(s);
562                     sb.append(StringPool.NEW_LINE);
563                 }
564 
565                 is.close();
566             }
567 
568             return sb.toString().trim();
569         }
570         else {
571             InputStream is = classLoader.getResourceAsStream(name);
572 
573             if (is == null) {
574                 throw new IOException(
575                     "Unable to open resource in class loader " + name);
576             }
577 
578             String s = read(is);
579 
580             is.close();
581 
582             return s;
583         }
584     }
585 
586     public static String read(InputStream is) throws IOException {
587         StringBuilder sb = new StringBuilder();
588 
589         BufferedReader br = new BufferedReader(new InputStreamReader(is));
590 
591         String line = null;
592 
593         while ((line = br.readLine()) != null) {
594             sb.append(line);
595             sb.append(CharPool.NEW_LINE);
596         }
597 
598         br.close();
599 
600         return sb.toString().trim();
601     }
602 
603     public static String remove(String s, String remove) {
604         return remove(s, remove, StringPool.COMMA);
605     }
606 
607     public static String remove(String s, String remove, String delimiter) {
608         if ((s == null) || (remove == null) || (delimiter == null)) {
609             return null;
610         }
611 
612         if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
613             s += delimiter;
614         }
615 
616         StringBuilder sb = new StringBuilder();
617 
618         sb.append(delimiter);
619         sb.append(remove);
620         sb.append(delimiter);
621 
622         String drd = sb.toString();
623 
624         sb = new StringBuilder();
625 
626         sb.append(remove);
627         sb.append(delimiter);
628 
629         String rd = sb.toString();
630 
631         while (contains(s, remove, delimiter)) {
632             int pos = s.indexOf(drd);
633 
634             if (pos == -1) {
635                 if (s.startsWith(rd)) {
636                     int x = remove.length() + delimiter.length();
637                     int y = s.length();
638 
639                     s = s.substring(x, y);
640                 }
641             }
642             else {
643                 int x = pos + remove.length() + delimiter.length();
644                 int y = s.length();
645 
646                 sb = new StringBuilder();
647 
648                 sb.append(s.substring(0, pos));
649                 sb.append(s.substring(x, y));
650 
651                 s = sb.toString();
652             }
653         }
654 
655         return s;
656     }
657 
658     public static String replace(String s, char oldSub, char newSub) {
659         if (s == null) {
660             return null;
661         }
662 
663         return s.replace(oldSub, newSub);
664     }
665 
666     public static String replace(String s, char oldSub, String newSub) {
667         if ((s == null) || (newSub == null)) {
668             return null;
669         }
670 
671         // The number 5 is arbitrary and is used as extra padding to reduce
672         // buffer expansion
673 
674         StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length());
675 
676         char[] charArray = s.toCharArray();
677 
678         for (char c : charArray) {
679             if (c == oldSub) {
680                 sb.append(newSub);
681             }
682             else {
683                 sb.append(c);
684             }
685         }
686 
687         return sb.toString();
688     }
689 
690     public static String replace(String s, String oldSub, String newSub) {
691         return replace(s, oldSub, newSub, 0);
692     }
693 
694     public static String replace(
695         String s, String oldSub, String newSub, int fromIndex) {
696 
697         if ((s == null) || (oldSub == null) || (newSub == null)) {
698             return null;
699         }
700 
701         int y = s.indexOf(oldSub, fromIndex);
702 
703         if (y >= 0) {
704 
705             // The number 5 is arbitrary and is used as extra padding to reduce
706             // buffer expansion
707 
708             StringBuilder sb = new StringBuilder(
709                 s.length() + 5 * newSub.length());
710 
711             int length = oldSub.length();
712             int x = 0;
713 
714             while (x <= y) {
715                 sb.append(s.substring(x, y));
716                 sb.append(newSub);
717 
718                 x = y + length;
719                 y = s.indexOf(oldSub, x);
720             }
721 
722             sb.append(s.substring(x));
723 
724             return sb.toString();
725         }
726         else {
727             return s;
728         }
729     }
730 
731     public static String replace(String s, String[] oldSubs, String[] newSubs) {
732         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
733             return null;
734         }
735 
736         if (oldSubs.length != newSubs.length) {
737             return s;
738         }
739 
740         for (int i = 0; i < oldSubs.length; i++) {
741             s = replace(s, oldSubs[i], newSubs[i]);
742         }
743 
744         return s;
745     }
746 
747     public static String replace(
748         String s, String[] oldSubs, String[] newSubs, boolean exactMatch) {
749 
750         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
751             return null;
752         }
753 
754         if (oldSubs.length != newSubs.length) {
755             return s;
756         }
757 
758         if (!exactMatch) {
759             replace(s, oldSubs, newSubs);
760         }
761         else {
762             for (int i = 0; i < oldSubs.length; i++) {
763                 s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]);
764             }
765         }
766 
767         return s;
768     }
769 
770     public static String replaceFirst(String s, char oldSub, char newSub) {
771         if (s == null) {
772             return null;
773         }
774 
775         return s.replaceFirst(String.valueOf(oldSub), String.valueOf(newSub));
776     }
777 
778     public static String replaceFirst(String s, char oldSub, String newSub) {
779         if ((s == null) || (newSub == null)) {
780             return null;
781         }
782 
783         return s.replaceFirst(String.valueOf(oldSub), newSub);
784     }
785 
786     public static String replaceFirst(String s, String oldSub, String newSub) {
787         if ((s == null) || (oldSub == null) || (newSub == null)) {
788             return null;
789         }
790 
791         return s.replaceFirst(oldSub, newSub);
792     }
793 
794     public static String replaceFirst(
795         String s, String[] oldSubs, String[] newSubs) {
796 
797         if ((s == null) || (oldSubs == null) || (newSubs == null)) {
798             return null;
799         }
800 
801         if (oldSubs.length != newSubs.length) {
802             return s;
803         }
804 
805         for (int i = 0; i < oldSubs.length; i++) {
806             s = replaceFirst(s, oldSubs[i], newSubs[i]);
807         }
808 
809         return s;
810     }
811 
812     /**
813      * Returns a string with replaced values. This method will replace all text
814      * in the given string, between the beginning and ending delimiter, with new
815      * values found in the given map. For example, if the string contained the
816      * text <code>[$HELLO$]</code>, and the beginning delimiter was
817      * <code>[$]</code>, and the ending delimiter was <code>$]</code>, and the
818      * values map had a key of <code>HELLO</code> that mapped to
819      * <code>WORLD</code>, then the replaced string will contain the text
820      * <code>[$WORLD$]</code>.
821      *
822      * @return a string with replaced values
823      */
824     public static String replaceValues(
825         String s, String begin, String end, Map<String, String> values) {
826 
827         if ((s == null) || (begin == null) || (end == null) ||
828             (values == null) || (values.size() == 0)) {
829 
830             return s;
831         }
832 
833         StringBuilder sb = new StringBuilder(s.length());
834 
835         int pos = 0;
836 
837         while (true) {
838             int x = s.indexOf(begin, pos);
839             int y = s.indexOf(end, x + begin.length());
840 
841             if ((x == -1) || (y == -1)) {
842                 sb.append(s.substring(pos, s.length()));
843 
844                 break;
845             }
846             else {
847                 sb.append(s.substring(pos, x + begin.length()));
848 
849                 String oldValue = s.substring(x + begin.length(), y);
850 
851                 String newValue = values.get(oldValue);
852 
853                 if (newValue == null) {
854                     newValue = oldValue;
855                 }
856 
857                 sb.append(newValue);
858 
859                 pos = y;
860             }
861         }
862 
863         return sb.toString();
864     }
865 
866     public static String reverse(String s) {
867         if (s == null) {
868             return null;
869         }
870 
871         char[] c = s.toCharArray();
872         char[] reverse = new char[c.length];
873 
874         for (int i = 0; i < c.length; i++) {
875             reverse[i] = c[c.length - i - 1];
876         }
877 
878         return new String(reverse);
879     }
880 
881     public static String safePath(String path) {
882         return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
883     }
884 
885     public static String shorten(String s) {
886         return shorten(s, 20);
887     }
888 
889     public static String shorten(String s, int length) {
890         return shorten(s, length, "...");
891     }
892 
893     public static String shorten(String s, String suffix) {
894         return shorten(s, 20, suffix);
895     }
896 
897     public static String shorten(String s, int length, String suffix) {
898         if ((s == null) || (suffix == null)) {
899             return null;
900         }
901 
902         if (s.length() > length) {
903             for (int j = length; j >= 0; j--) {
904                 if (Character.isWhitespace(s.charAt(j))) {
905                     length = j;
906 
907                     break;
908                 }
909             }
910 
911             StringBuilder sb = new StringBuilder();
912 
913             sb.append(s.substring(0, length));
914             sb.append(suffix);
915 
916             s =  sb.toString();
917         }
918 
919         return s;
920     }
921 
922     public static String[] split(String s) {
923         return split(s, StringPool.COMMA);
924     }
925 
926     public static String[] split(String s, String delimiter) {
927         if ((Validator.isNull(s)) || (delimiter == null) ||
928             (delimiter.equals(StringPool.BLANK))) {
929 
930             return new String[0];
931         }
932 
933         s = s.trim();
934 
935         if (!s.endsWith(delimiter)) {
936             StringBuilder sb = new StringBuilder();
937 
938             sb.append(s);
939             sb.append(delimiter);
940 
941             s = sb.toString();
942         }
943 
944         if (s.equals(delimiter)) {
945             return new String[0];
946         }
947 
948         List<String> nodeValues = new ArrayList<String>();
949 
950         if (delimiter.equals(StringPool.NEW_LINE) ||
951             delimiter.equals(StringPool.RETURN)) {
952 
953             try {
954                 BufferedReader br = new BufferedReader(new StringReader(s));
955 
956                 String line = null;
957 
958                 while ((line = br.readLine()) != null) {
959                     nodeValues.add(line);
960                 }
961 
962                 br.close();
963             }
964             catch (IOException ioe) {
965                 _log.error(ioe.getMessage());
966             }
967         }
968         else {
969             int offset = 0;
970             int pos = s.indexOf(delimiter, offset);
971 
972             while (pos != -1) {
973                 nodeValues.add(new String(s.substring(offset, pos)));
974 
975                 offset = pos + delimiter.length();
976                 pos = s.indexOf(delimiter, offset);
977             }
978         }
979 
980         return nodeValues.toArray(new String[nodeValues.size()]);
981     }
982 
983     public static boolean[] split(String s, boolean x) {
984         return split(s, StringPool.COMMA, x);
985     }
986 
987     public static boolean[] split(String s, String delimiter, boolean x) {
988         String[] array = split(s, delimiter);
989         boolean[] newArray = new boolean[array.length];
990 
991         for (int i = 0; i < array.length; i++) {
992             boolean value = x;
993 
994             try {
995                 value = Boolean.valueOf(array[i]).booleanValue();
996             }
997             catch (Exception e) {
998             }
999 
1000            newArray[i] = value;
1001        }
1002
1003        return newArray;
1004    }
1005
1006    public static double[] split(String s, double x) {
1007        return split(s, StringPool.COMMA, x);
1008    }
1009
1010    public static double[] split(String s, String delimiter, double x) {
1011        String[] array = split(s, delimiter);
1012        double[] newArray = new double[array.length];
1013
1014        for (int i = 0; i < array.length; i++) {
1015            double value = x;
1016
1017            try {
1018                value = Double.parseDouble(array[i]);
1019            }
1020            catch (Exception e) {
1021            }
1022
1023            newArray[i] = value;
1024        }
1025
1026        return newArray;
1027    }
1028
1029    public static float[] split(String s, float x) {
1030        return split(s, StringPool.COMMA, x);
1031    }
1032
1033    public static float[] split(String s, String delimiter, float x) {
1034        String[] array = split(s, delimiter);
1035        float[] newArray = new float[array.length];
1036
1037        for (int i = 0; i < array.length; i++) {
1038            float value = x;
1039
1040            try {
1041                value = Float.parseFloat(array[i]);
1042            }
1043            catch (Exception e) {
1044            }
1045
1046            newArray[i] = value;
1047        }
1048
1049        return newArray;
1050    }
1051
1052    public static int[] split(String s, int x) {
1053        return split(s, StringPool.COMMA, x);
1054    }
1055
1056    public static int[] split(String s, String delimiter, int x) {
1057        String[] array = split(s, delimiter);
1058        int[] newArray = new int[array.length];
1059
1060        for (int i = 0; i < array.length; i++) {
1061            int value = x;
1062
1063            try {
1064                value = Integer.parseInt(array[i]);
1065            }
1066            catch (Exception e) {
1067            }
1068
1069            newArray[i] = value;
1070        }
1071
1072        return newArray;
1073    }
1074
1075    public static long[] split(String s, long x) {
1076        return split(s, StringPool.COMMA, x);
1077    }
1078
1079    public static long[] split(String s, String delimiter, long x) {
1080        String[] array = split(s, delimiter);
1081        long[] newArray = new long[array.length];
1082
1083        for (int i = 0; i < array.length; i++) {
1084            long value = x;
1085
1086            try {
1087                value = Long.parseLong(array[i]);
1088            }
1089            catch (Exception e) {
1090            }
1091
1092            newArray[i] = value;
1093        }
1094
1095        return newArray;
1096    }
1097
1098    public static short[] split(String s, short x) {
1099        return split(s, StringPool.COMMA, x);
1100    }
1101
1102    public static short[] split(String s, String delimiter, short x) {
1103        String[] array = split(s, delimiter);
1104        short[] newArray = new short[array.length];
1105
1106        for (int i = 0; i < array.length; i++) {
1107            short value = x;
1108
1109            try {
1110                value = Short.parseShort(array[i]);
1111            }
1112            catch (Exception e) {
1113            }
1114
1115            newArray[i] = value;
1116        }
1117
1118        return newArray;
1119    }
1120
1121    public static boolean startsWith(String s, char begin) {
1122        return startsWith(s, (new Character(begin)).toString());
1123    }
1124
1125    public static boolean startsWith(String s, String start) {
1126        if ((s == null) || (start == null)) {
1127            return false;
1128        }
1129
1130        if (start.length() > s.length()) {
1131            return false;
1132        }
1133
1134        String temp = s.substring(0, start.length());
1135
1136        if (temp.equalsIgnoreCase(start)) {
1137            return true;
1138        }
1139        else {
1140            return false;
1141        }
1142    }
1143
1144    /**
1145     * Return the number of starting letters that s1 and s2 have in common
1146     * before they deviate.
1147     *
1148     * @return the number of starting letters that s1 and s2 have in common
1149     *         before they deviate
1150     */
1151    public static int startsWithWeight(String s1, String s2) {
1152        if ((s1 == null) || (s2 == null)) {
1153            return 0;
1154        }
1155
1156        char[] charArray1 = s1.toCharArray();
1157        char[] charArray2 = s2.toCharArray();
1158
1159        int i = 0;
1160
1161        for (; (i < charArray1.length) && (i < charArray2.length); i++) {
1162            if (charArray1[i] != charArray2[i]) {
1163                break;
1164            }
1165        }
1166
1167        return i;
1168    }
1169
1170    public static String stripBetween(String s, String begin, String end) {
1171        if ((s == null) || (begin == null) || (end == null)) {
1172            return s;
1173        }
1174
1175        StringBuilder sb = new StringBuilder(s.length());
1176
1177        int pos = 0;
1178
1179        while (true) {
1180            int x = s.indexOf(begin, pos);
1181            int y = s.indexOf(end, x + begin.length());
1182
1183            if ((x == -1) || (y == -1)) {
1184                sb.append(s.substring(pos, s.length()));
1185
1186                break;
1187            }
1188            else {
1189                sb.append(s.substring(pos, x));
1190
1191                pos = y + end.length();
1192            }
1193        }
1194
1195        return sb.toString();
1196    }
1197
1198    public static String trim(String s) {
1199        return trim(s, null);
1200    }
1201
1202    public static String trim(String s, char c) {
1203        return trim(s, new char[] {c});
1204    }
1205
1206    public static String trim(String s, char[] exceptions) {
1207        if (s == null) {
1208            return null;
1209        }
1210
1211        char[] charArray = s.toCharArray();
1212
1213        int len = charArray.length;
1214
1215        int x = 0;
1216        int y = charArray.length;
1217
1218        for (int i = 0; i < len; i++) {
1219            char c = charArray[i];
1220
1221            if (_isTrimable(c, exceptions)) {
1222                x = i + 1;
1223            }
1224            else {
1225                break;
1226            }
1227        }
1228
1229        for (int i = len - 1; i >= 0; i--) {
1230            char c = charArray[i];
1231
1232            if (_isTrimable(c, exceptions)) {
1233                y = i;
1234            }
1235            else {
1236                break;
1237            }
1238        }
1239
1240        if ((x != 0) || (y != len)) {
1241            return s.substring(x, y);
1242        }
1243        else {
1244            return s;
1245        }
1246    }
1247
1248    public static String trimLeading(String s) {
1249        return trimLeading(s, null);
1250    }
1251
1252    public static String trimLeading(String s, char c) {
1253        return trimLeading(s, new char[] {c});
1254    }
1255
1256    public static String trimLeading(String s, char[] exceptions) {
1257        if (s == null) {
1258            return null;
1259        }
1260
1261        char[] charArray = s.toCharArray();
1262
1263        int len = charArray.length;
1264
1265        int x = 0;
1266        int y = charArray.length;
1267
1268        for (int i = 0; i < len; i++) {
1269            char c = charArray[i];
1270
1271            if (_isTrimable(c, exceptions)) {
1272                x = i + 1;
1273            }
1274            else {
1275                break;
1276            }
1277        }
1278
1279        if ((x != 0) || (y != len)) {
1280            return s.substring(x, y);
1281        }
1282        else {
1283            return s;
1284        }
1285    }
1286
1287    public static String trimTrailing(String s) {
1288        return trimTrailing(s, null);
1289    }
1290
1291    public static String trimTrailing(String s, char c) {
1292        return trimTrailing(s, new char[] {c});
1293    }
1294
1295    public static String trimTrailing(String s, char[] exceptions) {
1296        if (s == null) {
1297            return null;
1298        }
1299
1300        char[] charArray = s.toCharArray();
1301
1302        int len = charArray.length;
1303
1304        int x = 0;
1305        int y = charArray.length;
1306
1307        for (int i = len - 1; i >= 0; i--) {
1308            char c = charArray[i];
1309
1310            if (_isTrimable(c, exceptions)) {
1311                y = i;
1312            }
1313            else {
1314                break;
1315            }
1316        }
1317
1318        if ((x != 0) || (y != len)) {
1319            return s.substring(x, y);
1320        }
1321        else {
1322            return s;
1323        }
1324    }
1325
1326    public static String upperCase(String s) {
1327        if (s == null) {
1328            return null;
1329        }
1330        else {
1331            return s.toUpperCase();
1332        }
1333    }
1334
1335    public static String upperCaseFirstLetter(String s) {
1336        char[] chars = s.toCharArray();
1337
1338        if ((chars[0] >= 97) && (chars[0] <= 122)) {
1339            chars[0] = (char)(chars[0] - 32);
1340        }
1341
1342        return new String(chars);
1343    }
1344
1345    public static String valueOf(Object obj) {
1346        return String.valueOf(obj);
1347    }
1348
1349    public static String wrap(String text) {
1350        return wrap(text, 80, StringPool.NEW_LINE);
1351    }
1352
1353    public static String wrap(String text, int width, String lineSeparator) {
1354        try {
1355            return _wrap(text, width, lineSeparator);
1356        }
1357        catch (IOException ioe) {
1358            _log.error(ioe.getMessage());
1359
1360            return text;
1361        }
1362    }
1363
1364    private static String _highlight(
1365        String s, Pattern pattern, String highlight1, String highlight2) {
1366
1367        StringTokenizer st = new StringTokenizer(s);
1368
1369        StringBuilder sb = new StringBuilder();
1370
1371        while (st.hasMoreTokens()) {
1372            String token = st.nextToken();
1373
1374            Matcher matcher = pattern.matcher(token);
1375
1376            if (matcher.find()) {
1377                String highlightedToken = matcher.replaceAll(
1378                    highlight1 + matcher.group() + highlight2);
1379
1380                sb.append(highlightedToken);
1381            }
1382            else {
1383                sb.append(token);
1384            }
1385
1386            if (st.hasMoreTokens()) {
1387                sb.append(StringPool.SPACE);
1388            }
1389        }
1390
1391        return sb.toString();
1392    }
1393
1394    private static boolean _isTrimable(char c, char[] exceptions) {
1395        if ((exceptions != null) && (exceptions.length > 0)) {
1396            for (int i = 0; i < exceptions.length; i++) {
1397                if (c == exceptions[i]) {
1398                    return false;
1399                }
1400            }
1401        }
1402
1403        return Character.isWhitespace(c);
1404    }
1405
1406    private static String _wrap(String text, int width, String lineSeparator)
1407        throws IOException {
1408
1409        if (text == null) {
1410            return null;
1411        }
1412
1413        StringBuilder sb = new StringBuilder();
1414
1415        BufferedReader br = new BufferedReader(new StringReader(text));
1416
1417        String s = StringPool.BLANK;
1418
1419        while ((s = br.readLine()) != null) {
1420            if (s.length() == 0) {
1421                sb.append(lineSeparator);
1422
1423                continue;
1424            }
1425
1426            int lineLength = 0;
1427
1428            String[] tokens = s.split(StringPool.SPACE);
1429
1430            for (String token : tokens) {
1431                if ((lineLength + token.length() + 1) > width) {
1432                    if (lineLength > 0) {
1433                        sb.append(lineSeparator);
1434                    }
1435
1436                    if (token.length() > width) {
1437                        int pos = token.indexOf(StringPool.OPEN_PARENTHESIS);
1438
1439                        if (pos != -1) {
1440                            sb.append(token.substring(0, pos + 1));
1441                            sb.append(lineSeparator);
1442
1443                            token = token.substring(pos + 1);
1444
1445                            sb.append(token);
1446
1447                            lineLength = token.length();
1448                        }
1449                        else {
1450                            sb.append(token);
1451
1452                            lineLength = token.length();
1453                        }
1454                    }
1455                    else {
1456                        sb.append(token);
1457
1458                        lineLength = token.length();
1459                    }
1460                }
1461                else {
1462                    if (lineLength > 0) {
1463                        sb.append(StringPool.SPACE);
1464
1465                        lineLength++;
1466                    }
1467
1468                    sb.append(token);
1469
1470                    lineLength += token.length();
1471                }
1472            }
1473
1474            sb.append(lineSeparator);
1475        }
1476
1477        return sb.toString();
1478    }
1479
1480    private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1481
1482}