KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.regex.Pattern JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26
27 public abstract class RegExQueryFilter extends AbstractQueryFilter {
28     public static final String JavaDoc REGEX_PREFIX = "REGEX:QUERY:";
29
30     /**
31      * Creates a regular expression query that is able to match an object using key-value pattern regex filtering
32      * @param next
33      */

34     protected RegExQueryFilter(QueryFilter next) {
35         super(next);
36     }
37
38     /**
39      * Separates the regular expressions queries from the usual queries. A query is a regex query, if it is key-value pair
40      * with the format <key>=<value>, and value is a pattern that satisfies the isRegularExpression method.
41      * @param queries - list of queries
42      * @return filtered objects that matches the regex query
43      * @throws Exception
44      */

45     public List JavaDoc query(List JavaDoc queries) throws Exception JavaDoc {
46         Map JavaDoc regex = new HashMap JavaDoc();
47         List JavaDoc newQueries = new ArrayList JavaDoc();
48
49         // Lets parse for regular expression queries
50
for (Iterator JavaDoc i=queries.iterator(); i.hasNext();) {
51             // Get key-value pair
52
String JavaDoc token = (String JavaDoc)i.next();
53             String JavaDoc key = "";
54             String JavaDoc val = "";
55             int pos = token.indexOf("=");
56             if (pos >= 0) {
57                 val = token.substring(pos + 1);
58                 key = token.substring(0, pos);
59             }
60
61             // Add the regex query to list and make it a non-factor in the succeeding queries
62
if (isRegularExpression(val)) {
63                 regex.put(key, compileQuery(val));
64
65             // Add the normal query to the query list
66
} else {
67                 newQueries.add(token);
68             }
69         }
70
71         // Filter the result using the regular expressions specified
72
return filterCollectionUsingRegEx(regex, next.query(newQueries));
73     }
74
75     /**
76      * Checks if a given string is a regular expression query. Currently, a pattern is a regex query, if it starts with
77      * the RegExQueryFilter.REGEX_PREFIX.
78      * @param query
79      * @return
80      */

81     protected boolean isRegularExpression(String JavaDoc query) {
82         return query.startsWith(REGEX_PREFIX);
83     }
84
85     /**
86      * Compiles the regex query to a pattern.
87      * @param query - query string to compile
88      * @return regex pattern
89      */

90     protected Pattern JavaDoc compileQuery(String JavaDoc query) {
91         return Pattern.compile(query.substring(REGEX_PREFIX.length()));
92     }
93
94     /**
95      * Filter the specified colleciton using the regex patterns extracted.
96      * @param regex - regex map
97      * @param data - list of objects to filter
98      * @return filtered list of objects that matches the regex map
99      * @throws Exception
100      */

101     protected List JavaDoc filterCollectionUsingRegEx(Map JavaDoc regex, List JavaDoc data) throws Exception JavaDoc {
102         // No regular expressions filtering needed
103
if (regex==null || regex.isEmpty()) {
104             return data;
105         }
106
107         List JavaDoc filteredElems = new ArrayList JavaDoc();
108
109         // Get each data object to filter
110
for (Iterator JavaDoc i=data.iterator(); i.hasNext();) {
111             Object JavaDoc dataElem = i.next();
112             // If properties of data matches all the regex pattern, add it
113
if (matches(dataElem, regex)) {
114                 filteredElems.add(dataElem);
115             }
116         }
117
118         return filteredElems;
119     }
120
121     /**
122      * Determines how the object is to be matched to the regex map.
123      * @param data - object to match
124      * @param regex - regex map
125      * @return true, if the object matches the regex map, false otherwise
126      * @throws Exception
127      */

128     protected abstract boolean matches(Object JavaDoc data, Map JavaDoc regex) throws Exception JavaDoc;
129 }
130
Popular Tags