001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.modules.cnd.gotodeclaration.util;
029:
030: import java.util.regex.Pattern;
031: import java.util.regex.PatternSyntaxException;
032: import org.netbeans.spi.jumpto.type.SearchType;
033:
034: /**
035: * A factory that provides comparators
036: * depending on SearchType
037: *
038: * @author Vladimir Kvashin
039: */
040: public class NameMatcherFactory {
041:
042: private NameMatcherFactory() {
043: }
044:
045: private static abstract class BaseNameMatcher implements
046: NameMatcher {
047:
048: protected String patternText;
049:
050: protected BaseNameMatcher(String patternText) {
051: this .patternText = patternText;
052: }
053: }
054:
055: private static final class ExactNameMatcher extends BaseNameMatcher
056: implements NameMatcher {
057:
058: public ExactNameMatcher(String patternText) {
059: super (patternText);
060: }
061:
062: public final boolean matches(String name) {
063: return patternText.equals(name);
064: }
065: }
066:
067: private static final class CaseInsensitiveExactNameMatcher extends
068: BaseNameMatcher implements NameMatcher {
069:
070: public CaseInsensitiveExactNameMatcher(String patternText) {
071: super (patternText);
072: }
073:
074: public final boolean matches(String name) {
075: return patternText.equalsIgnoreCase(name);
076: }
077: }
078:
079: private static final class PrefixNameMatcher extends
080: BaseNameMatcher implements NameMatcher {
081:
082: public PrefixNameMatcher(String patternText) {
083: super (patternText);
084: }
085:
086: public final boolean matches(String name) {
087: return name != null && name.startsWith(patternText);
088: }
089: }
090:
091: private static final class CaseInsensitivePrefixNameMatcher extends
092: BaseNameMatcher implements NameMatcher {
093:
094: public CaseInsensitivePrefixNameMatcher(String patternText) {
095: super (patternText.toLowerCase());
096: }
097:
098: public final boolean matches(String name) {
099: return name != null
100: && name.toLowerCase().startsWith(patternText);
101: }
102: }
103:
104: private static final class RegExpNameMatcher implements NameMatcher {
105:
106: Pattern pattern;
107:
108: public RegExpNameMatcher(String patternText,
109: boolean caseSensitive) {
110: pattern = Pattern.compile(patternText, caseSensitive ? 0
111: : Pattern.CASE_INSENSITIVE);
112: }
113:
114: public final boolean matches(String name) {
115: return name != null && pattern.matcher(name).matches();
116: }
117: }
118:
119: private static final class CamelCaseNameMatcher implements
120: NameMatcher {
121:
122: Pattern pattern;
123:
124: public CamelCaseNameMatcher(String name) {
125: if (name.length() == 0) {
126: throw new IllegalArgumentException();
127: }
128: final StringBuilder patternString = new StringBuilder();
129: for (int i = 0; i < name.length(); i++) {
130: char c = name.charAt(i);
131: patternString.append(c);
132: if (i == name.length() - 1) {
133: patternString.append("\\w*"); // NOI18N
134: } else {
135: patternString.append("[\\p{Lower}\\p{Digit}]*"); // NOI18N
136: }
137: }
138: pattern = Pattern.compile(patternString.toString());
139: }
140:
141: public final boolean matches(String name) {
142: return name != null && pattern.matcher(name).matches();
143: }
144: }
145:
146: /**
147: * Get a name
148: */
149: public static NameMatcher createNameMatcher(String text,
150: SearchType type) {
151: try {
152: switch (type) {
153: case EXACT_NAME:
154: return new ExactNameMatcher(text);
155: case CASE_INSENSITIVE_EXACT_NAME:
156: return new CaseInsensitiveExactNameMatcher(text);
157: case PREFIX:
158: return new PrefixNameMatcher(text);
159: case REGEXP:
160: return new RegExpNameMatcher(text, true);
161: case CASE_INSENSITIVE_REGEXP:
162: return new RegExpNameMatcher(text, false);
163: case CASE_INSENSITIVE_PREFIX:
164: return new CaseInsensitivePrefixNameMatcher(text);
165: case CAMEL_CASE:
166: return new CamelCaseNameMatcher(text);
167: default:
168: return null;
169: }
170: } catch (PatternSyntaxException ex) {
171: return null;
172: }
173: }
174: }
|