1 package be
.nikiroo
.utils
;
3 import java
.io
.ByteArrayInputStream
;
4 import java
.io
.IOException
;
5 import java
.security
.MessageDigest
;
6 import java
.security
.NoSuchAlgorithmException
;
7 import java
.text
.Normalizer
;
8 import java
.text
.Normalizer
.Form
;
9 import java
.text
.ParseException
;
10 import java
.text
.SimpleDateFormat
;
11 import java
.util
.Date
;
12 import java
.util
.Scanner
;
13 import java
.util
.regex
.Pattern
;
15 import org
.unbescape
.html
.HtmlEscape
;
16 import org
.unbescape
.html
.HtmlEscapeLevel
;
17 import org
.unbescape
.html
.HtmlEscapeType
;
20 * This class offer some utilities based around {@link String}s.
24 public class StringUtils
{
26 * This enum type will decide the alignment of a {@link String} when padding
27 * is applied or if there is enough horizontal space for it to be aligned.
29 public enum Alignment
{
30 /** Aligned at left. */
34 /** Aligned at right. */
38 static private Pattern marks
= Pattern
39 .compile("[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+");
42 * Fix the size of the given {@link String} either with space-padding or by
46 * the {@link String} to fix
48 * the size of the resulting {@link String} or -1 for a noop
50 * @return the resulting {@link String} of size <i>size</i>
52 static public String
padString(String text
, int width
) {
53 return padString(text
, width
, true, Alignment
.Beginning
);
57 * Fix the size of the given {@link String} either with space-padding or by
58 * optionally shortening it.
61 * the {@link String} to fix
63 * the size of the resulting {@link String} if the text fits or
64 * if cut is TRUE or -1 for a noop
66 * cut the {@link String} shorter if needed
68 * align the {@link String} in this position if we have enough
71 * @return the resulting {@link String} of size <i>size</i> minimum
73 static public String
padString(String text
, int width
, boolean cut
,
80 int diff
= width
- text
.length();
84 text
= text
.substring(0, width
);
85 } else if (diff
> 0) {
86 if (diff
< 2 && align
!= Alignment
.End
)
87 align
= Alignment
.Beginning
;
91 text
= text
+ new String(new char[diff
]).replace('\0', ' ');
94 text
= new String(new char[diff
]).replace('\0', ' ') + text
;
98 int pad1
= (diff
) / 2;
99 int pad2
= (diff
+ 1) / 2;
100 text
= new String(new char[pad1
]).replace('\0', ' ') + text
101 + new String(new char[pad2
]).replace('\0', ' ');
111 * Sanitise the given input to make it more Terminal-friendly by removing
112 * combining characters.
115 * the input to sanitise
116 * @param allowUnicode
117 * allow Unicode or only allow ASCII Latin characters
119 * @return the sanitised {@link String}
121 static public String
sanitize(String input
, boolean allowUnicode
) {
122 return sanitize(input
, allowUnicode
, !allowUnicode
);
126 * Sanitise the given input to make it more Terminal-friendly by removing
127 * combining characters.
130 * the input to sanitise
131 * @param allowUnicode
132 * allow Unicode or only allow ASCII Latin characters
133 * @param removeAllAccents
134 * TRUE to replace all accentuated characters by their non
135 * accentuated counter-parts
137 * @return the sanitised {@link String}
139 static public String
sanitize(String input
, boolean allowUnicode
,
140 boolean removeAllAccents
) {
142 if (removeAllAccents
) {
143 input
= Normalizer
.normalize(input
, Form
.NFKD
);
144 input
= marks
.matcher(input
).replaceAll("");
147 input
= Normalizer
.normalize(input
, Form
.NFKC
);
150 StringBuilder builder
= new StringBuilder();
151 for (int index
= 0; index
< input
.length(); index
++) {
152 char car
= input
.charAt(index
);
153 // displayable chars in ASCII are in the range 32<->255,
155 if (car
>= 32 && car
<= 255 && car
!= 127) {
159 input
= builder
.toString();
166 * Convert between time in milliseconds to {@link String} in a "static" way
167 * (to exchange data over the wire, for instance).
170 * the time in milliseconds
172 * @return the time as a {@link String}
174 static public String
fromTime(long time
) {
175 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
176 return sdf
.format(new Date(time
));
180 * Convert between time as a {@link String} to milliseconds in a "static"
181 * way (to exchange data over the wire, for instance).
184 * the time as a {@link String}
186 * @return the time in milliseconds
188 static public long toTime(String displayTime
) {
189 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
191 return sdf
.parse(displayTime
).getTime();
192 } catch (ParseException e
) {
198 * Return a hash of the given {@link String}.
205 static public String
getMd5Hash(String input
) {
207 MessageDigest md
= MessageDigest
.getInstance("MD5");
208 md
.update(input
.getBytes());
209 byte byteData
[] = md
.digest();
211 StringBuffer hexString
= new StringBuffer();
212 for (int i
= 0; i
< byteData
.length
; i
++) {
213 String hex
= Integer
.toHexString(0xff & byteData
[i
]);
214 if (hex
.length() == 1)
215 hexString
.append('0');
216 hexString
.append(hex
);
219 return hexString
.toString();
220 } catch (NoSuchAlgorithmException e
) {
226 * Remove the HTML content from the given input, and un-html-ize the rest.
229 * the HTML-encoded content
231 * @return the HTML-free equivalent content
233 public static String
unhtml(String html
) {
234 StringBuilder builder
= new StringBuilder();
237 for (char car
: html
.toCharArray()) {
240 } else if (car
== '>') {
242 } else if (inTag
<= 0) {
247 char nbsp
= ' '; // non-breakable space (a special char)
249 return HtmlEscape
.unescapeHtml(builder
.toString()).replace(nbsp
, space
);
253 * Escape the given {@link String} so it can be used in XML, as content.
256 * the input {@link String}
258 * @return the escaped {@link String}
260 public static String
xmlEscape(String input
) {
265 return HtmlEscape
.escapeHtml(input
,
266 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
267 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);
271 * Escape the given {@link String} so it can be used in XML, as text content
272 * inside double-quotes.
275 * the input {@link String}
277 * @return the escaped {@link String}
279 public static String
xmlEscapeQuote(String input
) {
284 return HtmlEscape
.escapeHtml(input
,
285 HtmlEscapeType
.HTML4_NAMED_REFERENCES_DEFAULT_TO_HEXA
,
286 HtmlEscapeLevel
.LEVEL_1_ONLY_MARKUP_SIGNIFICANT
);
289 public static String
zip64(String data
) {
291 return Base64
.encodeBytes(data
.getBytes(), Base64
.GZIP
);
292 } catch (IOException e
) {
298 public static String
unzip64(String data
) throws IOException
{
299 ByteArrayInputStream in
= new ByteArrayInputStream(Base64
.decode(data
,
302 Scanner scan
= new Scanner(in
);
303 scan
.useDelimiter("\\A");