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