View Javadoc

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