KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > util > 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.jdt.internal.ui.util;
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     /**
27      * Creates a pattern element from the pattern string which is either a reg-ex expression or in our old
28      * 'StringMatcher' format.
29      * @param pattern The search pattern
30      * @param isCaseSensitive Set to <code>true</code> to create a case insensitive pattern
31      * @param isRegexSearch <code>true</code> if the passed string already is a reg-ex pattern
32      * @return The created pattern
33      * @throws PatternSyntaxException
34      */

35     public static Pattern JavaDoc createPattern(String JavaDoc pattern, boolean isCaseSensitive, boolean isRegexSearch) throws PatternSyntaxException JavaDoc {
36         if (!isRegexSearch) {
37             pattern= asRegEx(pattern, new StringBuffer JavaDoc()).toString();
38         }
39         
40         if (!isCaseSensitive)
41             return Pattern.compile(pattern, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.MULTILINE);
42         
43         return Pattern.compile(pattern, Pattern.MULTILINE);
44     }
45     
46     /**
47      * Creates a pattern element from the pattern string which is either a reg-ex expression or in our old
48      * 'StringMatcher' format.
49      * @param patterns The search patterns
50      * @param isCaseSensitive Set to <code>true</code> to create a case insensitive pattern
51      * @param isRegexSearch <code>true</code> if the passed string already is a reg-ex pattern
52      * @return The created pattern
53      * @throws PatternSyntaxException
54      */

55     public static Pattern JavaDoc createPattern(String JavaDoc[] patterns, boolean isCaseSensitive, boolean isRegexSearch) throws PatternSyntaxException JavaDoc {
56         StringBuffer JavaDoc pattern= new StringBuffer JavaDoc();
57         for (int i= 0; i < patterns.length; i++) {
58             if (i > 0) {
59                 pattern.append('|');
60             }
61             if (isRegexSearch) {
62                 pattern.append(patterns[i]);
63             } else {
64                 asRegEx(patterns[i], pattern);
65             }
66         }
67         return createPattern(pattern.toString(), isCaseSensitive, true);
68     }
69     
70     
71     /**
72      * Translates a StringMatcher pattern (using '*' and '?') to a regex pattern string
73      * @param stringMatcherPattern a pattern using '*' and '?'
74      */

75     private static StringBuffer JavaDoc asRegEx(String JavaDoc stringMatcherPattern, StringBuffer JavaDoc out) {
76         boolean escaped= false;
77         boolean quoting= false;
78         
79         int i= 0;
80         while (i < stringMatcherPattern.length()) {
81             char ch= stringMatcherPattern.charAt(i++);
82             
83             if (ch == '*' && !escaped) {
84                 if (quoting) {
85                     out.append("\\E"); //$NON-NLS-1$
86
quoting= false;
87                 }
88                 out.append(".*"); //$NON-NLS-1$
89
escaped= false;
90                 continue;
91             } else if (ch == '?' && !escaped) {
92                 if (quoting) {
93                     out.append("\\E"); //$NON-NLS-1$
94
quoting= false;
95                 }
96                 out.append("."); //$NON-NLS-1$
97
escaped= false;
98                 continue;
99             } else if (ch == '\\' && !escaped) {
100                 escaped= true;
101                 continue;
102                 
103             } else if (ch == '\\' && escaped) {
104                 escaped= false;
105                 if (quoting) {
106                     out.append("\\E"); //$NON-NLS-1$
107
quoting= false;
108                 }
109                 out.append("\\\\"); //$NON-NLS-1$
110
continue;
111             }
112             
113             if (!quoting) {
114                 out.append("\\Q"); //$NON-NLS-1$
115
quoting= true;
116             }
117             if (escaped && ch != '*' && ch != '?' && ch != '\\')
118                 out.append('\\');
119             out.append(ch);
120             escaped= ch == '\\';
121             
122         }
123         if (quoting)
124             out.append("\\E"); //$NON-NLS-1$
125

126         return out;
127     }
128
129 }
130
Popular Tags