1 | |
package liquibase.util; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.Arrays; |
5 | |
import java.util.Collection; |
6 | |
import java.util.List; |
7 | |
import java.util.regex.Pattern; |
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | 0 | public class StringUtils { |
13 | |
public static String trimToEmpty(String string) { |
14 | 135 | if (string == null) { |
15 | 134 | return ""; |
16 | |
} |
17 | 1 | return string.trim(); |
18 | |
} |
19 | |
|
20 | |
public static String trimToNull(String string) { |
21 | 1337 | if (string == null) { |
22 | 839 | return null; |
23 | |
} |
24 | 498 | String returnString = string.trim(); |
25 | 498 | if (returnString.length() == 0) { |
26 | 95 | return null; |
27 | |
} else { |
28 | 403 | return returnString; |
29 | |
} |
30 | |
} |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
public static String[] processMutliLineSQL(String multiLineSQL, boolean stripComments, boolean splitStatements, |
42 | |
String endDelimiter) { |
43 | |
|
44 | 10 | String stripped = stripComments ? stripComments(multiLineSQL) : multiLineSQL; |
45 | 10 | if (splitStatements) { |
46 | 7 | return splitSQL(stripped, endDelimiter); |
47 | |
} else { |
48 | 3 | return new String[] { stripped }; |
49 | |
} |
50 | |
} |
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public static String[] splitSQL(String multiLineSQL, String endDelimiter) { |
56 | 12 | if (endDelimiter == null) { |
57 | 9 | endDelimiter = ";\\s*\n|;$|\n[gG][oO]\\s*\n|\n[Gg][oO]\\s*$"; |
58 | |
} else { |
59 | 3 | if (endDelimiter.equalsIgnoreCase("go")) { |
60 | 0 | endDelimiter = "\n[gG][oO]\\s*\n|\n[Gg][oO]\\s*$"; |
61 | |
} |
62 | |
} |
63 | 12 | String[] initialSplit = multiLineSQL.split(endDelimiter); |
64 | 12 | List<String> strings = new ArrayList<String>(); |
65 | 36 | for (String anInitialSplit : initialSplit) { |
66 | 24 | String singleLineSQL = anInitialSplit.trim(); |
67 | 24 | if (singleLineSQL.length() > 0) { |
68 | 24 | strings.add(singleLineSQL); |
69 | |
} |
70 | |
} |
71 | |
|
72 | 12 | return strings.toArray(new String[strings.size()]); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
public static String stripComments(String multiLineSQL) { |
82 | 17 | String strippedSingleLines = Pattern.compile("(.*?)\\s*\\-\\-.*\n").matcher(multiLineSQL).replaceAll("$1\n"); |
83 | 17 | strippedSingleLines = Pattern.compile("(.*?)\\s*\\-\\-.*$").matcher(strippedSingleLines).replaceAll("$1\n"); |
84 | 17 | return Pattern.compile("/\\*.*?\\*/", Pattern.DOTALL).matcher(strippedSingleLines).replaceAll("").trim(); |
85 | |
} |
86 | |
|
87 | |
public static String join(String[] array, String delimiter) { |
88 | 0 | return join(Arrays.asList(array), delimiter); |
89 | |
} |
90 | |
|
91 | |
public static String join(Collection<String> collection, String delimiter) { |
92 | 684 | if (collection == null) { |
93 | 0 | return null; |
94 | |
} |
95 | |
|
96 | 684 | if (collection.size() == 0) { |
97 | 0 | return ""; |
98 | |
} |
99 | |
|
100 | 684 | StringBuffer buffer = new StringBuffer(); |
101 | 684 | for (String val : collection) { |
102 | 2494 | buffer.append(val).append(delimiter); |
103 | |
} |
104 | |
|
105 | 684 | String returnString = buffer.toString(); |
106 | 684 | return returnString.substring(0, returnString.length() - delimiter.length()); |
107 | |
} |
108 | |
|
109 | |
public static List<String> splitAndTrim(String s, String regex) { |
110 | 9 | if (s == null) { |
111 | 2 | return null; |
112 | |
} |
113 | 7 | List<String> returnList = new ArrayList<String>(); |
114 | 18 | for (String string : s.split(regex)) { |
115 | 11 | returnList.add(string.trim()); |
116 | |
} |
117 | |
|
118 | 7 | return returnList; |
119 | |
|
120 | |
} |
121 | |
|
122 | |
public static String repeat(String string, int times) { |
123 | 3325 | String returnString = ""; |
124 | 18389 | for (int i = 0; i < times; i++) { |
125 | 15064 | returnString += string; |
126 | |
} |
127 | |
|
128 | 3325 | return returnString; |
129 | |
} |
130 | |
|
131 | |
public static String join(Integer[] array, String delimiter) { |
132 | 0 | if (array == null) { |
133 | 0 | return null; |
134 | |
} |
135 | |
|
136 | 0 | int[] ints = new int[array.length]; |
137 | 0 | for (int i = 0; i < ints.length; i++) { |
138 | 0 | ints[i] = array[i]; |
139 | |
} |
140 | 0 | return StringUtils.join(ints, delimiter); |
141 | |
} |
142 | |
|
143 | |
public static String join(int[] array, String delimiter) { |
144 | 0 | if (array == null) { |
145 | 0 | return null; |
146 | |
} |
147 | |
|
148 | 0 | if (array.length == 0) { |
149 | 0 | return ""; |
150 | |
} |
151 | |
|
152 | 0 | StringBuffer buffer = new StringBuffer(); |
153 | 0 | for (int val : array) { |
154 | 0 | buffer.append(val).append(delimiter); |
155 | |
} |
156 | |
|
157 | 0 | String returnString = buffer.toString(); |
158 | 0 | return returnString.substring(0, returnString.length() - delimiter.length()); |
159 | |
} |
160 | |
} |