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