View Javadoc

1   /*
2    * $Id: FilenameRegexFilter.java 10489 2008-01-23 17:53:38Z dfeist $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.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       * @param name The name of the file to apply the filter to.
32       * @return indication of acceptance as boolean.
33       */
34      // //@Override
35      public boolean accept(Object object)
36      {
37          if (object == null)
38          {
39              return false;
40          }
41  
42          boolean foundMatch = false;
43  
44          if (compiledPatterns != null)
45          {
46              for (int i = 0; i < compiledPatterns.length; i++)
47              {
48                  Pattern pattern = compiledPatterns[i];
49                  String string = object.toString();
50  
51                  /* Determine if there is an exact match. */
52                  Matcher matcher = pattern.matcher(string);
53                  foundMatch = matcher.matches();
54  
55                  if (foundMatch)
56                  {
57                      // we found a match, bail
58                      break;
59                  }
60              }
61          }
62  
63          return foundMatch;
64      }
65  
66      /*
67       * (non-Javadoc)
68       * 
69       * @see org.mule.routing.filters.WildcardFilter#setCaseSensitive(boolean)
70       */
71      // //@Override
72      public void setCaseSensitive(boolean caseSensitive)
73      {
74          super.setCaseSensitive(caseSensitive);
75          this.setPattern(pattern);
76      }
77  
78      /*
79       * (non-Javadoc)
80       * 
81       * @see org.mule.routing.filters.WildcardFilter#setPattern(java.lang.String)
82       */
83      // //@Override
84      public void setPattern(String pattern)
85      {
86          super.setPattern(pattern);
87  
88          if (patterns != null)
89          {
90              compiledPatterns = new Pattern[patterns.length];
91  
92              for (int i = 0; i < patterns.length; i++)
93              {
94                  if (!isCaseSensitive())
95                  {
96                      /* Add insensitive option if set in the configuration. */
97                      compiledPatterns[i] = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE);
98                  }
99                  else
100                 {
101                     compiledPatterns[i] = Pattern.compile(patterns[i]);
102                 }
103             }
104         }
105     }
106 
107 }