image --load now store the file as is
[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;
a1783d00
NR
7import java.io.File;
8import java.io.FileInputStream;
26d254a3 9import java.io.IOException;
a1783d00 10import java.io.InputStream;
cf77cb35
NR
11import java.security.MessageDigest;
12import java.security.NoSuchAlgorithmException;
296a0b75
NR
13import java.text.Normalizer;
14import java.text.Normalizer.Form;
b9f192ed
NR
15import java.text.ParseException;
16import java.text.SimpleDateFormat;
17import java.util.Date;
296a0b75
NR
18import java.util.regex.Pattern;
19
26d254a3
NR
20import javax.imageio.ImageIO;
21import javax.xml.bind.DatatypeConverter;
22
a3b510ab
NR
23import com.googlecode.lanterna.gui2.LinearLayout.Alignment;
24
25public class StringUtils {
296a0b75
NR
26 static private Pattern marks = Pattern
27 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
a3b510ab 28
296a0b75
NR
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
3634193b 36 * the size of the resulting {@link String} or -1 for a noop
296a0b75
NR
37 *
38 * @return the resulting {@link String} of size <i>size</i>
39 */
a3b510ab
NR
40 static public String padString(String text, int width) {
41 return padString(text, width, true, Alignment.Beginning);
42 }
43
296a0b75
NR
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
3634193b 52 * if cut is TRUE or -1 for a noop
296a0b75
NR
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 */
a3b510ab
NR
61 static public String padString(String text, int width, boolean cut,
62 Alignment align) {
26d254a3 63
a3b510ab
NR
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
296a0b75
NR
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) {
adf9c449
NR
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();
296a0b75
NR
149 }
150
151 return input;
152 }
b9f192ed
NR
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 }
cf77cb35 185
26d254a3
NR
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
a1783d00
NR
194 *
195 * @throws IOException
196 * in case of IO error
26d254a3 197 */
a1783d00 198 static public String fromImage(BufferedImage image) throws IOException {
26d254a3
NR
199 String imageString = null;
200 ByteArrayOutputStream out = new ByteArrayOutputStream();
201
a1783d00
NR
202 ImageIO.write(image, "jpeg", out);
203 byte[] imageBytes = out.toByteArray();
26d254a3 204
a1783d00 205 imageString = DatatypeConverter.printBase64Binary(imageBytes);
26d254a3 206
a1783d00 207 out.close();
26d254a3
NR
208
209 return imageString;
210 }
211
a1783d00
NR
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
26d254a3
NR
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
cf77cb35
NR
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 }
a3b510ab 293}