1 package be
.nikiroo
.utils
;
4 import java
.awt
.image
.BufferedImage
;
5 import java
.io
.ByteArrayInputStream
;
6 import java
.io
.ByteArrayOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.InputStream
;
10 import java
.security
.MessageDigest
;
11 import java
.security
.NoSuchAlgorithmException
;
12 import java
.text
.Normalizer
;
13 import java
.text
.Normalizer
.Form
;
14 import java
.text
.ParseException
;
15 import java
.text
.SimpleDateFormat
;
16 import java
.util
.Date
;
17 import java
.util
.regex
.Pattern
;
19 import javax
.imageio
.ImageIO
;
21 import org
.unbescape
.html
.HtmlEscape
;
22 import org
.unbescape
.html
.HtmlEscapeLevel
;
23 import org
.unbescape
.html
.HtmlEscapeType
;
26 * This class offer some utilities based around {@link String}s.
30 public class StringUtils
{
32 * This enum type will decide the alignment of a {@link String} when padding
33 * is applied or if there is enough horizontal space for it to be aligned.
35 public enum Alignment
{
36 /** Aligned at left. */
40 /** Aligned at right. */
44 static private Pattern marks
= Pattern
45 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
48 * Fix the size of the given {@link String} either with space-padding or by
52 * the {@link String} to fix
54 * the size of the resulting {@link String} or -1 for a noop
56 * @return the resulting {@link String} of size <i>size</i>
58 static public String
padString(String text
, int width
) {
59 return padString(text
, width
, true, Alignment
.Beginning
);
63 * Fix the size of the given {@link String} either with space-padding or by
64 * optionally shortening it.
67 * the {@link String} to fix
69 * the size of the resulting {@link String} if the text fits or
70 * if cut is TRUE or -1 for a noop
72 * cut the {@link String} shorter if needed
74 * align the {@link String} in this position if we have enough
77 * @return the resulting {@link String} of size <i>size</i> minimum
79 static public String
padString(String text
, int width
, boolean cut
,
86 int diff
= width
- text
.length();
90 text
= text
.substring(0, width
);
91 } else if (diff
> 0) {
92 if (diff
< 2 && align
!= Alignment
.End
)
93 align
= Alignment
.Beginning
;
97 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
100 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
104 int pad1
= (diff
) / 2;
105 int pad2
= (diff
+ 1) / 2;
106 text
= new String(new char[pad1
]).replace('\0', ' ') + text
107 + new String(new char[pad2
]).replace('\0', ' ');
117 * Sanitise the given input to make it more Terminal-friendly by removing
118 * combining characters.
121 * the input to sanitise
122 * @param allowUnicode
123 * allow Unicode or only allow ASCII Latin characters
125 * @return the sanitised {@link String}
127 static public String
sanitize(String input
, boolean allowUnicode
) {
128 return sanitize(input
, allowUnicode
, !allowUnicode
);
132 * Sanitise the given input to make it more Terminal-friendly by removing
133 * combining characters.
136 * the input to sanitise
137 * @param allowUnicode
138 * allow Unicode or only allow ASCII Latin characters
139 * @param removeAllAccents
140 * TRUE to replace all accentuated characters by their non
141 * accentuated counter-parts
143 * @return the sanitised {@link String}
145 static public String
sanitize(String input
, boolean allowUnicode
,
146 boolean removeAllAccents
) {
148 if (removeAllAccents
) {
149 input
= Normalizer
.normalize(input
, Form
.NFKD
);
150 input
= marks
.matcher(input
).replaceAll("");
153 input
= Normalizer
.normalize(input
, Form
.NFKC
);
156 StringBuilder builder
= new StringBuilder();
157 for (int index
= 0; index
< input
.length(); index
++) {
158 char car
= input
.charAt(index
);
159 // displayable chars in ASCII are in the range 32<->255,
161 if (car
>= 32 && car
<= 255 && car
!= 127) {
165 input
= builder
.toString();
172 * Convert between time in milliseconds to {@link String} in a "static" way
173 * (to exchange data over the wire, for instance).
176 * the time in milliseconds
178 * @return the time as a {@link String}
180 static public String
fromTime(long time
) {
181 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
182 return sdf
.format(new Date(time
));
186 * Convert between time as a {@link String} to milliseconds in a "static"
187 * way (to exchange data over the wire, for instance).
190 * the time as a {@link String}
192 * @return the time in milliseconds
194 static public long toTime(String display
) {
195 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
197 return sdf
.parse(display
).getTime();
198 } catch (ParseException e
) {
204 * Convert the given {@link Image} object into a Base64 representation of
205 * the same {@link Image}. object.
208 * the {@link Image} object to convert
210 * @return the Base64 representation
212 * @throws IOException
213 * in case of IO error
215 static public String
fromImage(BufferedImage image
) throws IOException
{
216 String imageString
= null;
217 ByteArrayOutputStream out
= new ByteArrayOutputStream();
219 ImageIO
.write(image
, "jpeg", out
);
220 byte[] imageBytes
= out
.toByteArray();
222 imageString
= new String(Base64
.encodeBytes(imageBytes
));
230 * Convert the given {@link File} image into a Base64 representation of the
234 * the {@link File} image to convert
236 * @return the Base64 representation
238 * @throws IOException
239 * in case of IO error
241 static public String
fromStream(InputStream in
) throws IOException
{
242 String fileString
= null;
243 ByteArrayOutputStream out
= new ByteArrayOutputStream();
245 byte[] buf
= new byte[8192];
248 while ((c
= in
.read(buf
, 0, buf
.length
)) > 0) {
249 out
.write(buf
, 0, c
);
254 fileString
= new String(Base64
.encodeBytes(out
.toByteArray()));
261 * Convert the given Base64 representation of an image into an {@link Image}
265 * the {@link Image} in Base64 format
267 * @return the {@link Image} object
269 * @throws IOException
270 * in case of IO error
272 static public BufferedImage
toImage(String b64data
) throws IOException
{
273 ByteArrayInputStream in
= new ByteArrayInputStream(
274 Base64
.decode(b64data
));
275 return IOUtils
.toImage(in
);
279 * Return a hash of the given {@link String}.
286 static public String
getHash(String input
) {
288 MessageDigest md
= MessageDigest
.getInstance("MD5");
289 md
.update(input
.getBytes());
290 byte byteData
[] = md
.digest();
292 StringBuffer hexString
= new StringBuffer();
293 for (int i
= 0; i
< byteData
.length
; i
++) {
294 String hex
= Integer
.toHexString(0xff & byteData
[i
]);
295 if (hex
.length() == 1)
296 hexString
.append('0');
297 hexString
.append(hex
);
300 return hexString
.toString();
301 } catch (NoSuchAlgorithmException e
) {
307 * Remove the HTML content from the given input, and un-html-ize the rest.
310 * the HTML-encoded content
312 * @return the HTML-free equivalent content
314 public static String
unhtml(String html
) {
315 StringBuilder builder
= new StringBuilder();
318 for (char car
: html
.toCharArray()) {
321 } else if (car
== '>') {
323 } else if (inTag
<= 0) {
328 return HtmlEscape
.unescapeHtml(builder
.toString());
332 * Escape the given {@link String} so it can be used in XML, as content.
335 * the input {@link String}
337 * @return the escaped {@link String}
339 public static String
xmlEscape(String input
) {
344 return HtmlEscape
.escapeHtml(input
,
345 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
346 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);
350 * Escape the given {@link String} so it can be used in XML, as text content
351 * inside double-quotes.
354 * the input {@link String}
356 * @return the escaped {@link String}
358 public static String
xmlEscapeQuote(String input
) {
363 return HtmlEscape
.escapeHtml(input
,
364 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
365 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);