KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > search > indexing > SourceIndexer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.indexing;
12
13 import org.eclipse.core.resources.IProject;
14 import org.eclipse.core.resources.ResourcesPlugin;
15 import org.eclipse.core.runtime.IPath;
16 import org.eclipse.core.runtime.Path;
17 import org.eclipse.jdt.core.JavaCore;
18 import org.eclipse.jdt.core.search.SearchDocument;
19 import org.eclipse.jdt.internal.compiler.SourceElementParser;
20 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
21 import org.eclipse.jdt.internal.core.JavaModelManager;
22 import org.eclipse.jdt.internal.core.jdom.CompilationUnit;
23 import org.eclipse.jdt.internal.core.search.processing.JobManager;
24
25 /**
26  * A SourceIndexer indexes java files using a java parser. The following items are indexed:
27  * Declarations of:
28  * - Classes<br>
29  * - Interfaces; <br>
30  * - Methods;<br>
31  * - Fields;<br>
32  * References to:
33  * - Methods (with number of arguments); <br>
34  * - Fields;<br>
35  * - Types;<br>
36  * - Constructors.
37  */

38 public class SourceIndexer extends AbstractIndexer implements SuffixConstants {
39     
40     public SourceIndexer(SearchDocument document) {
41         super(document);
42     }
43     public void indexDocument() {
44         // Create a new Parser
45
SourceIndexerRequestor requestor = new SourceIndexerRequestor(this);
46         String JavaDoc documentPath = this.document.getPath();
47         SourceElementParser parser = ((InternalSearchDocument) this.document).parser;
48         if (parser == null) {
49             IPath path = new Path(documentPath);
50             IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(path.segment(0));
51             parser = JavaModelManager.getJavaModelManager().indexManager.getSourceElementParser(JavaCore.create(project), requestor);
52         } else {
53             parser.requestor = requestor;
54         }
55         
56         // Launch the parser
57
char[] source = null;
58         char[] name = null;
59         try {
60             source = document.getCharContents();
61             name = documentPath.toCharArray();
62         } catch(Exception JavaDoc e){
63             // ignore
64
}
65         if (source == null || name == null) return; // could not retrieve document info (e.g. resource was discarded)
66
CompilationUnit compilationUnit = new CompilationUnit(source, name);
67         try {
68             parser.parseCompilationUnit(compilationUnit, true/*full parse*/);
69         } catch (Exception JavaDoc e) {
70             if (JobManager.VERBOSE) {
71                 e.printStackTrace();
72             }
73         }
74     }
75 }
76
Popular Tags