c4ff0ad4f3cffb408080508bec99fb57fbed3320
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / JavaProjectElementInfo.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.IClasspathEntry;
14 import net.sourceforge.phpdt.core.JavaModelException;
15
16 import org.eclipse.core.resources.IContainer;
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20
21 /** 
22  * Info for IJavaProject.
23  * <p>
24  * Note: <code>getChildren()</code> returns all of the <code>IPackageFragmentRoots</code>
25  * specified on the classpath for the project.  This can include roots external to the
26  * project. See <code>JavaProject#getAllPackageFragmentRoots()</code> and 
27  * <code>JavaProject#getPackageFragmentRoots()</code>.  To get only the <code>IPackageFragmentRoots</code>
28  * that are internal to the project, use <code>JavaProject#getChildren()</code>.
29  */
30
31 /* package */
32 class JavaProjectElementInfo extends OpenableElementInfo {
33
34         /**
35          * The name lookup facility to use with this project.
36          */
37         protected NameLookup fNameLookup = null;
38
39         /**
40          * The searchable builder environment facility used
41          * with this project (doubles as the builder environment). 
42          */
43         protected SearchableEnvironment fSearchableEnvironment = null;
44
45         /**
46          * A array with all the non-java resources contained by this PackageFragment
47          */
48         private Object[] fNonJavaResources;
49
50         /**
51          * Create and initialize a new instance of the receiver
52          */
53         public JavaProjectElementInfo() {
54                 fNonJavaResources = null;
55         }
56         
57         /**
58          * Compute the non-java resources contained in this java project.
59          */
60         private Object[] computeNonJavaResources(JavaProject project) {
61                 
62                 // determine if src == project and/or if bin == project
63                 IPath projectPath = project.getProject().getFullPath();
64                 boolean srcIsProject = false;
65                 boolean binIsProject = false;
66                 char[][] exclusionPatterns = null;
67                 IClasspathEntry[] classpath = null;
68                 IPath projectOutput = null;
69                 try {
70                         classpath = project.getResolvedClasspath(true/*ignore unresolved variable*/);
71                         for (int i = 0; i < classpath.length; i++) {
72                                 IClasspathEntry entry = classpath[i];
73                                 if (projectPath.equals(entry.getPath())) {
74                                         srcIsProject = true;
75                                         exclusionPatterns = ((ClasspathEntry)entry).fullExclusionPatternChars();
76                                         break;
77                                 }
78                         }
79                         projectOutput = project.getOutputLocation();
80                         binIsProject = projectPath.equals(projectOutput);
81                 } catch (JavaModelException e) {
82                         // ignore
83                 }
84
85                 Object[] nonJavaResources = new IResource[5];
86                 int nonJavaResourcesCounter = 0;
87                 try {
88                         IResource[] members = ((IContainer) project.getResource()).members();
89                         for (int i = 0, max = members.length; i < max; i++) {
90                                 IResource res = members[i];
91                                 switch (res.getType()) {
92                                         case IResource.FILE :
93                                                 IPath resFullPath = res.getFullPath();
94                                                 String resName = res.getName();
95                                                 
96                                                 // ignore a jar file on the classpath
97 //                                              if (Util.isArchiveFileName(resName) && this.isClasspathEntryOrOutputLocation(resFullPath, classpath, projectOutput)) {
98 //                                                      break;
99 //                                              }
100                                                 // ignore .java file if src == project
101                                                 if (srcIsProject 
102 //                                                      && Util.isValidCompilationUnitName(resName)
103                                                         && !Util.isExcluded(res, exclusionPatterns)) {
104                                                         break;
105                                                 }
106                                                 // ignore .class file if bin == project
107 //                                              if (binIsProject && Util.isValidClassFileName(resName)) {
108 //                                                      break;
109 //                                              }
110                                                 // else add non java resource
111                                                 if (nonJavaResources.length == nonJavaResourcesCounter) {
112                                                         // resize
113                                                         System.arraycopy(
114                                                                 nonJavaResources,
115                                                                 0,
116                                                                 (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]),
117                                                                 0,
118                                                                 nonJavaResourcesCounter);
119                                                 }
120                                                 nonJavaResources[nonJavaResourcesCounter++] = res;
121                                                 break;
122                                         case IResource.FOLDER :
123                                                 resFullPath = res.getFullPath();
124                                                 
125                                                 // ignore non-excluded folders on the classpath or that correspond to an output location
126                                                 if ((srcIsProject && !Util.isExcluded(res, exclusionPatterns) && Util.isValidFolderNameForPackage(res.getName()))
127                                                                 || this.isClasspathEntryOrOutputLocation(resFullPath, classpath, projectOutput)) {
128                                                         break;
129                                                 }
130                                                 // else add non java resource
131                                                 if (nonJavaResources.length == nonJavaResourcesCounter) {
132                                                         // resize
133                                                         System.arraycopy(
134                                                                 nonJavaResources,
135                                                                 0,
136                                                                 (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]),
137                                                                 0,
138                                                                 nonJavaResourcesCounter);
139                                                 }
140                                                 nonJavaResources[nonJavaResourcesCounter++] = res;
141                                 }
142                         }
143                         if (nonJavaResources.length != nonJavaResourcesCounter) {
144                                 System.arraycopy(
145                                         nonJavaResources,
146                                         0,
147                                         (nonJavaResources = new IResource[nonJavaResourcesCounter]),
148                                         0,
149                                         nonJavaResourcesCounter);
150                         }
151                 } catch (CoreException e) {
152                         nonJavaResources = NO_NON_JAVA_RESOURCES;
153                         nonJavaResourcesCounter = 0;
154                 }
155                 return nonJavaResources;
156         }
157         
158         /**
159          * @see IJavaProject
160          */
161         protected NameLookup getNameLookup() {
162
163                 return fNameLookup;
164         }
165         
166         /**
167          * Returns an array of non-java resources contained in the receiver.
168          */
169         Object[] getNonJavaResources(JavaProject project) {
170
171                 Object[] nonJavaResources = fNonJavaResources;
172                 if (nonJavaResources == null) {
173                         nonJavaResources = computeNonJavaResources(project);
174                         fNonJavaResources = nonJavaResources;
175                 }
176                 return nonJavaResources;
177         }
178         
179         /**
180          * @see IJavaProject 
181          */
182         protected SearchableEnvironment getSearchableEnvironment() {
183
184                 return fSearchableEnvironment;
185         }
186         /*
187          * Returns whether the given path is a classpath entry or an output location.
188          */
189         private boolean isClasspathEntryOrOutputLocation(IPath path, IClasspathEntry[] resolvedClasspath, IPath projectOutput) {
190                 if (projectOutput.equals(path)) return true;
191                 for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
192                         IClasspathEntry entry = resolvedClasspath[i];
193                         if (entry.getPath().equals(path)) {
194                                 return true;
195                         }
196                         IPath output;
197                         if ((output = entry.getOutputLocation()) != null && output.equals(path)) {
198                                 return true;
199                         }
200                 }
201                 return false;
202         }
203         
204         protected void setNameLookup(NameLookup newNameLookup) {
205
206                 fNameLookup = newNameLookup;
207
208                 // Reinitialize the searchable name environment since it caches
209                 // the name lookup.
210                 fSearchableEnvironment = null;
211         }
212         
213         /**
214          * Set the fNonJavaResources to res value
215          */
216         synchronized void setNonJavaResources(Object[] resources) {
217
218                 fNonJavaResources = resources;
219         }
220         
221         protected void setSearchableEnvironment(SearchableEnvironment newSearchableEnvironment) {
222
223                 fSearchableEnvironment = newSearchableEnvironment;
224         }
225 }