001/**
002 * Copyright (c) 2012, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.tools.twitter.modes.filter;
031
032import java.util.ArrayList;
033import java.util.List;
034import java.util.regex.Pattern;
035
036import org.kohsuke.args4j.Option;
037
038/**
039 * Uses {@link Pattern} to match regex
040 * @author Sina Samangooei (ss@ecs.soton.ac.uk)
041 *
042 */
043public class JavaRegexEngine implements RegexEngine {
044        enum RegexPatternMode{
045                CASE_INSENSITIVE {
046                        @Override
047                        public int ord() {
048                                // TODO Auto-generated method stub
049                                return 2;
050                        }
051                },// 2
052                MULTILINE {
053                        @Override
054                        public int ord() {
055                                // TODO Auto-generated method stub
056                                return 8;
057                        }
058                },// 8
059                DOTALL {
060                        @Override
061                        public int ord() {
062                                // TODO Auto-generated method stub
063                                return 32;
064                        }
065                },// 32
066                UNICODE_CASE {
067                        @Override
068                        public int ord() {
069                                // TODO Auto-generated method stub
070                                return 64;
071                        }
072                }, //64
073                CANON_EQ {
074                        @Override
075                        public int ord() {
076                                // TODO Auto-generated method stub
077                                return 128;
078                        }
079                }; // 128
080                public abstract int ord();
081        }
082
083        @Option(name="--regex-pattern-mode", aliases="-rpm", required=false, usage="The integer representing the mode handed to java's Pattern. All provided modes are logically OR-ed together", metaVar="STRING", multiValued=true)
084        List<RegexPatternMode> regexModes = new ArrayList<RegexPatternMode>();
085
086        private List<Pattern> patterns;
087
088        /**
089         *
090         */
091        public JavaRegexEngine() {
092                patterns = new ArrayList<Pattern>();
093        }
094
095
096
097
098        @Override
099        public void add(String regex) {
100                int patternMode = 0;
101                for (RegexPatternMode mode : this.regexModes) {
102                        patternMode |= mode.ord();
103                }
104                this.patterns.add(Pattern.compile(regex, patternMode));
105        }
106
107        @Override
108        public boolean matches(String str) {
109                for (Pattern p : this.patterns) {
110                        if(p.matcher(str).matches()){
111                                return true;
112                        }
113                }
114                return false;
115        }
116
117}