##### Coverage Report - org.webmacro.servlet.TextTool

Classes in this File | Line Coverage | Branch Coverage | Complexity
TextTool |
2% | 3/111
0% | 0/78
| 3.3846153846153846;3.385

1 |  |
     /*
2 |  |
      * Copyright (C) 1998-2000 Semiotek Inc.  All Rights Reserved.
3 |  |
      *
4 |  |
      * Redistribution and use in source and binary forms, with or without
5 |  |
      * modification, are permitted under the terms of either of the following
6 |  |
      * Open Source licenses:
7 |  |
      *
8 |  |
      * The GNU General Public License, version 2, or any later version, as
9 |  |
      * published by the Free Software Foundation
10 |  |
      * (http://www.fsf.org/copyleft/gpl.html);
11 |  |
      *
12 |  |
      *  or
13 |  |
      *
14 |  |
      * The Semiotek Public License (http://webmacro.org/LICENSE.)
15 |  |
      *
16 |  |
      * This software is provided "as is", with NO WARRANTY, not even the
17 |  |
      * implied warranties of fitness to purpose, or merchantability. You
18 |  |
      * assume all risks and liabilities associated with its use.
19 |  |
      *
20 |  |
      * See www.webmacro.org for more information on the WebMacro project.
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 |  |
      * A ContextTool for String manipulation.
41 |  |
      *
42 |  |
      * @author Eric B. Ridge (mailto:ebr@tcdi.com)
43 |  |
      */
44 |  |
45 |  |
     public class TextTool extends ContextTool
46 |  |
     {
47 |  |
48 |  |
         /** our lonely singleton */
49 | 2 |
         private static final TextTool _instance = new TextTool();
50 |  |
51 |  |
52 |  |
         /**
53 |  |
          * @return the static instance of the TextTool
54 |  |
          */
55 |  |
         public static TextTool getInstance ()
56 |  |
         {
57 | 0 |
             return _instance;
58 |  |
         }
59 |  |
60 |  |
61 |  |
         //
62 |  |
         // static TextTool methods
63 |  |
         //
64 |  |
65 |  |
         /**
66 |  |
          * Split the <code>input</code> String into a String[] at each
67 |  |
          * instance of <code>delimiter</code>.  The delimiter string is <b>not</b>
68 |  |
          * included in the returned array.<p>
69 |  |
          *
70 |  |
          * If either parameter is <code>null</code>, <code>split()</code> returns
71 |  |
          * a String[] of zero elements.
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 |  |
          * Join the <code>input</code> String[] into a single String, using
94 |  |
          * the specified <code>delimiter</code> between each value of the array.<p>
95 |  |
          *
96 |  |
          * If either parameter is <code>null</code>, <code>join()</code> returns
97 |  |
          * <code>null</code>.
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 |  |
          * Replace all occurrences of <code>from</code> to <code>to</code>
117 |  |
          * in <code>src</code>.
118 |  |
          * <P>
119 |  |
          * If any parameter is <code>null</code>, <code>replace()</code> returns
120 |  |
          * the value of <code>src</code>.<p>
121 |  |
          *
122 |  |
          * @return String with all occurrences of <code>from</code> replaced with
123 |  |
          *          <code>to</code> in <code>src</code> or <code>src</code> if
124 |  |
          *          <code>from</code> not found
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 |  |
          * URLEncode the specified <code>input</code> String.<p>
152 |  |
          *
153 |  |
          * If <code>input</code> is <code>null</code>, <code>URLEncode()</code>
154 |  |
          * will return <code>null</code>.
155 |  |
          *
156 |  |
          * @see java.net.URLEncoder
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 |  |
          * Decode a URLEncoded <code>input</code> String.
171 |  |
          * Assumes that the URL was encoded with URLEncode above.
172 |  |
          *
173 |  |
          * <p>
174 |  |
          *
175 |  |
          * If <code>input</code> is <code>null</code>, <code>URLEncode()</code>
176 |  |
          * will return <code>null</code>.
177 |  |
          *
178 |  |
          * @see java.net.URLDecoder
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 |  |
          * HTMLEncode will encode the specified <code>input</code> String
193 |  |
          * per the magic of <code>org.webmacro.util.HTMLEscaper</code>.<p>
194 |  |
          *
195 |  |
          * If the <code>input</code> is <code>null</code>, <code>HTMLEncode()</code>
196 |  |
          * will return <b><code>an empty string</code></b>.
197 |  |
          *
198 |  |
          * @see org.webmacro.util.HTMLEscaper
199 |  |
          */
200 |  |
         public static String HTMLEncode (String input)
201 |  |
         {
202 | 0 |
             return input == null ? "" : org.webmacro.util.HTMLEscaper.escape(input);
203 |  |
         }
204 |  |
205 |  |
         /**
206 |  |
          * Format a <code>java.util.Date</code> object using the specified date
207 |  |
          * format string.
208 |  |
          *
209 |  |
          * @see java.util.Date
210 |  |
          * @see java.text.SimpleDateFormat
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 |  |
          * Write the bytes of the specified InputStream into a String using
220 |  |
          * the default character encoding of your platform.  Upon completion, (or in
221 |  |
          * the event of an Exception) this method will close the input stream.<p>
222 |  |
          *
223 |  |
          * @throws java.io.IOException if the stream cannot be read
224 |  |
          */
225 |  |
         public static String streamToString (InputStream in) throws IOException
226 |  |
         {
227 | 0 |
             return streamToString(in, null);
228 |  |
         }
229 |  |
230 |  |
         /**
231 |  |
          * Write the bytes of the specified InputStream into a String using
232 |  |
          * the specified character encoding.  Upon completion (or in the event of
233 |  |
          * an Exception), this method will close the input stream.<p>
234 |  |
          *
235 |  |
          * @throws java.io.IOException if the stream cannot be read
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 |  |
          * convert a <code>byte[]</code> to a String using the system-default
264 |  |
          * encoding
265 |  |
          */
266 |  |
         public static String bytesToString (byte[] bytes)
267 |  |
         {
268 | 0 |
             return new String(bytes);
269 |  |
         }
270 |  |
271 |  |
         /**
272 |  |
          * convert a <code>byte[]</code> to a String using the specified
273 |  |
          * encoding
274 |  |
          */
275 |  |
         public static String bytesToString (byte[] bytes, String encoding) throws UnsupportedEncodingException
276 |  |
         {
277 | 0 |
             return new String(bytes, encoding);
278 |  |
         }
279 |  |
280 |  |
281 |  |
         /** remove any leading and trailing whitespace from a string */
282 |  |
         public static String trim (String s)
283 |  |
         {
284 | 0 |
             return s.trim();
285 |  |
         }
286 |  |
287 |  |
         /** remove any leading whitespace from a string */
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 |  |
             // if all WS return empty string
296 | 0 |
             return "";
297 |  |
         }
