KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > console > filter > WildcardToMsgSelectorTransformFilter


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.console.filter;
19
20 public class WildcardToMsgSelectorTransformFilter extends WildcardTransformFilter{
21     /**
22      * Creates a filter that is able to transform a wildcard query to a message selector format
23      * @param next - next query filter
24      */

25     public WildcardToMsgSelectorTransformFilter(QueryFilter next) {
26         super(next);
27     }
28
29     /**
30      * Use to determine if a query string is a wildcard query. A query string is a wildcard query if it is a key-value
31      * pair with the format <key>=<value> and the value is enclosed in '' and contains '*' and '?'.
32      * @param query - query string
33      * @return true, if the query string is a wildcard query, false otherwise
34      */

35     protected boolean isWildcardQuery(String JavaDoc query) {
36         // If the query is a key=value pair
37
String JavaDoc key = query;
38         String JavaDoc val = "";
39         int pos = key.indexOf("=");
40         if (pos >= 0) {
41             val = key.substring(pos + 1);
42             key = key.substring(0, pos);
43         }
44
45         // If the value contains wildcards and is enclose by '
46
return val.startsWith("'") && val.endsWith("'") && ((val.indexOf("*") >= 0) || (val.indexOf("?") >= 0));
47     }
48
49     /**
50      * Transform a wildcard query to message selector format
51      * @param query - query string to transform
52      * @return message selector format string
53      */

54     protected String JavaDoc transformWildcardQuery(String JavaDoc query) {
55         // If the query is a key=value pair
56
String JavaDoc key = query;
57         String JavaDoc val = "";
58         int pos = key.indexOf("=");
59         if (pos >= 0) {
60             val = key.substring(pos + 1);
61             key = key.substring(0, pos);
62         }
63
64         val = val.replaceAll("[?]", "_");
65         val = val.replaceAll("[*]", "%");
66
67         return key + " LIKE " + val;
68     }
69 }
70
Popular Tags