home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 4427 / aisp_memo-20090510.7z / hed_dat3_src / src / RegexFileFinder.java < prev   
Encoding:
Java Source  |  2009-04-18  |  2.0 KB  |  79 lines

  1. import java.io.*;
  2. import java.util.*;
  3. import java.util.regex.*;
  4. import java.util.logging.*;
  5.  
  6. /**
  7.  * ñóñδÑ╟ÑúÑ∞Ñ»Ñ╚ÑΩ░╩▓╝ñ╦ñóñΩíóñ½ñ─╞├─Ωñ╬└╡╡¼╔╜╕╜ñ╦Ñ▐Ñ├Ñ┴ñ╣ñδÑ╒ÑíÑñÑδñ≥
  8.  * ├╡ñ╣FileFinderÑ»ÑΘÑ╣
  9.  */
  10. public class RegexFileFinder extends FileFinder {
  11.     private Pattern mMatchPtn;
  12.     private Pattern[] mSkipPtns;
  13.  
  14.     private static Logger logger = Logger.getLogger("RegexFileFinder");
  15.  
  16.     //----------------------------------------------------------------------
  17.  
  18.     /**
  19.      * @param pattern    Ñ▐Ñ├Ñ┴ñ╡ñ╗ñδ└╡╡¼╔╜╕╜
  20.      */
  21.     public RegexFileFinder(Pattern matchPtn, Pattern[] skipPtns) {
  22.     mMatchPtn = matchPtn;
  23.     mSkipPtns = skipPtns;
  24.     }
  25.  
  26.     public RegexFileFinder(Pattern matchPtn) {
  27.     this(matchPtn, new Pattern[0]);
  28.     }
  29.  
  30.     private static boolean isSymLink(File f) {
  31.     return false;
  32.     }
  33.  
  34.     boolean filter(File f) {
  35.     // Ñ╒ÑíÑñÑδñ╟ñóñΩíóñ½ñ─╗╪─Ωñ╡ñ∞ñ┐└╡╡¼╔╜╕╜ñ╚Ñ▐Ñ├Ñ┴ñ╣ñδñ╚ñ¡
  36.     if(f.isFile()) {
  37.         Matcher m = mMatchPtn.matcher(f.getPath());
  38.         if(m.matches()) {
  39.         logger.fine("passed: " + f.getPath());
  40.         return true;
  41.         }
  42.     }
  43.     logger.fine("skipped: " + f.getPath());
  44.     return false;
  45.     }
  46.  
  47.     boolean prune(File f) {
  48.     // Ñ╖Ñ≤Ñ▄ÑΩÑ├Ñ»ÑΩÑ≤Ñ»ñ╧├⌐ñΘñ╩ññ
  49.     if(isSymLink(f)) {
  50.         logger.fine("pruned(symlink): " + f.getPath());
  51.         return true;
  52.     }
  53.     for(int i = 0; i < mSkipPtns.length; i++) {
  54.         if(mSkipPtns[i].matcher(f.getPath()).matches()) {
  55.         logger.fine("pruned(regexp): " + f.getPath());
  56.         return true;
  57.         }
  58.     }
  59.     return false;
  60.     }
  61.  
  62.     //----------------------------------------------------------------------
  63.  
  64.     public static void main(String argv[]) {
  65.     if(argv.length < 2)
  66.         return;
  67.  
  68.     Pattern matchPtn = Pattern.compile(argv[1]);
  69.     Pattern[] skipPtns = new Pattern[argv.length - 2];
  70.     for(int argc = 2; argc < argv.length; argc++)
  71.         skipPtns[argc - 2] = Pattern.compile(argv[argc]);
  72.  
  73.     RegexFileFinder rff = new RegexFileFinder(matchPtn, skipPtns);
  74.     File[] array = rff.find(new File(argv[0])).getArray();
  75.     for(int i = 0; i < array.length; i++)
  76.         System.out.println(array[i].getPath());
  77.     }
  78. }
  79.