01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.core.search.matching;
11:
12: import org.eclipse.jdt.core.compiler.CharOperation;
13: import org.eclipse.jdt.core.search.SearchPattern;
14:
15: public class PackageReferencePattern extends IntersectingPattern {
16:
17: protected char[] pkgName;
18:
19: protected char[][] segments;
20: protected int currentSegment;
21:
22: protected static char[][] CATEGORIES = { REF };
23:
24: public PackageReferencePattern(char[] pkgName, int matchRule) {
25: this (matchRule);
26:
27: if (pkgName == null || pkgName.length == 0) {
28: this .pkgName = null;
29: this .segments = new char[][] { CharOperation.NO_CHAR };
30: ((InternalSearchPattern) this ).mustResolve = false;
31: } else {
32: this .pkgName = (this .isCaseSensitive || this .isCamelCase) ? pkgName
33: : CharOperation.toLowerCase(pkgName);
34: this .segments = CharOperation.splitOn('.', this .pkgName);
35: ((InternalSearchPattern) this ).mustResolve = true;
36: }
37: }
38:
39: PackageReferencePattern(int matchRule) {
40: super (PKG_REF_PATTERN, matchRule);
41: }
42:
43: public void decodeIndexKey(char[] key) {
44: // Package reference keys are encoded as 'name' (where 'name' is the last segment of the package name)
45: this .pkgName = key;
46: }
47:
48: public SearchPattern getBlankPattern() {
49: return new PackageReferencePattern(R_EXACT_MATCH
50: | R_CASE_SENSITIVE);
51: }
52:
53: public char[] getIndexKey() {
54: // Package reference keys are encoded as 'name' (where 'name' is the last segment of the package name)
55: if (this .currentSegment >= 0)
56: return this .segments[this .currentSegment];
57: return null;
58: }
59:
60: public char[][] getIndexCategories() {
61: return CATEGORIES;
62: }
63:
64: protected boolean hasNextQuery() {
65: // if package has at least 4 segments, don't look at the first 2 since they are mostly
66: // redundant (eg. in 'org.eclipse.jdt.core.*' 'org.eclipse' is used all the time)
67: return --this .currentSegment >= (this .segments.length >= 4 ? 2
68: : 0);
69: }
70:
71: public boolean matchesDecodedKey(SearchPattern decodedPattern) {
72: return true; // index key is not encoded so query results all match
73: }
74:
75: protected void resetQuery() {
76: /* walk the segments from end to start as it will find less potential references using 'lang' than 'java' */
77: this .currentSegment = this .segments.length - 1;
78: }
79:
80: protected StringBuffer print(StringBuffer output) {
81: output.append("PackageReferencePattern: <"); //$NON-NLS-1$
82: if (this .pkgName != null)
83: output.append(this .pkgName);
84: else
85: output.append("*"); //$NON-NLS-1$
86: output.append(">"); //$NON-NLS-1$
87: return super.print(output);
88: }
89: }
|