KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > file > filters > FilenameRegexFilter


1 /*
2  * $Id: FilenameRegexFilter.java 4310 2006-12-19 12:34:06Z lajos $
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 MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.file.filters;
12
13 import java.util.regex.Matcher JavaDoc;
14 import java.util.regex.Pattern JavaDoc;
15
16 public class FilenameRegexFilter extends FilenameWildcardFilter
17 {
18     protected Pattern JavaDoc[] compiledPatterns = null;
19
20     // @Override
21
public boolean accept(Object JavaDoc object)
22     {
23         if (object == null)
24         {
25             return false;
26         }
27
28         boolean foundMatch = false;
29
30         if (compiledPatterns != null)
31         {
32             for (int i = 0; i < compiledPatterns.length; i++)
33             {
34                 Pattern JavaDoc pattern = compiledPatterns[i];
35                 String JavaDoc string = object.toString();
36
37                 /* Determine if there is an exact match. */
38                 Matcher JavaDoc matcher = pattern.matcher(string);
39                 foundMatch = matcher.matches();
40
41                 if (foundMatch)
42                 {
43                     // we found a match, bail
44
break;
45                 }
46             }
47         }
48
49         return foundMatch;
50     }
51
52     // @Override
53
public void setCaseSensitive(boolean caseSensitive)
54     {
55         super.setCaseSensitive(caseSensitive);
56         this.setPattern(pattern);
57     }
58
59     // @Override
60
public void setPattern(String JavaDoc pattern)
61     {
62         super.setPattern(pattern);
63
64         if (patterns != null)
65         {
66             compiledPatterns = new Pattern JavaDoc[patterns.length];
67
68             for (int i = 0; i < patterns.length; i++)
69             {
70                 if (!isCaseSensitive())
71                 {
72                     /* Add insensitive option if set in the configuration. */
73                     compiledPatterns[i] = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE);
74                 }
75                 else
76                 {
77                     compiledPatterns[i] = Pattern.compile(patterns[i]);
78                 }
79             }
80         }
81     }
82
83 }
84
Popular Tags