1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.routing.filters; |
12 | |
|
13 | |
import org.mule.api.MuleMessage; |
14 | |
import org.mule.api.routing.filter.Filter; |
15 | |
import org.mule.api.routing.filter.ObjectFilter; |
16 | |
import org.mule.api.transformer.TransformerException; |
17 | |
import org.mule.config.i18n.CoreMessages; |
18 | |
import org.mule.transformer.simple.ByteArrayToObject; |
19 | |
|
20 | |
import java.util.regex.Pattern; |
21 | |
|
22 | |
import org.apache.commons.logging.Log; |
23 | |
import org.apache.commons.logging.LogFactory; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
public class RegExFilter implements Filter, ObjectFilter |
31 | |
{ |
32 | 20 | protected transient Log logger = LogFactory.getLog(getClass()); |
33 | |
|
34 | |
private Pattern pattern; |
35 | |
|
36 | |
public RegExFilter() |
37 | |
{ |
38 | 4 | super(); |
39 | 4 | } |
40 | |
|
41 | |
public RegExFilter(String pattern) |
42 | 16 | { |
43 | 16 | this.pattern = Pattern.compile(pattern); |
44 | 16 | } |
45 | |
|
46 | |
public boolean accept(MuleMessage message) |
47 | |
{ |
48 | 14 | return accept(message.getPayload()); |
49 | |
} |
50 | |
|
51 | |
public boolean accept(Object object) |
52 | |
{ |
53 | 52 | if (object == null) |
54 | |
{ |
55 | 0 | return false; |
56 | |
} |
57 | |
|
58 | 52 | Object tempObject = object; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | 52 | if (object instanceof byte[]) |
65 | |
{ |
66 | 0 | ByteArrayToObject transformer = new ByteArrayToObject(); |
67 | |
try |
68 | |
{ |
69 | 0 | object = transformer.transform(object); |
70 | |
} |
71 | 0 | catch (TransformerException e) |
72 | |
{ |
73 | 0 | logger.warn(CoreMessages.transformFailedBeforeFilter(), e); |
74 | |
|
75 | 0 | object = tempObject; |
76 | 0 | } |
77 | 0 | } |
78 | 52 | else if (object instanceof char[]) |
79 | |
{ |
80 | 0 | object = new String((char[]) object); |
81 | |
} |
82 | |
|
83 | 52 | return (pattern != null && pattern.matcher(object.toString()).find()); |
84 | |
} |
85 | |
|
86 | |
public String getPattern() |
87 | |
{ |
88 | 6 | return (pattern == null ? null : pattern.pattern()); |
89 | |
} |
90 | |
|
91 | |
public void setPattern(String pattern) |
92 | |
{ |
93 | 14 | this.pattern = (pattern != null ? Pattern.compile(pattern) : null); |
94 | 14 | } |
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
public String getExpression() |
102 | |
{ |
103 | 0 | return getPattern(); |
104 | |
} |
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
public void setExpression(String expression) |
112 | |
{ |
113 | 0 | setPattern(expression); |
114 | 0 | } |
115 | |
|
116 | |
} |