| 1 | package be.nikiroo.jvcard.tui; |
| 2 | |
| 3 | import java.text.Normalizer; |
| 4 | import java.text.Normalizer.Form; |
| 5 | import java.text.ParseException; |
| 6 | import java.text.SimpleDateFormat; |
| 7 | import java.util.Date; |
| 8 | import java.util.regex.Pattern; |
| 9 | |
| 10 | import com.googlecode.lanterna.gui2.LinearLayout.Alignment; |
| 11 | |
| 12 | public class StringUtils { |
| 13 | static private Pattern marks = Pattern |
| 14 | .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+"); |
| 15 | |
| 16 | /** |
| 17 | * Fix the size of the given {@link String} either with space-padding or by |
| 18 | * shortening it. |
| 19 | * |
| 20 | * @param text |
| 21 | * the {@link String} to fix |
| 22 | * @param width |
| 23 | * the size of the resulting {@link String} if the text fits or |
| 24 | * if cut is TRUE |
| 25 | * |
| 26 | * @return the resulting {@link String} of size <i>size</i> |
| 27 | */ |
| 28 | static public String padString(String text, int width) { |
| 29 | return padString(text, width, true, Alignment.Beginning); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Fix the size of the given {@link String} either with space-padding or by |
| 34 | * optionally shortening it. |
| 35 | * |
| 36 | * @param text |
| 37 | * the {@link String} to fix |
| 38 | * @param width |
| 39 | * the size of the resulting {@link String} if the text fits or |
| 40 | * if cut is TRUE |
| 41 | * @param cut |
| 42 | * cut the {@link String} shorter if needed |
| 43 | * @param align |
| 44 | * align the {@link String} in this position if we have enough |
| 45 | * space |
| 46 | * |
| 47 | * @return the resulting {@link String} of size <i>size</i> minimum |
| 48 | */ |
| 49 | static public String padString(String text, int width, boolean cut, |
| 50 | Alignment align) { |
| 51 | |
| 52 | if (width >= 0) { |
| 53 | if (text == null) |
| 54 | text = ""; |
| 55 | |
| 56 | int diff = width - text.length(); |
| 57 | |
| 58 | if (diff < 0) { |
| 59 | if (cut) |
| 60 | text = text.substring(0, width); |
| 61 | } else if (diff > 0) { |
| 62 | if (diff < 2 && align != Alignment.End) |
| 63 | align = Alignment.Beginning; |
| 64 | |
| 65 | switch (align) { |
| 66 | case Beginning: |
| 67 | text = text + new String(new char[diff]).replace('\0', ' '); |
| 68 | break; |
| 69 | case End: |
| 70 | text = new String(new char[diff]).replace('\0', ' ') + text; |
| 71 | break; |
| 72 | case Center: |
| 73 | case Fill: |
| 74 | default: |
| 75 | int pad1 = (diff) / 2; |
| 76 | int pad2 = (diff + 1) / 2; |
| 77 | text = new String(new char[pad1]).replace('\0', ' ') + text |
| 78 | + new String(new char[pad2]).replace('\0', ' '); |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return text; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Sanitise the given input to make it more Terminal-friendly by removing |
| 89 | * combining characters. |
| 90 | * |
| 91 | * @param input |
| 92 | * the input to sanitise |
| 93 | * @param allowUnicode |
| 94 | * allow Unicode or only allow ASCII Latin characters |
| 95 | * |
| 96 | * @return the sanitised {@link String} |
| 97 | */ |
| 98 | static public String sanitize(String input, boolean allowUnicode) { |
| 99 | return sanitize(input, allowUnicode, !allowUnicode); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Sanitise the given input to make it more Terminal-friendly by removing |
| 104 | * combining characters. |
| 105 | * |
| 106 | * @param input |
| 107 | * the input to sanitise |
| 108 | * @param allowUnicode |
| 109 | * allow Unicode or only allow ASCII Latin characters |
| 110 | * @param removeAllAccents |
| 111 | * TRUE to replace all accentuated characters by their non |
| 112 | * accentuated counter-parts |
| 113 | * |
| 114 | * @return the sanitised {@link String} |
| 115 | */ |
| 116 | static public String sanitize(String input, boolean allowUnicode, |
| 117 | boolean removeAllAccents) { |
| 118 | |
| 119 | if (removeAllAccents) { |
| 120 | input = Normalizer.normalize(input, Form.NFKD); |
| 121 | input = marks.matcher(input).replaceAll(""); |
| 122 | } |
| 123 | |
| 124 | input = Normalizer.normalize(input, Form.NFKC); |
| 125 | |
| 126 | if (!allowUnicode) { |
| 127 | StringBuilder builder = new StringBuilder(); |
| 128 | for (int index = 0; index < input.length(); index++) { |
| 129 | char car = input.charAt(index); |
| 130 | // displayable chars in ASCII are in the range 32<->255, |
| 131 | // except DEL (127) |
| 132 | if (car >= 32 && car <= 255 && car != 127) { |
| 133 | builder.append(car); |
| 134 | } |
| 135 | } |
| 136 | input = builder.toString(); |
| 137 | } |
| 138 | |
| 139 | return input; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Convert between time in milliseconds to {@link String} in a "static" way |
| 144 | * (to exchange data over the wire, for instance). |
| 145 | * |
| 146 | * @param time |
| 147 | * the time in milliseconds |
| 148 | * |
| 149 | * @return the time as a {@link String} |
| 150 | */ |
| 151 | static public String fromTime(long time) { |
| 152 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 153 | return sdf.format(new Date(time)); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Convert between time as a {@link String} to milliseconds in a "static" |
| 158 | * way (to exchange data over the wire, for instance). |
| 159 | * |
| 160 | * @param time |
| 161 | * the time as a {@link String} |
| 162 | * |
| 163 | * @return the time in milliseconds |
| 164 | */ |
| 165 | static public long toTime(String display) { |
| 166 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| 167 | try { |
| 168 | return sdf.parse(display).getTime(); |
| 169 | } catch (ParseException e) { |
| 170 | return -1; |
| 171 | } |
| 172 | } |
| 173 | } |