1 package nmp.file_utils;
2
3 import java.io.*;
4
5 /***
6 * Implements a simple filename filter. Wildcard character is defined
7 * in wildChar.
8 *@author Nuno Preguica
9 */
10 public class SimpleFilenameFilter
11 implements FilenameFilter
12 {
13 private String nameFilter;
14
15 /***
16 * Initializes filter.
17 *@param s String containing filename filter. It should not contain
18 * directories' information, i.e., it should has the form
19 * [ \* | nameSubString]+
20 * Character stuffing used for wildChar
21 */
22 public SimpleFilenameFilter( String s)
23 {
24 nameFilter = s;
25 }
26
27 public final char wildChar = '*';
28
29 /***
30 * Returns true if str1.substring( from1, len1) could be a compacted
31 * form of str2.substring( from2, len2) (using wildcards)
32 */
33 private boolean match( String str1, int from1, int len1,
34 String str2, int from2, int len2)
35 {
36 char ch;
37 if( from1 == len1 && from2 == len2)
38 return true;
39 if( from1 == len1 || from2 == len2)
40 return false;
41
42 ch = str1.charAt( from1);
43 condition:
44 {
45 if( ch == wildChar )
46 {
47 from1++;
48 if( len1 == from1) //wild char e o ultimo caracter
49 return true;
50 ch = str1.charAt( from1);
51 if( ch == wildChar)
52 {
53 break condition;
54 }
55
56 for( ; from2 < len2 ;from2++ )
57 if( ch == str2.charAt( from2))
58 if( match( str1, from1 + 1, len1,
59 str2, from2 + 1, len2))
60 return true;
61 return false;
62 }
63 }
64 if( ch == str2.charAt( from2))
65 return match( str1, from1 + 1, len1,
66 str2, from2 + 1, len2);
67 return false;
68 }
69
70 private boolean match( String str1, int from1,
71 String str2, int from2)
72 {
73 return match( str1, from1, str1.length(),
74 str2, from2, str2.length());
75 }
76
77 /***
78 * Returns true if 1.st string could be a compacted form of
79 * the second (using wildcards)
80 */
81 private boolean match( String str1, String str2)
82 {
83 return match( str1, 0, str2, 0);
84 }
85
86 /***
87 * Setermines if the given name is acceptable under the current
88 * FilenameFilter
89 */
90 public boolean accept( File dir, String name)
91 {
92 return match( nameFilter, name);
93 }
94 }
95
This page was automatically generated by Maven