View Javadoc

1   /*
2    * $Id: RegExFilterDefinitionParser.java 21680 2011-04-14 11:32:39Z dirk.olmes $
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.config.spring.parsers.specific;
12  
13  import org.mule.config.spring.parsers.assembly.configuration.ValueMap;
14  import org.mule.routing.filters.RegExFilter;
15  import org.mule.util.StringUtils;
16  
17  import java.util.HashMap;
18  import java.util.Map;
19  import java.util.regex.Pattern;
20  
21  public class RegExFilterDefinitionParser extends FilterDefinitionParser
22  {
23      public RegExFilterDefinitionParser()
24      {
25          super(RegExFilter.class);
26          addMapping("flags", new FlagsMapping());
27      }
28  
29      /**
30       * Map a comma-separated string to an Integer object that can be used to set the flags
31       * on a {@link RegExFilter}.
32       */
33      public static class FlagsMapping implements ValueMap
34      {
35          private static final Map<String, Integer> FlagsMapping;
36  
37          static
38          {
39              FlagsMapping = new HashMap<String, Integer>();
40              FlagsMapping.put("CANON_EQ", Integer.valueOf(Pattern.CANON_EQ));
41              FlagsMapping.put("CASE_INSENSITIVE", Integer.valueOf(Pattern.CASE_INSENSITIVE));
42              FlagsMapping.put("DOTALL", Integer.valueOf(Pattern.DOTALL));
43              FlagsMapping.put("MULTILINE", Integer.valueOf(Pattern.MULTILINE));
44              FlagsMapping.put("UNICODE_CASE", Integer.valueOf(Pattern.UNICODE_CASE));
45          }
46  
47          public Object rewrite(String value)
48          {
49              int combinedFlags = 0;
50  
51              String[] flagStrings = StringUtils.split(value, ',');
52              for (String flagString : flagStrings)
53              {
54                  Integer flag = FlagsMapping.get(flagString);
55                  if (flag == null)
56                  {
57                      String message = String.format("Invalid flag '%1s'. Must be one of %2s", flagString,
58                          FlagsMapping.keySet().toString());
59                      throw new IllegalArgumentException(message);
60                  }
61  
62                  combinedFlags = combinedFlags | flag.intValue();
63              }
64  
65              return Integer.valueOf(combinedFlags);
66          }
67      }
68  }