1 package be
.nikiroo
.jvcard
.resources
;
4 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.ByteArrayInputStream
;
6 import java
.io
.ByteArrayOutputStream
;
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
;
20 import javax
.imageio
.ImageIO
;
21 import javax
.xml
.bind
.DatatypeConverter
;
23 import com
.googlecode
.lanterna
.gui2
.LinearLayout
.Alignment
;
25 public class StringUtils
{
26 static private Pattern marks
= Pattern
27 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
30 * Fix the size of the given {@link String} either with space-padding or by
34 * the {@link String} to fix
36 * the size of the resulting {@link String} or -1 for a noop
38 * @return the resulting {@link String} of size <i>size</i>
40 static public String
padString(String text
, int width
) {
41 return padString(text
, width
, true, Alignment
.Beginning
);
45 * Fix the size of the given {@link String} either with space-padding or by
46 * optionally shortening it.
49 * the {@link String} to fix
51 * the size of the resulting {@link String} if the text fits or
52 * if cut is TRUE or -1 for a noop
54 * cut the {@link String} shorter if needed
56 * align the {@link String} in this position if we have enough
59 * @return the resulting {@link String} of size <i>size</i> minimum
61 static public String
padString(String text
, int width
, boolean cut
,
68 int diff
= width
- text
.length();
72 text
= text
.substring(0, width
);
73 } else if (diff
> 0) {
74 if (diff
< 2 && align
!= Alignment
.End
)
75 align
= Alignment
.Beginning
;
79 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
82 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
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', ' ');
100 * Sanitise the given input to make it more Terminal-friendly by removing
101 * combining characters.
104 * the input to sanitise
105 * @param allowUnicode
106 * allow Unicode or only allow ASCII Latin characters
108 * @return the sanitised {@link String}
110 static public String
sanitize(String input
, boolean allowUnicode
) {
111 return sanitize(input
, allowUnicode
, !allowUnicode
);
115 * Sanitise the given input to make it more Terminal-friendly by removing
116 * combining characters.
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
126 * @return the sanitised {@link String}
128 static public String
sanitize(String input
, boolean allowUnicode
,
129 boolean removeAllAccents
) {
131 if (removeAllAccents
) {
132 input
= Normalizer
.normalize(input
, Form
.NFKD
);
133 input
= marks
.matcher(input
).replaceAll("");
136 input
= Normalizer
.normalize(input
, Form
.NFKC
);
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,
144 if (car
>= 32 && car
<= 255 && car
!= 127) {
148 input
= builder
.toString();
155 * Convert between time in milliseconds to {@link String} in a "static" way
156 * (to exchange data over the wire, for instance).
159 * the time in milliseconds
161 * @return the time as a {@link String}
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
));
169 * Convert between time as a {@link String} to milliseconds in a "static"
170 * way (to exchange data over the wire, for instance).
173 * the time as a {@link String}
175 * @return the time in milliseconds
177 static public long toTime(String display
) {
178 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
180 return sdf
.parse(display
).getTime();
181 } catch (ParseException e
) {
187 * Convert the given {@link Image} object into a Base64 representation of
188 * the same {@link Image}. object.
191 * the {@link Image} object to convert
193 * @return the Base64 representation
195 * @throws IOException
196 * in case of IO error
198 static public String
fromImage(BufferedImage image
) throws IOException
{
199 String imageString
= null;
200 ByteArrayOutputStream out
= new ByteArrayOutputStream();
202 ImageIO
.write(image
, "jpeg", out
);
203 byte[] imageBytes
= out
.toByteArray();
205 imageString
= DatatypeConverter
.printBase64Binary(imageBytes
);
213 * Convert the given {@link File} image into a Base64 representation of the
217 * the {@link File} image to convert
219 * @return the Base64 representation
221 * @throws IOException
222 * in case of IO error
224 static public String
fromImage(File file
) throws IOException
{
225 String fileString
= null;
226 ByteArrayOutputStream out
= new ByteArrayOutputStream();
228 byte[] buf
= new byte[8192];
229 InputStream in
= new FileInputStream(file
);
232 while ((c
= in
.read(buf
, 0, buf
.length
)) > 0) {
233 out
.write(buf
, 0, c
);
238 fileString
= DatatypeConverter
.printBase64Binary(out
.toByteArray());
245 * Convert the given Base64 representation of an image into an {@link Image}
249 * the {@link Image} in Base64 format
251 * @return the {@link Image} object
253 * @throws IOException
254 * in case of IO error
256 static public BufferedImage
toImage(String b64data
) throws IOException
{
257 BufferedImage image
= ImageIO
.read(new ByteArrayInputStream(
258 DatatypeConverter
.parseBase64Binary(b64data
)));
264 * Return a hash of the given {@link String}.
271 static public String
getHash(String input
) {
273 MessageDigest md
= MessageDigest
.getInstance("MD5");
274 md
.update(input
.getBytes());
275 byte byteData
[] = md
.digest();
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
);
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