1 11 package org.eclipse.search.internal.core.text; 12 13 import java.util.regex.Pattern ; 14 import java.util.regex.PatternSyntaxException ; 15 16 19 public class PatternConstructor { 20 21 22 private PatternConstructor() { 23 } 25 26 public static Pattern createPattern(String pattern, boolean isCaseSensitive, boolean isRegex) throws PatternSyntaxException { 27 return createPattern(pattern, isRegex, true, isCaseSensitive, false); 28 } 29 30 41 public static Pattern createPattern(String pattern, boolean isRegex, boolean isStringMatcher, boolean isCaseSensitive, boolean isWholeWord) throws PatternSyntaxException { 42 if (isRegex) { 43 if (isWholeWord) { 44 StringBuffer buffer= new StringBuffer (pattern.length() + 10); 45 buffer.append("\\b(?:").append(pattern).append(")\\b"); pattern= buffer.toString(); 47 } 48 } else { 49 int len= pattern.length(); 50 StringBuffer buffer= new StringBuffer (len + 10); 51 if (isWholeWord && len > 0 && isWordChar(pattern.charAt(0))) { 54 buffer.append("\\b"); } 56 appendAsRegEx(isStringMatcher, pattern, buffer); 57 if (isWholeWord && len > 0 && isWordChar(pattern.charAt(len - 1))) { 58 buffer.append("\\b"); } 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 82 public static Pattern createPattern(String [] patterns, boolean isCaseSensitive) throws PatternSyntaxException { 83 StringBuffer pattern= new StringBuffer (); 84 for (int i= 0; i < patterns.length; i++) { 85 if (i > 0) { 86 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 appendAsRegEx(boolean isStringMatcher, String pattern, StringBuffer buffer) { 97 boolean isEscaped= false; 98 for (int i = 0; i < pattern.length(); i++) { 99 char c = pattern.charAt(i); 100 switch(c) { 101 case '\\': 103 if (isStringMatcher && !isEscaped) { 105 isEscaped= true; 106 } 107 else { 108 buffer.append("\\\\"); isEscaped= false; 110 } 111 break; 112 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("\\\\"); 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(".*"); } 145 else { 146 buffer.append('\\'); 147 buffer.append(c); 148 isEscaped= false; 149 } 150 break; 151 default: 152 if (isEscaped) { 153 buffer.append("\\\\"); isEscaped= false; 155 } 156 buffer.append(c); 157 break; 158 } 159 } 160 if (isEscaped) { 161 buffer.append("\\\\"); isEscaped= false; 163 } 164 return buffer; 165 } 166 } 167 | Popular Tags |