298 |  |
299 |  |
         /** remove the trailing whitespace from a string */
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 |  |
             // if all WS return empty string
308 | 0 |
             return "";
309 |  |
         }
310 |  |
311 |  |
         /** converts a block of text into an array of strings
312 |  |
          * by tokenizing using \r\n as the delimiter
313 |  |
          * If no \r\n, it will look for \n or \r and use them instead.
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 |  |
         /** convert an array of strings into a block of text delimited by \r\n */
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 |  |
         /** remove the leading and trailing whitespace from each line in a block of text */
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 |  |
         /** remove the leading whitespace from each line in a block of text */
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 |  |
         /** remove the trailing whitespace from each line in a block of text */
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 |  |
         /** Returns a new  string which is in upper case. */
371 |  |
         public static String toUpperCase (String s)
372 |  |
         {
373 | 0 |
             return s.toUpperCase();
374 |  |
         }
375 |  |
376 |  |
         /** Returns a new  string which is in lower case. */
377 |  |
         public static String toLowerCase (String s)
378 |  |
         {
379 | 0 |
             return s.toLowerCase();
380 |  |
         }
381 |  |
382 |  |
383 |  |
384 |  |
         //
385 |  |
         // ContextTool implementation methods
386 |  |
         //
387 |  |
388 |  |
         /** default contsructor.  Does nothing */
389 |  |
         public TextTool ()
390 | 8 |
         {
391 | 8 |
         }
392 |  |
393 |  |
         /**
394 |  |
          * public constructor.  Does nothing.  The TextTool doesn't
395 |  |
          * interact with the Context, so it is ignored
396 |  |
          */
397 |  |
         public TextTool (Context context)
398 | 0 |
         {
399 | 0 |
         }
400 |  |
401 |  |
         /**
402 |  |
          * Tool initialization method.  The TextTool doesn't
403 |  |
          * interact with the context, so the <code>context</code>
404 |  |
          * parameter is ignored.
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 |  |
     }