KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Collections JavaDoc;
27
28 import org.jboss.logging.Logger;
29 import org.jboss.virtual.VirtualFile;
30 import org.jboss.virtual.VisitorAttributes;
31
32 /**
33  * Matches a file name against a list of suffixes.
34  *
35  * @author Scott.Stark@jboss.org
36  * @author adrian@jboss.org
37  * @version $Revision: 44223 $
38  */

39 public class SuffixMatchFilter extends AbstractVirtualFileFilterWithAttributes
40 {
41    private static Logger log = Logger.getLogger(SuffixMatchFilter.class);
42    /** The suffixes */
43    private Collection JavaDoc<String JavaDoc> suffixes;
44    private boolean trace;
45
46    /**
47     * Create a new SuffixMatchFilter,
48     * using {@link VisitorAttributes#DEFAULT}
49     *
50     * @param suffix the suffix
51     * @throws IllegalArgumentException for a null suffix
52     */

53    public SuffixMatchFilter(String JavaDoc suffix)
54    {
55       this(suffix, null);
56    }
57    
58    /**
59     * Create a new SuffixMatchFilter.
60     *
61     * @param suffix the suffix
62     * @param attributes the attributes, pass null to use {@link VisitorAttributes#DEFAULT}
63     * @throws IllegalArgumentException for a null suffix
64     */

65    @SuppressWarnings JavaDoc("unchecked")
66    public SuffixMatchFilter(String JavaDoc suffix, VisitorAttributes attributes)
67    {
68       this(Collections.EMPTY_LIST, attributes);
69       suffixes.add(suffix);
70       trace = log.isTraceEnabled();
71    }
72    /**
73     * Create a new SuffixMatchFilter.
74     * @param suffixes - the list of file suffixes to accept.
75     * @throws IllegalArgumentException for a null suffixes
76     */

77    public SuffixMatchFilter(Collection JavaDoc<String JavaDoc> suffixes)
78    {
79       this(suffixes, null);
80    }
81    /**
82     * Create a new SuffixMatchFilter.
83     * @param suffixes - the list of file suffixes to accept.
84     * @param attributes the attributes, pass null to use {@link VisitorAttributes#DEFAULT}
85     * @throws IllegalArgumentException for a null suffixes
86     */

87    public SuffixMatchFilter(Collection JavaDoc<String JavaDoc> suffixes, VisitorAttributes attributes)
88    {
89       super(attributes == null ? VisitorAttributes.DEFAULT : attributes);
90       if (suffixes == null)
91          throw new IllegalArgumentException JavaDoc("Null suffixes");
92       this.suffixes = new ArrayList JavaDoc<String JavaDoc>();
93       this.suffixes.addAll(suffixes);
94    }
95
96    /**
97     * Accept any file that ends with one of the filter suffixes. This checks
98     * that the file.getName() endsWith a suffix.
99     * @return true if the file matches a suffix, false otherwise.
100     */

101    public boolean accepts(VirtualFile file)
102    {
103       String JavaDoc name = file.getName();
104       boolean accepts = false;
105       for(String JavaDoc suffix : suffixes)
106       {
107          if (name.endsWith(suffix))
108          {
109             accepts = true;
110             break;
111          }
112       }
113       if( trace )
114          log.trace(file+" accepted: "+accepts);
115       return accepts;
116    }
117 }
118
Popular Tags