1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package org.webmacro.servlet; |
24 | |
|
25 | |
import org.webmacro.Context; |
26 | |
import org.webmacro.ContextTool; |
27 | |
import org.webmacro.PropertyException; |
28 | |
import org.webmacro.WebMacroRuntimeException; |
29 | |
|
30 | |
import java.io.IOException; |
31 | |
import java.io.InputStream; |
32 | |
import java.io.UnsupportedEncodingException; |
33 | |
import java.text.SimpleDateFormat; |
34 | |
import java.util.ArrayList; |
35 | |
import java.util.Arrays; |
36 | |
import java.util.Date; |
37 | |
import java.util.List; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
public class TextTool extends ContextTool |
46 | |
{ |
47 | |
|
48 | |
|
49 | 2 | private static final TextTool _instance = new TextTool(); |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public static TextTool getInstance () |
56 | |
{ |
57 | 0 | return _instance; |
58 | |
} |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
public static String[] split (String input, String delimiter) |
74 | |
{ |
75 | 0 | if (input == null || delimiter == null) |
76 | 0 | return new String[0]; |
77 | |
|
78 | 0 | List l = new ArrayList(); |
79 | 0 | int delimlen = delimiter.length(); |
80 | 0 | int idx, lastidx = 0; |
81 | |
|
82 | 0 | while ((idx = input.indexOf(delimiter, lastidx)) > -1) |
83 | |
{ |
84 | 0 | l.add(input.substring(lastidx, idx)); |
85 | 0 | lastidx = idx + delimlen; |
86 | |
} |
87 | 0 | if (lastidx < input.length()) l.add(input.substring(lastidx)); |
88 | |
|
89 | 0 | return (String[]) l.toArray(new String[0]); |
90 | |
} |
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
public static String join (String[] input, String delimiter) |
100 | |
{ |
101 | 0 | if (input == null || delimiter == null) |
102 | 0 | return null; |
103 | |
|
104 | 0 | StringBuffer sb = new StringBuffer(); |
105 | 0 | for (int x = 0; x < input.length; x++) |
106 | |
{ |
107 | 0 | if (x > 0) |
108 | 0 | sb.append(delimiter); |
109 | 0 | sb.append(input[x]); |
110 | |
} |
111 | |
|
112 | 0 | return sb.toString(); |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
public static String replace (String src, String from, String to) |
127 | |
{ |
128 | 0 | if (src == null || from == null || to == null) |
129 | 0 | return src; |
130 | |
|
131 | 0 | int fromlen = from.length(); |
132 | 0 | int idx, lastidx = 0; |
133 | |
|
134 | 0 | StringBuffer newstr = new StringBuffer(); |
135 | |
|
136 | 0 | while ((idx = src.indexOf(from, lastidx)) > -1) |
137 | |
{ |
138 | 0 | newstr.append(src.substring(lastidx, idx)); |
139 | 0 | newstr.append(to); |
140 | 0 | lastidx = idx + fromlen; |
141 | |
} |
142 | 0 | if (lastidx == 0) |
143 | 0 | return src; |
144 | |
else |
145 | 0 | newstr.append(src.substring(lastidx)); |
146 | |
|
147 | 0 | return newstr.toString(); |
148 | |
} |
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
public static String URLEncode (String input) |
159 | |
{ |
160 | 0 | if (input == null) |
161 | 0 | return null; |
162 | |
try { |
163 | 0 | return java.net.URLEncoder.encode(input, System.getProperty( "file.encoding" )); |
164 | 0 | } catch (UnsupportedEncodingException e) { |
165 | 0 | throw new WebMacroRuntimeException( "WebMacro bug System.file.encoding not supported", e ); |
166 | |
} |
167 | |
} |
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
public static String URLDecode (String input) |
181 | |
{ |
182 | 0 | if (input == null) |
183 | 0 | return null; |
184 | |
try { |
185 | 0 | return java.net.URLDecoder.decode(input, System.getProperty( "file.encoding" )); |
186 | 0 | } catch (UnsupportedEncodingException e) { |
187 | 0 | throw new WebMacroRuntimeException( "WebMacro bug System.file.encoding not supported", e ); |
188 | |
} |
189 | |
} |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
public static String HTMLEncode (String input) |
201 | |
{ |
202 | 0 | return input == null ? "" : org.webmacro.util.HTMLEscaper.escape(input); |
203 | |
} |
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
public static String formatDate (Date date, String format) |
213 | |
{ |
214 | 0 | SimpleDateFormat formatter = new SimpleDateFormat(format); |
215 | 0 | return formatter.format(date); |
216 | |
} |
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
public static String streamToString (InputStream in) throws IOException |
226 | |
{ |
227 | 0 | return streamToString(in, null); |
228 | |
} |
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
public static String streamToString (InputStream in, final String encoding) throws IOException |
238 | |
{ |
239 | 0 | if (in == null) |
240 | 0 | return null; |
241 | 0 | StringBuffer sb = new StringBuffer(); |
242 | |
try |
243 | |
{ |
244 | |
int cnt; |
245 | 0 | byte[] buff = new byte[4096]; |
246 | 0 | while ((cnt = in.read(buff)) > -1) |
247 | |
{ |
248 | 0 | if (encoding != null) |
249 | 0 | sb.append(new String(buff, 0, cnt, encoding)); |
250 | |
else |
251 | 0 | sb.append(new String(buff, 0, cnt)); |
252 | |
} |
253 | |
} |
254 | |
finally |
255 | |
{ |
256 | 0 | in.close(); |
257 | 0 | } |
258 | |
|
259 | 0 | return sb.toString(); |
260 | |
} |
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
public static String bytesToString (byte[] bytes) |
267 | |
{ |
268 | 0 | return new String(bytes); |
269 | |
} |
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
public static String bytesToString (byte[] bytes, String encoding) throws UnsupportedEncodingException |
276 | |
{ |
277 | 0 | return new String(bytes, encoding); |
278 | |
} |
279 | |
|
280 | |
|
281 | |
|
282 | |
public static String trim (String s) |
283 | |
{ |
284 | 0 | return s.trim(); |
285 | |
} |
286 | |
|
287 | |
|
288 | |
public static String ltrim (String s) |
289 | |
{ |
290 | 0 | if (s == null) return null; |
291 | 0 | for (int i = 0; i < s.length(); i++) |
292 | |
{ |
293 | 0 | if (!Character.isWhitespace(s.charAt(i))) return s.substring(i); |
294 | |
} |
295 | |
|
296 | 0 | return ""; |
297 | |
} |
298 | |
|
299 | |
|
300 | |
public static String rtrim (String s) |
301 | |
{ |
302 | 0 | if (s == null) return null; |
303 | 0 | for (int i = s.length() - 1; i > -1; i--) |
304 | |
{ |
305 | 0 | if (!Character.isWhitespace(s.charAt(i))) return s.substring(0, i + 1); |
306 | |
} |
307 | |
|
308 | 0 | return ""; |
309 | |
} |
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
public static String[] getLines (String block) |
316 | |
{ |
317 | 0 | if (block == null) return null; |
318 | 0 | String delim = "\r\n"; |
319 | 0 | if (block.indexOf("\r\n") == -1) |
320 | |
{ |
321 | 0 | if (block.indexOf('\n') > -1) |
322 | 0 | delim = "\n"; |
323 | |
else |
324 | 0 | delim = "\r"; |
325 | |
} |
326 | 0 | return TextTool.split(block, delim); |
327 | |
} |
328 | |
|
329 | |
|
330 | |
public static String makeBlock (String[] lines) |
331 | |
{ |
332 | 0 | if (lines == null || lines.length == 0) return null; |
333 | 0 | if (lines.length == 1) return lines[0]; |
334 | 0 | StringBuffer sb = new StringBuffer(lines[0]); |
335 | 0 | for (int i = 1; i < lines.length; i++) |
336 | |
{ |
337 | 0 | sb.append('\r').append('\n').append(lines[i]); |
338 | |
} |
339 | 0 | return sb.toString(); |
340 | |
} |
341 | |
|
342 | |
|
343 | |
|
344 | |
public static String trimBlock (String block) |
345 | |
{ |
346 | 0 | if (block == null) return null; |
347 | 0 | String[] lines = getLines(block); |
348 | 0 | for (int i = 0; i < lines.length; i++) lines[i] = trim(lines[i]); |
349 | 0 | return makeBlock(lines); |
350 | |
} |
351 | |
|
352 | |
|
353 | |
public static String ltrimBlock (String block) |
354 | |
{ |
355 | 0 | if (block == null) return null; |
356 | 0 | String[] lines = getLines(block); |
357 | 0 | for (int i = 0; i < lines.length; i++) lines[i] = ltrim(lines[i]); |
358 | 0 | return makeBlock(lines); |
359 | |
} |
360 | |
|
361 | |
|
362 | |
public static String rtrimBlock (String block) |
363 | |
{ |
364 | 0 | if (block == null) return null; |
365 | 0 | String[] lines = getLines(block); |
366 | 0 | for (int i = 0; i < lines.length; i++) lines[i] = rtrim(lines[i]); |
367 | 0 | return makeBlock(lines); |
368 | |
} |
369 | |
|
370 | |
|
371 | |
public static String toUpperCase (String s) |
372 | |
{ |
373 | 0 | return s.toUpperCase(); |
374 | |
} |
375 | |
|
376 | |
|
377 | |
public static String toLowerCase (String s) |
378 | |
{ |
379 | 0 | return s.toLowerCase(); |
380 | |
} |
381 | |
|
382 | |
|
383 | |
|
384 | |
|
385 | |
|
386 | |
|
387 | |
|
388 | |
|
389 | |
public TextTool () |
390 | 8 | { |
391 | 8 | } |
392 | |
|
393 | |
|
394 | |
|
395 | |
|
396 | |
|
397 | |
public TextTool (Context context) |
398 | 0 | { |
399 | 0 | } |
400 | |
|
401 | |
|
402 | |
|
403 | |
|
404 | |
|
405 | |
|
406 | |
public Object init (Context context) throws PropertyException |
407 | |
{ |
408 | 0 | return TextTool.getInstance(); |
409 | |
} |
410 | |
|
411 | |
|
412 | |
|
413 | |
|
414 | |
|
415 | |
public static void main (String[] args) throws Exception |
416 | |
{ |
417 | 0 | String input1 = "This is a test"; |
418 | 0 | String input2 = "This is a test"; |
419 | 0 | String[] split = TextTool.split(input2, " "); |
420 | |
|
421 | 0 | System.err.println(TextTool.replace(input1, "is", "isn't")); |
422 | |
|
423 | 0 | for (int x = 0; x < split.length; x++) |
424 | 0 | System.err.println("/" + split[x] + "/"); |
425 | |
|
426 | 0 | String s1 = "alpha\r\nbeta\r\ndelta\r\ngamma"; |
427 | 0 | String[] split2 = TextTool.split(s1, "\r\n"); |
428 | 0 | System.err.println("split2=" + Arrays.asList(split2)); |
429 | 0 | } |
430 | |
} |