KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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 java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Map JavaDoc;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.Path;
22 import org.eclipse.jdt.core.IClassFile;
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJarEntryResource;
25 import org.eclipse.jdt.core.IJavaElement;
26 import org.eclipse.jdt.core.IJavaModelStatusConstants;
27 import org.eclipse.jdt.core.JavaModelException;
28 import org.eclipse.jdt.internal.compiler.util.SuffixConstants;
29 import org.eclipse.jdt.internal.core.util.Util;
30
31 /**
32  * A package fragment that represents a package fragment found in a JAR.
33  *
34  * @see org.eclipse.jdt.core.IPackageFragment
35  */

36 class JarPackageFragment extends PackageFragment implements SuffixConstants {
37 /**
38  * Constructs a package fragment that is contained within a jar or a zip.
39  */

40 protected JarPackageFragment(PackageFragmentRoot root, String JavaDoc[] names) {
41     super(root, names);
42 }
43 /**
44  * Compute the children of this package fragment. Children of jar package fragments
45  * can only be IClassFile (representing .class files).
46  */

47 protected boolean computeChildren(OpenableElementInfo info, ArrayList JavaDoc entryNames) {
48     if (entryNames != null && entryNames.size() > 0) {
49         ArrayList JavaDoc vChildren = new ArrayList JavaDoc();
50         for (Iterator JavaDoc iter = entryNames.iterator(); iter.hasNext();) {
51             String JavaDoc child = (String JavaDoc) iter.next();
52             IClassFile classFile = getClassFile(child);
53             vChildren.add(classFile);
54         }
55         IJavaElement[] children= new IJavaElement[vChildren.size()];
56         vChildren.toArray(children);
57         info.setChildren(children);
58     } else {
59         info.setChildren(NO_ELEMENTS);
60     }
61     return true;
62 }
63 /**
64  * Compute all the non-java resources according to the entry name found in the jar file.
65  */

66 /* package */ void computeNonJavaResources(String JavaDoc[] resNames, JarPackageFragment pkg, JarPackageFragmentInfo info, String JavaDoc zipName) {
67     if (resNames == null) {
68         info.setNonJavaResources(null);
69         return;
70     }
71     int max = resNames.length;
72     if (max == 0) {
73         info.setNonJavaResources(JavaElementInfo.NO_NON_JAVA_RESOURCES);
74     } else {
75         HashMap JavaDoc jarEntries = new HashMap JavaDoc(); // map from IPath to IJarEntryResource
76
HashMap JavaDoc childrenMap = new HashMap JavaDoc(); // map from IPath to ArrayList<IJarEntryResource>
77
ArrayList JavaDoc topJarEntries = new ArrayList JavaDoc();
78         for (int i = 0; i < max; i++) {
79             String JavaDoc resName = resNames[i];
80             // consider that a .java file is not a non-java resource (see bug 12246 Packages view shows .class and .java files when JAR has source)
81
if (!Util.isJavaLikeFileName(resName)) {
82                 IPath filePath = new Path(resName);
83                 IPath childPath = filePath.removeFirstSegments(this.names.length);
84                 JarEntryFile file = new JarEntryFile(filePath.lastSegment());
85                 jarEntries.put(childPath, file);
86                 if (childPath.segmentCount() == 1) {
87                     file.setParent(pkg);
88                     topJarEntries.add(file);
89                 } else {
90                     IPath parentPath = childPath.removeLastSegments(1);
91                     while (parentPath.segmentCount() > 0) {
92                         ArrayList JavaDoc parentChildren = (ArrayList JavaDoc) childrenMap.get(parentPath);
93                         if (parentChildren == null) {
94                             Object JavaDoc dir = new JarEntryDirectory(parentPath.lastSegment());
95                             jarEntries.put(parentPath, dir);
96                             childrenMap.put(parentPath, parentChildren = new ArrayList JavaDoc());
97                             parentChildren.add(childPath);
98                             if (parentPath.segmentCount() == 1) {
99                                 topJarEntries.add(dir);
100                                 break;
101                             }
102                             childPath = parentPath;
103                             parentPath = childPath.removeLastSegments(1);
104                         } else {
105                             parentChildren.add(childPath);
106                             break; // all parents are already registered
107
}
108                     }
109                 }
110             }
111         }
112         Iterator JavaDoc entries = childrenMap.entrySet().iterator();
113         while (entries.hasNext()) {
114             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) entries.next();
115             IPath entryPath = (IPath) entry.getKey();
116             ArrayList JavaDoc entryValue = (ArrayList JavaDoc) entry.getValue();
117             JarEntryDirectory jarEntryDirectory = (JarEntryDirectory) jarEntries.get(entryPath);
118             int size = entryValue.size();
119             IJarEntryResource[] children = new IJarEntryResource[size];
120             for (int i = 0; i < size; i++) {
121                 JarEntryResource child = (JarEntryResource) jarEntries.get(entryValue.get(i));
122                 child.setParent(jarEntryDirectory);
123                 children[i] = child;
124             }
125             jarEntryDirectory.setChildren(children);
126             if (entryPath.segmentCount() == 1) {
127                 jarEntryDirectory.setParent(pkg);
128             }
129         }
130         Object JavaDoc[] res = topJarEntries.toArray(new Object JavaDoc[topJarEntries.size()]);
131         info.setNonJavaResources(res);
132     }
133 }
134 /**
135  * Returns true if this fragment contains at least one java resource.
136  * Returns false otherwise.
137  */

