Performance improvement:
[jvcard.git] / src / be / nikiroo / jvcard / resources / StringUtils.java
CommitLineData
7da41ecd 1package be.nikiroo.jvcard.resources;
a3b510ab 2
26d254a3
NR
3import java.awt.Image;
4import java.awt.image.BufferedImage;
5import java.io.ByteArrayInputStream;
6import java.io.ByteArrayOutputStream;
7import java.io.IOException;
cf77cb35
NR
8import java.security.MessageDigest;
9import java.security.NoSuchAlgorithmException;
296a0b75
NR
10import java.text.Normalizer;
11import java.text.Normalizer.Form;
b9f192ed
NR
12import java.text.ParseException;
13import java.text.SimpleDateFormat;
14import java.util.Date;
296a0b75
NR
15import java.util.regex.Pattern;
16
26d254a3
NR
17import javax.imageio.ImageIO;
18import javax.xml.bind.DatatypeConverter;
19
a3b510ab
NR
20import com.googlecode.lanterna.gui2.LinearLayout.Alignment;
21
22public class StringUtils {
296a0b75
NR
23 static private Pattern marks = Pattern
24 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
a3b510ab 25
296a0b75
NR
26 /**
27 * Fix the size of the given {@link String} either with space-padding or by
28 * shortening it.
29 *
30 * @param text
31 * the {@link String} to fix
32 * @param width
3634193b 33 * the size of the resulting {@link String} or -1 for a noop
296a0b75
NR
34 *
35 * @return the resulting {@link String} of size <i>size</i>
36 */
a3b510ab
NR
37 static public String padString(String text, int width) {
38 return padString(text, width, true, Alignment.Beginning);
39 }
40
296a0b75
NR
41 /**
42 * Fix the size of the given {@link String} either with space-padding or by
43 * optionally shortening it.
44 *
45 * @param text
46 * the {@link String} to fix
47 * @param width
48 * the size of the resulting {@link String} if the text fits or
3634193b 49 * if cut is TRUE or -1 for a noop
296a0b75
NR
50 * @param cut
51 * cut the {@link String} shorter if needed
52 * @param align
53 * align the {@link String} in this position if we have enough
54 * space
55 *
56 * @return the resulting {@link String} of size <i>size</i> minimum
57 */
a3b510ab
NR
58 static public String padString(String text, int width, boolean cut,
59 Alignment align) {
26d254a3 60
a3b510ab
NR
61 if (width >= 0) {
62 if (text == null)
63 text = "";
64
65 int diff = width - text.length();
66
67 if (diff < 0) {
68 if (cut)
69 text = text.substring(0, width);
70 } else if (diff > 0) {
71 if (diff < 2 && align != Alignment.End)
72 align = Alignment.Beginning;
73
74 switch (align) {
75 case Beginning:
76 text = text + new String(new char[diff]).replace('\0', ' ');
77 break;
78 case End:
79 text = new String(new char[diff]).replace('\0', ' ') + text;
80 break;
81 case Center:
82 case Fill:
83 default:
84 int pad1 = (diff) / 2;
85 int pad2 = (diff + 1) / 2;
86 text = new String(new char[pad1]).replace('\0', ' ') + text
87 + new String(new char[pad2]).replace('\0', ' ');
88 break;
89 }
90 }
91 }
92
93 return text;
94 }
95
296a0b75
NR
96 /**
97 * Sanitise the given input to make it more Terminal-friendly by removing
98 * combining characters.
99 *
100 * @param input
101 * the input to sanitise
102 * @param allowUnicode
103 * allow Unicode or only allow ASCII Latin characters
104 *
105 * @return the sanitised {@link String}
106 */
107 static public String sanitize(String input, boolean allowUnicode) {
108 return sanitize(input, allowUnicode, !allowUnicode);
109 }
110
111 /**
112 * Sanitise the given input to make it more Terminal-friendly by removing
113 * combining characters.
114 *
115 * @param input
116 * the input to sanitise
117 * @param allowUnicode
118 * allow Unicode or only allow ASCII Latin characters
119 * @param removeAllAccents
120 * TRUE to replace all accentuated characters by their non
121 * accentuated counter-parts
122 *
123 * @return the sanitised {@link String}
124 */
125 static public String sanitize(String input, boolean allowUnicode,
126 boolean removeAllAccents) {
127
128 if (removeAllAccents) {
129 input = Normalizer.normalize(input, Form.NFKD);
130 input = marks.matcher(input).replaceAll("");
131 }
132
133 input = Normalizer.normalize(input, Form.NFKC);
134
135 if (!allowUnicode) {
adf9c449
NR
136 StringBuilder builder = new StringBuilder();
137 for (int index = 0; index < input.length(); index++) {
138 char car = input.charAt(index);
139 // displayable chars in ASCII are in the range 32<->255,
140 // except DEL (127)
141 if (car >= 32 && car <= 255 && car != 127) {
142 builder.append(car);
143 }
144 }
145 input = builder.toString();
296a0b75
NR
146 }
147
148 return input;
149 }
b9f192ed
NR
150
151 /**
152 * Convert between time in milliseconds to {@link String} in a "static" way
153 * (to exchange data over the wire, for instance).
154 *
155 * @param time
156 * the time in milliseconds
157 *
158 * @return the time as a {@link String}
159 */
160 static public String fromTime(long time) {
161 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
162 return sdf.format(new Date(time));
163 }
164
165 /**
166 * Convert between time as a {@link String} to milliseconds in a "static"
167 * way (to exchange data over the wire, for instance).
168 *
169 * @param time
170 * the time as a {@link String}
171 *
172 * @return the time in milliseconds
173 */
174 static public long toTime(String display) {
175 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
176 try {
177 return sdf.parse(display).getTime();
178 } catch (ParseException e) {
179 return -1;
180 }
181 }
cf77cb35 182
26d254a3
NR
183 /**
184 * Convert the given {@link Image} object into a Base64 representation of
185 * the same {@link Image}. object.
186 *
187 * @param image
188 * the {@link Image} object to convert
189 *
190 * @return the Base64 representation
191 */
192 static public String fromImage(BufferedImage image) {
193 String imageString = null;
194 ByteArrayOutputStream out = new ByteArrayOutputStream();
195
196 try {
59597d59 197 ImageIO.write(image, "jpeg", out);
26d254a3
NR
198 byte[] imageBytes = out.toByteArray();
199
200 imageString = DatatypeConverter.printBase64Binary(imageBytes);
201
202 out.close();
203 } catch (IOException e) {
204 }
205
206 return imageString;
207 }
208
209 /**
210 * Convert the given Base64 representation of an image into an {@link Image}
211 * object.
212 *
213 * @param b64data
214 * the {@link Image} in Base64 format
215 *
216 * @return the {@link Image} object
217 *
218 * @throws IOException
219 * in case of IO error
220 */
221 static public BufferedImage toImage(String b64data) throws IOException {
222 BufferedImage image = ImageIO.read(new ByteArrayInputStream(
223 DatatypeConverter.parseBase64Binary(b64data)));
224 image.toString();
225 return image;
226 }
227
cf77cb35
NR
228 /**
229 * Return a hash of the given {@link String}.
230 *
231 * @param input
232 * the input data
233 *
234 * @return the hash
235 */
236 static public String getHash(String input) {
237 try {
238 MessageDigest md = MessageDigest.getInstance("MD5");
239 md.update(input.getBytes());
240 byte byteData[] = md.digest();
241
242 StringBuffer hexString = new StringBuffer();
243 for (int i = 0; i < byteData.length; i++) {
244 String hex = Integer.toHexString(0xff & byteData[i]);
245 if (hex.length() == 1)
246 hexString.append('0');
247 hexString.append(hex);
248 }
249
250 return hexString.toString();
251 } catch (NoSuchAlgorithmException e) {
252 // all JVM most probably have an MD5 implementation, but even if
253 // not, returning the input is "correct", if inefficient and
254 // unsecure
255 return input;
256 }
257 }
a3b510ab 258}