001 package net.sourceforge.mp3dings.filter;
002
003 /* Mp3dings - manage mp3 meta-information
004 * Copyright (C) 2003 Moritz Ringler
005 * $Id: StringSelector.java 13 2007-07-27 09:52:39Z ringler $
006 *
007 * This program is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU General Public License
009 * as published by the Free Software Foundation; either version 2
010 * of the License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU General Public License
018 * along with this program; if not, write to the Free Software
019 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020 */
021
022 import java.util.prefs.Preferences;
023 import java.util.regex.Pattern;
024 import java.util.regex.PatternSyntaxException;
025 import net.sourceforge.mp3dings.utils.Bean;
026
027 /** A filter that allows a certain string value to pass if and only if
028 * it matches a given regular expression.
029 * @author Moritz Ringler
030 * @version $Revision: 13 $ ($Date: 2007-07-27 11:52:39 +0200 (Fr, 27 Jul 2007) $)
031 */
032 public class StringSelector extends AbstractStringFilter{
033 static final long serialVersionUID = -7424036081020787786L;
034
035 private Pattern re = null;
036 private boolean include = true;
037 private boolean regExp = true;
038 private boolean wordMatch = false;
039 private boolean caseSensitive = false;
040 private String pattern = ".*"; //match everything
041
042 /** Default constructor.
043 */
044 public StringSelector(){
045 compilePattern();
046 }
047
048 /** returns a StringSelector or null **/
049 public static Bean load(Preferences pref, String key){
050 Bean b = Bean.load(pref, key);
051 if(! (b instanceof StringSelector) ){
052 b = null;
053 }
054 return b;
055 }
056
057 /** **/
058 public StringSelector(String pattern, boolean incl){
059 this.pattern = pattern;
060 include = incl;
061 compilePattern();
062 }
063
064 private void compilePattern(){
065 try{
066 re = compilePattern(pattern);
067 } catch (PatternSyntaxException ex){
068 setRegExp(false);
069 }
070 }
071
072 private Pattern compilePattern(String pp){
073 String p = (regExp)? pp : "\\Q"+pp+"\\E";
074 if(wordMatch){
075 p = "\\b" + p + "\\b";
076 }
077 if(caseSensitive){
078 p = "(?i)"+p;
079 }
080 return Pattern.compile(p);
081 }
082
083 public boolean isRegExp(){
084 return regExp;
085 }
086
087 public void setRegExp(boolean b){
088 if(regExp != b){
089 regExp = b;
090 try{
091 re = compilePattern(pattern);
092 } catch (Exception ex){
093 regExp = false;
094 }
095 firePropertyChange("regExp",!regExp, regExp);
096 }
097 }
098
099 public boolean isCaseSensitive(){
100 return caseSensitive;
101 }
102
103 public void setCaseSensitive(boolean b){
104 if(caseSensitive != b){
105 caseSensitive = b;
106 compilePattern();
107 firePropertyChange("caseSensitive",!b, b);
108 }
109 }
110
111 public boolean isWordMatch(){
112 return wordMatch;
113 }
114
115 public void setWordMatch(boolean b){
116 if(wordMatch != b){
117 wordMatch = b;
118 compilePattern();
119 firePropertyChange("wordMatch",!b, b);
120 }
121 }
122
123 public boolean getInclude(){
124 return include;
125 }
126
127 public void setInclude(boolean b){
128 if(include != b){
129 include = b;
130 firePropertyChange("include",!b, b);
131 }
132 }
133
134 public String getPattern(){
135 return pattern;
136 }
137
138 /** if p is not a valid regular expression. setRegExp(false) will be called**/
139 public void setPattern(String p){
140 if(!pattern.equals(p)){
141 String oldpattern = pattern;
142 pattern = p;
143 compilePattern();
144 firePropertyChange("pattern", oldpattern, pattern);
145 }
146 }
147
148 public String filterString(String str){
149 // condition is false when re matches and include or
150 // re does not match and not include
151 return (re.matcher(str).find() ^ include) ? null: str;
152 }
153
154 public boolean equals(Object o){
155 if(this == o){
156 return true;
157 }
158 boolean result = false;
159 if(o instanceof StringSelector){
160 StringSelector other = (StringSelector) o;
161 result = result &&
162 (include == other.include);
163 result = result &&
164 ((re == null)
165 ? (other.re == null)
166 : re.equals(other.re));
167 result = result &&
168 ((getName() == null)
169 ? (other.getName() == null)
170 : getName().equals(other.getName()));
171 }
172 return result;
173 }
174
175 public int hashCode(){
176 int result = 19;
177 result = 43 * result + (include? 0 : 1);
178 result = 43 * result +
179 ((re == null)? 0 : re.hashCode());
180 result = 43 * result +
181 ((getName() == null)? 0 : getName().hashCode());
182 return result;
183 }
184 }