-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringHelper.java
1599 lines (1538 loc) · 53.9 KB
/
StringHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.gootrip.util;
/*
* Static String formatting and query routines.
* Copyright (C) 2001-2005 Stephen Ostermiller
* http://ostermiller.org/contact.pl?regarding=Java+Utilities
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* See COPYING.TXT for details.
*/
import java.util.HashMap;
import java.util.regex.Pattern;
/**
* Utilities for String formatting, manipulation, and queries.
* More information about this class is available from <a target="_top" href=
* "http://ostermiller.org/utils/StringHelper.html">ostermiller.org</a>.
*
* @author Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities
* @since ostermillerutils 1.00.00
*/
public class StringHelper {
/**
* Pad the beginning of the given String with spaces until
* the String is of the given length.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String prepad(String s, int length){
return prepad(s, length, ' ');
}
/**
* Pre-pend the given character to the String until
* the result is the desired length.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @param c padding character.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String prepad(String s, int length, char c){
int needed = length - s.length();
if (needed <= 0){
return s;
}
char padding[] = new char[needed];
java.util.Arrays.fill(padding, c);
StringBuffer sb = new StringBuffer(length);
sb.append(padding);
sb.append(s);
return sb.toString();
}
/**
* Pad the end of the given String with spaces until
* the String is of the given length.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String postpad(String s, int length){
return postpad(s, length, ' ');
}
/**
* Append the given character to the String until
* the result is the desired length.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @param c padding character.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String postpad(String s, int length, char c){
int needed = length - s.length();
if (needed <= 0){
return s;
}
char padding[] = new char[needed];
java.util.Arrays.fill(padding, c);
StringBuffer sb = new StringBuffer(length);
sb.append(s);
sb.append(padding);
return sb.toString();
}
/**
* Pad the beginning and end of the given String with spaces until
* the String is of the given length. The result is that the original
* String is centered in the middle of the new string.
* <p>
* If the number of characters to pad is even, then the padding
* will be split evenly between the beginning and end, otherwise,
* the extra character will be added to the end.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String midpad(String s, int length){
return midpad(s, length, ' ');
}
/**
* Pad the beginning and end of the given String with the given character
* until the result is the desired length. The result is that the original
* String is centered in the middle of the new string.
* <p>
* If the number of characters to pad is even, then the padding
* will be split evenly between the beginning and end, otherwise,
* the extra character will be added to the end.
* <p>
* If a String is longer than the desired length,
* it will not be truncated, however no padding
* will be added.
*
* @param s String to be padded.
* @param length desired length of result.
* @param c padding character.
* @return padded String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String midpad(String s, int length, char c){
int needed = length - s.length();
if (needed <= 0){
return s;
}
int beginning = needed / 2;
int end = beginning + needed % 2;
char prepadding[] = new char[beginning];
java.util.Arrays.fill(prepadding, c);
char postpadding[] = new char[end];
java.util.Arrays.fill(postpadding, c);
StringBuffer sb = new StringBuffer(length);
sb.append(prepadding);
sb.append(s);
sb.append(postpadding);
return sb.toString();
}
/**
* Split the given String into tokens.
* <P>
* This method is meant to be similar to the split
* function in other programming languages but it does
* not use regular expressions. Rather the String is
* split on a single String literal.
* <P>
* Unlike java.util.StringTokenizer which accepts
* multiple character tokens as delimiters, the delimiter
* here is a single String literal.
* <P>
* Each null token is returned as an empty String.
* Delimiters are never returned as tokens.
* <P>
* If there is no delimiter because it is either empty or
* null, the only element in the result is the original String.
* <P>
* StringHelper.split("1-2-3", "-");<br>
* result: {"1","2","3"}<br>
* StringHelper.split("-1--2-", "-");<br>
* result: {"","1","","2",""}<br>
* StringHelper.split("123", "");<br>
* result: {"123"}<br>
* StringHelper.split("1-2---3----4", "--");<br>
* result: {"1-2","-3","","4"}<br>
*
* @param s String to be split.
* @param delimiter String literal on which to split.
* @return an array of tokens.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String[] split(String s, String delimiter){
int delimiterLength;
// the next statement has the side effect of throwing a null pointer
// exception if s is null.
int stringLength = s.length();
if (delimiter == null || (delimiterLength = delimiter.length()) == 0){
// it is not inherently clear what to do if there is no delimiter
// On one hand it would make sense to return each character because
// the null String can be found between each pair of characters in
// a String. However, it can be found many times there and we don'
// want to be returning multiple null tokens.
// returning the whole String will be defined as the correct behavior
// in this instance.
return new String[] {s};
}
// a two pass solution is used because a one pass solution would
// require the possible resizing and copying of memory structures
// In the worst case it would have to be resized n times with each
// resize having a O(n) copy leading to an O(n^2) algorithm.
int count;
int start;
int end;
// Scan s and count the tokens.
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1){
count++;
start = end + delimiterLength;
}
count++;
// allocate an array to return the tokens,
// we now know how big it should be
String[] result = new String[count];
// Scan s again, but this time pick out the tokens
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1){
result[count] = (s.substring(start, end));
count++;
start = end + delimiterLength;
}
end = stringLength;
result[count] = s.substring(start, end);
return (result);
}
/**
* Split the given String into tokens. Delimiters will
* be returned as tokens.
* <P>
* This method is meant to be similar to the split
* function in other programming languages but it does
* not use regular expressions. Rather the String is
* split on a single String literal.
* <P>
* Unlike java.util.StringTokenizer which accepts
* multiple character tokens as delimiters, the delimiter
* here is a single String literal.
* <P>
* Each null token is returned as an empty String.
* Delimiters are never returned as tokens.
* <P>
* If there is no delimiter because it is either empty or
* null, the only element in the result is the original String.
* <P>
* StringHelper.split("1-2-3", "-");<br>
* result: {"1","-","2","-","3"}<br>
* StringHelper.split("-1--2-", "-");<br>
* result: {"","-","1","-","","-","2","-",""}<br>
* StringHelper.split("123", "");<br>
* result: {"123"}<br>
* StringHelper.split("1-2--3---4----5", "--");<br>
* result: {"1-2","--","3","--","-4","--","","--","5"}<br>
*
* @param s String to be split.
* @param delimiter String literal on which to split.
* @return an array of tokens.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.05.00
*/
public static String[] splitIncludeDelimiters(String s, String delimiter){
int delimiterLength;
// the next statement has the side effect of throwing a null pointer
// exception if s is null.
int stringLength = s.length();
if (delimiter == null || (delimiterLength = delimiter.length()) == 0){
// it is not inherently clear what to do if there is no delimiter
// On one hand it would make sense to return each character because
// the null String can be found between each pair of characters in
// a String. However, it can be found many times there and we don'
// want to be returning multiple null tokens.
// returning the whole String will be defined as the correct behavior
// in this instance.
return new String[] {s};
}
// a two pass solution is used because a one pass solution would
// require the possible resizing and copying of memory structures
// In the worst case it would have to be resized n times with each
// resize having a O(n) copy leading to an O(n^2) algorithm.
int count;
int start;
int end;
// Scan s and count the tokens.
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1){
count+=2;
start = end + delimiterLength;
}
count++;
// allocate an array to return the tokens,
// we now know how big it should be
String[] result = new String[count];
// Scan s again, but this time pick out the tokens
count = 0;
start = 0;
while((end = s.indexOf(delimiter, start)) != -1){
result[count] = (s.substring(start, end));
count++;
result[count] = delimiter;
count++;
start = end + delimiterLength;
}
end = stringLength;
result[count] = s.substring(start, end);
return (result);
}
/**
* Join all the elements of a string array into a single
* String.
* <p>
* If the given array empty an empty string
* will be returned. Null elements of the array are allowed
* and will be treated like empty Strings.
*
* @param array Array to be joined into a string.
* @return Concatenation of all the elements of the given array.
* @throws NullPointerException if array is null.
*
* @since ostermillerutils 1.05.00
*/
public static String join(String[] array){
return join(array, "");
}
/**
* Join all the elements of a string array into a single
* String.
* <p>
* If the given array empty an empty string
* will be returned. Null elements of the array are allowed
* and will be treated like empty Strings.
*
* @param array Array to be joined into a string.
* @param delimiter String to place between array elements.
* @return Concatenation of all the elements of the given array with the the delimiter in between.
* @throws NullPointerException if array or delimiter is null.
*
* @since ostermillerutils 1.05.00
*/
public static String join(String[] array, String delimiter){
// Cache the length of the delimiter
// has the side effect of throwing a NullPointerException if
// the delimiter is null.
int delimiterLength = delimiter.length();
// Nothing in the array return empty string
// has the side effect of throwing a NullPointerException if
// the array is null.
if (array.length == 0) return "";
// Only one thing in the array, return it.
if (array.length == 1){
if (array[0] == null) return "";
return array[0];
}
// Make a pass through and determine the size
// of the resulting string.
int length = 0;
for (int i=0; i<array.length; i++){
if (array[i] != null) length+=array[i].length();
if (i<array.length-1) length+=delimiterLength;
}
// Make a second pass through and concatenate everything
// into a string buffer.
StringBuffer result = new StringBuffer(length);
for (int i=0; i<array.length; i++){
if (array[i] != null) result.append(array[i]);
if (i<array.length-1) result.append(delimiter);
}
return result.toString();
}
/**
* Replace occurrences of a substring.
*
* StringHelper.replace("1-2-3", "-", "|");<br>
* result: "1|2|3"<br>
* StringHelper.replace("-1--2-", "-", "|");<br>
* result: "|1||2|"<br>
* StringHelper.replace("123", "", "|");<br>
* result: "123"<br>
* StringHelper.replace("1-2---3----4", "--", "|");<br>
* result: "1-2|-3||4"<br>
* StringHelper.replace("1-2---3----4", "--", "---");<br>
* result: "1-2----3------4"<br>
*
* @param s String to be modified.
* @param find String to find.
* @param replace String to replace.
* @return a string with all the occurrences of the string to find replaced.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String replace(String s, String find, String replace){
int findLength;
// the next statement has the side effect of throwing a null pointer
// exception if s is null.
int stringLength = s.length();
if (find == null || (findLength = find.length()) == 0){
// If there is nothing to find, we won't try and find it.
return s;
}
if (replace == null){
// a null string and an empty string are the same
// for replacement purposes.
replace = "";
}
int replaceLength = replace.length();
// We need to figure out how long our resulting string will be.
// This is required because without it, the possible resizing
// and copying of memory structures could lead to an unacceptable runtime.
// In the worst case it would have to be resized n times with each
// resize having a O(n) copy leading to an O(n^2) algorithm.
int length;
if (findLength == replaceLength){
// special case in which we don't need to count the replacements
// because the count falls out of the length formula.
length = stringLength;
} else {
int count;
int start;
int end;
// Scan s and count the number of times we find our target.
count = 0;
start = 0;
while((end = s.indexOf(find, start)) != -1){
count++;
start = end + findLength;
}
if (count == 0){
// special case in which on first pass, we find there is nothing
// to be replaced. No need to do a second pass or create a string buffer.
return s;
}
length = stringLength - (count * (findLength - replaceLength));
}
int start = 0;
int end = s.indexOf(find, start);
if (end == -1){
// nothing was found in the string to replace.
// we can get this if the find and replace strings
// are the same length because we didn't check before.
// in this case, we will return the original string
return s;
}
// it looks like we actually have something to replace
// *sigh* allocate memory for it.
StringBuffer sb = new StringBuffer(length);
// Scan s and do the replacements
while (end != -1){
sb.append(s.substring(start, end));
sb.append(replace);
start = end + findLength;
end = s.indexOf(find, start);
}
end = stringLength;
sb.append(s.substring(start, end));
return (sb.toString());
}
/**
* Replaces characters that may be confused by a HTML
* parser with their equivalent character entity references.
* <p>
* Any data that will appear as text on a web page should
* be be escaped. This is especially important for data
* that comes from untrusted sources such as Internet users.
* A common mistake in CGI programming is to ask a user for
* data and then put that data on a web page. For example:<pre>
* Server: What is your name?
* User: <b>Joe<b>
* Server: Hello <b>Joe</b>, Welcome</pre>
* If the name is put on the page without checking that it doesn't
* contain HTML code or without sanitizing that HTML code, the user
* could reformat the page, insert scripts, and control the the
* content on your web server.
* <p>
* This method will replace HTML characters such as > with their
* HTML entity reference (&gt;) so that the html parser will
* be sure to interpret them as plain text rather than HTML or script.
* <p>
* This method should be used for both data to be displayed in text
* in the html document, and data put in form elements. For example:<br>
* <code><html><body><i>This in not a &lt;tag&gt;
* in HTML</i></body></html></code><br>
* and<br>
* <code><form><input type="hidden" name="date" value="<i>This data could
* be &quot;malicious&quot;</i>"></form></code><br>
* In the second example, the form data would be properly be resubmitted
* to your cgi script in the URLEncoded format:<br>
* <code><i>This data could be %22malicious%22</i></code>
*
* @param s String to be escaped
* @return escaped String
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String escapeHTML(String s){
int length = s.length();
int newLength = length;
boolean someCharacterEscaped = false;
// first check for characters that might
// be dangerous and calculate a length
// of the string that has escapes.
for (int i=0; i<length; i++){
char c = s.charAt(i);
int cint = 0xffff & c;
if (cint < 32){
switch(c){
case '\r':
case '\n':
case '\t':
case '\f':{
} break;
default: {
newLength -= 1;
someCharacterEscaped = true;
}
}
} else {
switch(c){
case '\"':{
newLength += 5;
someCharacterEscaped = true;
} break;
case '&':
case '\'':{
newLength += 4;
someCharacterEscaped = true;
} break;
case '<':
case '>':{
newLength += 3;
someCharacterEscaped = true;
} break;
}
}
}
if (!someCharacterEscaped){
// nothing to escape in the string
return s;
}
StringBuffer sb = new StringBuffer(newLength);
for (int i=0; i<length; i++){
char c = s.charAt(i);
int cint = 0xffff & c;
if (cint < 32){
switch(c){
case '\r':
case '\n':
case '\t':
case '\f':{
sb.append(c);
} break;
default: {
// Remove this character
}
}
} else {
switch(c){
case '\"':{
sb.append(""");
} break;
case '\'':{
sb.append("'");
} break;
case '&':{
sb.append("&");
} break;
case '<':{
sb.append("<");
} break;
case '>':{
sb.append(">");
} break;
default: {
sb.append(c);
}
}
}
}
return sb.toString();
}
/**
* Replaces characters that may be confused by an SQL
* parser with their equivalent escape characters.
* <p>
* Any data that will be put in an SQL query should
* be be escaped. This is especially important for data
* that comes from untrusted sources such as Internet users.
* <p>
* For example if you had the following SQL query:<br>
* <code>"SELECT * FROM addresses WHERE name='" + name + "' AND private='N'"</code><br>
* Without this function a user could give <code>" OR 1=1 OR ''='"</code>
* as their name causing the query to be:<br>
* <code>"SELECT * FROM addresses WHERE name='' OR 1=1 OR ''='' AND private='N'"</code><br>
* which will give all addresses, including private ones.<br>
* Correct usage would be:<br>
* <code>"SELECT * FROM addresses WHERE name='" + StringHelper.escapeSQL(name) + "' AND private='N'"</code><br>
* <p>
* Another way to avoid this problem is to use a PreparedStatement
* with appropriate placeholders.
*
* @param s String to be escaped
* @return escaped String
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String escapeSQL(String s){
int length = s.length();
int newLength = length;
// first check for characters that might
// be dangerous and calculate a length
// of the string that has escapes.
for (int i=0; i<length; i++){
char c = s.charAt(i);
switch(c){
case '\\':
case '\"':
case '\'':
case '\0':{
newLength += 1;
} break;
}
}
if (length == newLength){
// nothing to escape in the string
return s;
}
StringBuffer sb = new StringBuffer(newLength);
for (int i=0; i<length; i++){
char c = s.charAt(i);
switch(c){
case '\\':{
sb.append("\\\\");
} break;
case '\"':{
sb.append("\\\"");
} break;
case '\'':{
sb.append("\\\'");
} break;
case '\0':{
sb.append("\\0");
} break;
default: {
sb.append(c);
}
}
}
return sb.toString();
}
/**
* Replaces characters that are not allowed in a Java style
* string literal with their escape characters. Specifically
* quote ("), single quote ('), new line (\n), carriage return (\r),
* and backslash (\), and tab (\t) are escaped.
*
* @param s String to be escaped
* @return escaped String
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String escapeJavaLiteral(String s){
int length = s.length();
int newLength = length;
// first check for characters that might
// be dangerous and calculate a length
// of the string that has escapes.
for (int i=0; i<length; i++){
char c = s.charAt(i);
switch(c){
case '\"':
case '\'':
case '\n':
case '\r':
case '\t':
case '\\':{
newLength += 1;
} break;
}
}
if (length == newLength){
// nothing to escape in the string
return s;
}
StringBuffer sb = new StringBuffer(newLength);
for (int i=0; i<length; i++){
char c = s.charAt(i);
switch(c){
case '\"':{
sb.append("\\\"");
} break;
case '\'':{
sb.append("\\\'");
} break;
case '\n':{
sb.append("\\n");
} break;
case '\r':{
sb.append("\\r");
} break;
case '\t':{
sb.append("\\t");
} break;
case '\\':{
sb.append("\\\\");
} break;
default: {
sb.append(c);
}
}
}
return sb.toString();
}
/**
* Trim any of the characters contained in the second
* string from the beginning and end of the first.
*
* @param s String to be trimmed.
* @param c list of characters to trim from s.
* @return trimmed String.
* @throws NullPointerException if s is null.
*
* @since ostermillerutils 1.00.00
*/
public static String trim(String s, String c){
int length = s.length();
if (c == null){
return s;
}
int cLength = c.length();
if (c.length() == 0){
return s;
}
int start = 0;
int end = length;
boolean found; // trim-able character found.
int i;
// Start from the beginning and find the
// first non-trim-able character.
found = false;
for (i=0; !found && i<length; i++){
char ch = s.charAt(i);
found = true;
for (int j=0; found && j<cLength; j++){
if (c.charAt(j) == ch) found = false;
}
}
// if all characters are trim-able.
if (!found) return "";
start = i-1;
// Start from the end and find the
// last non-trim-able character.
found = false;
for (i=length-1; !found && i>=0; i--){
char ch = s.charAt(i);
found = true;
for (int j=0; found && j<cLength; j++){
if (c.charAt(j) == ch) found = false;
}
}
end = i+2;
return s.substring(start, end);
}
private static HashMap<String,Integer> htmlEntities = new HashMap<String,Integer>();
static {
htmlEntities.put("nbsp", new Integer(160));
htmlEntities.put("iexcl", new Integer(161));
htmlEntities.put("cent", new Integer(162));
htmlEntities.put("pound", new Integer(163));
htmlEntities.put("curren", new Integer(164));
htmlEntities.put("yen", new Integer(165));
htmlEntities.put("brvbar", new Integer(166));
htmlEntities.put("sect", new Integer(167));
htmlEntities.put("uml", new Integer(168));
htmlEntities.put("copy", new Integer(169));
htmlEntities.put("ordf", new Integer(170));
htmlEntities.put("laquo", new Integer(171));
htmlEntities.put("not", new Integer(172));
htmlEntities.put("shy", new Integer(173));
htmlEntities.put("reg", new Integer(174));
htmlEntities.put("macr", new Integer(175));
htmlEntities.put("deg", new Integer(176));
htmlEntities.put("plusmn", new Integer(177));
htmlEntities.put("sup2", new Integer(178));
htmlEntities.put("sup3", new Integer(179));
htmlEntities.put("acute", new Integer(180));
htmlEntities.put("micro", new Integer(181));
htmlEntities.put("para", new Integer(182));
htmlEntities.put("middot", new Integer(183));
htmlEntities.put("cedil", new Integer(184));
htmlEntities.put("sup1", new Integer(185));
htmlEntities.put("ordm", new Integer(186));
htmlEntities.put("raquo", new Integer(187));
htmlEntities.put("frac14", new Integer(188));
htmlEntities.put("frac12", new Integer(189));
htmlEntities.put("frac34", new Integer(190));
htmlEntities.put("iquest", new Integer(191));
htmlEntities.put("Agrave", new Integer(192));
htmlEntities.put("Aacute", new Integer(193));
htmlEntities.put("Acirc", new Integer(194));
htmlEntities.put("Atilde", new Integer(195));
htmlEntities.put("Auml", new Integer(196));
htmlEntities.put("Aring", new Integer(197));
htmlEntities.put("AElig", new Integer(198));
htmlEntities.put("Ccedil", new Integer(199));
htmlEntities.put("Egrave", new Integer(200));
htmlEntities.put("Eacute", new Integer(201));
htmlEntities.put("Ecirc", new Integer(202));
htmlEntities.put("Euml", new Integer(203));
htmlEntities.put("Igrave", new Integer(204));
htmlEntities.put("Iacute", new Integer(205));
htmlEntities.put("Icirc", new Integer(206));
htmlEntities.put("Iuml", new Integer(207));
htmlEntities.put("ETH", new Integer(208));
htmlEntities.put("Ntilde", new Integer(209));
htmlEntities.put("Ograve", new Integer(210));
htmlEntities.put("Oacute", new Integer(211));
htmlEntities.put("Ocirc", new Integer(212));
htmlEntities.put("Otilde", new Integer(213));
htmlEntities.put("Ouml", new Integer(214));
htmlEntities.put("times", new Integer(215));
htmlEntities.put("Oslash", new Integer(216));
htmlEntities.put("Ugrave", new Integer(217));
htmlEntities.put("Uacute", new Integer(218));
htmlEntities.put("Ucirc", new Integer(219));
htmlEntities.put("Uuml", new Integer(220));
htmlEntities.put("Yacute", new Integer(221));
htmlEntities.put("THORN", new Integer(222));
htmlEntities.put("szlig", new Integer(223));
htmlEntities.put("agrave", new Integer(224));
htmlEntities.put("aacute", new Integer(225));
htmlEntities.put("acirc", new Integer(226));
htmlEntities.put("atilde", new Integer(227));
htmlEntities.put("auml", new Integer(228));
htmlEntities.put("aring", new Integer(229));
htmlEntities.put("aelig", new Integer(230));
htmlEntities.put("ccedil", new Integer(231));
htmlEntities.put("egrave", new Integer(232));
htmlEntities.put("eacute", new Integer(233));
htmlEntities.put("ecirc", new Integer(234));
htmlEntities.put("euml", new Integer(235));
htmlEntities.put("igrave", new Integer(236));
htmlEntities.put("iacute", new Integer(237));
htmlEntities.put("icirc", new Integer(238));
htmlEntities.put("iuml", new Integer(239));
htmlEntities.put("eth", new Integer(240));
htmlEntities.put("ntilde", new Integer(241));
htmlEntities.put("ograve", new Integer(242));
htmlEntities.put("oacute", new Integer(243));
htmlEntities.put("ocirc", new Integer(244));
htmlEntities.put("otilde", new Integer(245));
htmlEntities.put("ouml", new Integer(246));
htmlEntities.put("divide", new Integer(247));
htmlEntities.put("oslash", new Integer(248));
htmlEntities.put("ugrave", new Integer(249));
htmlEntities.put("uacute", new Integer(250));
htmlEntities.put("ucirc", new Integer(251));
htmlEntities.put("uuml", new Integer(252));
htmlEntities.put("yacute", new Integer(253));
htmlEntities.put("thorn", new Integer(254));
htmlEntities.put("yuml", new Integer(255));
htmlEntities.put("fnof", new Integer(402));
htmlEntities.put("Alpha", new Integer(913));
htmlEntities.put("Beta", new Integer(914));
htmlEntities.put("Gamma", new Integer(915));
htmlEntities.put("Delta", new Integer(916));
htmlEntities.put("Epsilon", new Integer(917));
htmlEntities.put("Zeta", new Integer(918));
htmlEntities.put("Eta", new Integer(919));
htmlEntities.put("Theta", new Integer(920));
htmlEntities.put("Iota", new Integer(921));
htmlEntities.put("Kappa", new Integer(922));
htmlEntities.put("Lambda", new Integer(923));
htmlEntities.put("Mu", new Integer(924));
htmlEntities.put("Nu", new Integer(925));
htmlEntities.put("Xi", new Integer(926));
htmlEntities.put("Omicron", new Integer(927));
htmlEntities.put("Pi", new Integer(928));
htmlEntities.put("Rho", new Integer(929));
htmlEntities.put("Sigma", new Integer(931));
htmlEntities.put("Tau", new Integer(932));
htmlEntities.put("Upsilon", new Integer(933));
htmlEntities.put("Phi", new Integer(934));
htmlEntities.put("Chi", new Integer(935));
htmlEntities.put("Psi", new Integer(936));
htmlEntities.put("Omega", new Integer(937));
htmlEntities.put("alpha", new Integer(945));
htmlEntities.put("beta", new Integer(946));
htmlEntities.put("gamma", new Integer(947));
htmlEntities.put("delta", new Integer(948));
htmlEntities.put("epsilon", new Integer(949));
htmlEntities.put("zeta", new Integer(950));
htmlEntities.put("eta", new Integer(951));
htmlEntities.put("theta", new Integer(952));
htmlEntities.put("iota", new Integer(953));
htmlEntities.put("kappa", new Integer(954));
htmlEntities.put("lambda", new Integer(955));
htmlEntities.put("mu", new Integer(956));
htmlEntities.put("nu", new Integer(957));
htmlEntities.put("xi", new Integer(958));
htmlEntities.put("omicron", new Integer(959));
htmlEntities.put("pi", new Integer(960));
htmlEntities.put("rho", new Integer(961));
htmlEntities.put("sigmaf", new Integer(962));
htmlEntities.put("sigma", new Integer(963));
htmlEntities.put("tau", new Integer(964));
htmlEntities.put("upsilon", new Integer(965));
htmlEntities.put("phi", new Integer(966));
htmlEntities.put("chi", new Integer(967));
htmlEntities.put("psi", new Integer(968));
htmlEntities.put("omega", new Integer(969));
htmlEntities.put("thetasym", new Integer(977));
htmlEntities.put("upsih", new Integer(978));
htmlEntities.put("piv", new Integer(982));
htmlEntities.put("bull", new Integer(8226));
htmlEntities.put("hellip", new Integer(8230));
htmlEntities.put("prime", new Integer(8242));
htmlEntities.put("Prime", new Integer(8243));
htmlEntities.put("oline", new Integer(8254));
htmlEntities.put("frasl", new Integer(8260));
htmlEntities.put("weierp", new Integer(8472));