001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021
022 import java.io.IOException;
023 import java.io.InputStream;
024 import java.io.InputStreamReader;
025
026 import java.net.URL;
027
028 import java.util.ArrayList;
029 import java.util.Collection;
030 import java.util.Enumeration;
031 import java.util.List;
032 import java.util.Map;
033 import java.util.StringTokenizer;
034 import java.util.regex.Matcher;
035 import java.util.regex.Pattern;
036
037
043 public class StringUtil {
044
045 public static String add(String s, String add) {
046 return add(s, add, StringPool.COMMA);
047 }
048
049 public static String add(String s, String add, String delimiter) {
050 return add(s, add, delimiter, false);
051 }
052
053 public static String add(
054 String s, String add, String delimiter, boolean allowDuplicates) {
055
056 if ((add == null) || (delimiter == null)) {
057 return null;
058 }
059
060 if (s == null) {
061 s = StringPool.BLANK;
062 }
063
064 if (allowDuplicates || !contains(s, add, delimiter)) {
065 StringBundler sb = new StringBundler();
066
067 sb.append(s);
068
069 if (Validator.isNull(s) || s.endsWith(delimiter)) {
070 sb.append(add);
071 sb.append(delimiter);
072 }
073 else {
074 sb.append(delimiter);
075 sb.append(add);
076 sb.append(delimiter);
077 }
078
079 s = sb.toString();
080 }
081
082 return s;
083 }
084
085 public static String bytesToHexString(byte[] bytes) {
086 StringBuilder sb = new StringBuilder(bytes.length * 2);
087
088 for (byte b : bytes) {
089 String hex = Integer.toHexString(
090 0x0100 + (b & 0x00FF)).substring(1);
091
092 if (hex.length() < 2) {
093 sb.append("0");
094 }
095
096 sb.append(hex);
097 }
098
099 return sb.toString();
100 }
101
102 public static boolean contains(String s, String text) {
103 return contains(s, text, StringPool.COMMA);
104 }
105
106 public static boolean contains(String s, String text, String delimiter) {
107 if ((s == null) || (text == null) || (delimiter == null)) {
108 return false;
109 }
110
111 if (!s.endsWith(delimiter)) {
112 s = s.concat(delimiter);
113 }
114
115 String dtd = delimiter.concat(text).concat(delimiter);
116
117 int pos = s.indexOf(dtd);
118
119 if (pos == -1) {
120 String td = text.concat(delimiter);
121
122 if (s.startsWith(td)) {
123 return true;
124 }
125
126 return false;
127 }
128
129 return true;
130 }
131
132 public static int count(String s, String text) {
133 if ((s == null) || (text == null)) {
134 return 0;
135 }
136
137 int count = 0;
138
139 int pos = s.indexOf(text);
140
141 while (pos != -1) {
142 pos = s.indexOf(text, pos + text.length());
143
144 count++;
145 }
146
147 return count;
148 }
149
150 public static boolean endsWith(String s, char end) {
151 return endsWith(s, (new Character(end)).toString());
152 }
153
154 public static boolean endsWith(String s, String end) {
155 if ((s == null) || (end == null)) {
156 return false;
157 }
158
159 if (end.length() > s.length()) {
160 return false;
161 }
162
163 String temp = s.substring(s.length() - end.length(), s.length());
164
165 if (temp.equalsIgnoreCase(end)) {
166 return true;
167 }
168 else {
169 return false;
170 }
171 }
172
173 public static String extractChars(String s) {
174 if (s == null) {
175 return StringPool.BLANK;
176 }
177
178 StringBuilder sb = new StringBuilder();
179
180 char[] chars = s.toCharArray();
181
182 for (char c : chars) {
183 if (Validator.isChar(c)) {
184 sb.append(c);
185 }
186 }
187
188 return sb.toString();
189 }
190
191 public static String extractDigits(String s) {
192 if (s == null) {
193 return StringPool.BLANK;
194 }
195
196 StringBuilder sb = new StringBuilder();
197
198 char[] chars = s.toCharArray();
199
200 for (char c : chars) {
201 if (Validator.isDigit(c)) {
202 sb.append(c);
203 }
204 }
205
206 return sb.toString();
207 }
208
209 public static String extractFirst(String s, char delimiter) {
210 if (s == null) {
211 return null;
212 }
213 else {
214 int index = s.indexOf(delimiter);
215
216 if (index < 0) {
217 return null;
218 }
219 else {
220 return s.substring(0, index);
221 }
222 }
223 }
224
225 public static String extractFirst(String s, String delimiter) {
226 if (s == null) {
227 return null;
228 }
229 else {
230 int index = s.indexOf(delimiter);
231
232 if (index < 0) {
233 return null;
234 }
235 else {
236 return s.substring(0, index);
237 }
238 }
239 }
240
241 public static String extractLast(String s, char delimiter) {
242 if (s == null) {
243 return null;
244 }
245 else {
246 int index = s.lastIndexOf(delimiter);
247
248 if (index < 0) {
249 return null;
250 }
251 else {
252 return s.substring(index + 1);
253 }
254 }
255 }
256
257 public static String extractLast(String s, String delimiter) {
258 if (s == null) {
259 return null;
260 }
261 else {
262 int index = s.lastIndexOf(delimiter);
263
264 if (index < 0) {
265 return null;
266 }
267 else {
268 return s.substring(index + delimiter.length());
269 }
270 }
271 }
272
273
276 public static String highlight(String s, String keywords) {
277 return highlight(s, keywords, "<span class=\"highlight\">", "</span>");
278 }
279
280
283 public static String highlight(
284 String s, String keywords, String highlight1, String highlight2) {
285
286 if (Validator.isNull(s) || Validator.isNull(keywords)) {
287 return s;
288 }
289
290 Pattern pattern = Pattern.compile(
291 Pattern.quote(keywords), Pattern.CASE_INSENSITIVE);
292
293 return _highlight(s, pattern, highlight1, highlight2);
294 }
295
296 public static String highlight(String s, String[] queryTerms) {
297 return highlight(
298 s, queryTerms, "<span class=\"highlight\">", "</span>");
299 }
300
301 public static String highlight(
302 String s, String[] queryTerms, String highlight1, String highlight2) {
303
304 if (Validator.isNull(s) || Validator.isNull(queryTerms)) {
305 return s;
306 }
307
308 if (queryTerms.length == 0) {
309 return StringPool.BLANK;
310 }
311
312 StringBundler sb = new StringBundler(2 * queryTerms.length - 1);
313
314 for (int i = 0; i < queryTerms.length; i++) {
315 sb.append(Pattern.quote(queryTerms[i].trim()));
316
317 if ((i + 1) < queryTerms.length) {
318 sb.append(StringPool.PIPE);
319 }
320 }
321
322 int flags =
323 Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
324
325 Pattern pattern = Pattern.compile(sb.toString(), flags);
326
327 return _highlight(s, pattern, highlight1, highlight2);
328 }
329
330 public static String insert(String s, String insert, int offset) {
331 if (s == null) {
332 return null;
333 }
334
335 if (insert == null) {
336 return s;
337 }
338
339 if (offset > s.length()) {
340 return s.concat(insert);
341 }
342 else {
343 String prefix = s.substring(0, offset);
344 String postfix = s.substring(offset);
345
346 return prefix.concat(insert).concat(postfix);
347 }
348 }
349
350 public static String lowerCase(String s) {
351 if (s == null) {
352 return null;
353 }
354 else {
355 return s.toLowerCase();
356 }
357 }
358
359 public static boolean matches(String s, String pattern) {
360 String[] array = pattern.split("\\*");
361
362 for (String element : array) {
363 int pos = s.indexOf(element);
364
365 if (pos == -1) {
366 return false;
367 }
368
369 s = s.substring(pos + element.length());
370 }
371
372 return true;
373 }
374
375 public static boolean matchesIgnoreCase(String s, String pattern) {
376 return matches(lowerCase(s), lowerCase(pattern));
377 }
378
379 public static String merge(boolean[] array) {
380 return merge(array, StringPool.COMMA);
381 }
382
383 public static String merge(boolean[] array, String delimiter) {
384 if (array == null) {
385 return null;
386 }
387
388 if (array.length == 0) {
389 return StringPool.BLANK;
390 }
391
392 StringBundler sb = new StringBundler(2 * array.length - 1);
393
394 for (int i = 0; i < array.length; i++) {
395 sb.append(String.valueOf(array[i]).trim());
396
397 if ((i + 1) != array.length) {
398 sb.append(delimiter);
399 }
400 }
401
402 return sb.toString();
403 }
404
405 public static String merge(Collection<?> col) {
406 return merge(col, StringPool.COMMA);
407 }
408
409 public static String merge(Collection<?> col, String delimiter) {
410 if (col == null) {
411 return null;
412 }
413
414 return merge(col.toArray(new Object[col.size()]), delimiter);
415 }
416
417 public static String merge(double[] array) {
418 return merge(array, StringPool.COMMA);
419 }
420
421 public static String merge(double[] array, String delimiter) {
422 if (array == null) {
423 return null;
424 }
425
426 if (array.length == 0) {
427 return StringPool.BLANK;
428 }
429
430 StringBundler sb = new StringBundler(2 * array.length - 1);
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(float[] array) {
444 return merge(array, StringPool.COMMA);
445 }
446
447 public static String merge(float[] array, String delimiter) {
448 if (array == null) {
449 return null;
450 }
451
452 if (array.length == 0) {
453 return StringPool.BLANK;
454 }
455
456 StringBundler sb = new StringBundler(2 * array.length - 1);
457
458 for (int i = 0; i < array.length; i++) {
459 sb.append(String.valueOf(array[i]).trim());
460
461 if ((i + 1) != array.length) {
462 sb.append(delimiter);
463 }
464 }
465
466 return sb.toString();
467 }
468
469 public static String merge(int[] array) {
470 return merge(array, StringPool.COMMA);
471 }
472
473 public static String merge(int[] array, String delimiter) {
474 if (array == null) {
475 return null;
476 }
477
478 if (array.length == 0) {
479 return StringPool.BLANK;
480 }
481
482 StringBundler sb = new StringBundler(2 * array.length - 1);
483
484 for (int i = 0; i < array.length; i++) {
485 sb.append(String.valueOf(array[i]).trim());
486
487 if ((i + 1) != array.length) {
488 sb.append(delimiter);
489 }
490 }
491
492 return sb.toString();
493 }
494
495 public static String merge(long[] array) {
496 return merge(array, StringPool.COMMA);
497 }
498
499 public static String merge(long[] array, String delimiter) {
500 if (array == null) {
501 return null;
502 }
503
504 if (array.length == 0) {
505 return StringPool.BLANK;
506 }
507
508 StringBundler sb = new StringBundler(2 * array.length - 1);
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 merge(Object[] array) {
522 return merge(array, StringPool.COMMA);
523 }
524
525 public static String merge(Object[] array, String delimiter) {
526 if (array == null) {
527 return null;
528 }
529
530 if (array.length == 0) {
531 return StringPool.BLANK;
532 }
533
534 StringBundler sb = new StringBundler(2 * array.length - 1);
535
536 for (int i = 0; i < array.length; i++) {
537 sb.append(String.valueOf(array[i]).trim());
538
539 if ((i + 1) != array.length) {
540 sb.append(delimiter);
541 }
542 }
543
544 return sb.toString();
545 }
546
547 public static String merge(short[] array) {
548 return merge(array, StringPool.COMMA);
549 }
550
551 public static String merge(short[] array, String delimiter) {
552 if (array == null) {
553 return null;
554 }
555
556 if (array.length == 0) {
557 return StringPool.BLANK;
558 }
559
560 StringBundler sb = new StringBundler(2 * array.length - 1);
561
562 for (int i = 0; i < array.length; i++) {
563 sb.append(String.valueOf(array[i]).trim());
564
565 if ((i + 1) != array.length) {
566 sb.append(delimiter);
567 }
568 }
569
570 return sb.toString();
571 }
572
573 public static String quote(String s) {
574 return quote(s, CharPool.APOSTROPHE);
575 }
576
577 public static String quote(String s, char quote) {
578 if (s == null) {
579 return null;
580 }
581
582 return quote(s, String.valueOf(quote));
583 }
584
585 public static String quote(String s, String quote) {
586 if (s == null) {
587 return null;
588 }
589
590 return quote.concat(s).concat(quote);
591 }
592
593 public static String randomize(String s) {
594 return Randomizer.getInstance().randomize(s);
595 }
596
597 public static String read(ClassLoader classLoader, String name)
598 throws IOException {
599
600 return read(classLoader, name, false);
601 }
602
603 public static String read(ClassLoader classLoader, String name, boolean all)
604 throws IOException {
605
606 if (all) {
607 StringBundler sb = new StringBundler();
608
609 Enumeration<URL> enu = classLoader.getResources(name);
610
611 while (enu.hasMoreElements()) {
612 URL url = enu.nextElement();
613
614 InputStream is = url.openStream();
615
616 if (is == null) {
617 throw new IOException(
618 "Unable to open resource at " + url.toString());
619 }
620
621 String s = read(is);
622
623 if (s != null) {
624 sb.append(s);
625 sb.append(StringPool.NEW_LINE);
626 }
627
628 is.close();
629 }
630
631 return sb.toString().trim();
632 }
633 else {
634 InputStream is = classLoader.getResourceAsStream(name);
635
636 if (is == null) {
637 throw new IOException(
638 "Unable to open resource in class loader " + name);
639 }
640
641 String s = read(is);
642
643 is.close();
644
645 return s;
646 }
647 }
648
649 public static String read(InputStream is) throws IOException {
650 StringBundler sb = new StringBundler();
651
652 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
653 new InputStreamReader(is));
654
655 String line = null;
656
657 while ((line = unsyncBufferedReader.readLine()) != null) {
658 sb.append(line);
659 sb.append(CharPool.NEW_LINE);
660 }
661
662 unsyncBufferedReader.close();
663
664 return sb.toString().trim();
665 }
666
667 public static void readLines(InputStream is, Collection<String> lines)
668 throws IOException {
669
670 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
671 new InputStreamReader(is));
672
673 String line = null;
674
675 while ((line = unsyncBufferedReader.readLine()) != null) {
676 lines.add(line);
677 }
678
679 unsyncBufferedReader.close();
680 }
681
682 public static String remove(String s, String remove) {
683 return remove(s, remove, StringPool.COMMA);
684 }
685
686 public static String remove(String s, String remove, String delimiter) {
687 if ((s == null) || (remove == null) || (delimiter == null)) {
688 return null;
689 }
690
691 if (Validator.isNotNull(s) && !s.endsWith(delimiter)) {
692 s += delimiter;
693 }
694
695 String drd = delimiter.concat(remove).concat(delimiter);
696
697 String rd = remove.concat(delimiter);
698
699 while (contains(s, remove, delimiter)) {
700 int pos = s.indexOf(drd);
701
702 if (pos == -1) {
703 if (s.startsWith(rd)) {
704 int x = remove.length() + delimiter.length();
705 int y = s.length();
706
707 s = s.substring(x, y);
708 }
709 }
710 else {
711 int x = pos + remove.length() + delimiter.length();
712 int y = s.length();
713
714 String temp = s.substring(0, pos);
715
716 s = temp.concat(s.substring(x, y));
717 }
718 }
719
720 return s;
721 }
722
723 public static String replace(String s, char oldSub, char newSub) {
724 if (s == null) {
725 return null;
726 }
727
728 return s.replace(oldSub, newSub);
729 }
730
731 public static String replace(String s, char oldSub, String newSub) {
732 if ((s == null) || (newSub == null)) {
733 return null;
734 }
735
736
737
738
739 StringBuilder sb = new StringBuilder(s.length() + 5 * newSub.length());
740
741 char[] chars = s.toCharArray();
742
743 for (char c : chars) {
744 if (c == oldSub) {
745 sb.append(newSub);
746 }
747 else {
748 sb.append(c);
749 }
750 }
751
752 return sb.toString();
753 }
754
755 public static String replace(String s, String oldSub, String newSub) {
756 return replace(s, oldSub, newSub, 0);
757 }
758
759 public static String replace(
760 String s, String oldSub, String newSub, int fromIndex) {
761
762 if (s == null) {
763 return null;
764 }
765
766 if ((oldSub == null) || oldSub.equals(StringPool.BLANK)) {
767 return s;
768 }
769
770 if (newSub == null) {
771 newSub = StringPool.BLANK;
772 }
773
774 int y = s.indexOf(oldSub, fromIndex);
775
776 if (y >= 0) {
777 StringBundler sb = new StringBundler();
778
779 int length = oldSub.length();
780 int x = 0;
781
782 while (x <= y) {
783 sb.append(s.substring(x, y));
784 sb.append(newSub);
785
786 x = y + length;
787 y = s.indexOf(oldSub, x);
788 }
789
790 sb.append(s.substring(x));
791
792 return sb.toString();
793 }
794 else {
795 return s;
796 }
797 }
798
799 public static String replace(
800 String s, String begin, String end, Map<String, String> values) {
801
802 StringBundler sb = replaceToStringBundler(s, begin, end, values);
803
804 return sb.toString();
805 }
806
807 public static String replace(String s, String[] oldSubs, String[] newSubs) {
808 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
809 return null;
810 }
811
812 if (oldSubs.length != newSubs.length) {
813 return s;
814 }
815
816 for (int i = 0; i < oldSubs.length; i++) {
817 s = replace(s, oldSubs[i], newSubs[i]);
818 }
819
820 return s;
821 }
822
823 public static String replace(
824 String s, String[] oldSubs, String[] newSubs, boolean exactMatch) {
825
826 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
827 return null;
828 }
829
830 if (oldSubs.length != newSubs.length) {
831 return s;
832 }
833
834 if (!exactMatch) {
835 replace(s, oldSubs, newSubs);
836 }
837 else {
838 for (int i = 0; i < oldSubs.length; i++) {
839 s = s.replaceAll("\\b" + oldSubs[i] + "\\b" , newSubs[i]);
840 }
841 }
842
843 return s;
844 }
845
846 public static String replaceFirst(String s, char oldSub, char newSub) {
847 if (s == null) {
848 return null;
849 }
850
851 return replaceFirst(s, String.valueOf(oldSub), String.valueOf(newSub));
852 }
853
854 public static String replaceFirst(String s, char oldSub, String newSub) {
855 if ((s == null) || (newSub == null)) {
856 return null;
857 }
858
859 return replaceFirst(s, String.valueOf(oldSub), newSub);
860 }
861
862 public static String replaceFirst(String s, String oldSub, String newSub) {
863 if ((s == null) || (oldSub == null) || (newSub == null)) {
864 return null;
865 }
866
867 if (oldSub.equals(newSub)) {
868 return s;
869 }
870
871 int y = s.indexOf(oldSub);
872
873 if (y >= 0) {
874 return s.substring(0, y).concat(newSub).concat(
875 s.substring(y + oldSub.length()));
876 }
877 else {
878 return s;
879 }
880 }
881
882 public static String replaceFirst(
883 String s, String[] oldSubs, String[] newSubs) {
884
885 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
886 return null;
887 }
888
889 if (oldSubs.length != newSubs.length) {
890 return s;
891 }
892
893 for (int i = 0; i < oldSubs.length; i++) {
894 s = replaceFirst(s, oldSubs[i], newSubs[i]);
895 }
896
897 return s;
898 }
899
900 public static String replaceLast(String s, char oldSub, char newSub) {
901 if (s == null) {
902 return null;
903 }
904
905 return replaceLast(s, String.valueOf(oldSub), String.valueOf(newSub));
906 }
907
908 public static String replaceLast(String s, char oldSub, String newSub) {
909 if ((s == null) || (newSub == null)) {
910 return null;
911 }
912
913 return replaceLast(s, String.valueOf(oldSub), newSub);
914 }
915
916 public static String replaceLast(String s, String oldSub, String newSub) {
917 if ((s == null) || (oldSub == null) || (newSub == null)) {
918 return null;
919 }
920
921 if (oldSub.equals(newSub)) {
922 return s;
923 }
924
925 int y = s.lastIndexOf(oldSub);
926
927 if (y >= 0) {
928 return s.substring(0, y).concat(newSub).concat(
929 s.substring(y + oldSub.length()));
930 }
931 else {
932 return s;
933 }
934 }
935
936 public static String replaceLast(
937 String s, String[] oldSubs, String[] newSubs) {
938
939 if ((s == null) || (oldSubs == null) || (newSubs == null)) {
940 return null;
941 }
942
943 if (oldSubs.length != newSubs.length) {
944 return s;
945 }
946
947 for (int i = 0; i < oldSubs.length; i++) {
948 s = replaceLast(s, oldSubs[i], newSubs[i]);
949 }
950
951 return s;
952 }
953
954 public static StringBundler replaceToStringBundler(
955 String s, String begin, String end, Map<String, String> values) {
956
957 if ((s == null) || (begin == null) || (end == null) ||
958 (values == null) || (values.size() == 0)) {
959
960 return new StringBundler(s);
961 }
962
963 StringBundler sb = new StringBundler(values.size() * 2 + 1);
964
965 int pos = 0;
966
967 while (true) {
968 int x = s.indexOf(begin, pos);
969 int y = s.indexOf(end, x + begin.length());
970
971 if ((x == -1) || (y == -1)) {
972 sb.append(s.substring(pos, s.length()));
973
974 break;
975 }
976 else {
977 sb.append(s.substring(pos, x));
978
979 String oldValue = s.substring(x + begin.length(), y);
980
981 String newValue = values.get(oldValue);
982
983 if (newValue == null) {
984 newValue = oldValue;
985 }
986
987 sb.append(newValue);
988
989 pos = y + end.length();
990 }
991 }
992
993 return sb;
994 }
995
996 public static StringBundler replaceWithStringBundler(
997 String s, String begin, String end, Map<String, StringBundler> values) {
998
999 if ((s == null) || (begin == null) || (end == null) ||
1000 (values == null) || (values.size() == 0)) {
1001
1002 return new StringBundler(s);
1003 }
1004
1005 int size = values.size() + 1;
1006
1007 for (StringBundler valueSB : values.values()) {
1008 size += valueSB.index();
1009 }
1010
1011 StringBundler sb = new StringBundler(size);
1012
1013 int pos = 0;
1014
1015 while (true) {
1016 int x = s.indexOf(begin, pos);
1017 int y = s.indexOf(end, x + begin.length());
1018
1019 if ((x == -1) || (y == -1)) {
1020 sb.append(s.substring(pos, s.length()));
1021
1022 break;
1023 }
1024 else {
1025 sb.append(s.substring(pos, x));
1026
1027 String oldValue = s.substring(x + begin.length(), y);
1028
1029 StringBundler newValue = values.get(oldValue);
1030
1031 if (newValue == null) {
1032 sb.append(oldValue);
1033 }
1034 else {
1035 sb.append(newValue);
1036 }
1037
1038 pos = y + end.length();
1039 }
1040 }
1041
1042 return sb;
1043 }
1044
1045 public static String reverse(String s) {
1046 if (s == null) {
1047 return null;
1048 }
1049
1050 char[] chars = s.toCharArray();
1051 char[] reverse = new char[chars.length];
1052
1053 for (int i = 0; i < chars.length; i++) {
1054 reverse[i] = chars[chars.length - i - 1];
1055 }
1056
1057 return new String(reverse);
1058 }
1059
1060 public static String safePath(String path) {
1061 return replace(path, StringPool.DOUBLE_SLASH, StringPool.SLASH);
1062 }
1063
1064 public static String shorten(String s) {
1065 return shorten(s, 20);
1066 }
1067
1068 public static String shorten(String s, int length) {
1069 return shorten(s, length, "...");
1070 }
1071
1072 public static String shorten(String s, int length, String suffix) {
1073 if ((s == null) || (suffix == null)) {
1074 return null;
1075 }
1076
1077 if (s.length() > length) {
1078 for (int j = length; j >= 0; j--) {
1079 if (Character.isWhitespace(s.charAt(j))) {
1080 length = j;
1081
1082 break;
1083 }
1084 }
1085
1086 String temp = s.substring(0, length);
1087
1088 s = temp.concat(suffix);
1089 }
1090
1091 return s;
1092 }
1093
1094 public static String shorten(String s, String suffix) {
1095 return shorten(s, 20, suffix);
1096 }
1097
1098 public static String[] split(String s) {
1099 return split(s, CharPool.COMMA);
1100 }
1101
1102 public static boolean[] split(String s, boolean x) {
1103 return split(s, StringPool.COMMA, x);
1104 }
1105
1106 public static double[] split(String s, double x) {
1107 return split(s, StringPool.COMMA, x);
1108 }
1109
1110 public static float[] split(String s, float x) {
1111 return split(s, StringPool.COMMA, x);
1112 }
1113
1114 public static int[] split(String s, int x) {
1115 return split(s, StringPool.COMMA, x);
1116 }
1117
1118 public static long[] split(String s, long x) {
1119 return split(s, StringPool.COMMA, x);
1120 }
1121
1122 public static short[] split(String s, short x) {
1123 return split(s, StringPool.COMMA, x);
1124 }
1125
1126 public static String[] split(String s, char delimiter) {
1127 if (Validator.isNull(s)) {
1128 return _emptyStringArray;
1129 }
1130
1131 s = s.trim();
1132
1133 if (s.length() == 0) {
1134 return _emptyStringArray;
1135 }
1136
1137 if ((delimiter == CharPool.RETURN) ||
1138 (delimiter == CharPool.NEW_LINE)) {
1139
1140 return splitLines(s);
1141 }
1142
1143 List<String> nodeValues = new ArrayList<String>();
1144
1145 int offset = 0;
1146 int pos = s.indexOf(delimiter, offset);
1147
1148 while (pos != -1) {
1149 nodeValues.add(s.substring(offset, pos));
1150
1151 offset = pos + 1;
1152 pos = s.indexOf(delimiter, offset);
1153 }
1154
1155 if (offset < s.length()) {
1156 nodeValues.add(s.substring(offset));
1157 }
1158
1159 return nodeValues.toArray(new String[nodeValues.size()]);
1160 }
1161
1162 public static String[] split(String s, String delimiter) {
1163 if ((Validator.isNull(s)) || (delimiter == null) ||
1164 (delimiter.equals(StringPool.BLANK))) {
1165
1166 return _emptyStringArray;
1167 }
1168
1169 s = s.trim();
1170
1171 if (s.equals(delimiter)) {
1172 return _emptyStringArray;
1173 }
1174
1175 if (delimiter.length() == 1) {
1176 return split(s, delimiter.charAt(0));
1177 }
1178
1179 List<String> nodeValues = new ArrayList<String>();
1180
1181 int offset = 0;
1182 int pos = s.indexOf(delimiter, offset);
1183
1184 while (pos != -1) {
1185 nodeValues.add(s.substring(offset, pos));
1186
1187 offset = pos + delimiter.length();
1188 pos = s.indexOf(delimiter, offset);
1189 }
1190
1191 if (offset < s.length()) {
1192 nodeValues.add(s.substring(offset));
1193 }
1194
1195 return nodeValues.toArray(new String[nodeValues.size()]);
1196 }
1197
1198 public static boolean[] split(String s, String delimiter, boolean x) {
1199 String[] array = split(s, delimiter);
1200 boolean[] newArray = new boolean[array.length];
1201
1202 for (int i = 0; i < array.length; i++) {
1203 boolean value = x;
1204
1205 try {
1206 value = Boolean.valueOf(array[i]).booleanValue();
1207 }
1208 catch (Exception e) {
1209 }
1210
1211 newArray[i] = value;
1212 }
1213
1214 return newArray;
1215 }
1216
1217 public static double[] split(String s, String delimiter, double x) {
1218 String[] array = split(s, delimiter);
1219 double[] newArray = new double[array.length];
1220
1221 for (int i = 0; i < array.length; i++) {
1222 double value = x;
1223
1224 try {
1225 value = Double.parseDouble(array[i]);
1226 }
1227 catch (Exception e) {
1228 }
1229
1230 newArray[i] = value;
1231 }
1232
1233 return newArray;
1234 }
1235
1236 public static float[] split(String s, String delimiter, float x) {
1237 String[] array = split(s, delimiter);
1238 float[] newArray = new float[array.length];
1239
1240 for (int i = 0; i < array.length; i++) {
1241 float value = x;
1242
1243 try {
1244 value = Float.parseFloat(array[i]);
1245 }
1246 catch (Exception e) {
1247 }
1248
1249 newArray[i] = value;
1250 }
1251
1252 return newArray;
1253 }
1254
1255 public static int[] split(String s, String delimiter, int x) {
1256 String[] array = split(s, delimiter);
1257 int[] newArray = new int[array.length];
1258
1259 for (int i = 0; i < array.length; i++) {
1260 int value = x;
1261
1262 try {
1263 value = Integer.parseInt(array[i]);
1264 }
1265 catch (Exception e) {
1266 }
1267
1268 newArray[i] = value;
1269 }
1270
1271 return newArray;
1272 }
1273
1274 public static long[] split(String s, String delimiter, long x) {
1275 String[] array = split(s, delimiter);
1276 long[] newArray = new long[array.length];
1277
1278 for (int i = 0; i < array.length; i++) {
1279 long value = x;
1280
1281 try {
1282 value = Long.parseLong(array[i]);
1283 }
1284 catch (Exception e) {
1285 }
1286
1287 newArray[i] = value;
1288 }
1289
1290 return newArray;
1291 }
1292
1293 public static short[] split(String s, String delimiter, short x) {
1294 String[] array = split(s, delimiter);
1295 short[] newArray = new short[array.length];
1296
1297 for (int i = 0; i < array.length; i++) {
1298 short value = x;
1299
1300 try {
1301 value = Short.parseShort(array[i]);
1302 }
1303 catch (Exception e) {
1304 }
1305
1306 newArray[i] = value;
1307 }
1308
1309 return newArray;
1310 }
1311
1312 public static String[] splitLines(String s) {
1313 if (Validator.isNull(s)) {
1314 return _emptyStringArray;
1315 }
1316
1317 s = s.trim();
1318
1319 List<String> lines = new ArrayList<String>();
1320
1321 int lastIndex = 0;
1322
1323 while (true) {
1324 int returnIndex = s.indexOf(CharPool.RETURN, lastIndex);
1325 int newLineIndex = s.indexOf(CharPool.NEW_LINE, lastIndex);
1326
1327 if ((returnIndex == -1) && (newLineIndex == -1)) {
1328 break;
1329 }
1330
1331 if (returnIndex == -1) {
1332 lines.add(s.substring(lastIndex, newLineIndex));
1333
1334 lastIndex = newLineIndex + 1;
1335 }
1336 else if (newLineIndex == -1) {
1337 lines.add(s.substring(lastIndex, returnIndex));
1338
1339 lastIndex = returnIndex + 1;
1340 }
1341 else if (newLineIndex < returnIndex) {
1342 lines.add(s.substring(lastIndex, newLineIndex));
1343
1344 lastIndex = newLineIndex + 1;
1345 }
1346 else {
1347 lines.add(s.substring(lastIndex, returnIndex));
1348
1349 lastIndex = returnIndex + 1;
1350
1351 if (lastIndex == newLineIndex) {
1352 lastIndex++;
1353 }
1354 }
1355 }
1356
1357 if (lastIndex < s.length()) {
1358 lines.add(s.substring(lastIndex));
1359 }
1360
1361 return lines.toArray(new String[lines.size()]);
1362 }
1363
1364 public static boolean startsWith(String s, char begin) {
1365 return startsWith(s, (new Character(begin)).toString());
1366 }
1367
1368 public static boolean startsWith(String s, String start) {
1369 if ((s == null) || (start == null)) {
1370 return false;
1371 }
1372
1373 if (start.length() > s.length()) {
1374 return false;
1375 }
1376
1377 String temp = s.substring(0, start.length());
1378
1379 if (temp.equalsIgnoreCase(start)) {
1380 return true;
1381 }
1382 else {
1383 return false;
1384 }
1385 }
1386
1387
1394 public static int startsWithWeight(String s1, String s2) {
1395 if ((s1 == null) || (s2 == null)) {
1396 return 0;
1397 }
1398
1399 char[] chars1 = s1.toCharArray();
1400 char[] chars2 = s2.toCharArray();
1401
1402 int i = 0;
1403
1404 for (; (i < chars1.length) && (i < chars2.length); i++) {
1405 if (chars1[i] != chars2[i]) {
1406 break;
1407 }
1408 }
1409
1410 return i;
1411 }
1412
1413 public static String strip(String s, char remove) {
1414 if (s == null) {
1415 return null;
1416 }
1417
1418 int x = s.indexOf(remove);
1419
1420 if (x < 0) {
1421 return s;
1422 }
1423
1424 int y = 0;
1425
1426 StringBuilder sb = new StringBuilder(s.length());
1427
1428 while (x >= 0) {
1429 sb.append(s.subSequence(y, x));
1430
1431 y = x + 1;
1432
1433 x = s.indexOf(remove, y);
1434 }
1435
1436 sb.append(s.substring(y));
1437
1438 return sb.toString();
1439 }
1440
1441 public static String stripBetween(String s, String begin, String end) {
1442 if ((s == null) || (begin == null) || (end == null)) {
1443 return s;
1444 }
1445
1446 StringBuilder sb = new StringBuilder(s.length());
1447
1448 int pos = 0;
1449
1450 while (true) {
1451 int x = s.indexOf(begin, pos);
1452 int y = s.indexOf(end, x + begin.length());
1453
1454 if ((x == -1) || (y == -1)) {
1455 sb.append(s.substring(pos, s.length()));
1456
1457 break;
1458 }
1459 else {
1460 sb.append(s.substring(pos, x));
1461
1462 pos = y + end.length();
1463 }
1464 }
1465
1466 return sb.toString();
1467 }
1468
1469 public static String toCharCode(String s) {
1470 StringBundler sb = new StringBundler(s.length());
1471
1472 for (int i = 0; i < s.length(); i++) {
1473 sb.append(s.codePointAt(i));
1474 }
1475
1476 return sb.toString();
1477 }
1478
1479 public static String toHexString(int i) {
1480 char[] buffer = new char[8];
1481
1482 int index = 8;
1483
1484 do {
1485 buffer[--index] = _HEX_DIGITS[i & 15];
1486
1487 i >>>= 4;
1488 }
1489 while (i != 0);
1490
1491 return new String(buffer, index, 8 - index);
1492 }
1493
1494 public static String toHexString(long l) {
1495 char[] buffer = new char[16];
1496
1497 int index = 16;
1498
1499 do {
1500 buffer[--index] = _HEX_DIGITS[(int) (l & 15)];
1501
1502 l >>>= 4;
1503 }
1504 while (l != 0);
1505
1506 return new String(buffer, index, 16 - index);
1507 }
1508
1509 public static String toHexString(Object obj) {
1510 if (obj instanceof Integer) {
1511 return toHexString(((Integer)obj).intValue());
1512 }
1513 else if (obj instanceof Long) {
1514 return toHexString(((Long)obj).longValue());
1515 }
1516 else {
1517 return String.valueOf(obj);
1518 }
1519 }
1520
1521 public static String trim(String s) {
1522 return trim(s, null);
1523 }
1524
1525 public static String trim(String s, char c) {
1526 return trim(s, new char[] {c});
1527 }
1528
1529 public static String trim(String s, char[] exceptions) {
1530 if (s == null) {
1531 return null;
1532 }
1533
1534 char[] chars = s.toCharArray();
1535
1536 int len = chars.length;
1537
1538 int x = 0;
1539 int y = chars.length;
1540
1541 for (int i = 0; i < len; i++) {
1542 char c = chars[i];
1543
1544 if (_isTrimable(c, exceptions)) {
1545 x = i + 1;
1546 }
1547 else {
1548 break;
1549 }
1550 }
1551
1552 for (int i = len - 1; i >= 0; i--) {
1553 char c = chars[i];
1554
1555 if (_isTrimable(c, exceptions)) {
1556 y = i;
1557 }
1558 else {
1559 break;
1560 }
1561 }
1562
1563 if ((x != 0) || (y != len)) {
1564 return s.substring(x, y);
1565 }
1566 else {
1567 return s;
1568 }
1569 }
1570
1571 public static String trimLeading(String s) {
1572 return trimLeading(s, null);
1573 }
1574
1575 public static String trimLeading(String s, char c) {
1576 return trimLeading(s, new char[] {c});
1577 }
1578
1579 public static String trimLeading(String s, char[] exceptions) {
1580 if (s == null) {
1581 return null;
1582 }
1583
1584 char[] chars = s.toCharArray();
1585
1586 int len = chars.length;
1587
1588 int x = 0;
1589 int y = chars.length;
1590
1591 for (int i = 0; i < len; i++) {
1592 char c = chars[i];
1593
1594 if (_isTrimable(c, exceptions)) {
1595 x = i + 1;
1596 }
1597 else {
1598 break;
1599 }
1600 }
1601
1602 if ((x != 0) || (y != len)) {
1603 return s.substring(x, y);
1604 }
1605 else {
1606 return s;
1607 }
1608 }
1609
1610 public static String trimTrailing(String s) {
1611 return trimTrailing(s, null);
1612 }
1613
1614 public static String trimTrailing(String s, char c) {
1615 return trimTrailing(s, new char[] {c});
1616 }
1617
1618 public static String trimTrailing(String s, char[] exceptions) {
1619 if (s == null) {
1620 return null;
1621 }
1622
1623 char[] chars = s.toCharArray();
1624
1625 int len = chars.length;
1626
1627 int x = 0;
1628 int y = chars.length;
1629
1630 for (int i = len - 1; i >= 0; i--) {
1631 char c = chars[i];
1632
1633 if (_isTrimable(c, exceptions)) {
1634 y = i;
1635 }
1636 else {
1637 break;
1638 }
1639 }
1640
1641 if ((x != 0) || (y != len)) {
1642 return s.substring(x, y);
1643 }
1644 else {
1645 return s;
1646 }
1647 }
1648
1649 public static String unquote(String s) {
1650 if (Validator.isNull(s)) {
1651 return s;
1652 }
1653
1654 if ((s.charAt(0) == CharPool.APOSTROPHE) &&
1655 (s.charAt(s.length() - 1) == CharPool.APOSTROPHE)) {
1656
1657 return s.substring(1, s.length() - 1);
1658 }
1659 else if ((s.charAt(0) == CharPool.QUOTE) &&
1660 (s.charAt(s.length() - 1) == CharPool.QUOTE)) {
1661
1662 return s.substring(1, s.length() - 1);
1663 }
1664
1665 return s;
1666 }
1667
1668 public static String upperCase(String s) {
1669 if (s == null) {
1670 return null;
1671 }
1672 else {
1673 return s.toUpperCase();
1674 }
1675 }
1676
1677 public static String upperCaseFirstLetter(String s) {
1678 char[] chars = s.toCharArray();
1679
1680 if ((chars[0] >= 97) && (chars[0] <= 122)) {
1681 chars[0] = (char)(chars[0] - 32);
1682 }
1683
1684 return new String(chars);
1685 }
1686
1687 public static String valueOf(Object obj) {
1688 return String.valueOf(obj);
1689 }
1690
1691 public static String wrap(String text) {
1692 return wrap(text, 80, StringPool.NEW_LINE);
1693 }
1694
1695 public static String wrap(String text, int width, String lineSeparator) {
1696 try {
1697 return _wrap(text, width, lineSeparator);
1698 }
1699 catch (IOException ioe) {
1700 _log.error(ioe.getMessage());
1701
1702 return text;
1703 }
1704 }
1705
1706 private static String _highlight(
1707 String s, Pattern pattern, String highlight1, String highlight2) {
1708
1709 StringTokenizer st = new StringTokenizer(s);
1710
1711 if (st.countTokens() == 0) {
1712 return StringPool.BLANK;
1713 }
1714
1715 StringBundler sb = new StringBundler(2 * st.countTokens() - 1);
1716
1717 while (st.hasMoreTokens()) {
1718 String token = st.nextToken();
1719
1720 Matcher matcher = pattern.matcher(token);
1721
1722 if (matcher.find()) {
1723 StringBuffer hightlighted = new StringBuffer();
1724
1725 do {
1726 matcher.appendReplacement(
1727 hightlighted, highlight1 + matcher.group() +
1728 highlight2);
1729 }
1730 while (matcher.find());
1731
1732 matcher.appendTail(hightlighted);
1733
1734 sb.append(hightlighted);
1735 }
1736 else {
1737 sb.append(token);
1738 }
1739
1740 if (st.hasMoreTokens()) {
1741 sb.append(StringPool.SPACE);
1742 }
1743 }
1744
1745 return sb.toString();
1746 }
1747
1748 private static boolean _isTrimable(char c, char[] exceptions) {
1749 if ((exceptions != null) && (exceptions.length > 0)) {
1750 for (char exception : exceptions) {
1751 if (c == exception) {
1752 return false;
1753 }
1754 }
1755 }
1756
1757 return Character.isWhitespace(c);
1758 }
1759
1760 private static String _wrap(String text, int width, String lineSeparator)
1761 throws IOException {
1762
1763 if (text == null) {
1764 return null;
1765 }
1766
1767 StringBundler sb = new StringBundler();
1768
1769 UnsyncBufferedReader unsyncBufferedReader = new UnsyncBufferedReader(
1770 new UnsyncStringReader(text));
1771
1772 String s = StringPool.BLANK;
1773
1774 while ((s = unsyncBufferedReader.readLine()) != null) {
1775 if (s.length() == 0) {
1776 sb.append(lineSeparator);
1777
1778 continue;
1779 }
1780
1781 int lineLength = 0;
1782
1783 String[] tokens = s.split(StringPool.SPACE);
1784
1785 for (String token : tokens) {
1786 if ((lineLength + token.length() + 1) > width) {
1787 if (lineLength > 0) {
1788 sb.append(lineSeparator);
1789 }
1790
1791 if (token.length() > width) {
1792 int pos = token.indexOf(CharPool.OPEN_PARENTHESIS);
1793
1794 if (pos != -1) {
1795 sb.append(token.substring(0, pos + 1));
1796 sb.append(lineSeparator);
1797
1798 token = token.substring(pos + 1);
1799
1800 sb.append(token);
1801
1802 lineLength = token.length();
1803 }
1804 else {
1805 sb.append(token);
1806
1807 lineLength = token.length();
1808 }
1809 }
1810 else {
1811 sb.append(token);
1812
1813 lineLength = token.length();
1814 }
1815 }
1816 else {
1817 if (lineLength > 0) {
1818 sb.append(StringPool.SPACE);
1819
1820 lineLength++;
1821 }
1822
1823 sb.append(token);
1824
1825 lineLength += token.length();
1826 }
1827 }
1828
1829 sb.append(lineSeparator);
1830 }
1831
1832 return sb.toString();
1833 }
1834
1835 private static final char[] _HEX_DIGITS = {
1836 '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
1837 'e', 'f'
1838 };
1839
1840 private static String[] _emptyStringArray = new String[0];
1841
1842 private static Log _log = LogFactoryUtil.getLog(StringUtil.class);
1843
1844 }