1 package com.zmicer.utils;
2
3 import org.apache.log4j.Logger;
4
5 import java.io.PrintWriter;
6 import java.io.StringWriter;
7 import java.util.ArrayList;
8 import java.util.List;
9 import java.util.StringTokenizer;
10
11 /**
12 * This class contains utilities working with String object (advanced splitting functionality etc).
13 *
14 * $Author:: zmicer $<br/>
15 * $Rev:: 57 $<br/> * $Date:: 2007-08-23 09:16:37 #$<br/>
16 */
17 public class StringUtils
18 {
19 /**
20 * Logger instance.
21 */
22 final public static Logger LOG = Logger.getLogger(StringUtils.class);
23
24 /**
25 * Make the splitting of the String into the List of String. The <code>StringTokenizer</code> is used for splitting the String.
26 *
27 * @param splitter The String splitter, Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
28 * @param str String to be splitted, Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
29 *
30 * @return the List<String> of Objects were taken from the <code>str</code> source string
31 */
32 public static List<String> split(final String str, final String splitter)
33 {
34 InputArgumentUtils.checkStrings(true, str, splitter);
35
36 final List<String> result = new ArrayList<String>();
37 final StringTokenizer tokenizer = new StringTokenizer(str, splitter);
38 while (tokenizer.hasMoreTokens())
39 {
40 String candidate = tokenizer.nextToken();
41 if (null != candidate)
42 {
43 candidate = candidate.trim();
44 if (!"".equals(candidate))
45 {
46 result.add(candidate);
47 }
48 }
49 }
50 return result;
51 }
52
53
54 /**
55 * Safely convert the String to Boolean
56 *
57 * @param string String to be converted, Can not be null/empty (otherwise <code>IllegalArgumentException</code> would appear).
58 * @return the Boolean value, the null in the case the String doesn't contain the boolean value.
59 */
60 public static Boolean getBoolean(final String string)
61 {
62 InputArgumentUtils.checkStrings(true, string);
63 final String str = string.trim().toLowerCase();
64 if (str.equals(Boolean.TRUE.toString()))
65 {
66 return Boolean.TRUE;
67 }
68 else if (str.equals(Boolean.FALSE.toString()))
69 {
70 return Boolean.FALSE;
71 }
72 return null;
73 }
74
75 /**
76 * Return the Integer using the provided String. This method is used for the safe working with the integers are represented by the
77 * String objects
78 *
79 * @param string String to be processed. Can not be null or empty (otherwise <code>IllegalArgumentException</code> would appear).
80 * @return filled Integer or the null in the case the string stores incorrect value
81 */
82 public static Integer getInteger(final String string)
83 {
84 InputArgumentUtils.checkStrings(true, string);
85 Integer result = null;
86 try
87 {
88 result = Integer.parseInt(string);
89 }
90 catch (NumberFormatException ex)
91 {
92 return null;
93 }
94 return result;
95 }
96
97 /**
98 * Split the provided string using the line folding as separator.
99 *
100 * @param strings the List of String to be splitted. Can not be null (otherwise <code>IllegalArgumentException</code> would appear).
101 * @return the result string
102 */
103 public static String joinStringsWithLineFolding(final List<String> strings)
104 {
105 InputArgumentUtils.checkObjects(strings);
106 StringWriter sw = new StringWriter();
107 PrintWriter pw = new PrintWriter(sw);
108 for (String str : strings)
109 {
110 pw.println(str);
111 }
112 return sw.toString();
113 }
114 }