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.expression;
8   
9   import org.mule.api.DefaultMuleException;
10  import org.mule.api.routing.filter.Filter;
11  import org.mule.routing.filters.logic.AndFilter;
12  import org.mule.routing.filters.logic.OrFilter;
13  
14  import java.util.ArrayList;
15  import java.util.List;
16  
17  /**
18   * Will create a filter from one of more expression filters.  This parser will parse one or more filter expressions
19   * and understands the operators AND and OR. i.e.
20   *
21   * #[regex:.* bar] OR #[wildcard:foo*]
22   *
23   * or
24   *
25   * #[xpath:/Order/id != null] AND #[header:foo=bar]
26   */
27  public class ExpressionFilterParser
28  {
29      public Filter parseFilterString(String filterString) throws DefaultMuleException
30      {
31          List<String> strings = split(filterString);
32          Filter filter = null;
33  
34          for (String s : strings)
35          {
36              s = s.trim();
37              if (s.equals("AND"))
38              {
39                  filter = new AndFilter(filter);
40              }
41              else if (s.equals("OR"))
42              {
43                  filter = new OrFilter(filter);
44              }
45              else if (filter instanceof AndFilter)
46              {
47                  ((AndFilter) filter).getFilters().add(new org.mule.routing.filters.ExpressionFilter(s));
48              }
49              else if (filter instanceof OrFilter)
50              {
51                  ((OrFilter) filter).getFilters().add(new org.mule.routing.filters.ExpressionFilter(s));
52              }
53              else if (filter == null)
54              {
55                  filter = new org.mule.routing.filters.ExpressionFilter(s);
56              }
57              else
58              {
59                  throw new DefaultMuleException("Expression Filter is malformed. IF this is a nested filter make sure each expression is separated by either 'AND' or 'OR'");
60              }
61          }
62          return filter;
63      }
64  
65      protected List<String> split(String string)
66      {
67          List<String> strings = new ArrayList<String>();
68          int i = 0;
69  
70          while(i> -1)
71          {
72              int a = string.indexOf("AND", i);
73              int o = string.indexOf("OR", i);
74              if(a > 1)
75              {
76                  strings.add(string.substring(0, a));
77                  strings.add("AND");
78                  string = string.substring(a + 4).trim();
79  
80              }
81              else if(o > 1)
82              {
83                  strings.add(string.substring(0, o));
84                  strings.add("OR");
85                  string = string.substring(o + 3).trim();
86              }
87              else
88              {
89                  strings.add(string);
90                  i = -1;
91              }
92          }
93          return strings;
94      }
95  }