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