1 11 package org.eclipse.core.internal.propertytester; 12 13 import java.util.ArrayList ; 14 15 20 public class StringMatcher { 21 private static final char SINGLE_WILD_CARD = '\u0000'; 22 23 26 private int bound = 0; 27 28 private boolean hasLeadingStar; 29 30 private boolean hasTrailingStar; 31 32 private final String pattern; 33 34 private final int patternLength; 35 36 39 private String segments[]; 40 41 58 public StringMatcher(String pattern) { 59 if (pattern == null) 60 throw new IllegalArgumentException (); 61 this.pattern = pattern; 62 patternLength = pattern.length(); 63 parseWildCards(); 64 } 65 66 73 private int findPosition(String text, int start, int end, String p) { 74 boolean hasWildCard = p.indexOf(SINGLE_WILD_CARD) >= 0; 75 int plen = p.length(); 76 for (int i = start, max = end - plen; i <= max; ++i) { 77 if (hasWildCard) { 78 if (regExpRegionMatches(text, i, p, 0, plen)) 79 return i; 80 } else { 81 if (text.regionMatches(true, i, p, 0, plen)) 82 return i; 83 } 84 } 85 return -1; 86 } 87 88 94 public boolean match(String text) { 95 if (text == null) 96 return false; 97 final int end = text.length(); 98 final int segmentCount = segments.length; 99 if (segmentCount == 0 && (hasLeadingStar || hasTrailingStar)) return true; 101 if (end == 0) 102 return patternLength == 0; 103 if (patternLength == 0) 104 return false; 105 int currentTextPosition = 0; 106 if ((end - bound) < 0) 107 return false; 108 int segmentIndex = 0; 109 String current = segments[segmentIndex]; 110 111 112 if (!hasLeadingStar) { 113 int currentLength = current.length(); 114 if (!regExpRegionMatches(text, 0, current, 0, currentLength)) 115 return false; 116 segmentIndex++; 117 currentTextPosition = currentTextPosition + currentLength; 118 } 119 if ((segmentCount == 1) && (!hasLeadingStar) && (!hasTrailingStar)) { 120 return currentTextPosition == end; 122 } 123 124 while (segmentIndex < segmentCount) { 125 current = segments[segmentIndex]; 126 int currentMatch = findPosition(text, currentTextPosition, end, current); 127 if (currentMatch < 0) 128 return false; 129 currentTextPosition = currentMatch + current.length(); 130 segmentIndex++; 131 } 132 133 134 if (!hasTrailingStar && currentTextPosition != end) { 135 int currentLength = current.length(); 136 return regExpRegionMatches(text, end - currentLength, current, 0, currentLength); 137 } 138 return segmentIndex == segmentCount; 139 } 140 141 144 private void parseWildCards() { 145 if (pattern.startsWith("*")) hasLeadingStar = true; 147 if (pattern.endsWith("*")) { 149 if (patternLength > 1 && pattern.charAt(patternLength - 2) != '\\') { 150 hasTrailingStar = true; 151 } 152 } 153 154 ArrayList temp = new ArrayList (); 155 156 int pos = 0; 157 StringBuffer buf = new StringBuffer (); 158 while (pos < patternLength) { 159 char c = pattern.charAt(pos++); 160 switch (c) { 161 case '\\' : 162 if (pos >= patternLength) { 163 buf.append(c); 164 } else { 165 char next = pattern.charAt(pos++); 166 167 if (next == '*' || next == '?' || next == '\\') { 168 buf.append(next); 169 } else { 170 171 buf.append(c); 172 buf.append(next); 173 } 174 } 175 break; 176 case '*' : 177 if (buf.length() > 0) { 178 179 temp.add(buf.toString()); 180 bound += buf.length(); 181 buf.setLength(0); 182 } 183 break; 184 case '?' : 185 186 buf.append(SINGLE_WILD_CARD); 187 break; 188 default : 189 buf.append(c); 190 } 191 } 192 193 194 if (buf.length() > 0) { 195 temp.add(buf.toString()); 196 bound += buf.length(); 197 } 198 segments = (String []) temp.toArray(new String [temp.size()]); 199 } 200 201 210 private boolean regExpRegionMatches(String text, int tStart, String p, int pStart, int plen) { 211 while (plen-- > 0) { 212 char tchar = text.charAt(tStart++); 213 char pchar = p.charAt(pStart++); 214 215 if (pchar == SINGLE_WILD_CARD) 217 continue; 218 if (pchar == tchar) 219 continue; 220 if (Character.toUpperCase(tchar) == Character.toUpperCase(pchar)) 221 continue; 222 if (Character.toLowerCase(tchar) == Character.toLowerCase(pchar)) 225 continue; 226 return false; 227 } 228 return true; 229 } 230 } | Popular Tags |