1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.qedeq.base.utility; |
19 |
|
|
20 |
|
import java.io.BufferedReader; |
21 |
|
import java.io.ByteArrayOutputStream; |
22 |
|
import java.io.IOException; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.io.UnsupportedEncodingException; |
25 |
|
import java.util.Properties; |
26 |
|
|
27 |
|
import org.apache.commons.lang.SystemUtils; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
@version |
36 |
|
@author |
37 |
|
|
|
|
| 39,6% |
Uncovered Elements: 113 (187) |
Complexity: 51 |
Complexity Density: 0,46 |
|
38 |
|
public final class StringUtility { |
39 |
|
|
40 |
|
|
41 |
|
static final String FORMATED_ZERO = "0000000000000000000000000"; |
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
46 |
0
|
private StringUtility() {... |
47 |
|
|
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
|
@param |
55 |
|
@param |
56 |
|
@param |
57 |
|
@return |
58 |
|
|
|
|
| 78,9% |
Uncovered Elements: 4 (19) |
Complexity: 4 |
Complexity Density: 0,31 |
|
59 |
28509
|
public static String replace(final String text,... |
60 |
|
final String search, final String replace) { |
61 |
|
|
62 |
28509
|
final int len = search.length(); |
63 |
28509
|
if (len == 0) { |
64 |
0
|
return text; |
65 |
|
} |
66 |
28509
|
final StringBuffer result = new StringBuffer(); |
67 |
28509
|
int pos1 = 0; |
68 |
28509
|
int pos2; |
69 |
?
|
while (0 <= (pos2 = text.indexOf(search, pos1))) { |
70 |
8706
|
result.append(text.substring(pos1, pos2)); |
71 |
8706
|
result.append(replace); |
72 |
8706
|
pos1 = pos2 + len; |
73 |
|
} |
74 |
28509
|
if (pos1 < text.length()) { |
75 |
27405
|
result.append(text.substring(pos1)); |
76 |
|
} |
77 |
28509
|
return result.toString(); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
@param |
86 |
|
@param |
87 |
|
@param |
88 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0,33 |
|
89 |
28505
|
public static void replace(final StringBuffer text,... |
90 |
|
final String search, final String replace) { |
91 |
|
|
92 |
28505
|
final String result = replace(text.toString(), search, replace); |
93 |
28505
|
text.setLength(0); |
94 |
28505
|
text.append(result); |
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 |
|
@param |
120 |
|
@return |
121 |
|
@throws |
122 |
|
|
|
|
| 0% |
Uncovered Elements: 11 (11) |
Complexity: 3 |
Complexity Density: 0,43 |
|
123 |
0
|
public static String quote(final String unquoted) {... |
124 |
0
|
String result = "\""; |
125 |
|
|
126 |
0
|
for (int i = 0; i < unquoted.length(); i++) { |
127 |
0
|
if (unquoted.charAt(i) == '\"') { |
128 |
0
|
result += "\"\""; |
129 |
|
} else { |
130 |
0
|
result += unquoted.charAt(i); |
131 |
|
} |
132 |
|
} |
133 |
0
|
result += '\"'; |
134 |
0
|
return result; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
@return |
143 |
|
|
144 |
|
@throws |
145 |
|
|
|
|
| 0% |
Uncovered Elements: 16 (16) |
Complexity: 5 |
Complexity Density: 0,62 |
|
146 |
0
|
public static boolean isLetterDigitString(final String text) {... |
147 |
0
|
if (text.length() <= 0) { |
148 |
0
|
return false; |
149 |
|
} |
150 |
0
|
if (!Character.isLetter(text.charAt(0))) { |
151 |
0
|
return false; |
152 |
|
} |
153 |
0
|
for (int i = 1; i < text.length(); i++) { |
154 |
0
|
if (!Character.isLetterOrDigit(text.charAt(i))) { |
155 |
0
|
return false; |
156 |
|
} |
157 |
|
} |
158 |
0
|
return true; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
@param |
165 |
|
@return |
166 |
|
|
|
|
| 87,5% |
Uncovered Elements: 1 (8) |
Complexity: 2 |
Complexity Density: 0,5 |
|
167 |
4
|
public static StringBuffer getSpaces(final int length) {... |
168 |
4
|
final StringBuffer buffer = new StringBuffer(length >= 0 ? length : 0); |
169 |
76
|
for (int i = 0; i < length; i++) { |
170 |
72
|
buffer.append(' '); |
171 |
|
} |
172 |
4
|
return buffer; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
|
177 |
|
|
178 |
|
@param |
179 |
|
@return |
180 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
181 |
30222
|
public static String getClassName(final Class clazz) {... |
182 |
30222
|
return clazz.getName().substring(clazz.getName().lastIndexOf('.') + 1); |
183 |
|
} |
184 |
|
|
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
|
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
195 |
|
|
196 |
|
|
197 |
|
|
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
@param |
205 |
|
|
|
|
| 85,7% |
Uncovered Elements: 3 (21) |
Complexity: 9 |
Complexity Density: 0,82 |
|
206 |
1846
|
public static void deleteLineLeadingWhitespace(final StringBuffer buffer) {... |
207 |
1846
|
int start = -1; |
208 |
?
|
while (0 <= (start = buffer.indexOf("\n", start + 1))) { |
209 |
438
|
if (start + 1 < buffer.length() && '\n' != buffer.charAt(start + 1)) { |
210 |
392
|
break; |
211 |
|
} |
212 |
|
} |
213 |
1846
|
if (start >= 0) { |
214 |
392
|
int next = start + 1; |
215 |
5982
|
while (next < buffer.length() && Character.isWhitespace(buffer.charAt(next)) |
216 |
|
&& '\n' != buffer.charAt(next)) { |
217 |
5590
|
next++; |
218 |
|
} |
219 |
392
|
final String empty = buffer.substring(start, next); |
220 |
392
|
if (empty.length() > 0) { |
221 |
392
|
replace(buffer, empty, "\n"); |
222 |
|
} |
223 |
|
} |
224 |
|
} |
225 |
|
|
226 |
|
|
227 |
|
|
228 |
|
|
229 |
|
@param |
230 |
|
@return |
231 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0,3 |
|
232 |
0
|
public static String escapeProperty(final String value) {... |
233 |
0
|
Properties newprops = new Properties(); |
234 |
0
|
newprops.put("key", value); |
235 |
0
|
ByteArrayOutputStream out = new ByteArrayOutputStream(); |
236 |
0
|
try { |
237 |
0
|
newprops.store(out, null); |
238 |
|
} catch (IOException e) { |
239 |
0
|
throw new RuntimeException(e); |
240 |
|
} |
241 |
0
|
try { |
242 |
0
|
final String file = out.toString("ISO-8859-1"); |
243 |
0
|
return file.substring(file.indexOf('\n') + 1 + "key=".length()); |
244 |
|
} catch (UnsupportedEncodingException e) { |
245 |
0
|
throw new RuntimeException(e); |
246 |
|
} |
247 |
|
} |
248 |
|
|
249 |
|
|
250 |
|
|
251 |
|
|
252 |
|
@param |
253 |
|
@return |
254 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
255 |
3745
|
public static String decodeXmlMarkup(final StringBuffer value) {... |
256 |
|
|
257 |
|
|
258 |
3745
|
replace(value, "<", "<"); |
259 |
3745
|
replace(value, ">", ">"); |
260 |
3745
|
replace(value, "&", "&"); |
261 |
3745
|
return value.toString(); |
262 |
|
} |
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
@param |
268 |
|
@param |
269 |
|
@return |
270 |
|
|
|
|
| 0% |
Uncovered Elements: 10 (10) |
Complexity: 3 |
Complexity Density: 0,5 |
|
271 |
0
|
public static final String format(final long number, final int length) {... |
272 |
0
|
if (length > FORMATED_ZERO.length()) { |
273 |
0
|
throw new IllegalArgumentException("maximum length " + FORMATED_ZERO + " exceeded: " |
274 |
|
+ length); |
275 |
|
} |
276 |
0
|
if (number < 0) { |
277 |
0
|
throw new IllegalArgumentException("number must be bigger than 0: " + number); |
278 |
|
} |
279 |
0
|
final String temp = FORMATED_ZERO + number; |
280 |
0
|
return temp.substring(temp.length() - length); |
281 |
|
} |
282 |
|
|
283 |
|
|
284 |
|
|
285 |
|
|
286 |
|
@param |
287 |
|
@return |
288 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 5 |
Complexity Density: 0,45 |
|
289 |
0
|
public static String string2Hex(final String data) {... |
290 |
|
|
291 |
0
|
StringBuffer buffer = new StringBuffer(); |
292 |
0
|
for (int i = 0; i < data.length(); i++) { |
293 |
0
|
String b = Integer.toHexString(255 & data.charAt(i)); |
294 |
0
|
if (i != 0) { |
295 |
0
|
if (0 != i % 16) { |
296 |
0
|
buffer.append(" "); |
297 |
|
} else { |
298 |
0
|
buffer.append("\n"); |
299 |
|
} |
300 |
|
} |
301 |
0
|
if (b.length() < 2) { |
302 |
0
|
buffer.append("0"); |
303 |
|
} |
304 |
0
|
buffer.append(b.toUpperCase()); |
305 |
|
} |
306 |
0
|
return buffer.toString(); |
307 |
|
} |
308 |
|
|
309 |
|
|
310 |
|
|
311 |
|
|
312 |
|
@param |
313 |
|
@return |
314 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 7 |
Complexity Density: 0,44 |
|
315 |
0
|
public static String hex2String(final String hex) {... |
316 |
|
|
317 |
0
|
StringBuffer buffer = new StringBuffer(hex.length()); |
318 |
0
|
char c; |
319 |
0
|
for (int i = 0; i < hex.length(); i++) { |
320 |
0
|
c = hex.charAt(i); |
321 |
0
|
if (!Character.isWhitespace(c)) { |
322 |
0
|
if (!Character.isLetterOrDigit(c)) { |
323 |
0
|
throw new IllegalArgumentException("Illegal hex char"); |
324 |
|
} |
325 |
0
|
buffer.append(c); |
326 |
|
} |
327 |
|
} |
328 |
0
|
if (buffer.length() % 2 != 0) { |
329 |
0
|
throw new IllegalArgumentException("Bad padding"); |
330 |
|
} |
331 |
0
|
StringBuffer result = new StringBuffer(buffer.length() / 2); |
332 |
0
|
for (int i = 0; i < buffer.length() / 2; i++) { |
333 |
0
|
try { |
334 |
0
|
result.append((char) Integer.parseInt(buffer.substring(2 * i, 2 * i + 2), 16)); |
335 |
|
} catch (Exception e) { |
336 |
0
|
throw new IllegalArgumentException("Illegal hex char"); |
337 |
|
} |
338 |
|
} |
339 |
0
|
return result.toString(); |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
345 |
|
@return |
346 |
|
|
|
|
| 66,7% |
Uncovered Elements: 1 (3) |
Complexity: 1 |
Complexity Density: 1 |
|
347 |
573
|
public static String getSystemLineSeparator() {... |
348 |
573
|
return (SystemUtils.LINE_SEPARATOR != null ? SystemUtils.LINE_SEPARATOR |
349 |
|
: "\n"); |
350 |
|
} |
351 |
|
|
352 |
|
|
353 |
|
|
354 |
|
|
355 |
|
@param |
356 |
|
@return |
357 |
|
|
|
|
| 76,2% |
Uncovered Elements: 5 (21) |
Complexity: 5 |
Complexity Density: 0,33 |
|
358 |
573
|
public static String useSystemLineSeparator(final String text) {... |
359 |
573
|
if (text == null) { |
360 |
0
|
return null; |
361 |
|
} |
362 |
573
|
final StringBuffer buffer = new StringBuffer(text.length()); |
363 |
573
|
final BufferedReader reader = new BufferedReader(new StringReader(text)); |
364 |
573
|
final String separator = getSystemLineSeparator(); |
365 |
573
|
String line; |
366 |
573
|
boolean notFirst = false; |
367 |
573
|
try { |
368 |
?
|
while (null != (line = reader.readLine())) { |
369 |
1175
|
if (notFirst) { |
370 |
602
|
buffer.append(separator); |
371 |
|
} |
372 |
1175
|
buffer.append(line); |
373 |
1175
|
notFirst = true; |
374 |
|
} |
375 |
|
} catch (IOException e) { |
376 |
0
|
throw new RuntimeException(e); |
377 |
|
} |
378 |
573
|
return buffer.toString(); |
379 |
|
} |
380 |
|
|
381 |
|
} |