001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.console.filter;
018
019import java.util.ArrayList;
020import java.util.Iterator;
021import java.util.List;
022
023public abstract class WildcardTransformFilter extends AbstractQueryFilter {
024
025    /**
026     * Creates a wildcard transform filter that is able to convert a wildcard
027     * expression (determined by isWildcardQuery) to a another query type (use
028     * transformWildcardQuery).
029     * 
030     * @param next - the next query filter
031     */
032    protected WildcardTransformFilter(QueryFilter next) {
033        super(next);
034    }
035
036    /**
037     * Converts the query list to set of different queries
038     * 
039     * @param queries - query list to transform
040     * @return - result of the query
041     * @throws Exception
042     */
043    public List query(List queries) throws Exception {
044        List newQueries = new ArrayList();
045
046        for (Iterator i = queries.iterator(); i.hasNext();) {
047            String queryToken = (String)i.next();
048
049            // Transform the wildcard query
050            if (isWildcardQuery(queryToken)) {
051                // Transform the value part only
052                newQueries.add(transformWildcardQuery(queryToken));
053
054                // Maintain the query as is
055            } else {
056                newQueries.add(queryToken);
057            }
058        }
059
060        return next.query(newQueries);
061    }
062
063    /**
064     * Use to determine is a query string is a wildcard query
065     * 
066     * @param query - query string
067     * @return true, if the query string is a wildcard query, false otherwise
068     */
069    protected abstract boolean isWildcardQuery(String query);
070
071    /**
072     * Use to transform a wildcard query string to another query format
073     * 
074     * @param query - query string to transform
075     * @return transformed query
076     */
077    protected abstract String transformWildcardQuery(String query);
078}