1 package org.andromda.utils;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.StringReader;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
8 import org.andromda.utils.inflector.EnglishInflector;
9 import org.apache.commons.lang.StringUtils;
10 import org.apache.commons.lang.WordUtils;
11 import org.apache.log4j.Logger;
12
13
14
15
16
17
18
19
20
21
22
23 public class StringUtilsHelper
24 extends StringUtils
25 {
26
27
28
29 private static final Logger logger = Logger.getLogger(StringUtilsHelper.class);
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 public static String replaceSuffix(
45 final String src,
46 final String suffixOld,
47 final String suffixNew)
48 {
49 if (src.endsWith(suffixOld))
50 {
51 return src.substring(0, src.length() - suffixOld.length()) + suffixNew;
52 }
53 return src;
54 }
55
56
57
58
59
60
61
62
63
64
65
66
67
68 public static String upperCamelCaseName(final String string)
69 {
70 if (StringUtils.isEmpty(string))
71 {
72 return string;
73 }
74
75 final String[] parts = splitAtNonWordCharacters(string);
76 final StringBuilder conversionBuffer = new StringBuilder();
77 for (String part : parts)
78 {
79 if (part.length() < 2)
80 {
81 conversionBuffer.append(part.toUpperCase());
82 }
83 else
84 {
85 conversionBuffer.append(part.substring(0, 1).toUpperCase());
86 conversionBuffer.append(part.substring(1));
87 }
88 }
89 return conversionBuffer.toString();
90 }
91
92
93
94
95
96
97
98
99 public static String removeLastOccurrence(
100 String string,
101 final String value)
102 {
103 if (string != null && value != null)
104 {
105 StringBuilder buf = new StringBuilder();
106 int index = string.lastIndexOf(value);
107 if (index != -1)
108 {
109 buf.append(string.substring(0, index));
110 buf.append(string.substring(
111 index + value.length(),
112 string.length()));
113 string = buf.toString();
114 }
115 }
116 return string;
117 }
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public static String lowerCamelCaseName(final String string)
132 {
133 return uncapitalize(upperCamelCaseName(string));
134 }
135
136
137
138
139
140
141
142
143 public static Boolean startsWithLowercaseLetter(final String string)
144 {
145 if (string==null || string.length()<1)
146 {
147 return null;
148 }
149 final String start = string.substring(0, 1);
150 return isAllLowerCase(start) && isAlpha(start);
151 }
152
153
154
155
156
157
158
159
160 public static Boolean startsWithUppercaseLetter(final String string)
161 {
162 if (string==null)
163 {
164 return null;
165 }
166 final String start = string.substring(0, 1);
167 return isAllUpperCase(start) && isAlpha(start);
168 }
169
170
171
172
173
174
175
176
177
178 public static String toResourceMessageKey(final String string)
179 {
180 return separate(StringUtils.trimToEmpty(string), ".").toLowerCase();
181 }
182
183
184
185
186
187
188
189
190
191
192 public static String toPhrase(final String string)
193 {
194 return capitalize(separate(string, " "));
195 }
196
197
198
199
200
201
202
203
204 public static String separate(
205 final String string,
206 final String separator)
207 {
208 if (StringUtils.isBlank(string))
209 {
210 return string;
211 }
212
213 final String[] parts = splitAtNonWordCharacters(string);
214 final StringBuilder buffer = new StringBuilder();
215
216 for (int i = 0; i < parts.length - 1; i++)
217 {
218 if (StringUtils.isNotBlank(parts[i]))
219 {
220 buffer.append(parts[i]).append(separator);
221 }
222 }
223 return buffer.append(parts[parts.length - 1]).toString();
224 }
225
226
227
228
229
230 private static String[] splitAtNonWordCharacters(final String string)
231 {
232 final Pattern capitalSequencePattern = Pattern.compile("[A-Z]+");
233 final Matcher matcher = capitalSequencePattern.matcher(StringUtils.trimToEmpty(string));
234 final StringBuffer buffer = new StringBuffer();
235 while (matcher.find())
236 {
237 matcher.appendReplacement(buffer, ' ' + matcher.group());
238 }
239 matcher.appendTail(buffer);
240
241
242 return buffer.toString().split("[^A-Za-z0-9]+");
243 }
244
245
246
247
248
249
250
251
252
253 public static String suffixLines(
254 final String multiLines,
255 final String suffix)
256 {
257 final String[] lines = StringUtils.trimToEmpty(multiLines).split(LINE_SEPARATOR);
258 final StringBuilder linesBuffer = new StringBuilder();
259 for (String line : lines)
260 {
261 linesBuffer.append(line);
262 linesBuffer.append(suffix);
263 linesBuffer.append(LINE_SEPARATOR);
264 }
265 return linesBuffer.toString();
266 }
267
268
269
270
271
272
273
274
275
276 public static String toResourceMessage(String multiLines)
277 {
278 String resourceMessage = null;
279 if (StringUtils.isNotBlank(multiLines))
280 {
281 final String suffix = "\\";
282 multiLines = suffixLines(multiLines, ' ' + suffix).trim();
283 while (multiLines.endsWith(suffix))
284 {
285 multiLines = multiLines.substring(
286 0,
287 multiLines.lastIndexOf(suffix)).trim();
288 }
289 resourceMessage = multiLines;
290 }
291 return resourceMessage;
292 }
293
294
295
296
297
298
299
300
301
302
303
304 public static String prefixWithAPredicate(final String word)
305 {
306
307
308 final StringBuilder formattedBuffer = new StringBuilder();
309
310 formattedBuffer.append("a ");
311 formattedBuffer.append(word);
312
313 char firstChar = word.charAt(0);
314 switch (firstChar)
315 {
316 case 'a':
317 case 'e':
318 case 'i':
319 case 'o':
320 formattedBuffer.insert(1, 'n');
321 break;
322 default:
323 }
324
325 return formattedBuffer.toString();
326 }
327
328
329
330
331
332
333
334
335
336
337 public static String toSingleLine(String string)
338 {
339
340 return (string == null) ? "" : string.replaceAll("[$\\s]+", " ").trim();
341 }
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361 public static String pluralize(final String singularNoun)
362 {
363 final String plural = EnglishInflector.pluralize(singularNoun);
364 return plural == null ? "" : plural.trim();
365 }
366
367
368
369
370
371
372
373
374
375 public static String format(final String plainText)
376 {
377 return format(plainText, "");
378 }
379
380
381
382
383
384
385
386
387
388
389 public static String format(
390 final String plainText,
391 final String indentation)
392 {
393 return format(plainText, indentation, 100 - indentation.length());
394 }
395
396
397
398
399
400
401
402
403
404
405
406
407 public static String format(
408 final String plainText,
409 final String indentation,
410 final int wrapAtColumn)
411 {
412 return format(plainText, indentation, wrapAtColumn, true);
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 public static String format(
449 final String plainText,
450 String indentation,
451 final int wrapAtColumn,
452 final boolean htmlStyle)
453 {
454
455 if (wrapAtColumn < 1)
456 {
457 throw new IllegalArgumentException("Cannot wrap at column less than 1: " + wrapAtColumn);
458 }
459
460
461 if (indentation == null)
462 {
463 indentation = "";
464 }
465
466
467 if (StringUtils.isBlank(plainText))
468 {
469 return indentation;
470 }
471
472 final String lineSeparator = LINE_SEPARATOR;
473
474 String format;
475
476 try
477 {
478
479 final StringBuilder formattedText = new StringBuilder();
480
481
482 final BufferedReader reader = new BufferedReader(new StringReader(plainText));
483
484 String line = reader.readLine();
485
486
487 while (line != null)
488 {
489 if (StringUtils.isNotBlank(line))
490 {
491
492
493
494 if (htmlStyle)
495 {
496 formattedText.append(indentation);
497 formattedText.append("<p>");
498 formattedText.append(lineSeparator);
499 }
500
501
502
503 formattedText.append(indentation);
504
505
506
507 formattedText.append(WordUtils.wrap(
508 line.trim(),
509 wrapAtColumn,
510 lineSeparator + indentation,
511 false));
512
513
514 if (htmlStyle)
515 {
516 formattedText.append(lineSeparator);
517 formattedText.append(indentation);
518 formattedText.append("</p>");
519 }
520 }
521
522
523 line = reader.readLine();
524
525
526
527 if (formattedText.length() > 0 && StringUtils.isNotBlank(line))
528 {
529 formattedText.append(lineSeparator);
530 }
531 }
532
533
534 reader.close();
535
536
537 format = formattedText.toString();
538 }
539 catch (final IOException ioException)
540 {
541 logger.error("Could not format text: " + plainText, ioException);
542 format = plainText;
543 }
544
545 return format;
546 }
547
548
549
550
551 private static final String LINE_SEPARATOR = "\n";
552
553
554
555
556
557
558 public static String getLineSeparator()
559 {
560
561
562
563 return LINE_SEPARATOR;
564 }
565 }