001/**
002 * Copyright 2010-2014 The Kuali Foundation
003 *
004 * Licensed under the Educational Community License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.opensource.org/licenses/ecl2.php
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.kuali.common.util;
017
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021import java.util.regex.Matcher;
022import java.util.regex.Pattern;
023
024import org.slf4j.Logger;
025import org.slf4j.LoggerFactory;
026
027/**
028 * This class provides logic for filtering strings based on regular expressions.
029 * 
030 * @deprecated Use org.kuali.common.util.filter.StringFilter instead
031 */
032@Deprecated
033public class StringFilter {
034
035        private static final Logger logger = LoggerFactory.getLogger(StringFilter.class);
036
037        /**
038         * List of include regular expressions.
039         */
040        final List<String> includes;
041
042        /**
043         * List of exclude regular expressions.
044         */
045        final List<String> excludes;
046
047        protected List<Pattern> includePatterns;
048        protected List<Pattern> excludePatterns;
049
050        protected StringFilter(List<String> includes, List<String> excludes) {
051                this.includes = includes;
052                this.excludes = excludes;
053        }
054
055        public static final StringFilter getInstance(List<String> includes, List<String> excludes) {
056                StringFilter filter = new StringFilter(includes, excludes);
057                filter.compilePatterns();
058                return filter;
059        }
060
061        public List<String> getIncludes() {
062                return includes;
063        }
064
065        public List<String> getExcludes() {
066                return excludes;
067        }
068
069        /**
070         * Return true if the string should be included.
071         */
072        public boolean include(String s) {
073                if (exclude(s)) {
074                        logger.debug("Excluding {}", s);
075                        return false;
076                } else {
077                        return CollectionUtils.isEmpty(includePatterns) || isMatch(s, includePatterns);
078                }
079        }
080
081        /**
082         * Return true if the string should be excluded.
083         */
084        private boolean exclude(String s) {
085                return !CollectionUtils.isEmpty(excludePatterns) && isMatch(s, excludePatterns);
086        }
087
088        /**
089         * Return true if the string matches any of the patterns
090         */
091        private boolean isMatch(String s, List<Pattern> patterns) {
092                // Loop through the patterns looking for a match
093                for (Pattern pattern : patterns) {
094                        Matcher matcher = pattern.matcher(s);
095                        // We found a match, return true
096                        if (matcher.matches()) {
097                                return true;
098                        }
099                }
100                // We cycled through all of the patterns without finding a match
101                return false;
102        }
103
104        /**
105         * Compile the string patterns into Pattern objects
106         */
107        protected void compilePatterns() {
108                this.includePatterns = getPatterns(includes);
109                this.excludePatterns = getPatterns(excludes);
110        }
111
112        /**
113         * Convert a {@code List<String>} into {@code List<Pattern>}
114         */
115        protected List<Pattern> getPatterns(List<String> patterns) {
116                if (CollectionUtils.isEmpty(patterns)) {
117                        return Collections.<Pattern> emptyList();
118                }
119                List<Pattern> regexPatterns = new ArrayList<Pattern>();
120                for (String pattern : patterns) {
121                        Pattern regexPattern = Pattern.compile(pattern);
122                        regexPatterns.add(regexPattern);
123                }
124                return regexPatterns;
125        }
126}