• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 /*
19  * $Id$
20  */
21 package org.apache.qetest.xsl;
22 
23 import java.io.File;
24 import java.io.FilenameFilter;
25 import java.util.Hashtable;
26 import java.util.StringTokenizer;
27 
28 /**
29  * Bugzilla-specific file filter: .java or .xsl files.
30  * Has crude support for an excludes list of filename bases.
31  * @see #accept(File, String)
32  * @author shane_curcuru@lotus.com
33  * @version $Id$
34  */
35 public class BugzillaFileRules implements FilenameFilter
36 {
37 
38     /** Initialize for defaults (not using exclusion list) no-op. */
BugzillaFileRules()39     public BugzillaFileRules(){}
40 
41     /**
42      * Initialize with a case-sensitive Hash of file names to exclude.
43      *
44      * @param excludesHash - keys are basenames of files to exclude
45      */
BugzillaFileRules(Hashtable excludesHash)46     public BugzillaFileRules(Hashtable excludesHash)
47     {
48         setExcludes(excludesHash);
49     }
50 
51     /**
52      * Initialize with a case-insensitive semicolon-delimited String of file names to exclude.
53      *
54      * @param excludesStr is specific file names to exclude
55      */
BugzillaFileRules(String excludesStr)56     public BugzillaFileRules(String excludesStr)
57     {
58         setExcludes(excludesStr);
59     }
60 
61     /**
62      * Hash of file name portions to exclude.
63      * <p>Keys are base file names, values in hash are ignored. Note that
64      * file names may be case-sensitive.</p>
65      * <p>Note that we will exclude any filename in our excludes.</p>
66      */
67     protected Hashtable excludeFiles = null;
68 
69     /**
70      * Accessor methods to set a case-sensitive Hash of file names to exclude.
71      *
72      * @param exFiles hash keys are filenames to exclude
73      */
setExcludes(Hashtable exFiles)74     public void setExcludes(Hashtable exFiles)
75     {
76 
77         if (exFiles != null)
78             excludeFiles = (Hashtable) exFiles.clone();
79         else
80             excludeFiles = null;
81     }
82 
83     /**
84      * Accessor methods to set a case-sensitive Hash of file names to exclude.
85      *
86      * @return clone of our excludes hash
87      */
getExcludes()88     public Hashtable getExcludes()
89     {
90 
91         if (excludeFiles != null)
92         {
93             Hashtable tempHash = (Hashtable) excludeFiles.clone();
94 
95             return tempHash;
96         }
97         else
98         {
99             return null;
100         }
101     }
102 
103     /**
104      * Accessor method to set a list of case-insensitive String
105      * directory name(s) to exclude.
106      * Names should be separated by {@link #SEPARATOR semicolon}.
107      *
108      * @param exFiles are specific file names to exclude
109      */
setExcludes(String exFiles)110     public void setExcludes(String exFiles)
111     {
112         setExcludes(exFiles, false);
113     }
114 
115     /** Semicolon separator for {@link #setExcludes(java.lang.String)}. */
116     public static final String SEPARATOR = ";";
117 
118     /**
119      * Accessor method to set an optionally case-sensitive String file name(s) to exclude.
120      * <p><b>Note:</b> simply uses .toUpperCase() and .toLowerCase() on the input string(s);
121      * does not do full case-checking on the entire string!</p>
122      *
123      * @param exFiles is specific file names to exclude
124      * @param caseSensitive is we should attempt to be
125      */
setExcludes(String exFiles, boolean caseSensitive)126     public void setExcludes(String exFiles, boolean caseSensitive)
127     {
128 
129         StringTokenizer st = new StringTokenizer(exFiles, SEPARATOR);
130 
131         excludeFiles = null;
132         excludeFiles = new Hashtable();
133 
134         for (int i = 0; st.hasMoreTokens(); i++)
135         {
136             String fName = st.nextToken();
137 
138             excludeFiles.put(fName, "");
139 
140             if (!caseSensitive)
141             {
142                 excludeFiles.put(fName.toUpperCase(), "");
143                 excludeFiles.put(fName.toLowerCase(), "");
144             }
145         }
146     }
147 
148     /**
149      * Tests if a specified file should be included in a file list.
150      * <p>Returns true for: filenames that begin with the directory
151      * name, and: are *.java, or are *.xsl.  If one of each exists,
152      * then we only return true for the *.java file (and not for the
153      * *.xsl file).</p>
154      * <p>The essence here is that we return only one file for each
155      * conceptual 'test' that the user wrote.  If they only wrote a
156      * .xsl stylesheet, we return that.  If they only wrote a .java
157      * file (presumably a Testlet), we return that.  If they wrote
158      * both, only return the .java file, since it should have all the
159      * logic necessary to run the test, including hardcoded .xsl
160      * file name.  In this case, it might not be a valid test to
161      * simply transform the .xsl file because the Testlet may
162      * expect that parameters are set, etc.</p>
163      * <p><b>Except:</b> if any filenames contain an item
164      * in excludeFiles.</p>
165      * @param dir the directory in which the file was found.
166      * @param name the name of the file.
167      * @return <code>true</code> if the name should be included in the file list; <code>false</code> otherwise.
168      * @since JDK1.0
169      */
accept(File dir, String name)170     public boolean accept(File dir, String name)
171     {
172 
173         // Shortcuts for bogus filenames and dirs
174         if (name == null || dir == null)
175             return false;
176 
177         // Exclude any files that match an exclude rule
178         if ((excludeFiles != null) && (excludeFiles.containsKey(name)))
179             return false;
180 
181         File file = new File(dir, name);
182         // Skip any dirs
183         if (file.isDirectory())
184             return false;
185 
186         // Only accept files that start with 'bugzilla'
187         // HACK: we should really look at the last part of the
188         //  directory name here, but in this one case it's much
189         //  easier to just hard-code the name (this is in
190         //  response to specifying inputDir=tests/bugzilla
191         //  on a Windows platform)
192         if (!(name.toLowerCase().startsWith("bugzilla")))
193             return false;
194 
195         // Accept any .java files
196         if (name.toLowerCase().endsWith("java"))
197             return true;
198 
199         // If it's a .xsl file..
200         if (name.toLowerCase().endsWith("xsl"))
201         {
202             // Construct matching foo.java from foo.xsl (xsl len = 3)
203             File matchingJava = new File(dir,
204                     name.substring(0, (name.length() - 3)) + "java");
205             // ..Only accept if matchingJava does not exist
206             return !matchingJava.exists();
207         }
208 
209         // Fall-through: doesn't match, return false
210         return false;
211     }
212 }
213