01: package org.mortbay.cometd;
02:
03: import java.util.regex.Pattern;
04:
05: import org.mortbay.util.StringUtil;
06:
07: /** Channel Pattern
08: * A string matcher that matches channel name as follows: <pre>
09: * /channel/name - absolute match
10: * /channel,/other - coma separated list of patterns
11: * /foo*bah/blah - wild card not including /
12: * /foo**bah - wild card including /
13: * @author gregw
14: *
15: */
16: public class ChannelPattern {
17: String _template;
18: Pattern _pattern;
19:
20: public ChannelPattern(String pattern) {
21: _template = pattern;
22: if (pattern.indexOf('(') >= 0 || pattern.indexOf(')') >= 0
23: || pattern.indexOf('|') >= 0
24: || pattern.indexOf('[') >= 0
25: || pattern.indexOf(']') >= 0
26: || pattern.indexOf('?') >= 0
27: || pattern.indexOf('+') >= 0
28: || pattern.indexOf('{') >= 0
29: || pattern.indexOf('\\') >= 0)
30: throw new IllegalArgumentException("Illegal pattern "
31: + pattern);
32:
33: pattern = "(" + pattern + ")";
34: pattern = StringUtil.replace(pattern, ",", ")|(");
35: pattern = StringUtil.replace(pattern, "/**/",
36: "(/|/([^,]{1,}/))");
37: pattern = StringUtil.replace(pattern, "**", "[^,]{0,}");
38: pattern = StringUtil.replace(pattern, "*", "[^/,]{0,}");
39:
40: _pattern = Pattern.compile(pattern);
41: }
42:
43: public boolean matches(String channel) {
44: if (channel.endsWith("/"))
45: throw new IllegalArgumentException("Bad channel name: "
46: + channel);
47: return _pattern.matcher(channel).matches();
48: }
49:
50: public boolean equals(Object obj) {
51: if (obj instanceof ChannelPattern)
52: return ((ChannelPattern) obj)._template.equals(_template);
53: return false;
54: }
55:
56: public int hashCode() {
57: return _template.hashCode();
58: }
59:
60: public String toString() {
61: return _template + "[" + _pattern + "]";
62: }
63: }
|