KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > routing > filters > WildcardFilter


1 /*
2  * $Id: WildcardFilter.java 4219 2006-12-09 10:15:14Z lajos $
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 MuleSource MPL
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.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 /**
20  * <code>WildcardFilter</code> is used to match Strings against wildcards. It
21  * performs matches with "*", i.e. "jms.events.*" would catch "jms.events.customer"
22  * and "jms.events.receipts". This filter accepts a comma-separated list of patterns,
23  * so more than one filter pattern can be matched for a given argument:
24  * "jms.events.*, jms.actions.*" will match "jms.events.system" and "jms.actions" but
25  * not "jms.queue".
26  */

27
28 public class WildcardFilter implements UMOFilter, ObjectFilter
29 {
30     protected final Log logger = LogFactory.getLog(this.getClass());
31
32     protected String JavaDoc pattern;
33     protected String JavaDoc[] patterns;
34     private boolean caseSensitive = true;
35
36     public WildcardFilter()
37     {
38         super();
39     }
40
41     public WildcardFilter(String JavaDoc 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 JavaDoc e)
53         {
54             logger.warn("An exception occured while filtering", e);
55             return false;
56         }
57     }
58
59     public boolean accept(Object JavaDoc 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 JavaDoc pattern = patterns[x];
72
73                 if ("*".equals(pattern) || "**".equals(pattern))
74                 {
75                     return true;
76                 }
77
78                 String JavaDoc 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 JavaDoc getPattern()
119     {
120         return pattern;
121     }
122
123     public void setPattern(String JavaDoc 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