View Javadoc

1   /*
2    * $Id: RegExFilter.java 11921 2008-06-03 14:00:06Z dfeist $
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.api.MuleMessage;
14  import org.mule.api.routing.filter.Filter;
15  import org.mule.api.routing.filter.ObjectFilter;
16  import org.mule.api.transformer.TransformerException;
17  import org.mule.config.i18n.CoreMessages;
18  import org.mule.transformer.simple.ByteArrayToObject;
19  
20  import java.util.regex.Pattern;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  /**
26   * <code>RegExFilter</code> is used to match a String argument against a regular
27   * pattern.
28   */
29  
30  public class RegExFilter implements Filter, ObjectFilter
31  {
32      protected transient Log logger = LogFactory.getLog(getClass());
33  
34      private Pattern pattern;
35  
36      public RegExFilter()
37      {
38          super();
39      }
40  
41      public RegExFilter(String pattern)
42      {
43          this.pattern = Pattern.compile(pattern);
44      }
45  
46      public boolean accept(MuleMessage message)
47      {
48          return accept(message.getPayload());
49      }
50  
51      public boolean accept(Object object)
52      {
53          if (object == null)
54          {
55              return false;
56          }
57  
58          Object tempObject = object;
59  
60          // check whether the payload is a byte[] or a char[]. If it is, then it has
61          // to be transformed otherwise the toString will not represent the true
62          // contents
63          // of the payload for the RegEx filter to use.
64          if (object instanceof byte[])
65          {
66              ByteArrayToObject transformer = new ByteArrayToObject();
67              try
68              {
69                  object = transformer.transform(object);
70              }
71              catch (TransformerException e)
72              {
73                  logger.warn(CoreMessages.transformFailedBeforeFilter(), e);
74                  // revert transformation
75                  object = tempObject;
76              }
77          }
78          else if (object instanceof char[])
79          {
80              object = new String((char[]) object);
81          }
82  
83          return (pattern != null && pattern.matcher(object.toString()).find());
84      }
85  
86      public String getPattern()
87      {
88          return (pattern == null ? null : pattern.pattern());
89      }
90  
91      public void setPattern(String pattern)
92      {
93          this.pattern = (pattern != null ? Pattern.compile(pattern) : null);
94      }
95  
96      /**
97       * @return
98       * @deprecated Use {@link #getPattern()} This method name was changed to be
99       *             consistent with other filters
100      */
101     public String getExpression()
102     {
103         return getPattern();
104     }
105 
106     /**
107      * @param
108      * @deprecated Use {@link #getPattern()} This method name was changed to be
109      *             consistent with other filters
110      */
111     public void setExpression(String expression)
112     {
113         setPattern(expression);
114     }
115 
116 }