KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > josql > functions > StringFunctions


1 /*
2  * Copyright 2004-2005 Gary Bentley
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may
5  * not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */

15 package org.josql.functions;
16
17 import java.io.File JavaDoc;
18 import java.io.FileReader JavaDoc;
19 import java.io.BufferedReader JavaDoc;
20
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.josql.functions.regexp.RegExpFactory;
25 import org.josql.functions.regexp.RegExp;
26
27 import org.josql.Query;
28 import org.josql.QueryExecutionException;
29
30 /**
31  * This class holds functions that operate on strings in some way.
32  * <p>
33  * Last Modified By: $Author: barrygently $<br />
34  * Last Modified On: $Date: 2005/01/07 17:10:40 $<br />
35  * Current Revision: $Revision: 1.1 $<br />
36  */

37 public class StringFunctions extends AbstractFunctionHandler
38 {
39
40     public static final String JavaDoc HANDLER_ID = "_internal_string";
41
42     /**
43      * Match a regular expression against the object passed in.
44      *
45      * @param o The object to match against, <code>toString</code> is called on the object.
46      * @param re The regular expression to match.
47      * @return <code>true</code> if the expression matches.
48      * @throws QueryExecutionException If the match cannot be performed, or if there is no suitable
49      * regular expression library available to the {@link RegExpFactory}.
50      */

51     public boolean regexp (Object JavaDoc o,
52                String JavaDoc re)
53                        throws QueryExecutionException
54     {
55
56     RegExp regexp = RegExpFactory.getDefaultInstance ();
57
58     if (regexp == null)
59     {
60
61         throw new QueryExecutionException ("No default regular expression library available for: " +
62                            RegExpFactory.getDefaultInstanceName ());
63
64     }
65
66     if (o == null)
67     {
68
69         return false;
70
71     }
72
73     String JavaDoc v = o.toString ();
74
75     return regexp.match (re,
76                  v);
77
78     }
79
80     /**
81      * Match a regular expression against the object passed in using the specified regular expression
82      * library, pre-defined library names can be found in: {@link RegExpFactory}.
83      *
84      * @param o The object to match against, <code>toString</code> is called on the object.
85      * @param re The regular expression to match.
86      * @param instName The name of the regular expression library to use.
87      * @return <code>true</code> if the expression matches.
88      * @throws QueryExecutionException If the match cannot be performed, or if the <b>instName</b>
89      * regular expression library is not available to the {@link RegExpFactory}.
90      */

91     public boolean regexp (Object JavaDoc o,
92                String JavaDoc re,
93                String JavaDoc instName)
94                        throws QueryExecutionException
95     {
96
97     RegExp regexp = RegExpFactory.getInstance (instName);
98
99     if (regexp == null)
100     {
101
102         throw new QueryExecutionException ("No regular expression library available for: " +
103                            instName);
104
105     }
106
107     if (o == null)
108     {
109
110         return false;
111
112     }
113
114     String JavaDoc v = o.toString ();
115
116     return regexp.match (re,
117                  v);
118
119     }
120
121     /**
122      * <a target="_blank" HREF="http://www.gnu.org/software/grep/grep.html">grep</a>
123      * through a file, line by line, and determine what matches there are to the nominated
124      * String. Return a List of {@link FileMatch} objects.
125      *
126      * @param f The File to match against.
127      * @param s The string to match.
128      * @param ignoreCase If set to <code>true</code> then the case of the line and string to
129      * match are ignored.
130      * @return The List of {@link FileMatch} objects.
131      */

132     public List JavaDoc grep (File JavaDoc f,
133               String JavaDoc s,
134               boolean ignoreCase)
135                   throws QueryExecutionException
136     {
137
138     if ((f == null)
139         ||
140         (!f.exists ())
141         ||
142         (f.isDirectory ())
143         ||
144         (!f.canRead ())
145        )
146     {
147
148         return null;
149
150     }
151
152     List JavaDoc retData = new ArrayList JavaDoc ();
153
154     try
155     {
156
157         BufferedReader JavaDoc br = new BufferedReader JavaDoc (new FileReader JavaDoc (f));
158     
159         String JavaDoc l = br.readLine ();
160
161         int lc = 1;
162         
163         String JavaDoc ss = s;
164
165         if (ignoreCase)
166         {
167
168         ss = s.toLowerCase ();
169
170         }
171
172         while (l != null)
173         {
174
175         int ind = -1;
176
177         if (ignoreCase)
178         {
179
180             ind = l.toLowerCase ().indexOf (ss);
181
182         } else {
183
184             ind = l.indexOf (ss);
185         
186         }
187
188         if (ind != -1)
189         {
190
191             retData.add (new FileMatch (f,
192                         lc,
193                         ind,
194                         s,
195                         l));
196             
197         }
198         
199         l = br.readLine ();
200         
201         lc++;
202
203         }
204
205         br.close ();
206
207     } catch (Exception JavaDoc e) {
208
209         throw new QueryExecutionException ("Unable to read from file: " +
210                            f,
211                            e);
212
213     }
214
215     return retData;
216
217     }
218
219     /**
220      * <a target="_blank" HREF="http://www.gnu.org/software/grep/grep.html">grep</a>
221      * through a file, line by line, and determine what matches there are to the nominated
222      * regular expression using the specified regular expression implementation.
223      * Return a List of {@link FileMatch} objects.
224      *
225      * @param f The File to match against.
226      * @param regexp The regular expression to match against each line. This will use the
227      * default regular expression library. In this case the location of the match
228      * (i.e. {@link FileMatch#getColumn()}) will be -1 since the regular expression
229      * handling does not support location matching. Also, {@link FileMatch#getString()}
230      * will contain the regular expression used.
231      * @param instName The instance name to use.
232      * @return The List of {@link FileMatch} objects.
233      * @throws QueryExecutionException If the default regular expression implementation is not
234      * available or if the file cannot be read.
235      */

236     public List JavaDoc rgrep (File JavaDoc f,
237                String JavaDoc regexp,
238                String JavaDoc instName)
239                    throws QueryExecutionException
240     {
241
242     RegExp reImpl = RegExpFactory.getInstance (instName);
243
244     if (reImpl == null)
245     {
246
247         throw new QueryExecutionException ("No default regular expression library available for: " +
248                            instName);
249
250     }
251
252     return this.rgrep (f,
253                regexp,
254                reImpl);
255
256     }
257
258     /**
259      * <a target="_blank" HREF="http://www.gnu.org/software/grep/grep.html">grep</a>
260      * through a file, line by line, and determine what matches there are to the nominated
261      * regular expression. Return a List of {@link FileMatch} objects.
262      *
263      * @param f The File to match against.
264      * @param regexp The regular expression to match against each line. This will use the
265      * default regular expression library. In this case the location of the match
266      * (i.e. {@link FileMatch#getColumn()}) will be -1 since the regular expression
267      * handling does not support location matching. Also, {@link FileMatch#getString()}
268      * will contain the regular expression used.
269      * @return The List of {@link FileMatch} objects.
270      * @throws QueryExecutionException If the default regular expression implementation is not
271      * available or if the file cannot be read.
272      */

273     public List JavaDoc rgrep (File JavaDoc f,
274                String JavaDoc regexp)
275                    throws QueryExecutionException
276     {
277
278     RegExp reImpl = RegExpFactory.getDefaultInstance ();
279
280     if (reImpl == null)
281     {
282
283         throw new QueryExecutionException ("No default regular expression library available for: " +
284                            RegExpFactory.getDefaultInstanceName ());
285
286     }
287
288     return this.rgrep (f,
289                regexp,
290                reImpl);
291
292     }
293
294     private List JavaDoc rgrep (File JavaDoc f,
295             String JavaDoc regexp,
296             RegExp reImpl)
297                     throws QueryExecutionException
298     {
299
300     if ((f == null)
301         ||
302         (!f.exists ())
303         ||
304         (f.isDirectory ())
305         ||
306         (!f.canRead ())
307        )
308     {
309
310         return null;
311
312     }
313
314     List JavaDoc retData = new ArrayList JavaDoc ();
315
316     try
317     {
318
319         BufferedReader JavaDoc br = new BufferedReader JavaDoc (new FileReader JavaDoc (f));
320     
321         String JavaDoc l = br.readLine ();
322
323         int lc = 1;
324         
325         while (l != null)
326         {
327
328         if (reImpl.match (regexp,
329                   l))
330         {
331
332             retData.add (new FileMatch (f,
333                         lc,
334                         -1,
335                         regexp,
336                         l));
337             
338         }
339         
340         l = br.readLine ();
341         
342         lc++;
343
344         }
345
346         br.close ();
347
348     } catch (Exception JavaDoc e) {
349
350         throw new QueryExecutionException ("Unable to read from file: " +
351                            f,
352                            e);
353
354     }
355
356     return retData;
357
358     }
359
360     /**
361      * <a target="_blank" HREF="http://www.gnu.org/software/grep/grep.html">grep</a>
362      * through a file, line by line, and determine what matches there are to the nominated
363      * String. Return a List of {@link FileMatch} objects.
364      *
365      * @param f The File to match against.
366      * @param s The string to match.
367      * @return The List of {@link FileMatch} objects.
368      */

369     public List JavaDoc grep (File JavaDoc f,
370               String JavaDoc s)
371                   throws QueryExecutionException
372     {
373
374     return this.grep (f,
375               s,
376               false);
377
378     }
379
380     /**
381      * Given a string trim the passed in string from the front and end, set <b>v</b> to <code>null</code>
382      * to have just whitespace trimmed. Both are converted to strings first.
383      *
384      * @param o The string to trim.
385      * @param v The string to trim from the front and end. Set to <code>null</code> to just trim
386      * whitespace.
387      * @return The trimmed string.
388      */

389     public String JavaDoc trim (Object JavaDoc o,
390             Object JavaDoc v)
391     {
392
393     if (o == null)
394     {
395
396         return null;
397
398     }
399
400     String JavaDoc os = o.toString ();
401
402     if (v == null)
403     {
404
405         return os.trim ();
406
407     }
408
409     String JavaDoc vs = v.toString ();
410
411     if (os.endsWith (vs))
412     {
413
414         os = os.substring (0,
415                    vs.length ());
416
417     }
418
419     if (os.startsWith (vs))
420     {
421
422         os = os.substring (vs.length ());
423
424     }
425
426     return os;
427
428     }
429
430     /**
431      * A thinly veiled wrapper around the {@link String#lastIndexOf(String)} method.
432      * Both <b>o</b> and <b>i</b> are converted to Strings and then the "lastIndexoOf" method
433      * is called on <b>o</b> with <b>i</b> as the argument.
434      *
435      * @param o The string to search.
436      * @param i The string to match.
437      * @return The last index of <b>i</b> within <b>o</b>. If <b>o</b> is <code>null</code> then
438      * -1 is returned. If <b>i</b> is <code>null</code> then -1 is returned.
439      */

440     public double lastIndexOf (Object JavaDoc o,
441                    Object JavaDoc i)
442     {
443
444     if (o == null)
445     {
446
447         return -1;
448
449     }
450
451     if (i == null)
452     {
453
454         return -1;
455
456     }
457
458     String JavaDoc os = o.toString ();
459     String JavaDoc is = i.toString ();
460
461     return os.lastIndexOf (is);
462
463     }
464
465     /**
466      * Return a substring of the passed in object (in a string form). See {@link #subStr(Object,double,double)}
467      * for the full details since this is just a thin-wrapper around that method with the <b>t</b>
468      * parameter set to -1.
469      *
470      * @param o The object to convert to a string and return the substring.
471      * @param f The start index. If this is set to 0 then the entire string is returned.
472      * @return The substring.
473      */

474     public String JavaDoc subStr (Object JavaDoc o,
475               double f)
476     {
477
478     return this.subStr (o,
479                 f,
480                 -1);
481
482     }
483
484     /**
485      * A function to return a substring of a String. If the passed in object isn't
486      * a string then it is converted to a string before processing.
487      *
488      * @param o The object to convert to a string and return the substring.
489      * @param f The start index. If it's < 0 then "" is returned. If the start is out of
490      * range of the string then "" is returned.
491      * @param t The end index. If it's > f then it is reset to -1. If it's -1 then
492      * it's ignored and the substring from the start is used. If the end is greater
493      * than the length of the string then it is ignored.
494      * @return The substring.
495      */

496     public String JavaDoc subStr (Object JavaDoc o,
497               double f,
498               double t)
499     {
500
501     if (o == null)
502     {
503
504         return null;
505
506     }
507
508     int fi = (int) f;
509     int ti = (int) t;
510
511     String JavaDoc s = o.toString ();
512
513     if ((fi < 0)
514         ||
515         (fi > s.length ())
516        )
517     {
518
519         return "";
520
521     }
522
523     if ((ti < fi)
524         ||
525         (ti > s.length ())
526        )
527     {
528
529         ti = -1;
530
531     }
532
533     if (ti == -1)
534     {
535
536         return s.substring (fi);
537
538     }
539
540     return s.substring (fi,
541                 ti);
542
543     }
544
545     public double length (Object JavaDoc o)
546     {
547
548     if (o == null)
549     {
550
551         return 0;
552
553     }
554
555     if (o instanceof String JavaDoc)
556     {
557
558         return ((String JavaDoc) o).length ();
559
560     }
561
562     return o.toString ().length ();
563
564     }
565
566 }
567
Popular Tags