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