KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
14
15 import org.eclipse.jdt.core.compiler.CharOperation;
16 import org.eclipse.jdt.core.search.SearchPattern;
17 import org.eclipse.jdt.internal.core.index.*;
18
19 public class MultiTypeDeclarationPattern extends JavaSearchPattern {
20
21 public char[][] simpleNames;
22 public char[][] qualifications;
23
24 // set to CLASS_SUFFIX for only matching classes
25
// set to INTERFACE_SUFFIX for only matching interfaces
26
// set to ENUM_SUFFIX for only matching enums
27
// set to ANNOTATION_TYPE_SUFFIX for only matching annotation types
28
// set to TYPE_SUFFIX for matching both classes and interfaces
29
public char typeSuffix;
30
31 protected static char[][] CATEGORIES = { TYPE_DECL };
32
33 public MultiTypeDeclarationPattern(
34     char[][] qualifications,
35     char[][] simpleNames,
36     char typeSuffix,
37     int matchRule) {
38
39     this(matchRule);
40
41     if (isCaseSensitive() || qualifications == null) {
42         this.qualifications = qualifications;
43     } else {
44         int length = qualifications.length;
45         this.qualifications = new char[length][];
46         for (int i = 0; i < length; i++)
47             this.qualifications[i] = CharOperation.toLowerCase(qualifications[i]);
48     }
49     // null simple names are allowed (should return all names)
50
if (simpleNames != null) {
51         if ((isCaseSensitive() || isCamelCase()) ) {
52             this.simpleNames = simpleNames;
53         } else {
54             int length = simpleNames.length;
55             this.simpleNames = new char[length][];
56             for (int i = 0; i < length; i++)
57                 this.simpleNames[i] = CharOperation.toLowerCase(simpleNames[i]);
58         }
59     }
60     this.typeSuffix = typeSuffix;
61
62     ((InternalSearchPattern)this).mustResolve = typeSuffix != TYPE_SUFFIX; // only used to report type declarations, not their positions
63
}
64 MultiTypeDeclarationPattern(int matchRule) {
65     super(TYPE_DECL_PATTERN, matchRule);
66 }
67 public SearchPattern getBlankPattern() {
68     return new QualifiedTypeDeclarationPattern(R_EXACT_MATCH | R_CASE_SENSITIVE);
69 }
70 public char[][] getIndexCategories() {
71     return CATEGORIES;
72 }
73 public boolean matchesDecodedKey(SearchPattern decodedPattern) {
74     QualifiedTypeDeclarationPattern pattern = (QualifiedTypeDeclarationPattern) decodedPattern;
75     
76     // check type suffix
77
if (this.typeSuffix != pattern.typeSuffix && typeSuffix != TYPE_SUFFIX) {
78         if (!matchDifferentTypeSuffixes(this.typeSuffix, pattern.typeSuffix)) {
79             return false;
80         }
81     }
82
83     // check qualified name
84
if (this.qualifications != null) {
85         int count = 0;
86         int max = this.qualifications.length;
87         if (max == 0 && pattern.qualification.length > 0) {
88             return false;
89         }
90         if (max > 0) {
91             for (; count < max; count++)
92                 if (matchesName(this.qualifications[count], pattern.qualification))
93                     break;
94             if (count == max) return false;
95         }
96     }
97
98     // check simple name (null are allowed)
99
if (this.simpleNames == null) return true;
100     int count = 0;
101     int max = this.simpleNames.length;
102     for (; count < max; count++)
103         if (matchesName(this.simpleNames[count], pattern.simpleName))
104             break;
105     return count < max;
106 }
107 EntryResult[] queryIn(Index index) throws IOException JavaDoc {
108     if (this.simpleNames == null) {
109         // if no simple names then return all possible ones from index
110
return index.query(getIndexCategories(), null, -1); // match rule is irrelevant when the key is null
111
}
112
113     int count = -1;
114     int numOfNames = this.simpleNames.length;
115     EntryResult[][] allResults = numOfNames > 1 ? new EntryResult[numOfNames][] : null;
116     for (int i = 0; i < numOfNames; i++) {
117         char[] key = this.simpleNames[i];
118         int matchRule = getMatchRule();
119
120         switch(getMatchMode()) {
121             case R_PREFIX_MATCH :
122                 // do a prefix query with the simpleName
123
break;
124             case R_EXACT_MATCH :
125                 if (!this.isCamelCase) {
126                     // do a prefix query with the simpleName
127
matchRule &= ~R_EXACT_MATCH;
128                     matchRule |= R_PREFIX_MATCH;
129                     key = CharOperation.append(key, SEPARATOR);
130                 }
131                 break;
132             case R_PATTERN_MATCH :
133                 if (key[key.length - 1] != '*')
134                     key = CharOperation.concat(key, ONE_STAR, SEPARATOR);
135                 break;
136             case R_REGEXP_MATCH :
137                 // TODO (frederic) implement regular expression match
138
break;
139         }
140
141         EntryResult[] entries = index.query(getIndexCategories(), key, matchRule); // match rule is irrelevant when the key is null
142
if (entries != null) {
143             if (allResults == null) return entries;
144             allResults[++count] = entries;
145         }
146     }
147
148     if (count == -1) return null;
149     int total = 0;
150     for (int i = 0; i <= count; i++)
151         total += allResults[i].length;
152     EntryResult[] allEntries = new EntryResult[total];
153     int next = 0;
154     for (int i = 0; i <= count; i++) {
155         EntryResult[] entries = allResults[i];
156         System.arraycopy(entries, 0, allEntries, next, entries.length);
157         next += entries.length;
158     }
159     return allEntries;
160 }
161 protected StringBuffer JavaDoc print(StringBuffer JavaDoc output) {
162     switch (this.typeSuffix){
163         case CLASS_SUFFIX :
164             output.append("MultiClassDeclarationPattern: "); //$NON-NLS-1$
165
break;
166         case CLASS_AND_INTERFACE_SUFFIX :
167             output.append("MultiClassAndInterfaceDeclarationPattern: "); //$NON-NLS-1$
168
break;
169         case CLASS_AND_ENUM_SUFFIX :
170             output.append("MultiClassAndEnumDeclarationPattern: "); //$NON-NLS-1$
171
break;
172         case INTERFACE_SUFFIX :
173             output.append("MultiInterfaceDeclarationPattern: "); //$NON-NLS-1$
174
break;
175         case INTERFACE_AND_ANNOTATION_SUFFIX :
176             output.append("MultiInterfaceAndAnnotationDeclarationPattern: "); //$NON-NLS-1$
177
break;
178         case ENUM_SUFFIX :
179             output.append("MultiEnumDeclarationPattern: "); //$NON-NLS-1$
180
break;
181         case ANNOTATION_TYPE_SUFFIX :
182             output.append("MultiAnnotationTypeDeclarationPattern: "); //$NON-NLS-1$
183
break;
184         default :
185             output.append("MultiTypeDeclarationPattern: "); //$NON-NLS-1$
186
break;
187     }
188     if (qualifications != null) {
189         output.append("qualifications: <"); //$NON-NLS-1$
190
for (int i = 0; i < qualifications.length; i++){
191             output.append(qualifications[i]);
192             if (i < qualifications.length - 1)
193                 output.append(", "); //$NON-NLS-1$
194
}
195         output.append("> "); //$NON-NLS-1$
196
}
197     if (simpleNames != null) {
198         output.append("simpleNames: <"); //$NON-NLS-1$
199
for (int i = 0; i < simpleNames.length; i++){
200             output.append(simpleNames[i]);
201             if (i < simpleNames.length - 1)
202                 output.append(", "); //$NON-NLS-1$
203
}
204         output.append(">"); //$NON-NLS-1$
205
}
206     return super.print(output);
207 }
208 }
209
Popular Tags