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