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