Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
FilenameRegexFilter |
|
| 3.6666666666666665;3.667 |
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 | 12 | public class FilenameRegexFilter extends FilenameWildcardFilter |
22 | { | |
23 | 12 | 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 | 14 | if (object == null) |
38 | { | |
39 | 0 | return false; |
40 | } | |
41 | ||
42 | 14 | boolean foundMatch = false; |
43 | ||
44 | 14 | if (compiledPatterns != null) |
45 | { | |
46 | 18 | for (int i = 0; i < compiledPatterns.length; i++) |
47 | { | |
48 | 12 | Pattern pattern = compiledPatterns[i]; |
49 | 12 | String string = object.toString(); |
50 | ||
51 | /* Determine if there is an exact match. */ | |
52 | 12 | Matcher matcher = pattern.matcher(string); |
53 | 12 | foundMatch = matcher.matches(); |
54 | ||
55 | 12 | if (foundMatch) |
56 | { | |
57 | // we found a match, bail | |
58 | 6 | break; |
59 | } | |
60 | } | |
61 | } | |
62 | ||
63 | 14 | 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 | 16 | super.setCaseSensitive(caseSensitive); |
75 | 16 | this.setPattern(pattern); |
76 | 16 | } |
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 | 32 | super.setPattern(pattern); |
87 | ||
88 | 32 | if (patterns != null) |
89 | { | |
90 | 20 | compiledPatterns = new Pattern[patterns.length]; |
91 | ||
92 | 40 | for (int i = 0; i < patterns.length; i++) |
93 | { | |
94 | 20 | if (!isCaseSensitive()) |
95 | { | |
96 | /* Add insensitive option if set in the configuration. */ | |
97 | 12 | compiledPatterns[i] = Pattern.compile(patterns[i], Pattern.CASE_INSENSITIVE); |
98 | } | |
99 | else | |
100 | { | |
101 | 8 | compiledPatterns[i] = Pattern.compile(patterns[i]); |
102 | } | |
103 | } | |
104 | } | |
105 | 32 | } |
106 | ||
107 | } |