Source code: com/further/jaudit/SourceFilenameFilter.java
1 package com.further.jaudit;
2
3 import java.io.FilenameFilter;
4 import java.io.File;
5 import java.util.Set;
6 import java.util.HashSet;
7 import java.util.Iterator;
8
9 /**
10 * This filter implements a source file filter for directory traversal.
11 * The intent of the filter is to allow the configuration to statically
12 * specify file extensions that are valid source files, then the system
13 * uses the SourceFilenameFilter to test the contents of the source
14 * tree against those extensions.
15 *
16 * @author Kristopher Wehner <kris@further.com>
17 * @since 1.0
18 * @version 1.0
19 */
20 public class SourceFilenameFilter implements FilenameFilter {
21
22 /**
23 * This set contains the list of extensions that constitute
24 * source files, without the last "."
25 */
26 private static Set sourceFileExtensions = new HashSet();
27 /**
28 * Register the given extension as a valid source file extension
29 * for cataloging. This assumes the given extension does not
30 * include the trailing ".".
31 *
32 * @param ext The source file extension to register.
33 */
34 public static void registerSourceExtension(String ext) {
35 sourceFileExtensions.add(ext);
36 }
37
38 /**
39 * The singleton instance of the source filename filter
40 */
41 private static SourceFilenameFilter instance =
42 new SourceFilenameFilter();
43
44 /**
45 * Retreive the global singleton for the source filename filter.
46 *
47 * @return The global singleton instance of the source filename
48 * filter
49 */
50 public static SourceFilenameFilter getInstance() {
51 return instance;
52 }
53
54 /**
55 * This class is a singleton, it cannot be created.
56 */
57 private SourceFilenameFilter() { }
58
59 /**
60 * Test if a file is a source file, which implies that it
61 * has a specific extension that is registered with the
62 * configuration system (and subsequently in the source file
63 * extension set)
64 *
65 * @param dir The directory containing the file
66 * @param name The name of the file
67 * @return true if the file is a source file, false otherwise
68 */
69 public boolean accept(File dir, String name) {
70 File completeFile = new File(dir + File.separator + name);
71 if (completeFile.isDirectory())
72 return false;
73 for (Iterator i = sourceFileExtensions.iterator();
74 i.hasNext(); ) {
75 String ext = (String)i.next();
76 if (name.endsWith("." + ext))
77 return true;
78 }
79 return false;
80 }
81 }