KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > matching > PackageReferencePattern


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.core.search.matching;
12
13 import org.eclipse.jdt.core.compiler.CharOperation;
14 import org.eclipse.jdt.core.search.SearchPattern;
15
16 public class PackageReferencePattern extends AndPattern {
17
18 protected char[] pkgName;
19
20 protected char[][] segments;
21 protected int currentSegment;
22
23 protected static char[][] CATEGORIES = { REF };
24
25 public PackageReferencePattern(char[] pkgName, int matchRule) {
26     this(matchRule);
27
28     if (pkgName == null || pkgName.length == 0) {
29         this.pkgName = null;
30         this.segments = new char[][] {CharOperation.NO_CHAR};
31         ((InternalSearchPattern)this).mustResolve = false;
32     } else {
33         this.pkgName = (isCaseSensitive() || isCamelCase()) ? pkgName : CharOperation.toLowerCase(pkgName);
34         this.segments = CharOperation.splitOn('.', this.pkgName);
35         ((InternalSearchPattern)this).mustResolve = true;
36     }
37 }
38 PackageReferencePattern(int matchRule) {
39     super(PKG_REF_PATTERN, matchRule);
40 }
41 public void decodeIndexKey(char[] key) {
42     // Package reference keys are encoded as 'name' (where 'name' is the last segment of the package name)
43
this.pkgName = key;
44 }
45 public SearchPattern getBlankPattern() {
46     return new PackageReferencePattern(R_EXACT_MATCH | R_CASE_SENSITIVE);
47 }
48 public char[] getIndexKey() {
49     // Package reference keys are encoded as 'name' (where 'name' is the last segment of the package name)
50
if (this.currentSegment >= 0)
51         return this.segments[this.currentSegment];
52     return null;
53 }
54 public char[][] getIndexCategories() {
55     return CATEGORIES;
56 }
57 protected boolean hasNextQuery() {
58     // if package has at least 4 segments, don't look at the first 2 since they are mostly
59
// redundant (eg. in 'org.eclipse.jdt.core.*' 'org.eclipse' is used all the time)
60
return --this.currentSegment >= (this.segments.length >= 4 ? 2 : 0);
61 }
62 public boolean matchesDecodedKey(SearchPattern decodedPattern) {
63     return true; // index key is not encoded so query results all match
64
}
65 protected void resetQuery() {
66     /* walk the segments from end to start as it will find less potential references using 'lang' than 'java' */
67     this.currentSegment = this.segments.length - 1;
68 }
69 protected StringBuffer JavaDoc print(StringBuffer JavaDoc output) {
70     output.append("PackageReferencePattern: <"); //$NON-NLS-1$
71
if (this.pkgName != null)
72         output.append(this.pkgName);
73     else
74         output.append("*"); //$NON-NLS-1$
75
output.append(">"); //$NON-NLS-1$
76
return super.print(output);
77 }
78 }
79
Popular Tags