Coverage Report - org.mule.routing.filters.RegExFilter
 
Classes in this File Line Coverage Branch Coverage Complexity
RegExFilter
56%
15/27
79%
11/14
1.625
 
 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  20
     protected transient Log logger = LogFactory.getLog(getClass());
 33  
 
 34  
     private Pattern pattern;
 35  
 
 36  
     public RegExFilter()
 37  
     {
 38  4
         super();
 39  4
     }
 40  
 
 41  
     public RegExFilter(String pattern)
 42  16
     {
 43  16
         this.pattern = Pattern.compile(pattern);
 44  16
     }
 45  
 
 46  
     public boolean accept(MuleMessage message)
 47  
     {
 48  14
         return accept(message.getPayload());
 49  
     }
 50  
 
 51  
     public boolean accept(Object object)
 52  
     {
 53  52
         if (object == null)
 54  
         {
 55  0
             return false;
 56  
         }
 57  
 
 58  52
         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  52
         if (object instanceof byte[])
 65  
         {
 66  0
             ByteArrayToObject transformer = new ByteArrayToObject();
 67  
             try
 68  
             {
 69  0
                 object = transformer.transform(object);
 70  
             }
 71  0
             catch (TransformerException e)
 72  
             {
 73  0
                 logger.warn(CoreMessages.transformFailedBeforeFilter(), e);
 74  
                 // revert transformation
 75  0
                 object = tempObject;
 76  0
             }
 77  0
         }
 78  52
         else if (object instanceof char[])
 79  
         {
 80  0
             object = new String((char[]) object);
 81  
         }
 82  
 
 83  52
         return (pattern != null && pattern.matcher(object.toString()).find());
 84  
     }
 85  
 
 86  
     public String getPattern()
 87  
     {
 88  6
         return (pattern == null ? null : pattern.pattern());
 89  
     }
 90  
 
 91  
     public void setPattern(String pattern)
 92  
     {
 93  14
         this.pattern = (pattern != null ? Pattern.compile(pattern) : null);
 94  14
     }
 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  0
         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  0
         setPattern(expression);
 114  0
     }
 115  
 
 116  
 }