001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.internal.misc;
011:
012: import java.util.Vector;
013:
014: /**
015: * A string pattern matcher, suppporting "*" and "?" wildcards.
016: */
017: public class StringMatcher {
018: protected String fPattern;
019:
020: protected int fLength; // pattern length
021:
022: protected boolean fIgnoreWildCards;
023:
024: protected boolean fIgnoreCase;
025:
026: protected boolean fHasLeadingStar;
027:
028: protected boolean fHasTrailingStar;
029:
030: protected String fSegments[]; //the given pattern is split into * separated segments
031:
032: /* boundary value beyond which we don't need to search in the text */
033: protected int fBound = 0;
034:
035: protected static final char fSingleWildCard = '\u0000';
036:
037: public static class Position {
038: int start; //inclusive
039:
040: int end; //exclusive
041:
042: public Position(int start, int end) {
043: this .start = start;
044: this .end = end;
045: }
046:
047: public int getStart() {
048: return start;
049: }
050:
051: public int getEnd() {
052: return end;
053: }
054: }
055:
056: /**
057: * StringMatcher constructor takes in a String object that is a simple
058: * pattern which may contain '*' for 0 and many characters and
059: * '?' for exactly one character.
060: *
061: * Literal '*' and '?' characters must be escaped in the pattern
062: * e.g., "\*" means literal "*", etc.
063: *
064: * Escaping any other character (including the escape character itself),
065: * just results in that character in the pattern.
066: * e.g., "\a" means "a" and "\\" means "\"
067: *
068: * If invoking the StringMatcher with string literals in Java, don't forget
069: * escape characters are represented by "\\".
070: *
071: * @param pattern the pattern to match text against
072: * @param ignoreCase if true, case is ignored
073: * @param ignoreWildCards if true, wild cards and their escape sequences are ignored
074: * (everything is taken literally).
075: */
076: public StringMatcher(String pattern, boolean ignoreCase,
077: boolean ignoreWildCards) {
078: if (pattern == null) {
079: throw new IllegalArgumentException();
080: }
081: fIgnoreCase = ignoreCase;
082: fIgnoreWildCards = ignoreWildCards;
083: fPattern = pattern;
084: fLength = pattern.length();
085:
086: if (fIgnoreWildCards) {
087: parseNoWildCards();
088: } else {
089: parseWildCards();
090: }
091: }
092:
093: /**
094: * Find the first occurrence of the pattern between <code>start</code)(inclusive)
095: * and <code>end</code>(exclusive).
096: * @param text the String object to search in
097: * @param start the starting index of the search range, inclusive
098: * @param end the ending index of the search range, exclusive
099: * @return an <code>StringMatcher.Position</code> object that keeps the starting
100: * (inclusive) and ending positions (exclusive) of the first occurrence of the
101: * pattern in the specified range of the text; return null if not found or subtext
102: * is empty (start==end). A pair of zeros is returned if pattern is empty string
103: * Note that for pattern like "*abc*" with leading and trailing stars, position of "abc"
104: * is returned. For a pattern like"*??*" in text "abcdf", (1,3) is returned
105: */
106: public StringMatcher.Position find(String text, int start, int end) {
107: if (text == null) {
108: throw new IllegalArgumentException();
109: }
110:
111: int tlen = text.length();
112: if (start < 0) {
113: start = 0;
114: }
115: if (end > tlen) {
116: end = tlen;
117: }
118: if (end < 0 || start >= end) {
119: return null;
120: }
121: if (fLength == 0) {
122: return new Position(start, start);
123: }
124: if (fIgnoreWildCards) {
125: int x = posIn(text, start, end);
126: if (x < 0) {
127: return null;
128: }
129: return new Position(x, x + fLength);
130: }
131:
132: int segCount = fSegments.length;
133: if (segCount == 0) {
134: return new Position(start, end);
135: }
136:
137: int curPos = start;
138: int matchStart = -1;
139: int i;
140: for (i = 0; i < segCount && curPos < end; ++i) {
141: String current = fSegments[i];
142: int nextMatch = regExpPosIn(text, curPos, end, current);
143: if (nextMatch < 0) {
144: return null;
145: }
146: if (i == 0) {
147: matchStart = nextMatch;
148: }
149: curPos = nextMatch + current.length();
150: }
151: if (i < segCount) {
152: return null;
153: }
154: return new Position(matchStart, curPos);
155: }
156:
157: /**
158: * match the given <code>text</code> with the pattern
159: * @return true if matched otherwise false
160: * @param text a String object
161: */
162: public boolean match(String text) {
163: if (text == null) {
164: return false;
165: }
166: return match(text, 0, text.length());
167: }
168:
169: /**
170: * Given the starting (inclusive) and the ending (exclusive) positions in the
171: * <code>text</code>, determine if the given substring matches with aPattern
172: * @return true if the specified portion of the text matches the pattern
173: * @param text a String object that contains the substring to match
174: * @param start marks the starting position (inclusive) of the substring
175: * @param end marks the ending index (exclusive) of the substring
176: */
177: public boolean match(String text, int start, int end) {
178: if (null == text) {
179: throw new IllegalArgumentException();
180: }
181:
182: if (start > end) {
183: return false;
184: }
185:
186: if (fIgnoreWildCards) {
187: return (end - start == fLength)
188: && fPattern.regionMatches(fIgnoreCase, 0, text,
189: start, fLength);
190: }
191: int segCount = fSegments.length;
192: if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) {
193: return true;
194: }
195: if (start == end) {
196: return fLength == 0;
197: }
198: if (fLength == 0) {
199: return start == end;
200: }
201:
202: int tlen = text.length();
203: if (start < 0) {
204: start = 0;
205: }
206: if (end > tlen) {
207: end = tlen;
208: }
209:
210: int tCurPos = start;
211: int bound = end - fBound;
212: if (bound < 0) {
213: return false;
214: }
215: int i = 0;
216: String current = fSegments[i];
217: int segLength = current.length();
218:
219: /* process first segment */
220: if (!fHasLeadingStar) {
221: if (!regExpRegionMatches(text, start, current, 0, segLength)) {
222: return false;
223: } else {
224: ++i;
225: tCurPos = tCurPos + segLength;
226: }
227: }
228: if ((fSegments.length == 1) && (!fHasLeadingStar)
229: && (!fHasTrailingStar)) {
230: // only one segment to match, no wildcards specified
231: return tCurPos == end;
232: }
233: /* process middle segments */
234: while (i < segCount) {
235: current = fSegments[i];
236: int currentMatch;
237: int k = current.indexOf(fSingleWildCard);
238: if (k < 0) {
239: currentMatch = textPosIn(text, tCurPos, end, current);
240: if (currentMatch < 0) {
241: return false;
242: }
243: } else {
244: currentMatch = regExpPosIn(text, tCurPos, end, current);
245: if (currentMatch < 0) {
246: return false;
247: }
248: }
249: tCurPos = currentMatch + current.length();
250: i++;
251: }
252:
253: /* process final segment */
254: if (!fHasTrailingStar && tCurPos != end) {
255: int clen = current.length();
256: return regExpRegionMatches(text, end - clen, current, 0,
257: clen);
258: }
259: return i == segCount;
260: }
261:
262: /**
263: * This method parses the given pattern into segments seperated by wildcard '*' characters.
264: * Since wildcards are not being used in this case, the pattern consists of a single segment.
265: */
266: private void parseNoWildCards() {
267: fSegments = new String[1];
268: fSegments[0] = fPattern;
269: fBound = fLength;
270: }
271:
272: /**
273: * Parses the given pattern into segments seperated by wildcard '*' characters.
274: * @param p, a String object that is a simple regular expression with '*' and/or '?'
275: */
276: private void parseWildCards() {
277: if (fPattern.startsWith("*")) { //$NON-NLS-1$
278: fHasLeadingStar = true;
279: }
280: if (fPattern.endsWith("*")) {//$NON-NLS-1$
281: /* make sure it's not an escaped wildcard */
282: if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
283: fHasTrailingStar = true;
284: }
285: }
286:
287: Vector temp = new Vector();
288:
289: int pos = 0;
290: StringBuffer buf = new StringBuffer();
291: while (pos < fLength) {
292: char c = fPattern.charAt(pos++);
293: switch (c) {
294: case '\\':
295: if (pos >= fLength) {
296: buf.append(c);
297: } else {
298: char next = fPattern.charAt(pos++);
299: /* if it's an escape sequence */
300: if (next == '*' || next == '?' || next == '\\') {
301: buf.append(next);
302: } else {
303: /* not an escape sequence, just insert literally */
304: buf.append(c);
305: buf.append(next);
306: }
307: }
308: break;
309: case '*':
310: if (buf.length() > 0) {
311: /* new segment */
312: temp.addElement(buf.toString());
313: fBound += buf.length();
314: buf.setLength(0);
315: }
316: break;
317: case '?':
318: /* append special character representing single match wildcard */
319: buf.append(fSingleWildCard);
320: break;
321: default:
322: buf.append(c);
323: }
324: }
325:
326: /* add last buffer to segment list */
327: if (buf.length() > 0) {
328: temp.addElement(buf.toString());
329: fBound += buf.length();
330: }
331:
332: fSegments = new String[temp.size()];
333: temp.copyInto(fSegments);
334: }
335:
336: /**
337: * @param text a string which contains no wildcard
338: * @param start the starting index in the text for search, inclusive
339: * @param end the stopping point of search, exclusive
340: * @return the starting index in the text of the pattern , or -1 if not found
341: */
342: protected int posIn(String text, int start, int end) {//no wild card in pattern
343: int max = end - fLength;
344:
345: if (!fIgnoreCase) {
346: int i = text.indexOf(fPattern, start);
347: if (i == -1 || i > max) {
348: return -1;
349: }
350: return i;
351: }
352:
353: for (int i = start; i <= max; ++i) {
354: if (text.regionMatches(true, i, fPattern, 0, fLength)) {
355: return i;
356: }
357: }
358:
359: return -1;
360: }
361:
362: /**
363: * @param text a simple regular expression that may only contain '?'(s)
364: * @param start the starting index in the text for search, inclusive
365: * @param end the stopping point of search, exclusive
366: * @param p a simple regular expression that may contains '?'
367: * @return the starting index in the text of the pattern , or -1 if not found
368: */
369: protected int regExpPosIn(String text, int start, int end, String p) {
370: int plen = p.length();
371:
372: int max = end - plen;
373: for (int i = start; i <= max; ++i) {
374: if (regExpRegionMatches(text, i, p, 0, plen)) {
375: return i;
376: }
377: }
378: return -1;
379: }
380:
381: /**
382: *
383: * @return boolean
384: * @param text a String to match
385: * @param start int that indicates the starting index of match, inclusive
386: * @param end</code> int that indicates the ending index of match, exclusive
387: * @param p String, String, a simple regular expression that may contain '?'
388: * @param ignoreCase boolean indicating wether code>p</code> is case sensitive
389: */
390: protected boolean regExpRegionMatches(String text, int tStart,
391: String p, int pStart, int plen) {
392: while (plen-- > 0) {
393: char tchar = text.charAt(tStart++);
394: char pchar = p.charAt(pStart++);
395:
396: /* process wild cards */
397: if (!fIgnoreWildCards) {
398: /* skip single wild cards */
399: if (pchar == fSingleWildCard) {
400: continue;
401: }
402: }
403: if (pchar == tchar) {
404: continue;
405: }
406: if (fIgnoreCase) {
407: if (Character.toUpperCase(tchar) == Character
408: .toUpperCase(pchar)) {
409: continue;
410: }
411: // comparing after converting to upper case doesn't handle all cases;
412: // also compare after converting to lower case
413: if (Character.toLowerCase(tchar) == Character
414: .toLowerCase(pchar)) {
415: continue;
416: }
417: }
418: return false;
419: }
420: return true;
421: }
422:
423: /**
424: * @param text the string to match
425: * @param start the starting index in the text for search, inclusive
426: * @param end the stopping point of search, exclusive
427: * @param p a pattern string that has no wildcard
428: * @return the starting index in the text of the pattern , or -1 if not found
429: */
430: protected int textPosIn(String text, int start, int end, String p) {
431:
432: int plen = p.length();
433: int max = end - plen;
434:
435: if (!fIgnoreCase) {
436: int i = text.indexOf(p, start);
437: if (i == -1 || i > max) {
438: return -1;
439: }
440: return i;
441: }
442:
443: for (int i = start; i <= max; ++i) {
444: if (text.regionMatches(true, i, p, 0, plen)) {
445: return i;
446: }
447: }
448:
449: return -1;
450: }
451: }
|