View Javadoc

1   /*
2    * $Id: RegExFilter.java 7976 2007-08-21 14:26:13Z 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.routing.filters;
12  
13  import org.mule.umo.UMOFilter;
14  import org.mule.umo.UMOMessage;
15  
16  import java.util.regex.Pattern;
17  
18  /**
19   * <code>RegExFilter</code> is used to match a String argument against a regular
20   * expression.
21   */
22  
23  public class RegExFilter implements UMOFilter, ObjectFilter
24  {
25      private Pattern pattern;
26  
27      public RegExFilter()
28      {
29          super();
30      }
31  
32      public RegExFilter(String pattern)
33      {
34          this.pattern = Pattern.compile(pattern);
35      }
36  
37      public boolean accept(UMOMessage message)
38      {
39          return accept(message.getPayload());
40      }
41  
42      public boolean accept(Object object)
43      {
44          if (object == null)
45          {
46              return false;
47          }
48  
49          return (pattern != null ? pattern.matcher(object.toString()).find() : false);
50      }
51  
52      public String getPattern()
53      {
54          return (pattern == null ? null : pattern.pattern());
55      }
56  
57      public void setPattern(String pattern)
58      {
59          this.pattern = (pattern != null ? Pattern.compile(pattern) : null);
60      }
61  
62  }