View Javadoc

1   //Copyright 2006-2007, by the California Institute of Technology.
2   //ALL RIGHTS RESERVED. United States Government Sponsorship acknowledged.
3   //Any commercial use must be negotiated with the Office of Technology Transfer
4   //at the California Institute of Technology.
5   //
6   //This software is subject to U. S. export control laws and regulations
7   //(22 C.F.R. 120-130 and 15 C.F.R. 730-774). To the extent that the software
8   //is subject to U.S. export control laws and regulations, the recipient has
9   //the responsibility to obtain export licenses or other export authority as
10  //may be required before exporting such information to foreign countries or
11  //providing access to foreign nationals.
12  //
13  //	 $Id: WildcardOSFilter.java 3461 2008-08-07 17:43:26Z pramirez $ 
14  //
15  
16  package gov.nasa.pds.tools.file.filefilter;
17  
18  import java.io.File;
19  import java.util.ArrayList;
20  import java.util.Iterator;
21  import java.util.List;
22  
23  import org.apache.commons.io.filefilter.AbstractFileFilter;
24  import org.apache.commons.io.FilenameUtils;
25  
26  /***
27   * Filters files using supplied wildcard(s). Based on the Apache
28   * WildcardFilter class in the Commons IO package. Difference is
29   * that in this class, it uses the 
30   * org.apache.commons.io.FilenameUtils.wildcardMatchOnSystem()
31   * for its matching rules, which means that pattern matching using
32   * this class is OS dependent (case-insensitive on Windows and
33   * case-sensitive on Unix, Linux, MAC)
34   * 
35   * @author mcayanan
36   * @version $Revision: 3461 $
37   * 
38   */
39  
40  public class WildcardOSFilter extends AbstractFileFilter {
41  
42  	private List wildcards = null;
43  	
44  	/***
45  	 * Constructor for a single wildcard
46  	 * 
47  	 * @param wc a single filter to set
48  	 * @throws NullPointerException if the pattern is null
49  	 */
50  	public WildcardOSFilter(String wc) {
51  		if(wc == null) {
52  			throw new NullPointerException();
53  		}
54  		
55  		this.wildcards = new ArrayList();
56  		this.wildcards.add(wc); 
57  	}
58  
59  	/***
60  	 * Returns list of filters that were set
61  	 * @return a list of filters
62  	 */
63  	public List getWildcards() {
64  		return wildcards;
65  	}
66  	
67  	/***
68  	 * Constructor for a list of wildcards
69  	 * 
70  	 * @param wc a list of filters to set
71  	 * @throws NullPointerException if the pattern list is null
72  	 */
73  	public WildcardOSFilter(List wc) {
74  		if(wc == null) {
75  			throw new NullPointerException();
76  		}
77  		
78  		this.wildcards = new ArrayList();
79  		this.wildcards.addAll(wc);
80  	}
81  	
82  	/***
83  	 * Checks to see if the filename matches one of the wildcards. Matching is 
84  	 * case-insensitive for Windows and case-sensitive for Unix.
85  	 * 
86  	 * @param file the file to check
87  	 * @return true if the filename matches one of the wildcards
88  	 * @throws NullPointerException if the file is null
89  	 */
90  	
91  	public boolean accept(File file) {
92  		
93  		if(file == null)
94  			throw new NullPointerException("No file specified");
95  		
96  		for(Iterator i = wildcards.iterator(); i.hasNext(); ) {
97  			if(FilenameUtils.wildcardMatchOnSystem(file.getName(), i.next().toString())) {
98  				return true;
99  			}
100 		}
101 		
102 		return false;
103 	}
104 	
105 	/***
106 	 * Checks to see if the filename matches one of the wildcards. Matching is 
107 	 * case-insensitive for Windows and case-sensitive for Unix.
108 	 * 
109 	 * @param dir the directory to check
110 	 * @param name the file name within the directory to check
111 	 * @return true if the filename matches one of the wildcards, false otherwise
112 	 * @throws NullPointerException if the file is null
113 	 */
114 	public boolean accept(File dir, String name) {
115 		return accept(new File(dir, name));
116 	}
117 	
118 }