KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ubermq > jms > common > routing > impl > RegexpHelper


1 package com.ubermq.jms.common.routing.impl;
2
3 /**
4  * A helper class providing regex translation facilities.
5  */

6 public class RegexpHelper
7 {
8     /**
9      * The UberMQ topic matching scheme is quite simple, but provides
10      * access to pure regex if that functionality is desired.
11      * <P>
12      *
13      * <code>*</code> matches exactly one topic level, e.g.
14      * <code>a.b.*</code> matches <code>a.b.x</code>, <code>a.b.y</code>,
15      * but not <code>a.b.y.z</code>
16      * <P>
17      *
18      * <code>#</code> matches any number of topic levels, e.g.
19      * <code>a.b.#</code> matches <code>a.b.x</code>, <code>a.b.y</code>,
20      * and <code>a.b.y.z</code>
21      * <P>
22      *
23      * Topic spaces preceded by a . will not be matched by wildcards,
24      * so .secret will not be matched by #, nor *. This serves
25      * to hide topics from aggressive wildcarding. These topics can be
26      * subscribed to by explicitly naming them.
27      * <P>
28      *
29      * System topics begin with a <code>.$</code>. Please do not subscribe to or use these,
30      * or your application behavior will be undefined.
31      * <P>
32      *
33      * Finally, for regex fans, the underlying regex engine can be
34      * accessed by specifying a tilde (~) as the first character of the topic name.
35      * So, <code>~.*</code> will match all topic names, even hidden or system topics.
36      */

37     public static String JavaDoc xlat(String JavaDoc friendly)
38     {
39         // bypass the translation if the first character is a ~
40
if (friendly.length() > 0 && friendly.charAt(0) == '~')
41             return friendly.substring(1);
42             
43         StringBuffer JavaDoc regexp = new StringBuffer JavaDoc(friendly);
44         replaceChar(regexp, '$', "\\$");
45         replaceChar(regexp, '.', "\\.");
46         replaceChar(regexp, '*', "[^\\.]*");
47         replaceChar(regexp, '#', "[^\\.].*");
48         
49         return regexp.toString();
50     }
51     
52     /**
53      * Replaces all instances of a character in a string buffer with
54      * a character sequence.
55      */

56     public static void replaceChar(StringBuffer JavaDoc regexp, char ch, String JavaDoc repl)
57     {
58         for(int i=0;i < regexp.length();i++)
59         {
60             if (regexp.charAt(i) == ch)
61             {
62                 regexp.replace(i, i+1, repl);
63                 i += repl.length();
64             }
65         }
66     }
67 }
68
Popular Tags