Source Code Cross Referenced for StringMatcher.java in  » IDE-Eclipse » ui-ide » org » eclipse » ui » internal » ide » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » IDE Eclipse » ui ide » org.eclipse.ui.internal.ide 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.ide;
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 &#39*&#39 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 &quot;*abc*&quot; with leading and trailing stars, position of &quot;abc&quot;
104:             * is returned. For a pattern like&quot;*&#63;&#63;*&quot; in text &quot;abcdf&quot;, (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 eitherwise false
160:             * @param text  a String object 
161:             */
162:            public boolean match(String text) {
163:                return match(text, 0, text.length());
164:            }
165:
166:            /**
167:             * Given the starting (inclusive) and the ending (exclusive) positions in the   
168:             * <code>text</code>, determine if the given substring matches with aPattern  
169:             * @return true if the specified portion of the text matches the pattern
170:             * @param text  a <code>String</code> object that contains the substring to match 
171:             * @param start  marks the starting position (inclusive) of the substring
172:             * @param end  marks the ending index (exclusive) of the substring 
173:             */
174:            public boolean match(String text, int start, int end) {
175:                if (null == text) {
176:                    throw new IllegalArgumentException();
177:                }
178:
179:                if (start > end) {
180:                    return false;
181:                }
182:
183:                if (fIgnoreWildCards) {
184:                    return (end - start == fLength)
185:                            && fPattern.regionMatches(fIgnoreCase, 0, text,
186:                                    start, fLength);
187:                }
188:                int segCount = fSegments.length;
189:                if (segCount == 0 && (fHasLeadingStar || fHasTrailingStar)) {
190:                    return true;
191:                }
192:                if (start == end) {
193:                    return fLength == 0;
194:                }
195:                if (fLength == 0) {
196:                    return start == end;
197:                }
198:
199:                int tlen = text.length();
200:                if (start < 0) {
201:                    start = 0;
202:                }
203:                if (end > tlen) {
204:                    end = tlen;
205:                }
206:
207:                int tCurPos = start;
208:                int bound = end - fBound;
209:                if (bound < 0) {
210:                    return false;
211:                }
212:                int i = 0;
213:                String current = fSegments[i];
214:                int segLength = current.length();
215:
216:                /* process first segment */
217:                if (!fHasLeadingStar) {
218:                    if (!regExpRegionMatches(text, start, current, 0, segLength)) {
219:                        return false;
220:                    } else {
221:                        ++i;
222:                        tCurPos = tCurPos + segLength;
223:                    }
224:                }
225:                if ((fSegments.length == 1) && (!fHasLeadingStar)
226:                        && (!fHasTrailingStar)) {
227:                    // only one segment to match, no wildcards specified
228:                    return tCurPos == end;
229:                }
230:                /* process middle segments */
231:                while (i < segCount) {
232:                    current = fSegments[i];
233:                    int currentMatch;
234:                    int k = current.indexOf(fSingleWildCard);
235:                    if (k < 0) {
236:                        currentMatch = textPosIn(text, tCurPos, end, current);
237:                        if (currentMatch < 0) {
238:                            return false;
239:                        }
240:                    } else {
241:                        currentMatch = regExpPosIn(text, tCurPos, end, current);
242:                        if (currentMatch < 0) {
243:                            return false;
244:                        }
245:                    }
246:                    tCurPos = currentMatch + current.length();
247:                    i++;
248:                }
249:
250:                /* process final segment */
251:                if (!fHasTrailingStar && tCurPos != end) {
252:                    int clen = current.length();
253:                    return regExpRegionMatches(text, end - clen, current, 0,
254:                            clen);
255:                }
256:                return i == segCount;
257:            }
258:
259:            /**
260:             * This method parses the given pattern into segments seperated by wildcard '*' characters.
261:             * Since wildcards are not being used in this case, the pattern consists of a single segment.
262:             */
263:            private void parseNoWildCards() {
264:                fSegments = new String[1];
265:                fSegments[0] = fPattern;
266:                fBound = fLength;
267:            }
268:
269:            /**
270:             * Parses the given pattern into segments seperated by wildcard &#39;*&#39; characters.
271:             * @param p, a String object that is a simple regular expression with ?*? and/or &#39;&#63;&#39;
272:             */
273:            private void parseWildCards() {
274:                if (fPattern.startsWith("*")) { //$NON-NLS-1$
275:                    fHasLeadingStar = true;
276:                }
277:                if (fPattern.endsWith("*")) {//$NON-NLS-1$
278:                    /* make sure it's not an escaped wildcard */
279:                    if (fLength > 1 && fPattern.charAt(fLength - 2) != '\\') {
280:                        fHasTrailingStar = true;
281:                    }
282:                }
283:
284:                Vector temp = new Vector();
285:
286:                int pos = 0;
287:                StringBuffer buf = new StringBuffer();
288:                while (pos < fLength) {
289:                    char c = fPattern.charAt(pos++);
290:                    switch (c) {
291:                    case '\\':
292:                        if (pos >= fLength) {
293:                            buf.append(c);
294:                        } else {
295:                            char next = fPattern.charAt(pos++);
296:                            /* if it's an escape sequence */
297:                            if (next == '*' || next == '?' || next == '\\') {
298:                                buf.append(next);
299:                            } else {
300:                                /* not an escape sequence, just insert literally */
301:                                buf.append(c);
302:                                buf.append(next);
303:                            }
304:                        }
305:                        break;
306:                    case '*':
307:                        if (buf.length() > 0) {
308:                            /* new segment */
309:                            temp.addElement(buf.toString());
310:                            fBound += buf.length();
311:                            buf.setLength(0);
312:                        }
313:                        break;
314:                    case '?':
315:                        /* append special character representing single match wildcard */
316:                        buf.append(fSingleWildCard);
317:                        break;
318:                    default:
319:                        buf.append(c);
320:                    }
321:                }
322:
323:                /* add last buffer to segment list */
324:                if (buf.length() > 0) {
325:                    temp.addElement(buf.toString());
326:                    fBound += buf.length();
327:                }
328:
329:                fSegments = new String[temp.size()];
330:                temp.copyInto(fSegments);
331:            }
332:
333:            /** 
334:             * @param text  a string which contains no wildcard
335:             * @param start  the starting index in the text for search, inclusive
336:             * @param end  the stopping point of search, exclusive
337:             * @return the starting index in the text of the pattern , or -1 if not found 
338:             */
339:            protected int posIn(String text, int start, int end) {//no wild card in pattern
340:                int max = end - fLength;
341:
342:                if (!fIgnoreCase) {
343:                    int i = text.indexOf(fPattern, start);
344:                    if (i == -1 || i > max) {
345:                        return -1;
346:                    }
347:                    return i;
348:                }
349:
350:                for (int i = start; i <= max; ++i) {
351:                    if (text.regionMatches(true, i, fPattern, 0, fLength)) {
352:                        return i;
353:                    }
354:                }
355:
356:                return -1;
357:            }
358:
359:            /** 
360:             * @param text  a simple regular expression that may only contain '&#63;'(s)
361:             * @param start  the starting index in the text for search, inclusive
362:             * @param end  the stopping point of search, exclusive
363:             * @param p  a simple regular expression that may contains '&#63;'
364:             * @return the starting index in the text of the pattern , or -1 if not found 
365:             */
366:            protected int regExpPosIn(String text, int start, int end, String p) {
367:                int plen = p.length();
368:
369:                int max = end - plen;
370:                for (int i = start; i <= max; ++i) {
371:                    if (regExpRegionMatches(text, i, p, 0, plen)) {
372:                        return i;
373:                    }
374:                }
375:                return -1;
376:            }
377:
378:            /**
379:             * 
380:             * @return boolean
381:             * @param  text  a String to match
382:             * @param tStart  int that indicates the starting index of match, inclusive
383:             * @param p  String,  String, a simple regular expression that may contain '&#63;'
384:             * @param pStart
385:             * @param plen 	
386:             */
387:            protected boolean regExpRegionMatches(String text, int tStart,
388:                    String p, int pStart, int plen) {
389:                while (plen-- > 0) {
390:                    char tchar = text.charAt(tStart++);
391:                    char pchar = p.charAt(pStart++);
392:
393:                    /* process wild cards */
394:                    if (!fIgnoreWildCards) {
395:                        /* skip single wild cards */
396:                        if (pchar == fSingleWildCard) {
397:                            continue;
398:                        }
399:                    }
400:                    if (pchar == tchar) {
401:                        continue;
402:                    }
403:                    if (fIgnoreCase) {
404:                        if (Character.toUpperCase(tchar) == Character
405:                                .toUpperCase(pchar)) {
406:                            continue;
407:                        }
408:                        // comparing after converting to upper case doesn't handle all cases;
409:                        // also compare after converting to lower case
410:                        if (Character.toLowerCase(tchar) == Character
411:                                .toLowerCase(pchar)) {
412:                            continue;
413:                        }
414:                    }
415:                    return false;
416:                }
417:                return true;
418:            }
419:
420:            /** 
421:             * @param text  the string to match
422:             * @param start  the starting index in the text for search, inclusive
423:             * @param end  the stopping point of search, exclusive
424:             * @param p  a string that has no wildcard
425:             * @return the starting index in the text of the pattern , or -1 if not found 
426:             */
427:            protected int textPosIn(String text, int start, int end, String p) {
428:
429:                int plen = p.length();
430:                int max = end - plen;
431:
432:                if (!fIgnoreCase) {
433:                    int i = text.indexOf(p, start);
434:                    if (i == -1 || i > max) {
435:                        return -1;
436:                    }
437:                    return i;
438:                }
439:
440:                for (int i = start; i <= max; ++i) {
441:                    if (text.regionMatches(true, i, p, 0, plen)) {
442:                        return i;
443:                    }
444:                }
445:
446:                return -1;
447:            }
448:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.