1 10 11 package org.mule.routing.filters; 12 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.mule.umo.UMOFilter; 16 import org.mule.umo.UMOMessage; 17 import org.mule.util.StringUtils; 18 19 27 28 public class WildcardFilter implements UMOFilter, ObjectFilter 29 { 30 protected final Log logger = LogFactory.getLog(this.getClass()); 31 32 protected String pattern; 33 protected String [] patterns; 34 private boolean caseSensitive = true; 35 36 public WildcardFilter() 37 { 38 super(); 39 } 40 41 public WildcardFilter(String pattern) 42 { 43 this.setPattern(pattern); 44 } 45 46 public boolean accept(UMOMessage message) 47 { 48 try 49 { 50 return accept(message.getPayloadAsString()); 51 } 52 catch (Exception e) 53 { 54 logger.warn("An exception occured while filtering", e); 55 return false; 56 } 57 } 58 59 public boolean accept(Object object) 60 { 61 if (object == null) 62 { 63 return false; 64 } 65 66 if (patterns != null) 67 { 68 for (int x = 0; x < patterns.length; x++) 69 { 70 boolean foundMatch; 71 String pattern = patterns[x]; 72 73 if ("*".equals(pattern) || "**".equals(pattern)) 74 { 75 return true; 76 } 77 78 String candidate = object.toString(); 79 80 if (!isCaseSensitive()) 81 { 82 pattern = pattern.toLowerCase(); 83 candidate = candidate.toLowerCase(); 84 } 85 86 int i = pattern.indexOf('*'); 87 if (i == -1) 88 { 89 foundMatch = pattern.equals(candidate); 90 } 91 else 92 { 93 int i2 = pattern.indexOf('*', i + 1); 94 if (i2 > 1) 95 { 96 foundMatch = candidate.indexOf(pattern.substring(1, i2)) > -1; 97 } 98 else if (i == 0) 99 { 100 foundMatch = candidate.endsWith(pattern.substring(1)); 101 } 102 else 103 { 104 foundMatch = candidate.startsWith(pattern.substring(0, i)); 105 } 106 } 107 108 if (foundMatch) 109 { 110 return true; 111 } 112 } 113 } 114 115 return false; 116 } 117 118 public String getPattern() 119 { 120 return pattern; 121 } 122 123 public void setPattern(String pattern) 124 { 125 this.pattern = pattern; 126 this.patterns = StringUtils.splitAndTrim(pattern, ","); 127 } 128 129 public boolean isCaseSensitive() 130 { 131 return caseSensitive; 132 } 133 134 public void setCaseSensitive(boolean caseSensitive) 135 { 136 this.caseSensitive = caseSensitive; 137 } 138 139 } 140 | Popular Tags |