KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > util > SimpleStringPattern


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Thomas Singer.
17  * Portions created by Thomas Singer Copyright (C) 2001.
18  * All Rights Reserved.
19  *
20  * Contributor(s): Thomas Singer, Milos Kleint
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.util;
23
24 import java.util.*;
25
26 /**
27  * @author Thomas Singer
28  */

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     /**
38      * Creates a SimpleStringPattern for the specified definition.
39      * The definition might contain two special characters ('*' and '?').
40      */

41     public SimpleStringPattern(String JavaDoc definition) {
42         splitInSubPattern(definition);
43     }
44
45     /**
46      * Returns whether the specified string matches thiz pattern.
47      */

48     public boolean doesMatch(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc match = definition.substring(prevIndex, index);
86                 addSubPattern(match, prevSubPattern);
87                 prevSubPattern = MATCH_ONE;
88                 index++;
89                 continue;
90             }
91         }
92         String JavaDoc match = definition.substring(prevIndex);
93         addSubPattern(match, prevSubPattern);
94     }
95
96     private void addSubPattern(String JavaDoc 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 JavaDoc toString() {
114         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
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 JavaDoc 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 JavaDoc[] arguments) {
132         StringPattern sp = new SimpleStringPattern("a*b"); //NOI18N
133

134         test(sp, "ab", true); //NOI18N
135
test(sp, "aab", true); //NOI18N
136
test(sp, "ba", false); //NOI18N
137
test(sp, "abc", false); //NOI18N
138

139         sp = new SimpleStringPattern("*.txt"); //NOI18N
140

141         test(sp, "datei.txt", true); //NOI18N
142
test(sp, ".txt", true); //NOI18N
143
test(sp, "datei.tx", false); //NOI18N
144
test(sp, "datei.txt.txt", true); //NOI18N
145

146         sp = new SimpleStringPattern("datei*1*"); //NOI18N
147

148         test(sp, "datei0.txt", false); //NOI18N
149
test(sp, "datei1.txt", true); //NOI18N
150
test(sp, "datei.tx", false); //NOI18N
151
test(sp, "datei1.txt.txt", true); //NOI18N
152

153         sp = new SimpleStringPattern("Makefile"); //NOI18N
154

155         test(sp, "Makefile", true); //NOI18N
156
test(sp, "Makefile.mak", false); //NOI18N
157
test(sp, "Makefile1", false); //NOI18N
158
test(sp, ".Makefile", false); //NOI18N
159
test(sp, ".Makefile.", false); //NOI18N
160

161         sp = new SimpleStringPattern("*~"); //NOI18N
162

163         test(sp, "datei~", true); //NOI18N
164
test(sp, "datei~1", false); //NOI18N
165
test(sp, "datei~1~", true); //NOI18N
166

167         // Equality:
168
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 JavaDoc testString, boolean shouldResult) {
182         System.err.print('"' + sp.toString() + '"' + ": " + testString + " " + shouldResult); //NOI18N
183

184         boolean doesMatch = sp.doesMatch(testString);
185
186         if (doesMatch == shouldResult) {
187             System.err.println(" proved"); //NOI18N
188
}
189         else {
190             System.err.println(" **denied**"); //NOI18N
191
}
192     }
193
194     private static abstract class SubPattern {
195         protected final String JavaDoc match;
196
197         protected SubPattern(String JavaDoc match) {
198             this.match = match;
199         }
200
201         /**
202          * @parameter string ... the whole string to test for matching
203          * @parameter index ... the index in string where this' test should begin
204          * @returns ... if successful the next test-position, if not -1
205          */

206         public abstract int doesMatch(String JavaDoc string, int index);
207
208         public boolean checkEnding(String JavaDoc string, int index) {
209             return false;
210         }
211         
212         public boolean equals(Object JavaDoc 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 JavaDoc match) {
224             super(match);
225         }
226
227         public int doesMatch(String JavaDoc string, int index) {
228             if (!string.startsWith(match, index)) {
229                 return -1;
230             }
231             return index + match.length();
232         }
233
234         public String JavaDoc toString() {
235             return match;
236         }
237     }
238
239     private static class MatchEachCharPattern extends SubPattern {
240         public MatchEachCharPattern(String JavaDoc match) {
241             super(match);
242         }
243
244         public int doesMatch(String JavaDoc 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 JavaDoc string, int index) {
253             return string.endsWith(match);
254         }
255
256         public String JavaDoc toString() {
257             return MATCH_EACH + match;
258         }
259     }
260
261     private static class MatchOneCharPattern extends MatchExactSubPattern {
262         public MatchOneCharPattern(String JavaDoc match) {
263             super(match);
264         }
265
266         public int doesMatch(String JavaDoc 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 JavaDoc toString() {
275             return MATCH_ONE + match;
276         }
277     }
278 }
279
Popular Tags