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