KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > Matcher


1 // $Id: Matcher.java,v 1.2 2001/12/07 11:41:24 ramsdell Exp $
2

3 // A string matcher.
4

5 /*
6  * Copyright 1997 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 /**
26  * A string matcher.
27  * This class implements pattern matches of the form:
28  * pattern = prefix + "%" + suffix. If the pattern does contain '%',
29  * the percent sign is added to the beginning of the pattern.
30  * A pattern with more than one percent sign is erroneous.
31  * @version November 1997
32  * @author John D. Ramsdell
33  */

34
35 final class Matcher
36 {
37   // The wild card for string patterns.
38
private final static char wildCard = '%';
39
40   private String JavaDoc prefix = "";
41   private String JavaDoc suffix = null;
42   private int prefixLength;
43   private int suffixLength;
44
45   /**
46    * Create a matcher associated with a given pattern.
47    * When given a bad pattern, a matcher that matches nothing is created.
48    */

49   Matcher(String JavaDoc pattern) {
50     if (isPatternOkay(pattern)) {
51       suffix = pattern;
52       prefixLength = pattern.indexOf(wildCard);
53       if (prefixLength >= 0) {
54     prefix = pattern.substring(0, prefixLength);
55     suffix = pattern.substring(prefixLength + 1);
56       }
57       else
58     prefixLength = 0;
59       suffixLength = suffix.length();
60     }
61   }
62
63   /**
64    * Match a string against the pattern.
65    * @return null if there is no match, otherwise a substitution
66    * that makes the pattern equal to the string
67    */

68   String JavaDoc match(String JavaDoc string) {
69     if (suffix != null
70     && string.length() >= prefixLength + suffixLength
71     && string.startsWith(prefix)
72     && string.endsWith(suffix)) {
73       string = string.substring(0, string.length() - suffixLength);
74       string = string.substring(prefixLength);
75       return string;
76     }
77     else
78       return null;
79   }
80
81   /**
82    * Substitute the wild cards in the replacement with the match.
83    */

84   static String JavaDoc subst(String JavaDoc match, String JavaDoc replacement) {
85     if (match == null)
86       return replacement;
87     int i = replacement.indexOf(wildCard);
88     if (i < 0)
89       return replacement;
90     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
91     do {
92       sb.append(replacement.substring(0, i));
93       sb.append(match);
94       replacement = replacement.substring(i + 1);
95       i = replacement.indexOf(wildCard);
96     }
97     while (i >= 0);
98     sb.append(replacement);
99     return sb.toString();
100   }
101
102   /**
103    * Test if a string has at least one wild card.
104    */

105   static boolean isPattern(String JavaDoc string) {
106     return string.indexOf(wildCard) >= 0;
107   }
108
109   /**
110    * Test if a pattern has at most one wild card.
111    */

112   static boolean isPatternOkay(String JavaDoc pattern) {
113     int wildCards = 0;
114     for (int i = 0; i < pattern.length(); i++)
115       if (pattern.charAt(i) == wildCard)
116     wildCards++;
117     return wildCards <= 1;
118   }
119 }
120
Popular Tags