1 /*------------------------------------------------------------------------------
  2  Name:      FilenameFilter.java
  3  Project:   xmlBlaster.org
  4  Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5  ------------------------------------------------------------------------------*/
  6 package org.xmlBlaster.client.filepoller;
  7 
  8 import java.io.File;
  9 import java.io.FileFilter;
 10 
 11 import org.xmlBlaster.util.XmlBlasterException;
 12 import org.xmlBlaster.util.def.ErrorCode;
 13 
 14 import gnu.regexp.RE;
 15 import gnu.regexp.REException;
 16 //import java.util.regex.Pattern;
 17 
 18 /**
 19  * FilenameFilter. This code is based on the BasicFileChooserUI swing code. The
 20  * difference is that id returns false if the found file is a directory.
 21  * 
 22  * @author <a href="mailto:michele@laghi.eu">Michele Laghi </a>
 23  * @deprectated it is now replaced by the corresponding class in org.xmlBlaster.contrib.filewatcher
 24  */
 25 public class FilenameFilter implements FileFilter {
 26 
 27    private RE regex;
 28    private String pattern;
 29    
 30    public FilenameFilter() {
 31    }
 32 
 33    public FilenameFilter(String pattern, boolean trueRegex) throws XmlBlasterException {
 34       this();
 35       setPattern(pattern, trueRegex);
 36    }
 37 
 38    public void setPattern(String globPattern, boolean trueRegex) throws XmlBlasterException {
 39       if (trueRegex) {
 40          this.pattern = globPattern;
 41       }
 42       else {
 43          char[] gPat = globPattern.toCharArray();
 44          char[] rPat = new char[gPat.length * 2];
 45          boolean isWin32 = (File.separatorChar == '\\');
 46          boolean inBrackets = false;
 47          int j = 0;
 48          if (isWin32) {
 49             //    On windows, a regex ending with *.* is equal to ending with *
 50             int len = gPat.length;
 51             if (globPattern.endsWith("*.*")) {
 52                len -= 2;
 53             }
 54             for (int i = 0; i < len; i++) {
 55                if (gPat[i] == '*') {
 56                   rPat[j++] = '.';
 57                }
 58                rPat[j++] = gPat[i];
 59             }
 60          }
 61          else {
 62             for (int i = 0; i < gPat.length; i++) {
 63                switch (gPat[i]) {
 64                   case '*':
 65                      if (!inBrackets) {
 66                         rPat[j++] = '.';
 67                      }
 68                      rPat[j++] = '*';
 69                      break;
 70                   case '?':
 71                      rPat[j++] = inBrackets ? '?' : '.';
 72                      break;
 73                   case '[':
 74                      inBrackets = true;
 75                      rPat[j++] = gPat[i];
 76                      if (i < gPat.length - 1) {
 77                         switch (gPat[i + 1]) {
 78                            case '!':
 79                            case '^':
 80                               rPat[j++] = '^';
 81                               i++;
 82                               break;
 83                            case ']':
 84                               rPat[j++] = gPat[++i];
 85                               break;
 86                         }
 87                      }
 88                      break;
 89                   case ']':
 90                      rPat[j++] = gPat[i];
 91                      inBrackets = false;
 92                      break;
 93                   case '\\':
 94                      if (i == 0 && gPat.length > 1 && gPat[1] == '~') {
 95                         rPat[j++] = gPat[++i];
 96                      }
 97                      else {
 98                         rPat[j++] = '\\';
 99                         if (i < gPat.length - 1 && "*?[]".indexOf(gPat[i + 1]) >= 0) {
100                            rPat[j++] = gPat[++i];
101                         }
102                         else {
103                            rPat[j++] = '\\';
104                         }
105                      }
106                      break;
107                   default:
108                      //if ("+()|^$.{}<>".indexOf(gPat[i]) >= 0) {
109                      if (!Character.isLetterOrDigit(gPat[i])) {
110                         rPat[j++] = '\\';
111                      }
112                      rPat[j++] = gPat[i];
113                      break;
114                }
115             }
116          }
117          this.pattern = new String(rPat, 0, j);
118       }
119       
120       try {
121          this.regex = new RE(this.pattern, RE.REG_ICASE);
122       }
123       catch (REException ex) {
124          throw new XmlBlasterException(null, ErrorCode.USER_CONFIGURATION, "FilenameFilter", "wrong regex expression for filter '" + this.pattern + "'", ex);
125       }
126       //this.pattern = Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE);
127    }
128 
129    /**
130     * @see java.io.FileFilter#accept(java.io.File)
131     */
132    public boolean accept(File f) {
133       if (f == null) {
134          return false;
135       }
136       if (f.isDirectory()) {
137          return false;
138       }
139       return regex.isMatch(f.getName());
140       // return regex.matcher(f.getName()).matches();
141    }
142 
143    /**
144     * @return Returns the pattern.
145     */
146    public String getPattern() {
147       return this.pattern;
148    }
149 }


syntax highlighted by Code2HTML, v. 0.9.1