KickJava   Java API By Example, From Geeks To Geeks.

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


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.indexing;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.resources.IResource;
16 import org.eclipse.core.resources.IResourceProxy;
17 import org.eclipse.core.resources.IResourceProxyVisitor;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.jdt.core.JavaCore;
22 import org.eclipse.jdt.internal.compiler.SourceElementParser;
23 import org.eclipse.jdt.internal.core.index.Index;
24 import org.eclipse.jdt.internal.core.search.processing.JobManager;
25 import org.eclipse.jdt.internal.core.util.Util;
26
27 class AddFolderToIndex extends IndexRequest {
28     IPath folderPath;
29     IProject project;
30     char[][] inclusionPatterns;
31     char[][] exclusionPatterns;
32
33     public AddFolderToIndex(IPath folderPath, IProject project, char[][] inclusionPatterns, char[][] exclusionPatterns, IndexManager manager) {
34         super(project.getFullPath(), manager);
35         this.folderPath = folderPath;
36         this.project = project;
37         this.inclusionPatterns = inclusionPatterns;
38         this.exclusionPatterns = exclusionPatterns;
39     }
40     public boolean execute(IProgressMonitor progressMonitor) {
41
42         if (this.isCancelled || progressMonitor != null && progressMonitor.isCanceled()) return true;
43         if (!project.isAccessible()) return true; // nothing to do
44
IResource folder = this.project.getParent().findMember(this.folderPath);
45         if (folder == null || folder.getType() == IResource.FILE) return true; // nothing to do, source folder was removed
46

47         /* ensure no concurrent write access to index */
48         Index index = this.manager.getIndex(this.containerPath, true, /*reuse index file*/ true /*create if none*/);
49         if (index == null) return true;
50         ReadWriteMonitor monitor = index.monitor;
51         if (monitor == null) return true; // index got deleted since acquired
52

53         try {
54             monitor.enterRead(); // ask permission to read
55

56             final IPath container = this.containerPath;
57             final IndexManager indexManager = this.manager;
58             final SourceElementParser parser = indexManager.getSourceElementParser(JavaCore.create(this.project), null/*requestor will be set by indexer*/);
59             if (this.exclusionPatterns == null && this.inclusionPatterns == null) {
60                 folder.accept(
61                     new IResourceProxyVisitor() {
62                         public boolean visit(IResourceProxy proxy) /* throws CoreException */{
63                             if (proxy.getType() == IResource.FILE) {
64                                 if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName()))
65                                     indexManager.addSource((IFile) proxy.requestResource(), container, parser);
66                                 return false;
67                             }
68                             return true;
69                         }
70                     },
71                     IResource.NONE
72                 );
73             } else {
74                 folder.accept(
75                     new IResourceProxyVisitor() {
76                         public boolean visit(IResourceProxy proxy) /* throws CoreException */{
77                             switch(proxy.getType()) {
78                                 case IResource.FILE :
79                                     if (org.eclipse.jdt.internal.core.util.Util.isJavaLikeFileName(proxy.getName())) {
80                                         IResource resource = proxy.requestResource();
81                                         if (!Util.isExcluded(resource, inclusionPatterns, exclusionPatterns))
82                                             indexManager.addSource((IFile)resource, container, parser);
83                                     }
84                                     return false;
85                                 case IResource.FOLDER :
86                                     if (exclusionPatterns != null && inclusionPatterns == null) {
87                                         // if there are inclusion patterns then we must walk the children
88
if (Util.isExcluded(proxy.requestFullPath(), inclusionPatterns, exclusionPatterns, true))
89                                             return false;
90                                     }
91                             }
92                             return true;
93                         }
94                     },
95                     IResource.NONE
96                 );
97             }
98         } catch (CoreException e) {
99             if (JobManager.VERBOSE) {
100                 Util.verbose("-> failed to add " + this.folderPath + " to index because of the following exception:", System.err); //$NON-NLS-1$ //$NON-NLS-2$
101
e.printStackTrace();
102             }
103             return false;
104         } finally {
105             monitor.exitRead(); // free read lock
106
}
107         return true;
108     }
109     public String JavaDoc toString() {
110         return "adding " + this.folderPath + " to index " + this.containerPath; //$NON-NLS-1$ //$NON-NLS-2$
111
}
112 }
113
Popular Tags