061a5f3553e47eee290e71843b846dd4d8a0e09d
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / PackageFragmentRootInfo.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.IPackageFragmentRoot;
16 import net.sourceforge.phpdt.core.JavaModelException;
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  * The element info for <code>PackageFragmentRoot</code>s.
25  */
26 class PackageFragmentRootInfo extends OpenableElementInfo {
27
28         /**
29          * The SourceMapper for this JAR (or <code>null</code> if
30          * this JAR does not have source attached).
31          */
32 //      protected SourceMapper sourceMapper = null;
33
34         /**
35          * The kind of the root associated with this info.
36          * Valid kinds are: <ul>
37          * <li><code>IPackageFragmentRoot.K_SOURCE</code>
38          * <li><code>IPackageFragmentRoot.K_BINARY</code></ul>
39          */
40         protected int fRootKind= IPackageFragmentRoot.K_SOURCE;
41
42         /**
43          * A array with all the non-java resources contained by this PackageFragment
44          */
45         protected Object[] fNonJavaResources;
46 /**
47  * Create and initialize a new instance of the receiver
48  */
49 public PackageFragmentRootInfo() {
50         fNonJavaResources = null;
51 }
52 /**
53  * Starting at this folder, create non-java resources for this package fragment root 
54  * and add them to the non-java resources collection.
55  * 
56  * @exception JavaModelException  The resource associated with this package fragment does not exist
57  */
58 static Object[] computeFolderNonJavaResources(JavaProject project, IContainer folder, char[][] exclusionPatterns) throws JavaModelException {
59         Object[] nonJavaResources = new IResource[5];
60         int nonJavaResourcesCounter = 0;
61         try {
62                 IClasspathEntry[] classpath = project.getResolvedClasspath(true/*ignore unresolved variable*/);
63                 IResource[] members = folder.members();
64                 nextResource: for (int i = 0, max = members.length; i < max; i++) {
65                         IResource member = members[i];
66                         switch (member.getType()) {
67                                 case IResource.FILE :
68                                         String fileName = member.getName();
69                                         
70                                         // ignore .java files that are not excluded
71                                         if (Util.isValidCompilationUnitName(fileName) && !Util.isExcluded(member, exclusionPatterns)) 
72                                                 continue nextResource;
73                                         // ignore .class files
74 //                                      if (Util.isValidClassFileName(fileName)) 
75 //                                              continue nextResource;
76 //                                      // ignore .zip or .jar file on classpath
77 //                                      if (Util.isArchiveFileName(fileName) && isClasspathEntry(member.getFullPath(), classpath)) 
78 //                                              continue nextResource;
79                                         break;
80
81                                 case IResource.FOLDER :
82                                         // ignore valid packages or excluded folders that correspond to a nested pkg fragment root
83 //                                      if (Util.isValidFolderNameForPackage(member.getName())
84 //                                                      && (!Util.isExcluded(member, exclusionPatterns) 
85 //                                                              || isClasspathEntry(member.getFullPath(), classpath)))
86 //                                              continue nextResource;
87                                         break;
88                         }
89                         if (nonJavaResources.length == nonJavaResourcesCounter) {
90                                 // resize
91                                 System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0, nonJavaResourcesCounter);
92                         }
93                         nonJavaResources[nonJavaResourcesCounter++] = member;
94
95                 }
96                 if (nonJavaResources.length != nonJavaResourcesCounter) {
97                         System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]), 0, nonJavaResourcesCounter);
98                 }
99                 return nonJavaResources;
100         } catch (CoreException e) {
101                 throw new JavaModelException(e);
102         }
103 }
104 /**
105  * Compute the non-package resources of this package fragment root.
106  * 
107  * @exception JavaModelException  The resource associated with this package fragment root does not exist
108  */
109 private Object[] computeNonJavaResources(IJavaProject project, IResource underlyingResource, PackageFragmentRoot handle) {
110         Object[] nonJavaResources = NO_NON_JAVA_RESOURCES;
111         try {
112                 // the underlying resource may be a folder or a project (in the case that the project folder
113                 // is actually the package fragment root)
114                 if (underlyingResource.getType() == IResource.FOLDER || underlyingResource.getType() == IResource.PROJECT) {
115                         nonJavaResources = 
116                                 computeFolderNonJavaResources(
117                                         (JavaProject)project, 
118                                         (IContainer) underlyingResource,  
119                                         handle.fullExclusionPatternChars());
120                 }
121         } catch (JavaModelException e) {
122         }
123         return nonJavaResources;
124 }
125 /**
126  * Returns an array of non-java resources contained in the receiver.
127  */
128 synchronized Object[] getNonJavaResources(IJavaProject project, IResource underlyingResource, PackageFragmentRoot handle) {
129         Object[] nonJavaResources = fNonJavaResources;
130         if (nonJavaResources == null) {
131                 nonJavaResources = this.computeNonJavaResources(project, underlyingResource, handle);
132                 fNonJavaResources = nonJavaResources;
133         }
134         return nonJavaResources;
135 }
136 /**
137  * Returns the kind of this root.
138  */
139 public int getRootKind() {
140         return fRootKind;
141 }
142 /**
143  * Retuns the SourceMapper for this root, or <code>null</code>
144  * if this root does not have attached source.
145  */
146 //protected synchronized SourceMapper getSourceMapper() {
147 //      return this.sourceMapper;
148 //}
149 private static boolean isClasspathEntry(IPath path, IClasspathEntry[] resolvedClasspath) {
150         for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
151                 IClasspathEntry entry = resolvedClasspath[i];
152                 if (entry.getPath().equals(path)) {
153                         return true;
154                 }
155         }
156         return false;
157 }
158 /**
159  * Set the fNonJavaResources to res value
160  */
161 synchronized void setNonJavaResources(Object[] resources) {
162         fNonJavaResources = resources;
163 }
164 /**
165  * Sets the kind of this root.
166  */
167 protected void setRootKind(int newRootKind) {
168         fRootKind = newRootKind;
169 }
170 /**
171  * Sets the SourceMapper for this root.
172  */
173 //protected synchronized void setSourceMapper(SourceMapper mapper) {
174 //      this.sourceMapper= mapper;
175 //}
176 }