1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.spring.parsers.specific; |
8 | |
|
9 | |
import org.mule.config.spring.parsers.assembly.configuration.ValueMap; |
10 | |
import org.mule.routing.filters.RegExFilter; |
11 | |
import org.mule.util.StringUtils; |
12 | |
|
13 | |
import java.util.HashMap; |
14 | |
import java.util.Map; |
15 | |
import java.util.regex.Pattern; |
16 | |
|
17 | |
public class RegExFilterDefinitionParser extends FilterDefinitionParser |
18 | |
{ |
19 | |
public RegExFilterDefinitionParser() |
20 | |
{ |
21 | 0 | super(RegExFilter.class); |
22 | 0 | addMapping("flags", new FlagsMapping()); |
23 | 0 | } |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | 0 | public static class FlagsMapping implements ValueMap |
30 | |
{ |
31 | |
private static final Map<String, Integer> FlagsMapping; |
32 | |
|
33 | |
static |
34 | |
{ |
35 | 0 | FlagsMapping = new HashMap<String, Integer>(); |
36 | 0 | FlagsMapping.put("CANON_EQ", Integer.valueOf(Pattern.CANON_EQ)); |
37 | 0 | FlagsMapping.put("CASE_INSENSITIVE", Integer.valueOf(Pattern.CASE_INSENSITIVE)); |
38 | 0 | FlagsMapping.put("DOTALL", Integer.valueOf(Pattern.DOTALL)); |
39 | 0 | FlagsMapping.put("MULTILINE", Integer.valueOf(Pattern.MULTILINE)); |
40 | 0 | FlagsMapping.put("UNICODE_CASE", Integer.valueOf(Pattern.UNICODE_CASE)); |
41 | 0 | } |
42 | |
|
43 | |
public Object rewrite(String value) |
44 | |
{ |
45 | 0 | int combinedFlags = 0; |
46 | |
|
47 | 0 | String[] flagStrings = StringUtils.split(value, ','); |
48 | 0 | for (String flagString : flagStrings) |
49 | |
{ |
50 | 0 | Integer flag = FlagsMapping.get(flagString); |
51 | 0 | if (flag == null) |
52 | |
{ |
53 | 0 | String message = String.format("Invalid flag '%1s'. Must be one of %2s", flagString, |
54 | |
FlagsMapping.keySet().toString()); |
55 | 0 | throw new IllegalArgumentException(message); |
56 | |
} |
57 | |
|
58 | 0 | combinedFlags = combinedFlags | flag.intValue(); |
59 | |
} |
60 | |
|
61 | 0 | return Integer.valueOf(combinedFlags); |
62 | |
} |
63 | |
} |
64 | |
} |