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