home *** CD-ROM | disk | FTP | other *** search
Java Source | 1998-09-08 | 2.3 KB | 119 lines |
- package com.symantec.itools.io;
-
-
- import java.io.File;
- import java.io.FilenameFilter;
- import java.io.IOException;
- import com.symantec.itools.lang.StringUtils;
-
-
- /**
- * @author Symantec Internet Tools Division
- * @version 1.0
- * @since VCafe 3.0
- */
-
- public class WildCardFilenameFilter
- implements FilenameFilter
- {
-
- /**
- * @since VCafe 3.0
- */
- protected String pattern;
-
- /**
- * @since VCafe 3.0
- */
- protected boolean caseSensitive;
-
- public WildCardFilenameFilter()
- {
- this("*");
- }
-
- public WildCardFilenameFilter(String pattern)
- {
- this(pattern, true);
- }
-
- public WildCardFilenameFilter(String pat, boolean f)
- {
- pattern = pat;
- caseSensitive = f;
- }
-
- /**
- * @param dir TODO
- * @param name TODO
- * @since VCafe 3.0
- */
-
- public boolean accept(File dir, String name)
- {
- return (matchFilename(name));
- }
-
- /**
- * @param name TODO
- * @since VCafe 3.0
- */
-
- protected boolean matchFilename(String name)
- {
- int loc;
- String pre;
- String post;
-
- // common patterns
- if(pattern.equals("*") || pattern.equals("*.*"))
- {
- return (true);
- }
-
- loc = pattern.indexOf('*');
-
- if(loc == -1)
- {
- if(caseSensitive)
- {
- return (pattern.equals(name));
- }
-
- return (pattern.equalsIgnoreCase(name));
- }
-
- if(loc == 0)
- {
- return (name.endsWith(pattern.substring(1, pattern.length())));
- }
-
- pre = pattern.substring(0, loc);
- post = pattern.substring(loc + 1, pattern.length());
-
- if(caseSensitive)
- {
- if(!(name.startsWith(pre)))
- {
- return (false);
- }
-
- if(!(name.endsWith(post)))
- {
- return (false);
- }
- }
-
- if(!(StringUtils.startsWithIgnoreCase(name, pre)))
- {
- return (false);
- }
-
- if(!(StringUtils.endsWithIgnoreCase(name, post)))
- {
- return (false);
- }
-
- return (true);
- }
- }