1 package be
.nikiroo
.jvcard
.resources
;
4 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.ByteArrayInputStream
;
6 import java
.io
.ByteArrayOutputStream
;
7 import java
.io
.IOException
;
8 import java
.security
.MessageDigest
;
9 import java
.security
.NoSuchAlgorithmException
;
10 import java
.text
.Normalizer
;
11 import java
.text
.Normalizer
.Form
;
12 import java
.text
.ParseException
;
13 import java
.text
.SimpleDateFormat
;
14 import java
.util
.Date
;
15 import java
.util
.regex
.Pattern
;
17 import javax
.imageio
.ImageIO
;
18 import javax
.xml
.bind
.DatatypeConverter
;
20 import com
.googlecode
.lanterna
.gui2
.LinearLayout
.Alignment
;
22 public class StringUtils
{
23 static private Pattern marks
= Pattern
24 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
27 * Fix the size of the given {@link String} either with space-padding or by
31 * the {@link String} to fix
33 * the size of the resulting {@link String} or -1 for a noop
35 * @return the resulting {@link String} of size <i>size</i>
37 static public String
padString(String text
, int width
) {
38 return padString(text
, width
, true, Alignment
.Beginning
);
42 * Fix the size of the given {@link String} either with space-padding or by
43 * optionally shortening it.
46 * the {@link String} to fix
48 * the size of the resulting {@link String} if the text fits or
49 * if cut is TRUE or -1 for a noop
51 * cut the {@link String} shorter if needed
53 * align the {@link String} in this position if we have enough
56 * @return the resulting {@link String} of size <i>size</i> minimum
58 static public String
padString(String text
, int width
, boolean cut
,
65 int diff
= width
- text
.length();
69 text
= text
.substring(0, width
);
70 } else if (diff
> 0) {
71 if (diff
< 2 && align
!= Alignment
.End
)
72 align
= Alignment
.Beginning
;
76 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
79 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
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', ' ');
97 * Sanitise the given input to make it more Terminal-friendly by removing
98 * combining characters.
101 * the input to sanitise
102 * @param allowUnicode
103 * allow Unicode or only allow ASCII Latin characters
105 * @return the sanitised {@link String}
107 static public String
sanitize(String input
, boolean allowUnicode
) {
108 return sanitize(input
, allowUnicode
, !allowUnicode
);
112 * Sanitise the given input to make it more Terminal-friendly by removing
113 * combining characters.
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
123 * @return the sanitised {@link String}
125 static public String
sanitize(String input
, boolean allowUnicode
,
126 boolean removeAllAccents
) {
128 if (removeAllAccents
) {
129 input
= Normalizer
.normalize(input
, Form
.NFKD
);
130 input
= marks
.matcher(input
).replaceAll("");
133 input
= Normalizer
.normalize(input
, Form
.NFKC
);
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,
141 if (car
>= 32 && car
<= 255 && car
!= 127) {
145 input
= builder
.toString();
152 * Convert between time in milliseconds to {@link String} in a "static" way
153 * (to exchange data over the wire, for instance).
156 * the time in milliseconds
158 * @return the time as a {@link String}
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
));
166 * Convert between time as a {@link String} to milliseconds in a "static"
167 * way (to exchange data over the wire, for instance).
170 * the time as a {@link String}
172 * @return the time in milliseconds
174 static public long toTime(String display
) {
175 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
177 return sdf
.parse(display
).getTime();
178 } catch (ParseException e
) {
184 * Convert the given {@link Image} object into a Base64 representation of
185 * the same {@link Image}. object.
188 * the {@link Image} object to convert
190 * @return the Base64 representation
192 static public String
fromImage(BufferedImage image
) {
193 String imageString
= null;
194 ByteArrayOutputStream out
= new ByteArrayOutputStream();
197 ImageIO
.write(image
, "png", out
);
198 byte[] imageBytes
= out
.toByteArray();
200 imageString
= DatatypeConverter
.printBase64Binary(imageBytes
);
203 } catch (IOException e
) {
210 * Convert the given Base64 representation of an image into an {@link Image}
214 * the {@link Image} in Base64 format
216 * @return the {@link Image} object
218 * @throws IOException
219 * in case of IO error
221 static public BufferedImage
toImage(String b64data
) throws IOException
{
222 BufferedImage image
= ImageIO
.read(new ByteArrayInputStream(
223 DatatypeConverter
.parseBase64Binary(b64data
)));
229 * Return a hash of the given {@link String}.
236 static public String
getHash(String input
) {
238 MessageDigest md
= MessageDigest
.getInstance("MD5");
239 md
.update(input
.getBytes());
240 byte byteData
[] = md
.digest();
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
);
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