Clover Coverage Report
Coverage timestamp: Sun Mar 23 2008 08:24:39 GMT
67   222   18   16.75
32   141   0.31   4
4     5.25  
1    
 
 
  WildCardFilter       Line # 53 67 18 67% 0.6699029
 
No Tests
 
1    /*
2    * Copyright (c) 2006, University of Kent
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are met:
7    *
8    * Redistributions of source code must retain the above copyright notice, this
9    * list of conditions and the following disclaimer.
10    *
11    * Redistributions in binary form must reproduce the above copyright notice,
12    * this list of conditions and the following disclaimer in the documentation
13    * and/or other materials provided with the distribution.
14    *
15    * 1. Neither the name of the University of Kent nor the names of its
16    * contributors may be used to endorse or promote products derived from this
17    * software without specific prior written permission.
18    *
19    * 2. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
20    * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21    * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22    * PURPOSE ARE DISCLAIMED.
23    *
24    * 3. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25    * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26    * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27    * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28    * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29    * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31    * POSSIBILITY OF SUCH DAMAGE.
32    *
33    * 4. YOU AGREE THAT THE EXCLUSIONS IN PARAGRAPHS 2 AND 3 ABOVE ARE REASONABLE
34    * IN THE CIRCUMSTANCES. IN PARTICULAR, YOU ACKNOWLEDGE (1) THAT THIS
35    * SOFTWARE HAS BEEN MADE AVAILABLE TO YOU FREE OF CHARGE, (2) THAT THIS
36    * SOFTWARE IS NOT "PRODUCT" QUALITY, BUT HAS BEEN PRODUCED BY A RESEARCH
37    * GROUP WHO DESIRE TO MAKE THIS SOFTWARE FREELY AVAILABLE TO PEOPLE WHO WISH
38    * TO USE IT, AND (3) THAT BECAUSE THIS SOFTWARE IS NOT OF "PRODUCT" QUALITY
39    * IT IS INEVITABLE THAT THERE WILL BE BUGS AND ERRORS, AND POSSIBLY MORE
40    * SERIOUS FAULTS, IN THIS SOFTWARE.
41    *
42    * 5. This license is governed, except to the extent that local laws
43    * necessarily apply, by the laws of England and Wales.
44    */
45   
46    package issrg.SAWS;
47   
48    import java.io.File;
49    import java.io.FilenameFilter;
50    import java.util.Vector;
51    import java.util.StringTokenizer;
52   
 
53    public class WildCardFilter implements FilenameFilter
54    {
55    String wildPattern = null;
56    Vector pattern = new Vector();
57   
58    final String FIND = "find";
59    final String EXPECT = "expect";
60    final String ANYTHING = "anything";
61    final String NOTHING = "nothing";
62   
 
63  3 toggle public WildCardFilter(String wildString)
64    {
65  3 wildPattern = wildString;
66   
67  3 wildString = wildString.toLowerCase();
68   
69  3 int i = wildString.indexOf("**");
70  3 while ( i >= 0 )
71    {
72  0 wildString = wildString.substring(0, i+1)
73    + wildString.substring(i+2);
74   
75  0 i = wildString.indexOf("**");
76    }
77   
78  3 StringTokenizer tokens = new StringTokenizer(wildString, "*", true);
79  3 String token = null;
80  15 while (tokens.hasMoreTokens())
81    {
82  12 token = tokens.nextToken();
83   
84  12 if (token.equals("*"))
85    {
86  9 pattern.addElement(FIND);
87  9 if (tokens.hasMoreTokens())
88    {
89  9 token = tokens.nextToken();
90  9 pattern.addElement(token);
91    }
92    else
93    {
94  0 pattern.addElement(ANYTHING);
95    }
96    }
97    else
98    {
99  3 pattern.addElement(EXPECT);
100  3 pattern.addElement(token);
101    }
102    }
103   
104  3 if ( !token.equals("*") )
105    {
106  3 pattern.addElement(EXPECT);
107  3 pattern.addElement(NOTHING);
108    }
109   
110    }
111   
112   
 
113  17 toggle public boolean accept(File dir, String name)
114    {
115  17 String path = dir.getPath();
116  17 if ( !path.endsWith("/") && !path.endsWith("\\") )
117    {
118  17 path += File.separator;
119    }
120  17 File tempFile = new File(path, name);
121   
122  17 if ( tempFile.isDirectory() )
123    {
124  3 return true;
125    }
126   
127  14 name = name.toLowerCase();
128   
129  14 boolean acceptName = true;
130   
131  14 String command = null;
132  14 String param = null;
133   
134  14 int currPos = 0;
135  14 int cmdPos = 0;
136   
137  70 while ( cmdPos < pattern.size() )
138    {
139  70 command = (String) pattern.elementAt(cmdPos);
140  70 param = (String) pattern.elementAt(cmdPos + 1);
141   
142  70 if ( command.equals(FIND) )
143    {
144  42 if ( param.equals(ANYTHING) )
145    {
146  0 break;
147    }
148   
149  42 int nextPos = name.indexOf(param, currPos);
150  42 if (nextPos >= 0)
151    {
152   
153  42 currPos = nextPos + param.length();
154    }
155    else
156    {
157  0 acceptName = false;
158  0 break;
159    }
160    }
161    else
162    {
163  28 if ( command.equals(EXPECT) )
164    {
165   
166  28 if ( param.equals(NOTHING) )
167    {
168  14 if ( currPos != name.length() )
169    {
170  0 acceptName = false;
171    }
172   
173  14 break;
174    }
175    else
176    {
177  14 int nextPos = name.indexOf(param, currPos);
178  14 if ( nextPos != currPos )
179    {
180  0 acceptName = false;
181  0 break;
182    }
183   
184  14 currPos += param.length();
185    }
186    }
187    }
188   
189  56 cmdPos += 2;
190    }
191   
192  14 return acceptName;
193    }
194   
195   
 
196  0 toggle public String toString()
197    {
198  0 return wildPattern;
199    }
200   
201   
 
202  0 toggle public String toPattern()
203    {
204  0 StringBuffer out = new StringBuffer();
205   
206  0 int i=0;
207  0 while (i<pattern.size())
208    {
209  0 out.append( "(" );
210  0 out.append( (String) pattern.elementAt(i) );
211  0 out.append( " " );
212  0 out.append( (String) pattern.elementAt(i+1) );
213  0 out.append( ") " );
214   
215  0 i += 2;
216    }
217   
218  0 return out.toString();
219    }
220    }
221   
222