1 22 package org.netbeans.lib.cvsclient.util; 23 24 import java.util.*; 25 26 29 public class SimpleStringPattern 30 implements StringPattern { 31 32 private static final char MATCH_EACH = '*'; 33 private static final char MATCH_ONE = '?'; 34 35 private final List subPatterns = new LinkedList(); 36 37 41 public SimpleStringPattern(String definition) { 42 splitInSubPattern(definition); 43 } 44 45 48 public boolean doesMatch(String string) { 49 int index = 0; 50 SubPattern subPattern = null; 51 for (Iterator it = subPatterns.iterator(); it.hasNext();) { 52 subPattern = (SubPattern)it.next(); 53 index = subPattern.doesMatch(string, index); 54 if (index < 0) { 55 return false; 56 } 57 } 58 59 if (index == string.length()) { 60 return true; 61 } 62 if (subPattern == null) { 63 return false; 64 } 65 return subPattern.checkEnding(string, index); 66 } 67 68 private void splitInSubPattern(String definition) { 69 char prevSubPattern = ' '; 70 71 int prevIndex = 0; 72 for (int index = 0; index >= 0;) { 73 prevIndex = index; 74 75 index = definition.indexOf(MATCH_EACH, prevIndex); 76 if (index >= 0) { 77 String match = definition.substring(prevIndex, index); 78 addSubPattern(match, prevSubPattern); 79 prevSubPattern = MATCH_EACH; 80 index++; 81 continue; 82 } 83 index = definition.indexOf(MATCH_ONE, prevIndex); 84 if (index >= 0) { 85 String match = definition.substring(prevIndex, index); 86 addSubPattern(match, prevSubPattern); 87 prevSubPattern = MATCH_ONE; 88 index++; 89 continue; 90 } 91 } 92 String match = definition.substring(prevIndex); 93 addSubPattern(match, prevSubPattern); 94 } 95 96 private void addSubPattern(String match, char subPatternMode) { 97 SubPattern subPattern = null; 98 switch (subPatternMode) { 99 case MATCH_EACH: 100 subPattern = new MatchEachCharPattern(match); 101 break; 102 case MATCH_ONE: 103 subPattern = new MatchOneCharPattern(match); 104 break; 105 default: 106 subPattern = new MatchExactSubPattern(match); 107 break; 108 } 109 110 subPatterns.add(subPattern); 111 } 112 113 public String toString() { 114 StringBuffer buffer = new StringBuffer (); 115 for (Iterator it = subPatterns.iterator(); it.hasNext();) { 116 SubPattern subPattern = (SubPattern)it.next(); 117 buffer.append(subPattern.toString()); 118 } 119 return buffer.toString(); 120 } 121 122 public boolean equals(Object obj) { 123 if (!(obj instanceof SimpleStringPattern)) return false; 124 return subPatterns.equals(((SimpleStringPattern) obj).subPatterns); 125 } 126 127 public int hashCode() { 128 return -subPatterns.hashCode(); 129 } 130 131 public static void main(String [] arguments) { 132 StringPattern sp = new SimpleStringPattern("a*b"); 134 test(sp, "ab", true); test(sp, "aab", true); test(sp, "ba", false); test(sp, "abc", false); 139 sp = new SimpleStringPattern("*.txt"); 141 test(sp, "datei.txt", true); test(sp, ".txt", true); test(sp, "datei.tx", false); test(sp, "datei.txt.txt", true); 146 sp = new SimpleStringPattern("datei*1*"); 148 test(sp, "datei0.txt", false); test(sp, "datei1.txt", true); test(sp, "datei.tx", false); test(sp, "datei1.txt.txt", true); 153 sp = new SimpleStringPattern("Makefile"); 155 test(sp, "Makefile", true); test(sp, "Makefile.mak", false); test(sp, "Makefile1", false); test(sp, ".Makefile", false); test(sp, ".Makefile.", false); 161 sp = new SimpleStringPattern("*~"); 163 test(sp, "datei~", true); test(sp, "datei~1", false); test(sp, "datei~1~", true); 167 SimpleStringPattern pattern1 = new SimpleStringPattern("*.class"); 169 SimpleStringPattern pattern2 = new SimpleStringPattern("*.class"); 170 System.err.println(pattern1+".equals("+pattern2+") = "+pattern1.equals(pattern2)); 171 172 pattern1 = new SimpleStringPattern("?.class"); 173 pattern2 = new SimpleStringPattern("*.class"); 174 System.err.println(pattern1+".equals("+pattern2+") = "+pattern1.equals(pattern2)); 175 176 pattern1 = new SimpleStringPattern("*.clazz"); 177 pattern2 = new SimpleStringPattern("*.class"); 178 System.err.println(pattern1+".equals("+pattern2+") = "+pattern1.equals(pattern2)); 179 } 180 181 private static void test(StringPattern sp, String testString, boolean shouldResult) { 182 System.err.print('"' + sp.toString() + '"' + ": " + testString + " " + shouldResult); 184 boolean doesMatch = sp.doesMatch(testString); 185 186 if (doesMatch == shouldResult) { 187 System.err.println(" proved"); } 189 else { 190 System.err.println(" **denied**"); } 192 } 193 194 private static abstract class SubPattern { 195 protected final String match; 196 197 protected SubPattern(String match) { 198 this.match = match; 199 } 200 201 206 public abstract int doesMatch(String string, int index); 207 208 public boolean checkEnding(String string, int index) { 209 return false; 210 } 211 212 public boolean equals(Object obj) { 213 if (!(this.getClass().isInstance(obj))) return false; 214 return match.equals(((SubPattern) obj).match); 215 } 216 217 public int hashCode() { 218 return -match.hashCode(); 219 } 220 } 221 222 private static class MatchExactSubPattern extends SubPattern { 223 public MatchExactSubPattern(String match) { 224 super(match); 225 } 226 227 public int doesMatch(String string, int index) { 228 if (!string.startsWith(match, index)) { 229 return -1; 230 } 231 return index + match.length(); 232 } 233 234 public String toString() { 235 return match; 236 } 237 } 238 239 private static class MatchEachCharPattern extends SubPattern { 240 public MatchEachCharPattern(String match) { 241 super(match); 242 } 243 244 public int doesMatch(String string, int index) { 245 int matchIndex = string.indexOf(match, index); 246 if (matchIndex < 0) { 247 return -1; 248 } 249 return matchIndex + match.length(); 250 } 251 252 public boolean checkEnding(String string, int index) { 253 return string.endsWith(match); 254 } 255 256 public String toString() { 257 return MATCH_EACH + match; 258 } 259 } 260 261 private static class MatchOneCharPattern extends MatchExactSubPattern { 262 public MatchOneCharPattern(String match) { 263 super(match); 264 } 265 266 public int doesMatch(String string, int index) { 267 index++; 268 if (string.length() < index) { 269 return -1; 270 } 271 return super.doesMatch(string, index); 272 } 273 274 public String toString() { 275 return MATCH_ONE + match; 276 } 277 } 278 } 279 | Popular Tags |