View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
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          super(RegExFilter.class);
22          addMapping("flags", new FlagsMapping());
23      }
24  
25      /**
26       * Map a comma-separated string to an Integer object that can be used to set the flags
27       * on a {@link RegExFilter}.
28       */
29      public static class FlagsMapping implements ValueMap
30      {
31          private static final Map<String, Integer> FlagsMapping;
32  
33          static
34          {
35              FlagsMapping = new HashMap<String, Integer>();
36              FlagsMapping.put("CANON_EQ", Integer.valueOf(Pattern.CANON_EQ));
37              FlagsMapping.put("CASE_INSENSITIVE", Integer.valueOf(Pattern.CASE_INSENSITIVE));
38              FlagsMapping.put("DOTALL", Integer.valueOf(Pattern.DOTALL));
39              FlagsMapping.put("MULTILINE", Integer.valueOf(Pattern.MULTILINE));
40              FlagsMapping.put("UNICODE_CASE", Integer.valueOf(Pattern.UNICODE_CASE));
41          }
42  
43          public Object rewrite(String value)
44          {
45              int combinedFlags = 0;
46  
47              String[] flagStrings = StringUtils.split(value, ',');
48              for (String flagString : flagStrings)
49              {
50                  Integer flag = FlagsMapping.get(flagString);
51                  if (flag == null)
52                  {
53                      String message = String.format("Invalid flag '%1s'. Must be one of %2s", flagString,
54                          FlagsMapping.keySet().toString());
55                      throw new IllegalArgumentException(message);
56                  }
57  
58                  combinedFlags = combinedFlags | flag.intValue();
59              }
60  
61              return Integer.valueOf(combinedFlags);
62          }
63      }
64  }