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