1 18 package org.apache.activemq.console.filter; 19 20 import java.util.regex.Pattern ; 21 import java.util.List ; 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 public abstract class RegExQueryFilter extends AbstractQueryFilter { 28 public static final String REGEX_PREFIX = "REGEX:QUERY:"; 29 30 34 protected RegExQueryFilter(QueryFilter next) { 35 super(next); 36 } 37 38 45 public List query(List queries) throws Exception { 46 Map regex = new HashMap (); 47 List newQueries = new ArrayList (); 48 49 for (Iterator i=queries.iterator(); i.hasNext();) { 51 String token = (String )i.next(); 53 String key = ""; 54 String 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 if (isRegularExpression(val)) { 63 regex.put(key, compileQuery(val)); 64 65 } else { 67 newQueries.add(token); 68 } 69 } 70 71 return filterCollectionUsingRegEx(regex, next.query(newQueries)); 73 } 74 75 81 protected boolean isRegularExpression(String query) { 82 return query.startsWith(REGEX_PREFIX); 83 } 84 85 90 protected Pattern compileQuery(String query) { 91 return Pattern.compile(query.substring(REGEX_PREFIX.length())); 92 } 93 94 101 protected List filterCollectionUsingRegEx(Map regex, List data) throws Exception { 102 if (regex==null || regex.isEmpty()) { 104 return data; 105 } 106 107 List filteredElems = new ArrayList (); 108 109 for (Iterator i=data.iterator(); i.hasNext();) { 111 Object dataElem = i.next(); 112 if (matches(dataElem, regex)) { 114 filteredElems.add(dataElem); 115 } 116 } 117 118 return filteredElems; 119 } 120 121 128 protected abstract boolean matches(Object data, Map regex) throws Exception ; 129 } 130 | Popular Tags |