1 26 27 28 package net.sourceforge.groboutils.util.io.v1; 29 30 import java.io.IOException ; 31 import java.io.File ; 32 import java.io.FilenameFilter ; 33 34 35 36 54 public class ExtensionFilenameFilter implements FilenameFilter 55 { 56 59 private String extensions[] = new String [0]; 61 private boolean allowDirs = true; 62 private boolean caseInsensitive = true; 63 64 65 68 71 public ExtensionFilenameFilter() 72 { 73 } 75 76 79 public ExtensionFilenameFilter( String extension ) 80 { 81 addExtension( extension ); 82 } 83 84 88 public ExtensionFilenameFilter( String exts[] ) 89 { 90 if (exts == null) 91 { 92 throw new IllegalArgumentException ( "no null args" ); 93 } 94 for (int i = exts.length; --i >= 0;) 95 { 96 addExtension( exts[i] ); 97 } 98 } 99 100 101 104 public ExtensionFilenameFilter( boolean caseInsensitive ) 105 { 106 this.caseInsensitive = caseInsensitive; 107 } 108 109 110 113 public ExtensionFilenameFilter( String extension, boolean caseInsensitive ) 114 { 115 this.caseInsensitive = caseInsensitive; 117 addExtension( extension ); 118 } 119 120 121 124 public ExtensionFilenameFilter( String exts[], boolean caseInsensitive ) 125 { 126 if (exts == null) 127 { 128 throw new IllegalArgumentException ( "no null args" ); 129 } 130 131 this.caseInsensitive = caseInsensitive; 133 for (int i = exts.length; --i >= 0;) 134 { 135 addExtension( exts[i] ); 136 } 137 } 138 139 140 143 144 147 public void addExtension( String ext ) 148 { 149 if (ext == null) 150 { 151 throw new IllegalArgumentException ( "no null args" ); 152 } 153 int len = this.extensions.length; 154 String temp[] = new String [ len+1 ]; 155 System.arraycopy( this.extensions, 0, temp, 0, len ); 156 if (isCaseInsensitive()) 157 { 158 ext = ext.toLowerCase(); 159 } 160 temp[ len ] = ext; 161 this.extensions = temp; 162 } 163 164 165 168 public boolean allowsDirectories() 169 { 170 return this.allowDirs; 171 } 172 173 174 177 public void allowsDirectories( boolean on ) 178 { 179 this.allowDirs = on; 180 } 181 182 183 186 public boolean isCaseInsensitive() 187 { 188 return this.caseInsensitive; 189 } 190 191 192 195 public boolean accept( File dir, String name ) 196 { 197 if (name == null) 198 { 199 throw new IllegalArgumentException ( "no null args" ); 200 } 201 return matches( name, new File ( dir, name ) ); 202 } 203 204 205 206 209 210 216 protected boolean matches( String filename, File file ) 217 { 218 if (filename == null || file == null) 219 { 220 throw new IllegalArgumentException ( "no null args" ); 221 } 222 if (allowsDirectories() && file.isDirectory()) 223 { 224 return true; 225 } 226 if (isCaseInsensitive()) 227 { 228 filename = filename.toLowerCase(); 229 } 230 231 for (int i = this.extensions.length; --i >= 0;) 232 { 233 if (filename.endsWith( this.extensions[i] )) 234 { 235 return true; 236 } 237 } 238 return false; 239 } 240 241 } 242 | Popular Tags |