1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
5 import java
.io
.UnsupportedEncodingException
;
6 import java
.security
.MessageDigest
;
7 import java
.security
.NoSuchAlgorithmException
;
8 import java
.text
.Normalizer
;
9 import java
.text
.Normalizer
.Form
;
10 import java
.text
.ParseException
;
11 import java
.text
.SimpleDateFormat
;
12 import java
.util
.Date
;
13 import java
.util
.Scanner
;
14 import java
.util
.regex
.Pattern
;
16 import org
.unbescape
.html
.HtmlEscape
;
17 import org
.unbescape
.html
.HtmlEscapeLevel
;
18 import org
.unbescape
.html
.HtmlEscapeType
;
21 * This class offer some utilities based around {@link String}s.
25 public class StringUtils
{
27 * This enum type will decide the alignment of a {@link String} when padding
28 * is applied or if there is enough horizontal space for it to be aligned.
30 public enum Alignment
{
31 /** Aligned at left. */
35 /** Aligned at right. */
39 static private Pattern marks
= Pattern
40 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
43 * Fix the size of the given {@link String} either with space-padding or by
47 * the {@link String} to fix
49 * the size of the resulting {@link String} or -1 for a noop
51 * @return the resulting {@link String} of size <i>size</i>
53 static public String
padString(String text
, int width
) {
54 return padString(text
, width
, true, Alignment
.Beginning
);
58 * Fix the size of the given {@link String} either with space-padding or by
59 * optionally shortening it.
62 * the {@link String} to fix
64 * the size of the resulting {@link String} if the text fits or
65 * if cut is TRUE or -1 for a noop
67 * cut the {@link String} shorter if needed
69 * align the {@link String} in this position if we have enough
72 * @return the resulting {@link String} of size <i>size</i> minimum
74 static public String
padString(String text
, int width
, boolean cut
,
81 int diff
= width
- text
.length();
85 text
= text
.substring(0, width
);
86 } else if (diff
> 0) {
87 if (diff
< 2 && align
!= Alignment
.End
)
88 align
= Alignment
.Beginning
;
92 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
95 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
99 int pad1
= (diff
) / 2;
100 int pad2
= (diff
+ 1) / 2;
101 text
= new String(new char[pad1
]).replace('\0', ' ') + text
102 + new String(new char[pad2
]).replace('\0', ' ');
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
120 * @return the sanitised {@link String}
122 static public String
sanitize(String input
, boolean allowUnicode
) {
123 return sanitize(input
, allowUnicode
, !allowUnicode
);
127 * Sanitise the given input to make it more Terminal-friendly by removing
128 * combining characters.
131 * the input to sanitise
132 * @param allowUnicode
133 * allow Unicode or only allow ASCII Latin characters
134 * @param removeAllAccents
135 * TRUE to replace all accentuated characters by their non
136 * accentuated counter-parts
138 * @return the sanitised {@link String}
140 static public String
sanitize(String input
, boolean allowUnicode
,
141 boolean removeAllAccents
) {
143 if (removeAllAccents
) {
144 input
= Normalizer
.normalize(input
, Form
.NFKD
);
145 input
= marks
.matcher(input
).replaceAll("");
148 input
= Normalizer
.normalize(input
, Form
.NFKC
);
151 StringBuilder builder
= new StringBuilder();
152 for (int index
= 0; index
< input
.length(); index
++) {
153 char car
= input
.charAt(index
);
154 // displayable chars in ASCII are in the range 32<->255,
156 if (car
>= 32 && car
<= 255 && car
!= 127) {
160 input
= builder
.toString();
167 * Convert between time in milliseconds to {@link String} in a "static" way
168 * (to exchange data over the wire, for instance).
171 * the time in milliseconds
173 * @return the time as a {@link String}
175 static public String
fromTime(long time
) {
176 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
177 return sdf
.format(new Date(time
));
181 * Convert between time as a {@link String} to milliseconds in a "static"
182 * way (to exchange data over the wire, for instance).
185 * the time as a {@link String}
187 * @return the time in milliseconds
189 static public long toTime(String displayTime
) {
190 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
192 return sdf
.parse(displayTime
).getTime();
193 } catch (ParseException e
) {
199 * Return a hash of the given {@link String}.
206 static public String
getMd5Hash(String input
) {
208 MessageDigest md
= MessageDigest
.getInstance("MD5");
209 md
.update(input
.getBytes("UTF-8"));
210 byte byteData
[] = md
.digest();
212 StringBuffer hexString
= new StringBuffer();
213 for (int i
= 0; i
< byteData
.length
; i
++) {
214 String hex
= Integer
.toHexString(0xff & byteData
[i
]);
215 if (hex
.length() == 1)
216 hexString
.append('0');
217 hexString
.append(hex
);
220 return hexString
.toString();
221 } catch (NoSuchAlgorithmException e
) {
223 } catch (UnsupportedEncodingException e
) {
229 * Remove the HTML content from the given input, and un-html-ize the rest.
232 * the HTML-encoded content
234 * @return the HTML-free equivalent content
236 public static String
unhtml(String html
) {
237 StringBuilder builder
= new StringBuilder();
240 for (char car
: html
.toCharArray()) {
243 } else if (car
== '>') {
245 } else if (inTag
<= 0) {
250 char nbsp
= ' '; // non-breakable space (a special char)
252 return HtmlEscape
.unescapeHtml(builder
.toString()).replace(nbsp
, space
);
256 * Escape the given {@link String} so it can be used in XML, as content.
259 * the input {@link String}
261 * @return the escaped {@link String}
263 public static String
xmlEscape(String input
) {
268 return HtmlEscape
.escapeHtml(input
,
269 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
270 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);
274 * Escape the given {@link String} so it can be used in XML, as text content
275 * inside double-quotes.
278 * the input {@link String}
280 * @return the escaped {@link String}
282 public static String
xmlEscapeQuote(String input
) {
287 return HtmlEscape
.escapeHtml(input
,
288 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
289 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);
292 public static String
zip64(String data
) {
294 return Base64
.encodeBytes(data
.getBytes(), Base64
.GZIP
);
295 } catch (IOException e
) {
301 public static String
unzip64(String data
) throws IOException
{
302 ByteArrayInputStream in
= new ByteArrayInputStream(Base64
.decode(data
,
305 Scanner scan
= new Scanner(in
);
306 scan
.useDelimiter("\\A");