1 package com.ubermq.jms.common.routing.impl; 2 3 import com.ubermq.jms.common.routing.*; 4 import java.io.*; 5 import java.util.regex.*; 6 7 11 public class RegexpSourceSpec 12 implements Externalizable, SourceSpec 13 { 14 private transient Pattern regexp; 15 private String expr; 16 private String nice; 17 private static final long serialVersionUID = 5; 18 19 public RegexpSourceSpec() 20 { 21 } 22 23 public RegexpSourceSpec(String expr) 24 { 25 this(expr, expr); 26 } 27 28 public RegexpSourceSpec(String expr, String nice) 29 throws IllegalArgumentException 30 { 31 this.expr = expr; 32 this.nice = nice; 33 this.regexp = Pattern.compile(this.expr, Pattern.CASE_INSENSITIVE); 34 } 35 36 public void readExternal(ObjectInput in) 37 throws IOException 38 { 39 nice = in.readUTF(); 40 expr = in.readUTF(); 41 regexp = Pattern.compile(expr, Pattern.CASE_INSENSITIVE); 42 } 43 44 public void writeExternal(ObjectOutput out) 45 throws IOException 46 { 47 out.writeUTF(nice); 48 out.writeUTF(expr); 49 } 50 51 54 public String getDisplayName() 55 { 56 return nice; 57 } 58 59 65 public boolean shouldCacheResults() 66 { 67 return true; 68 } 69 70 public boolean matches(SourceDescriptor s) 71 { 72 return regexp.matcher(s.getMatchValue()).matches(); 73 } 74 75 public boolean isMoreSpecificThan(SourceSpec s) 76 { 77 try { 78 RegexpSourceSpec rss = (RegexpSourceSpec)s; 79 return (specificity() > rss.specificity()); 80 } 81 catch(ClassCastException cce) {return false;} 82 } 83 84 private int specificity() 85 { 86 int score = 0; 87 88 java.util.StringTokenizer st = new java.util.StringTokenizer (nice, "."); 89 while(st.hasMoreTokens()) 90 { 91 String token = st.nextToken(); 92 char ch = token.charAt(0); 93 if (Character.isLetterOrDigit(ch)) 94 score += 3; 95 else if (ch == '*') 96 score += 2; 97 else if (ch == '#') 98 score += 1; 99 } 100 101 return score; 102 } 103 104 public String toString() 105 { 106 return nice; 107 } 108 109 public boolean equals(Object obj) 110 { 111 if(obj instanceof RegexpSourceSpec) 112 { 113 return nice.equals( ((RegexpSourceSpec)obj).nice ); 114 } 115 else 116 return false; 117 } 118 119 public int hashCode() 120 { 121 return nice.hashCode(); 122 } 123 124 public boolean isIdempotentForEqualDescriptors() 125 { 126 return true; 127 } 128 } 129 130 | Popular Tags |