1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.qedeq.base.utility;
17
18 import java.io.BufferedReader;
19 import java.io.ByteArrayOutputStream;
20 import java.io.IOException;
21 import java.io.StringReader;
22 import java.io.UnsupportedEncodingException;
23 import java.util.ArrayList;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Locale;
27 import java.util.Map;
28 import java.util.Properties;
29 import java.util.Set;
30
31 import org.apache.commons.lang.ArrayUtils;
32 import org.apache.commons.lang.StringEscapeUtils;
33 import org.apache.commons.lang.SystemUtils;
34
35
36
37
38
39
40
41
42
43
44 public final class StringUtility {
45
46
47 static final String FORMATED_ZERO = "00000000000000000000";
48
49
50 static final String FORMATED_SPACES = " ";
51
52
53
54
55 private StringUtility() {
56
57 }
58
59
60
61
62
63
64
65
66
67
68 public static String replace(final String text,
69 final String search, final String replace) {
70
71 if (text == null) {
72 return "";
73 }
74 final int len = search != null ? search.length() : 0;
75 if (len == 0) {
76 return text;
77 }
78 final StringBuffer result = new StringBuffer();
79 int pos1 = 0;
80 int pos2;
81 while (0 <= (pos2 = text.indexOf(search, pos1))) {
82 result.append(text.substring(pos1, pos2));
83 if (replace != null) {
84 result.append(replace);
85 }
86 pos1 = pos2 + len;
87 }
88 if (pos1 < text.length()) {
89 result.append(text.substring(pos1));
90 }
91 return result.toString();
92 }
93
94
95
96
97
98
99
100
101
102
103 public static void replace(final StringBuffer text,
104 final String search, final String replace) {
105 if (search == null || search.length() <= 0) {
106 return;
107 }
108 final StringBuffer result = new StringBuffer(text.length() + 16);
109 int pos1 = 0;
110 int pos2;
111 final int len = search.length();
112 while (0 <= (pos2 = text.indexOf(search, pos1))) {
113 result.append(text.substring(pos1, pos2));
114 result.append(replace != null ? replace : "");
115 pos1 = pos2 + len;
116 }
117 if (pos1 < text.length()) {
118 result.append(text.substring(pos1));
119 }
120 text.setLength(0);
121 text.append(result);
122 }
123
124
125
126
127
128
129
130
131
132
133
134
135 public static String substring(final String text, final int position, final int length) {
136 final int start = Math.max(0, position);
137 int l = position + length - start;
138 if (l <= 0) {
139 return "";
140 }
141 int end = start + l;
142 if (end < text.length()) {
143 return text.substring(start, end);
144 }
145 return text.substring(start);
146 }
147
148
149
150
151
152
153
154
155 public static String toString(final Object[] list) {
156 final StringBuffer buffer = new StringBuffer(30);
157 buffer.append("(");
158 if (list != null) {
159 for (int i = 0; i < list.length; i++) {
160 if (i > 0) {
161 buffer.append(", ");
162 }
163 if (list[i] instanceof CharSequence) {
164 buffer.append("\"");
165 buffer.append(list[i].toString());
166 buffer.append("\"");
167 } else {
168 buffer.append(String.valueOf(list[i]));
169 }
170 }
171 }
172 buffer.append(")");
173 return buffer.toString();
174 }
175
176
177
178
179
180
181
182
183 public static String toString(final Set set) {
184 final StringBuffer buffer = new StringBuffer(30);
185 buffer.append("{");
186 if (set != null) {
187 Iterator e = set.iterator();
188 boolean notFirst = false;
189 while (e.hasNext()) {
190 if (notFirst) {
191 buffer.append(", ");
192 } else {
193 notFirst = true;
194 }
195 final Object obj = e.next();
196 if (obj instanceof CharSequence) {
197 buffer.append("\"");
198 buffer.append(String.valueOf(obj));
199 buffer.append("\"");
200 } else {
201 buffer.append(String.valueOf(obj));
202 }
203 }
204 }
205 buffer.append("}");
206 return buffer.toString();
207 }
208
209
210
211
212
213
214
215
216 public static String toString(final Map map) {
217 final StringBuffer buffer = new StringBuffer(30);
218 buffer.append("{");
219 if (map != null) {
220 Iterator e = map.entrySet().iterator();
221 boolean notFirst = false;
222 while (e.hasNext()) {
223 if (notFirst) {
224 buffer.append(", ");
225 } else {
226 notFirst = true;
227 }
228 final Map.Entry entry = (Map.Entry) e.next();
229 buffer.append(String.valueOf(entry.getKey()));
230 buffer.append("=");
231 final Object value = entry.getValue();
232 if (value instanceof CharSequence) {
233 buffer.append("\"");
234 buffer.append(String.valueOf(value));
235 buffer.append("\"");
236 } else {
237 buffer.append(String.valueOf(value));
238 }
239 }
240 }
241 buffer.append("}");
242 return buffer.toString();
243 }
244
245
246
247
248
249
250
251
252 public static String asLines(final Set set) {
253 final StringBuffer buffer = new StringBuffer(30);
254 if (set != null) {
255 Iterator e = set.iterator();
256 boolean notFirst = false;
257 while (e.hasNext()) {
258 if (notFirst) {
259 buffer.append("\n");
260 } else {
261 notFirst = true;
262 }
263 buffer.append(String.valueOf(e.next()));
264 }
265 }
266 return buffer.toString();
267 }
268
269
270
271
272
273
274
275
276
277
278
279 public static String quote(final String unquoted) {
280 StringBuffer result = new StringBuffer(unquoted.length() + 4);
281 result.append('\"');
282
283 for (int i = 0; i < unquoted.length(); i++) {
284 if (unquoted.charAt(i) == '\"') {
285 result.append("\"\"");
286 } else {
287 result.append(unquoted.charAt(i));
288 }
289 }
290 result.append('\"');
291 return result.toString();
292 }
293
294
295
296
297
298
299
300
301
302
303 public static boolean isLetterDigitString(final String text) {
304 if (text.length() <= 0) {
305 return false;
306 }
307 if (!Character.isLetter(text.charAt(0))) {
308 return false;
309 }
310 for (int i = 1; i < text.length(); i++) {
311 if (!Character.isLetterOrDigit(text.charAt(i))) {
312 return false;
313 }
314 }
315 return true;
316 }
317
318
319
320
321
322
323
324 public static StringBuffer getSpaces(final int length) {
325 final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0);
326 for (int i = 0; i < length; i++) {
327 buffer.append(' ');
328 }
329 return buffer;
330 }
331
332
333
334
335
336
337
338 public static String getLastDotString(final String full) {
339 if (full == null) {
340 return "";
341 }
342 final int p = full.lastIndexOf('.');
343 if (p < 0) {
344 return full;
345 }
346 return full.substring(p + 1);
347 }
348
349
350
351
352
353
354
355 public static String getLastTwoDotStrings(final String full) {
356 if (full == null) {
357 return "";
358 }
359 final int p = full.lastIndexOf('.');
360 if (p < 1) {
361 return full;
362 }
363 final int q = full.lastIndexOf('.', p - 1);
364 if (q < 0) {
365 return full;
366 }
367 return full.substring(q + 1);
368 }
369
370
371
372
373
374
375
376 public static String getClassName(final Class clazz) {
377 return getLastDotString(clazz.getName());
378 }
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401 public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {
402 int current = 0;
403 int lastLf = -1;
404
405
406 while (current < buffer.length()) {
407 if (!Character.isWhitespace(buffer.charAt(current))) {
408 break;
409 }
410 if ('\n' == buffer.charAt(current)) {
411 lastLf = current;
412 }
413 current++;
414 }
415
416 final String empty = buffer.substring(lastLf + 1, current);
417
418
419 if (empty.length() > 0) {
420
421 buffer.delete(lastLf + 1 , current);
422 replace(buffer, "\n" + empty, "\n");
423 }
424 }
425
426
427
428
429
430
431
432 public static String escapeProperty(final String value) {
433 Properties newprops = new Properties();
434 newprops.put("key", value);
435 ByteArrayOutputStream out = new ByteArrayOutputStream();
436 try {
437 newprops.store(out, null);
438 } catch (IOException e) {
439
440 throw new RuntimeException(e);
441 }
442 try {
443 final String file = out.toString("ISO-8859-1");
444 return file.substring(file.indexOf('\n') + 1 + "key=".length()).trim();
445 } catch (UnsupportedEncodingException e) {
446
447 throw new RuntimeException(e);
448 }
449 }
450
451
452
453
454
455
456
457
458
459
460 public static final String alignRight(final long number, final int length) {
461 return alignRight("" + number, length);
462 }
463
464
465
466
467
468
469
470
471 public static final String alignRight(final String string, final int length) {
472 if (length > FORMATED_SPACES.length()) {
473 throw new IllegalArgumentException("maximum length " + FORMATED_SPACES + " exceeded: "
474 + length);
475 }
476 if (length < 1) {
477 throw new IllegalArgumentException("length must be bigger than 0: " + length);
478 }
479 final String temp = FORMATED_SPACES + string;
480 return temp.substring(Math.min(temp.length() - length, FORMATED_SPACES.length()));
481 }
482
483
484
485
486
487
488
489
490 public static final String format(final long number, final int length) {
491 if (length > FORMATED_ZERO.length()) {
492 throw new IllegalArgumentException("maximum length " + FORMATED_ZERO + " exceeded: "
493 + length);
494 }
495 if (length < 1) {
496 throw new IllegalArgumentException("length must be bigger than 0: " + length);
497 }
498 final String temp = FORMATED_ZERO + number;
499 return temp.substring(Math.min(temp.length() - length, FORMATED_ZERO.length()));
500 }
501
502
503
504
505
506
507
508 public static String byte2Hex(final byte[] data) {
509
510 StringBuffer buffer = new StringBuffer();
511 for (int i = 0; i < data.length; i++) {
512 String b = Integer.toHexString(255 & data[i]);
513 if (i != 0) {
514 if (0 != i % 16) {
515 buffer.append(" ");
516 } else {
517 buffer.append("\n");
518 }
519 }
520 if (b.length() < 2) {
521 buffer.append("0");
522 }
523 buffer.append(b.toUpperCase(Locale.US));
524 }
525 return buffer.toString();
526 }
527
528
529
530
531
532
533
534 public static String string2Hex(final String data) {
535 try {
536 return byte2Hex(data.getBytes("UTF8"));
537 } catch (UnsupportedEncodingException e) {
538
539 throw new RuntimeException(e);
540 }
541 }
542
543
544
545
546
547
548
549
550
551 public static String string2Hex(final String data, final String encoding)
552 throws UnsupportedEncodingException {
553 return byte2Hex(data.getBytes(encoding));
554 }
555
556
557
558
559
560
561
562
563 public static byte[] hex2byte(final String hex) {
564
565 StringBuffer buffer = new StringBuffer(hex.length());
566 char c;
567 for (int i = 0; i < hex.length(); i++) {
568 c = hex.charAt(i);
569 if (!Character.isWhitespace(c)) {
570 if (!Character.isLetterOrDigit(c)) {
571 throw new IllegalArgumentException("Illegal hex char");
572 }
573 buffer.append(c);
574 }
575 }
576 if (buffer.length() % 2 != 0) {
577 throw new IllegalArgumentException("Bad padding");
578 }
579 byte[] result = new byte[buffer.length() / 2];
580 for (int i = 0; i < buffer.length() / 2; i++) {
581 try {
582 result[i] = (byte) Integer.parseInt(buffer.substring(2 * i, 2 * i + 2), 16);
583 } catch (Exception e) {
584 throw new IllegalArgumentException("Illegal hex char");
585 }
586 }
587 return result;
588 }
589
590
591
592
593
594
595
596
597 public static String hex2String(final String hex) {
598 try {
599 return new String(hex2byte(hex), "UTF8");
600 } catch (UnsupportedEncodingException e) {
601
602 throw new RuntimeException(e);
603 }
604 }
605
606
607
608
609
610
611
612
613
614
615 public static String hex2String(final String hex, final String encoding)
616 throws UnsupportedEncodingException {
617 return new String(hex2byte(hex), encoding);
618 }
619
620
621
622
623
624
625 public static String getSystemLineSeparator() {
626 return (SystemUtils.LINE_SEPARATOR != null ? SystemUtils.LINE_SEPARATOR
627 : "\n");
628 }
629
630
631
632
633
634
635
636
637
638 public static String useSystemLineSeparator(final String text) {
639 if (text == null) {
640 return null;
641 }
642 final StringBuffer buffer = new StringBuffer(text.length());
643 final BufferedReader reader = new BufferedReader(new StringReader(text));
644 final String separator = getSystemLineSeparator();
645 String line;
646 try {
647 while (null != (line = reader.readLine())) {
648 buffer.append(line);
649 buffer.append(separator);
650 }
651 } catch (IOException e) {
652 throw new RuntimeException(e);
653 }
654 return buffer.toString();
655 }
656
657
658
659
660
661
662
663
664
665
666 public static String[] split(final String text, final String delimiter) {
667 final List list = new ArrayList();
668 int start = 0;
669 int found = -delimiter.length();
670 while (-1 < (found = text.indexOf(delimiter, start))) {
671 list.add(text.substring(start, found));
672 start = found + delimiter.length();
673 }
674 list.add(text.substring(start));
675 return (String[]) list.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
676 }
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695 public static String escapeXml(final String value) {
696 return StringEscapeUtils.escapeXml(value);
697 }
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714 public static String unescapeXml(final String value) {
715 return StringEscapeUtils.unescapeXml(value);
716 }
717
718
719
720
721
722
723
724
725 public static boolean isNotIn(final String lookFor, final String[] array) {
726 if (lookFor == null || lookFor.length() <= 0) {
727 return false;
728 }
729 for (int i = 0; i < array.length; i++) {
730 if (lookFor.equals(array[i])) {
731 return false;
732 }
733 }
734 return true;
735 }
736
737
738
739
740
741
742
743
744 public static boolean isIn(final String lookFor, final String[] array) {
745 return !isNotIn(lookFor, array);
746 }
747
748
749 }