KickJava   Java API By Example, From Geeks To Geeks.

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


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 WildcardToRegExTransformFilter extends WildcardTransformFilter {
21     /**
22      * Creates a filter that is able to transform a wildcard query to a regular expression query string
23      * @param next - next query filter
24      */

25     public WildcardToRegExTransformFilter(RegExQueryFilter 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 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
46
return ((val.indexOf("*") >= 0) || (val.indexOf("?") >= 0));
47     }
48
49     /**
50      * Transform a wildcard query to regular expression format
51      * @param query - query string to transform
52      * @return regex query string
53      */

54     protected String JavaDoc transformWildcardQuery(String JavaDoc query) {
55         // Get the 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("[.]", "\\\\."); // Escape all dot characters. From (.) to (\.)
65
val = val.replaceAll("[?]", "."); // Match single characters
66
val = val.replaceAll("[*]", ".*?"); // Match all characters, use reluctant quantifier
67
val = "(" + val +")"; // Let's group the query for clarity
68
val = RegExQueryFilter.REGEX_PREFIX + val; // Flag as a regular expression query
69

70         return key + "=" + val;
71     }
72 }
73
Popular Tags