1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.common.utility;
21
22 import com.google.common.io.Files;
23 import org.dom4j.Node;
24 import org.waarp.common.exception.FileTransferException;
25 import org.waarp.common.exception.InvalidArgumentException;
26 import org.waarp.common.logging.WaarpLogger;
27 import org.waarp.common.logging.WaarpLoggerFactory;
28
29 import java.io.File;
30 import java.io.IOException;
31 import java.nio.charset.Charset;
32 import java.sql.Timestamp;
33 import java.text.DateFormat;
34 import java.text.ParseException;
35 import java.text.SimpleDateFormat;
36 import java.util.Arrays;
37 import java.util.Calendar;
38 import java.util.Date;
39 import java.util.GregorianCalendar;
40 import java.util.TimeZone;
41 import java.util.regex.Pattern;
42
43
44
45
46 public final class WaarpStringUtils {
47
48
49
50 private static final WaarpLogger logger =
51 WaarpLoggerFactory.getLogger(WaarpStringUtils.class);
52
53 public static final String UTF_8 = "UTF-8";
54
55
56
57 public static final Charset UTF8 = Charset.forName(UTF_8);
58
59 public static final String BLANK_REGEX = "\\s+";
60 public static final Pattern BLANK = Pattern.compile(BLANK_REGEX);
61 private static final TimeZone z = TimeZone.getTimeZone("GMT");
62
63 private WaarpStringUtils() {
64 }
65
66
67
68
69
70
71
72
73
74
75
76 public static String readFileException(final String filename)
77 throws FileTransferException {
78 final File file = new File(filename);
79
80 if (file.length() > Integer.MAX_VALUE) {
81 throw new FileTransferException(
82 "File is too big for this convenience method (" + file.length() +
83 " bytes).");
84 }
85 try {
86 return Files.asCharSource(file, UTF8).read();
87 } catch (final IOException e) {
88 logger.error("Error on File while trying to read: " + filename + ": {}",
89 e.getMessage());
90 throw new FileTransferException("Error on File while trying to read", e);
91 }
92 }
93
94
95
96
97
98
99
100
101 public static String readFile(final String filename) {
102 try {
103 return readFileException(filename);
104 } catch (final FileTransferException e) {
105 logger.error("Error while trying to read: " + filename + ": {}",
106 e.getMessage());
107 return "";
108 }
109 }
110
111
112
113
114
115
116
117
118 public static Timestamp fixDate(final String date) {
119 Timestamp tdate = null;
120 if (date == null) {
121 return null;
122 }
123 String ndate = date.replaceAll("/|:|\\.|\\s|-", "");
124 if (!ndate.isEmpty()) {
125 if (ndate.length() < 15) {
126 final int len = ndate.length();
127 ndate += "000000000000000".substring(len);
128 }
129 final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
130 try {
131 final Date ddate = format.parse(ndate);
132 tdate = new Timestamp(ddate.getTime());
133 } catch (final ParseException e) {
134 logger.debug("start {}", e.getMessage());
135 }
136 }
137 return tdate;
138 }
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public static Timestamp fixDate(final String date, final Timestamp before) {
154 Timestamp tdate = null;
155 if (date == null) {
156 return null;
157 }
158 String ndate = date.replaceAll("/|:|\\.|\\s|-", "");
159 if (!ndate.isEmpty()) {
160 if (ndate.length() < 15) {
161 final int len = ndate.length();
162 ndate += "000000000000000".substring(len);
163 }
164 final SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
165 try {
166 Date ddate = format.parse(ndate);
167 if (before != null) {
168 final Date bef = new Date(before.getTime());
169 if (bef.compareTo(ddate) >= 0) {
170 ddate = new Date(bef.getTime() + 1000 * 3600 * 24 - 1);
171 }
172 }
173 tdate = new Timestamp(ddate.getTime());
174 } catch (final ParseException e) {
175 logger.debug("start {}", e.getMessage());
176 }
177 }
178 return tdate;
179 }
180
181 public static Timestamp getTodayMidnight() {
182 final GregorianCalendar calendar = new GregorianCalendar();
183 calendar.set(Calendar.HOUR_OF_DAY, 0);
184 calendar.set(Calendar.MINUTE, 0);
185 calendar.set(Calendar.SECOND, 0);
186 calendar.set(Calendar.MILLISECOND, 0);
187 return new Timestamp(calendar.getTimeInMillis());
188 }
189
190
191
192
193
194
195
196
197 public static boolean getBoolean(final Node node) {
198 final String val = node.getText();
199 boolean bval;
200 try {
201 final int ival = Integer.parseInt(val);
202 bval = ival == 1;
203 } catch (final NumberFormatException e) {
204 bval = Boolean.parseBoolean(val);
205 }
206 return bval;
207 }
208
209
210
211
212
213
214
215
216
217
218 public static int getInteger(final Node node)
219 throws InvalidArgumentException {
220 if (node == null) {
221 throw new InvalidArgumentException("Node empty");
222 }
223 final String val = node.getText();
224 final int ival;
225 try {
226 ival = Integer.parseInt(val);
227 } catch (final NumberFormatException e) {
228 throw new InvalidArgumentException("Incorrect value");
229 }
230 return ival;
231 }
232
233
234
235
236
237
238
239
240
241
242
243 public static boolean replace(final StringBuilder builder, final String find,
244 final String replace) {
245 if (find == null) {
246 return false;
247 }
248 final int start = builder.indexOf(find);
249 if (start == -1) {
250 return false;
251 }
252 final int end = start + find.length();
253 if (replace != null) {
254 builder.replace(start, end, replace);
255 } else {
256 builder.replace(start, end, "");
257 }
258 return true;
259 }
260
261
262
263
264
265
266
267
268
269 public static void replaceAll(final StringBuilder builder, final String find,
270 final String replace) {
271 while (replace(builder, find, replace)) {
272
273 }
274 }
275
276
277
278
279
280
281
282
283
284 public static String fillString(final char fillChar, final int count) {
285 final char[] chars = new char[count];
286 Arrays.fill(chars, fillChar);
287 return new String(chars);
288 }
289
290
291
292
293
294
295
296
297
298 public static String cleanJsonForHtml(final String json) {
299 return json.replaceAll("([^\\\\])\\\\n", "$1")
300 .replaceAll("([^\\\\])\\\\r", "$1");
301 }
302
303 public static DateFormat getDateFormat() {
304 final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd z");
305 dateFormat.setTimeZone(z);
306 return dateFormat;
307 }
308
309 public static DateFormat getTimeFormat() {
310 final DateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss z");
311 timeFormat.setTimeZone(z);
312 return timeFormat;
313 }
314
315 public static DateFormat getTimestampFormat() {
316 final DateFormat timestampFormat =
317 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
318 timestampFormat.setTimeZone(z);
319 return timestampFormat;
320 }
321 }