KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > virtual > plugins > vfs > helpers > ExtensibleFilter


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.virtual.plugins.vfs.helpers;
23
24 import java.util.Arrays JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Collections JavaDoc;
28
29 import org.jboss.virtual.VirtualFile;
30 import org.jboss.virtual.VirtualFileFilter;
31
32 /**
33  * An extensible filter for VFS files. Three arrays are
34  * maintained for checking: a prefix, suffix, and match array. If the
35  * filename starts with any of the prefixes, ends with any of the
36  * suffixes, or exactly matches any of the matches, then the accepts
37  * method will return false.
38  *
39  * NOTE: the arrays *must* be sorted for the string matching to work,
40  * and suffixes use the 'reverseComparator'
41  *
42  * @author somebody@jboss.org
43  * @author Scott.Stark@jboss.org
44  * @version $Revision: 57108 $
45  */

46 public class ExtensibleFilter implements VirtualFileFilter
47 {
48    /**
49     * Compare the strings backwards. This assists in suffix comparisons.
50     */

51    private static final Comparator JavaDoc<String JavaDoc> reverseComparator = new Comparator JavaDoc<String JavaDoc>()
52    {
53       public int compare(String JavaDoc o1, String JavaDoc o2)
54       {
55          int idx1 = o1.length();
56          int idx2 = o2.length();
57          int comp = 0;
58
59          while (comp == 0 && idx1 > 0 && idx2 > 0)
60             comp = o1.charAt(--idx1) - o2.charAt(--idx2);
61
62          return (comp == 0) ? (idx1 - idx2) : comp;
63       }
64    };
65
66    /** the default prefix list */
67    private static final String JavaDoc[] DEFAULT_PREFIXES =
68       {"#", "%", ",", ".", "_$"};
69
70    /** the default suffix list */
71    private static final String JavaDoc[] DEFAULT_SUFFIXES =
72       {"#", "$", "%", "~", ",v", ".BAK", ".bak", ".old", ".orig", ".tmp", ".rej", ".sh" };
73
74    /** the default matches list */
75    private static final String JavaDoc[] DEFAULT_MATCHES =
76       {".make.state", ".nse_depinfo", "CVS", "CVS.admin", "RCS", "RCSLOG",
77           "SCCS", "TAGS", "core", "tags"};
78
79    /** The list of disallowed suffixes, sorted using reverse values */
80    private ArrayList JavaDoc<String JavaDoc> suffixes;
81
82    /** The sorted list of disallowed prefixes */
83    private ArrayList JavaDoc<String JavaDoc> prefixes;
84
85    /** The sorted list of disallowed values */
86    private ArrayList JavaDoc<String JavaDoc> matches;
87
88    /** Use the default values for suffixes, prefixes, and matches */
89    public ExtensibleFilter()
90    {
91       this(DEFAULT_MATCHES, DEFAULT_PREFIXES, DEFAULT_SUFFIXES);
92    }
93
94    /**
95     * Create using a custom set of matches, prefixes, and suffixes. If any of
96     * these arrays are null, then the corresponding default will be
97     * substituted.
98     */

99    public ExtensibleFilter(String JavaDoc[] matches, String JavaDoc[] prefixes, String JavaDoc[] suffixes)
100    {
101       if( matches == null )
102          matches = DEFAULT_MATCHES;
103       Arrays.sort(matches);
104       this.matches = new ArrayList JavaDoc<String JavaDoc>(Arrays.asList(matches));
105
106       if( prefixes == null )
107          prefixes = DEFAULT_PREFIXES;
108       Arrays.sort(prefixes);
109       this.prefixes = new ArrayList JavaDoc<String JavaDoc>(Arrays.asList(prefixes));
110
111       if( suffixes == null )
112          suffixes = DEFAULT_SUFFIXES;
113       Arrays.sort(suffixes, reverseComparator);
114       this.suffixes = new ArrayList JavaDoc<String JavaDoc>(Arrays.asList(suffixes));
115    }
116
117    public void addPrefix(String JavaDoc prefix)
118    {
119       this.prefixes.add(prefix);
120       Collections.sort(this.prefixes);
121    }
122    public void addPrefixes(String JavaDoc[] prefixes)
123    {
124       this.prefixes.addAll(Arrays.asList(prefixes));
125       Collections.sort(this.prefixes);
126    }
127
128    public void delPrefix(String JavaDoc prefix)
129    {
130       this.prefixes.remove(prefix);
131    }
132    public void delPrefixes(String JavaDoc[] prefixes)
133    {
134       this.prefixes.removeAll(Arrays.asList(prefixes));
135       Collections.sort(this.prefixes);
136    }
137
138    public void addSuffix(String JavaDoc suffix)
139    {
140       this.suffixes.add(suffix);
141       Collections.sort(this.suffixes, reverseComparator);
142    }
143    public void addSuffixes(String JavaDoc[] suffixes)
144    {
145       this.suffixes.addAll(Arrays.asList(suffixes));
146       Collections.sort(this.suffixes, reverseComparator);
147    }
148
149    public void delSuffix(String JavaDoc suffix)
150    {
151       this.suffixes.remove(suffix);
152    }
153    public void delSuffixes(String JavaDoc[] suffixes)
154    {
155       this.suffixes.removeAll(Arrays.asList(suffixes));
156       Collections.sort(this.suffixes, reverseComparator);
157    }
158
159    public String JavaDoc[] getSuffixes()
160    {
161       String JavaDoc[] tmp = new String JavaDoc[suffixes.size()];
162       suffixes.toArray(tmp);
163       return tmp;
164    }
165    public void setSuffixes(String JavaDoc[] suffixes)
166    {
167       Arrays.sort(suffixes, reverseComparator);
168       this.suffixes.clear();
169       this.suffixes.addAll(Arrays.asList(suffixes));
170    }
171
172    public String JavaDoc[] getPrefixes()
173    {
174       String JavaDoc[] tmp = new String JavaDoc[prefixes.size()];
175       prefixes.toArray(tmp);
176       return tmp;
177    }
178    public void setPrefixes(String JavaDoc[] prefixes)
179    {
180       Arrays.sort(prefixes);
181       this.prefixes.clear();
182       this.prefixes.addAll(Arrays.asList(prefixes));
183    }
184
185    public String JavaDoc[] getMatches()
186    {
187       String JavaDoc[] tmp = new String JavaDoc[matches.size()];
188       matches.toArray(tmp);
189       return tmp;
190    }
191    public void setMatches(String JavaDoc[] matches)
192    {
193       Arrays.sort(matches);
194       this.matches.clear();
195       this.matches.addAll(Arrays.asList(matches));
196    }
197
198    /**
199     * If the filename matches any string in the prefix, suffix, or matches
200     * array, return false. Perhaps a bit of overkill, but this method
201     * operates in log(n) time, where n is the size of the arrays.
202     *
203     * @param file The file to be tested
204     * @return <code>false</code> if the filename matches any of the prefixes,
205     * suffixes, or matches.
206     */

207    public boolean accepts(VirtualFile file)
208    {
209       String JavaDoc name = file.getName();
210       // check exact match
211
int index = Collections.binarySearch(matches, name);
212       if (index >= 0)
213          return false;
214
215       // check prefix
216
index = Collections.binarySearch(prefixes, name);
217       if (index >= 0)
218          return false;
219       if (index < -1)
220       {
221          // The < 0 index gives the first index greater than name
222
int firstLessIndex = -2 - index;
223          String JavaDoc prefix = (String JavaDoc) prefixes.get(firstLessIndex);
224          // If name starts with an ingored prefix ignore name
225
if( name.startsWith(prefix) )
226             return false;
227       }
228
229       // check suffix
230
index = Collections.binarySearch(suffixes, name, reverseComparator);
231       if (index >= 0)
232          return false;
233       if (index < -1)
234       {
235          // The < 0 index gives the first index greater than name
236
int firstLessIndex = -2 - index;
237          String JavaDoc suffix = (String JavaDoc) suffixes.get(firstLessIndex);
238          // If name ends with an ingored suffix ignore name
239
if( name.endsWith(suffix) )
240             return false;
241       }
242
243       // everything checks out.
244
return true;
245    }
246 }
247
Popular Tags