KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > search > internal > core > text > PatternConstructor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.search.internal.core.text;
12
13 import java.util.regex.Pattern JavaDoc;
14 import java.util.regex.PatternSyntaxException JavaDoc;
15
16 /**
17  *
18  */

19 public class PatternConstructor {
20     
21
22     private PatternConstructor() {
23         // don't instantiate
24
}
25     
26     public static Pattern JavaDoc createPattern(String JavaDoc pattern, boolean isCaseSensitive, boolean isRegex) throws PatternSyntaxException JavaDoc {
27         return createPattern(pattern, isRegex, true, isCaseSensitive, false);
28     }
29
30     /**
31      * Creates a pattern element from the pattern string which is either a reg-ex expression or in our old
32      * 'StringMatcher' format.
33      * @param pattern The search pattern
34      * @param isRegex <code>true</code> if the passed string already is a reg-ex pattern
35      * @param isStringMatcher <code>true</code> if the passed string is in the StringMatcher format.
36      * @param isCaseSensitive Set to <code>true</code> to create a case insensitive pattern
37      * @param isWholeWord <code>true</code> to create a pattern that requires a word boundary at the beginning and the end.
38      * @return The created pattern
39      * @throws PatternSyntaxException
40      */

41     public static Pattern JavaDoc createPattern(String JavaDoc pattern, boolean isRegex, boolean isStringMatcher, boolean isCaseSensitive, boolean isWholeWord) throws PatternSyntaxException JavaDoc {
42         if (isRegex) {
43             if (isWholeWord) {
44                 StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(pattern.length() + 10);
45                 buffer.append("\\b(?:").append(pattern).append(")\\b"); //$NON-NLS-1$ //$NON-NLS-2$
46
pattern= buffer.toString();
47             }
48         } else {
49             int len= pattern.length();
50             StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(len + 10);
51             // don't add a word boundary if the search text does not start with
52
// a word char. (this works around a user input error).
53
if (isWholeWord && len > 0 && isWordChar(pattern.charAt(0))) {
54                 buffer.append("\\b"); //$NON-NLS-1$
55
}
56             appendAsRegEx(isStringMatcher, pattern, buffer);
57             if (isWholeWord && len > 0 && isWordChar(pattern.charAt(len - 1))) {
58                 buffer.append("\\b"); //$NON-NLS-1$
59
}
60             pattern= buffer.toString();
61         }
62
63         int regexOptions= Pattern.MULTILINE;
64         if (!isCaseSensitive) {
65             regexOptions|= Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE;
66         }
67         return Pattern.compile(pattern, regexOptions);
68     }
69     
70     private static boolean isWordChar(char c) {
71         return Character.isLetterOrDigit(c);
72     }
73
74     /**
75      * Creates a pattern element from an array of patterns in the old
76      * 'StringMatcher' format.
77      * @param patterns The search patterns
78      * @param isCaseSensitive Set to <code>true</code> to create a case insensitive pattern
79      * @return The created pattern
80      * @throws PatternSyntaxException
81      */

82     public static Pattern JavaDoc createPattern(String JavaDoc[] patterns, boolean isCaseSensitive) throws PatternSyntaxException JavaDoc {
83         StringBuffer JavaDoc pattern= new StringBuffer JavaDoc();
84         for (int i= 0; i < patterns.length; i++) {
85             if (i > 0) {
86                 // note that this works only as we know that the operands of the
87
// or expression will be simple and need no brackets.
88
pattern.append('|');
89             }
90             appendAsRegEx(true, patterns[i], pattern);
91         }
92         return createPattern(pattern.toString(), true, true, isCaseSensitive, false);
93     }
94     
95     
96     public static StringBuffer JavaDoc appendAsRegEx(boolean isStringMatcher, String JavaDoc pattern, StringBuffer JavaDoc buffer) {
97         boolean isEscaped= false;
98         for (int i = 0; i < pattern.length(); i++) {
99             char c = pattern.charAt(i);
100             switch(c) {
101             // the backslash
102
case '\\':
103                 // the backslash is escape char in string matcher
104
if (isStringMatcher && !isEscaped) {
105                     isEscaped= true;
106                 }
107                 else {
108                     buffer.append("\\\\"); //$NON-NLS-1$
109
isEscaped= false;
110                 }
111                 break;
112             // characters that need to be escaped in the regex.
113
case '(':
114             case ')':
115             case '{':
116             case '}':
117             case '.':
118             case '[':
119             case ']':
120             case '$':
121             case '^':
122             case '+':
123             case '|':
124                 if (isEscaped) {
125                     buffer.append("\\\\"); //$NON-NLS-1$
126
isEscaped= false;
127                 }
128                 buffer.append('\\');
129                 buffer.append(c);
130                 break;
131             case '?':
132                 if (isStringMatcher && !isEscaped) {
133                     buffer.append('.');
134                 }
135                 else {
136                     buffer.append('\\');
137                     buffer.append(c);
138                     isEscaped= false;
139                 }
140                 break;
141             case '*':
142                 if (isStringMatcher && !isEscaped) {
143                     buffer.append(".*"); //$NON-NLS-1$
144
}
145                 else {
146                     buffer.append('\\');
147                     buffer.append(c);
148                     isEscaped= false;
149                 }
150                 break;
151             default:
152                 if (isEscaped) {
153                     buffer.append("\\\\"); //$NON-NLS-1$
154
isEscaped= false;
155                 }
156                 buffer.append(c);
157                 break;
158             }
159         }
160         if (isEscaped) {
161             buffer.append("\\\\"); //$NON-NLS-1$
162
isEscaped= false;
163         }
164         return buffer;
165     }
166 }
167
Popular Tags