KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > util > PatternConstructor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.pde.internal.core.util;
12
13 import java.util.regex.Pattern JavaDoc;
14
15 public class PatternConstructor {
16     private static final Pattern JavaDoc PATTERN_BACK_SLASH = Pattern.compile("\\\\"); //$NON-NLS-1$
17

18     private static final Pattern JavaDoc PATTERN_QUESTION = Pattern.compile("\\?"); //$NON-NLS-1$
19

20     private static final Pattern JavaDoc PATTERN_STAR = Pattern.compile("\\*"); //$NON-NLS-1$
21

22     private static final Pattern JavaDoc PATTERN_LBRACKET = Pattern.compile("\\("); //$NON-NLS-1$
23

24     private static final Pattern JavaDoc PATTERN_RBRACKET = Pattern.compile("\\)"); //$NON-NLS-1$
25
/*
26      * Converts user string to regular expres '*' and '?' to regEx variables.
27      *
28      */

29     private static String JavaDoc asRegEx(String JavaDoc pattern, boolean group) {
30         // Replace \ with \\, * with .* and ? with .
31
// Quote remaining characters
32
String JavaDoc result1 = PATTERN_BACK_SLASH.matcher(pattern).replaceAll(
33                 "\\\\E\\\\\\\\\\\\Q"); //$NON-NLS-1$
34
String JavaDoc result2 = PATTERN_STAR.matcher(result1).replaceAll(
35                 "\\\\E.*\\\\Q"); //$NON-NLS-1$
36
String JavaDoc result3 = PATTERN_QUESTION.matcher(result2).replaceAll(
37                 "\\\\E.\\\\Q"); //$NON-NLS-1$
38
if (group) {
39             result3 = PATTERN_LBRACKET.matcher(result3).replaceAll(
40                     "\\\\E(\\\\Q"); //$NON-NLS-1$
41
result3 = PATTERN_RBRACKET.matcher(result3).replaceAll(
42                     "\\\\E)\\\\Q"); //$NON-NLS-1$
43
}
44         return "\\Q" + result3 + "\\E"; //$NON-NLS-1$ //$NON-NLS-2$
45
}
46
47     /**
48      * Creates a regular expression pattern from the pattern string (which is
49      * our old 'StringMatcher' format).
50      *
51      * @param pattern
52      * The search pattern
53      * @param isCaseSensitive
54      * Set to <code>true</code> to create a case insensitve pattern
55      * @return The created pattern
56      */

57     public static Pattern JavaDoc createPattern(String JavaDoc pattern, boolean isCaseSensitive) {
58         if (isCaseSensitive)
59             return Pattern.compile(asRegEx(pattern, false));
60         return Pattern.compile(asRegEx(pattern, false), Pattern.CASE_INSENSITIVE
61                 | Pattern.UNICODE_CASE);
62     }
63     public static Pattern JavaDoc createGroupedPattern(String JavaDoc pattern, boolean isCaseSensitive) {
64         if (isCaseSensitive)
65             return Pattern.compile(asRegEx(pattern, true));
66         return Pattern.compile(asRegEx(pattern, true), Pattern.CASE_INSENSITIVE
67                 | Pattern.UNICODE_CASE);
68     }
69     
70     private PatternConstructor() {
71     }
72 }
73
Popular Tags