1 | package eu.clarin.cmdi.vlo; |
---|
2 | |
---|
3 | import java.util.Arrays; |
---|
4 | import java.util.HashSet; |
---|
5 | import java.util.Set; |
---|
6 | import java.util.regex.Matcher; |
---|
7 | import java.util.regex.Pattern; |
---|
8 | |
---|
9 | public final class StringUtils { |
---|
10 | |
---|
11 | /** |
---|
12 | * Converts a newlines in to html (<br/>), not using <p> because it renders differently on firefox/safari/chrome/ie etc... |
---|
13 | * Heavily inspired on: |
---|
14 | * {@link org.apache.wicket.markup.html.basic.MultiLineLabel} |
---|
15 | * {@link org.apache.wicket.util.string.Strings#toMultilineMarkup(CharSequence)} |
---|
16 | * |
---|
17 | * @param s |
---|
18 | */ |
---|
19 | public static CharSequence toMultiLineHtml(CharSequence s) { |
---|
20 | StringBuilder result = new StringBuilder(); |
---|
21 | if (s == null) { |
---|
22 | return result; |
---|
23 | } |
---|
24 | int newlineCount = 0; |
---|
25 | for (int i = 0; i < s.length(); i++) { |
---|
26 | char c = s.charAt(i); |
---|
27 | switch (c) { |
---|
28 | case '\n': |
---|
29 | newlineCount++; |
---|
30 | break; |
---|
31 | case '\r': |
---|
32 | break; |
---|
33 | default: |
---|
34 | if (newlineCount == 1) { |
---|
35 | result.append("<br/>"); |
---|
36 | } else if (newlineCount > 1) { |
---|
37 | result.append("<br/><br/>"); |
---|
38 | } |
---|
39 | result.append(c); |
---|
40 | newlineCount = 0; |
---|
41 | break; |
---|
42 | } |
---|
43 | } |
---|
44 | if (newlineCount == 1) { |
---|
45 | result.append("<br/>"); |
---|
46 | } else if (newlineCount > 1) { |
---|
47 | result.append("<br/><br/>"); |
---|
48 | } |
---|
49 | return result; |
---|
50 | } |
---|
51 | |
---|
52 | private static final Set<Character> reservedCharacters = new HashSet<Character>(Arrays.asList('!', '*', '\'', '(', |
---|
53 | ')', ';', ':', '@', '&', '=', '+', '$', ',', '/', '?', '#', '[', ']')); |
---|
54 | |
---|
55 | /** |
---|
56 | * Return normalized String where all reserved characters in URL encoding |
---|
57 | * are replaced by their ASCII code (in underscores) |
---|
58 | * |
---|
59 | * @param idString String that will be normalized |
---|
60 | * @return normalized version of value where all reserved |
---|
61 | * characters in URL encoding are replaced by their ASCII code |
---|
62 | */ |
---|
63 | public static String normalizeIdString(String idString) { |
---|
64 | StringBuilder normalizedString = new StringBuilder(); |
---|
65 | for (int i = 0; i < idString.length(); i++) { |
---|
66 | Character character = idString.charAt(i); |
---|
67 | if (reservedCharacters.contains(character)) { |
---|
68 | normalizedString.append("_").append((int) character).append("_"); |
---|
69 | } else { |
---|
70 | normalizedString.append(character); |
---|
71 | } |
---|
72 | } |
---|
73 | return normalizedString.toString(); |
---|
74 | } |
---|
75 | |
---|
76 | public static String uncapitalizeFirstLetter(String value){ |
---|
77 | return value.substring(0, 1).toLowerCase() + value.substring(1); |
---|
78 | } |
---|
79 | |
---|
80 | public static String capitalizeFirstLetter(String value){ |
---|
81 | return value.substring(0, 1).toUpperCase() + value.substring(1); |
---|
82 | } |
---|
83 | |
---|
84 | public static String createStringFromArray(String... values){ |
---|
85 | String res = ""; |
---|
86 | |
---|
87 | for(String str: values){ |
---|
88 | res += str; |
---|
89 | } |
---|
90 | |
---|
91 | return res; |
---|
92 | |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | } |
---|