View Javadoc

1   /*
2    * $Id: FilenameRegexFilter.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.transport.file.filters;
12  
13  import java.util.regex.Matcher;
14  import java.util.regex.Pattern;
15  
16  /**
17   * <code>FilenameRegexFilter</code> filters incoming files from a directory, based
18   * on a regular expression. If the expression evaluates to true, then the file will
19   * be accepted.
20   */
21  public class FilenameRegexFilter extends FilenameWildcardFilter
22  {
23      protected volatile Pattern[] compiledPatterns = null;
24  
25      /**
26       * Filter condition decider method.
27       * <p>
28       * Returns <code>boolean</code> <code>TRUE</code> if the file conforms to the
29       * regular expression pattern or <code>FALSE</code> otherwise.
30       * 
31       * @return indication of acceptance as boolean.
32       */
33      @Override
34      public boolean accept(Object object)
35      {
36          if (object == null)
37          {
38              return false;
39          }
40  
41          boolean foundMatch = false;
42  
43          if (compiledPatterns != null)
44          {
45              for (int i = 0; i < compiledPatterns.length; i++)
46              {
47                  Pattern pattern = compiledPatterns[i];
48                  String string = object.toString();
49  
50                  /* Determine if there is an exact match. */
51                  Matcher matcher = pattern.matcher(string);
52                  foundMatch = matcher.matches();
53  
54                  if (foundMatch)
55                  {
56                      // we found a match, bail
57                      break;
58                  }
59              }
60          }
61  
62          return foundMatch;
63      }
64  
65      @Override
66      public void setCaseSensitive(boolean caseSensitive)
67      {
68          super.setCaseSensitive(caseSensitive);
69          this.setPattern(pattern);
70      }
71  
72      @Override
73      public void setPattern(String pattern)
74      {
75          super.setPattern(pattern);
76  
77          if (patterns != null)
78          {
79              compiledPatterns = new Pattern[patterns.length];
80  
81              for (int i = 0; i < patterns.length; i++)
82              {
83                  if (!isCaseSensitive())
84                  {
85                      /* Add insensitive option if set in the configuration. */
86                      compiledPatterns[i] = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE);
87                  }
88                  else
89                  {
90                      compiledPatterns[i] = Pattern.compile(patterns[i]);
91                  }
92              }
93          }
94      }
95  
96  }