138 public boolean containsJavaResources() throws JavaModelException {
139     return ((JarPackageFragmentInfo) getElementInfo()).containsJavaResources();
140 }
141 /**
142  * @see org.eclipse.jdt.core.IPackageFragment
143  */

144 public ICompilationUnit createCompilationUnit(String JavaDoc cuName, String JavaDoc contents, boolean force, IProgressMonitor monitor) throws JavaModelException {
145     throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this));
146 }
147 /**
148  * @see JavaElement
149  */

150 protected Object JavaDoc createElementInfo() {
151     return null; // not used for JarPackageFragments: info is created when jar is opened
152
}
153 /*
154  * @see JavaElement#generateInfos
155  */

156 protected void generateInfos(Object JavaDoc info, HashMap JavaDoc newElements, IProgressMonitor pm) throws JavaModelException {
157     // Open my jar: this creates all the pkg infos
158
Openable openableParent = (Openable)this.parent;
159     if (!openableParent.isOpen()) {
160         openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm);
161     }
162 }
163 /**
164  * @see org.eclipse.jdt.core.IPackageFragment
165  */

166 public IClassFile[] getClassFiles() throws JavaModelException {
167     ArrayList JavaDoc list = getChildrenOfType(CLASS_FILE);
168     IClassFile[] array= new IClassFile[list.size()];
169     list.toArray(array);
170     return array;
171 }
172 /**
173  * A jar package fragment never contains compilation units.
174  * @see org.eclipse.jdt.core.IPackageFragment
175  */

176 public ICompilationUnit[] getCompilationUnits() {
177     return NO_COMPILATION_UNITS;
178 }
179 /**
180  * A package fragment in a jar has no corresponding resource.
181  *
182  * @see IJavaElement
183  */

184 public IResource getCorrespondingResource() {
185     return null;
186 }
187 /**
188  * Returns an array of non-java resources contained in the receiver.
189  */

190 public Object JavaDoc[] getNonJavaResources() throws JavaModelException {
191     if (this.isDefaultPackage()) {
192         // We don't want to show non java resources of the default package (see PR #1G58NB8)
193
return JavaElementInfo.NO_NON_JAVA_RESOURCES;
194     } else {
195         return this.storedNonJavaResources();
196     }
197 }
198 /**
199  * Jars and jar entries are all read only
200  */

201 public boolean isReadOnly() {
202     return true;
203 }
204 protected Object JavaDoc[] storedNonJavaResources() throws JavaModelException {
205     return ((JarPackageFragmentInfo) getElementInfo()).getNonJavaResources();
206 }
207 }
208
Popular Tags