KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > core > PackageFragmentRootInfo


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.core;
12
13 import org.eclipse.core.resources.IResource;
14 import org.eclipse.core.resources.IContainer;
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IPath;
17
18 import org.eclipse.jdt.core.*;
19 import org.eclipse.jdt.internal.core.util.Util;
20
21 /**
22  * The element info for <code>PackageFragmentRoot</code>s.
23  */

24 class PackageFragmentRootInfo extends OpenableElementInfo {
25
26     /**
27      * The SourceMapper for this JAR (or <code>null</code> if
28      * this JAR does not have source attached).
29      */

30     protected SourceMapper sourceMapper = null;
31
32     /**
33      * The kind of the root associated with this info.
34      * Valid kinds are: <ul>
35      * <li><code>IPackageFragmentRoot.K_SOURCE</code>
36      * <li><code>IPackageFragmentRoot.K_BINARY</code></ul>
37      */

38     protected int fRootKind= IPackageFragmentRoot.K_SOURCE;
39
40     /**
41      * A array with all the non-java resources contained by this PackageFragment
42      */

43     protected Object JavaDoc[] fNonJavaResources;
44 /**
45  * Create and initialize a new instance of the receiver
46  */

47 public PackageFragmentRootInfo() {
48     this.fNonJavaResources = null;
49 }
50 /**
51  * Starting at this folder, create non-java resources for this package fragment root
52  * and add them to the non-java resources collection.
53  *
54  * @exception JavaModelException The resource associated with this package fragment does not exist
55  */

56 static Object JavaDoc[] computeFolderNonJavaResources(JavaProject project, IContainer folder, char[][] inclusionPatterns, char[][] exclusionPatterns) throws JavaModelException {
57     Object JavaDoc[] nonJavaResources = new IResource[5];
58     int nonJavaResourcesCounter = 0;
59     try {
60         IClasspathEntry[] classpath = project.getResolvedClasspath();
61         IResource[] members = folder.members();
62         int length = members.length;
63         if (length > 0) {
64             String JavaDoc sourceLevel = project.getOption(JavaCore.COMPILER_SOURCE, true);
65             String JavaDoc complianceLevel = project.getOption(JavaCore.COMPILER_COMPLIANCE, true);
66             nextResource: for (int i = 0; i < length; i++) {
67                 IResource member = members[i];
68                 switch (member.getType()) {
69                     case IResource.FILE :
70                         String JavaDoc fileName = member.getName();
71                     
72                         // ignore .java files that are not excluded
73
if (Util.isValidCompilationUnitName(fileName, sourceLevel, complianceLevel) && !Util.isExcluded(member, inclusionPatterns, exclusionPatterns))
74                             continue nextResource;
75                         // ignore .class files
76
if (Util.isValidClassFileName(fileName, sourceLevel, complianceLevel))
77                             continue nextResource;
78                         // ignore .zip or .jar file on classpath
79
if (org.eclipse.jdt.internal.compiler.util.Util.isArchiveFileName(fileName) && isClasspathEntry(member.getFullPath(), classpath))
80                             continue nextResource;
81                         break;
82
83                     case IResource.FOLDER :
84                         // ignore valid packages or excluded folders that correspond to a nested pkg fragment root
85
if (Util.isValidFolderNameForPackage(member.getName(), sourceLevel, complianceLevel)
86                                 && (!Util.isExcluded(member, inclusionPatterns, exclusionPatterns)
87                                         || isClasspathEntry(member.getFullPath(), classpath)))
88                             continue nextResource;
89                         break;
90                 }
91                 if (nonJavaResources.length == nonJavaResourcesCounter) {
92                     // resize
93
System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter * 2]), 0, nonJavaResourcesCounter);
94                 }
95                 nonJavaResources[nonJavaResourcesCounter++] = member;
96             }
97         }
98         if (nonJavaResources.length != nonJavaResourcesCounter) {
99             System.arraycopy(nonJavaResources, 0, (nonJavaResources = new IResource[nonJavaResourcesCounter]), 0, nonJavaResourcesCounter);
100         }
101         return nonJavaResources;
102     } catch (CoreException e) {
103         throw new JavaModelException(e);
104     }
105 }
106 /**
107  * Compute the non-package resources of this package fragment root.
108  */

109 private Object JavaDoc[] computeNonJavaResources(IJavaProject project, IResource underlyingResource, PackageFragmentRoot handle) {
110     Object JavaDoc[] 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.fullInclusionPatternChars(),
120                     handle.fullExclusionPatternChars());
121         }
122     } catch (JavaModelException e) {
123         // ignore
124
}
125     return nonJavaResources;
126 }
127 /**
128  * Returns an array of non-java resources contained in the receiver.
129  */

130 synchronized Object JavaDoc[] getNonJavaResources(IJavaProject project, IResource underlyingResource, PackageFragmentRoot handle) {
131     Object JavaDoc[] nonJavaResources = this.fNonJavaResources;
132     if (nonJavaResources == null) {
133         nonJavaResources = this.computeNonJavaResources(project, underlyingResource, handle);
134         this.fNonJavaResources = nonJavaResources;
135     }
136     return nonJavaResources;
137 }
138 /**
139  * Returns the kind of this root.
140  */

141 public int getRootKind() {
142     return this.fRootKind;
143 }
144 /**
145  * Retuns the SourceMapper for this root, or <code>null</code>
146  * if this root does not have attached source.
147  */

148 protected SourceMapper getSourceMapper() {
149     return this.sourceMapper;
150 }
151 private static boolean isClasspathEntry(IPath path, IClasspathEntry[] resolvedClasspath) {
152     for (int i = 0, length = resolvedClasspath.length; i < length; i++) {
153         IClasspathEntry entry = resolvedClasspath[i];
154         if (entry.getPath().equals(path)) {
155             return true;
156         }
157     }
158     return false;
159 }
160 /**
161  * Set the fNonJavaResources to res value
162  */

163 void setNonJavaResources(Object JavaDoc[] resources) {
164     this.fNonJavaResources = resources;
165 }
166 /**
167  * Sets the kind of this root.
168  */

169 protected void setRootKind(int newRootKind) {
170     this.fRootKind = newRootKind;
171 }
172 /**
173  * Sets the SourceMapper for this root.
174  */

175 protected void setSourceMapper(SourceMapper mapper) {
176     this.sourceMapper= mapper;
177 }
178 }
179
Popular Tags