View | Details | Raw Unified | Return to bug 148944
Collapse All | Expand All

(-)model/org/eclipse/jdt/core/IPackageFragment.java (+5 lines)
Lines 165-170 Link Here
165
	 * inclusion/exclusion patterns on the corresponding source classpath entry
165
	 * inclusion/exclusion patterns on the corresponding source classpath entry
166
	 * are considered non-Java resources and will appear in the result
166
	 * are considered non-Java resources and will appear in the result
167
	 * (possibly in a folder).
167
	 * (possibly in a folder).
168
	 * </p><p>
169
	 * Since 3.3, if this package fragment is inside an archive, the non-Java resources
170
	 * are a tree of {@link IJarEntryResource}s. One can navigate this tree using
171
	 * the {@link IJarEntryResource#getChildren()} and 
172
	 * {@link IJarEntryResource#getParent()} methods.
168
	 * </p>
173
	 * </p>
169
	 * 
174
	 * 
170
	 * @exception JavaModelException if this element does not exist or if an
175
	 * @exception JavaModelException if this element does not exist or if an
(-)model/org/eclipse/jdt/core/IPackageFragmentRoot.java (+6 lines)
Lines 270-276 Link Here
270
	 * entry are considered non-Java resources and will appear in the result
270
	 * entry are considered non-Java resources and will appear in the result
271
	 * (possibly in a folder). Thus when a nested source folder is excluded, it will appear
271
	 * (possibly in a folder). Thus when a nested source folder is excluded, it will appear
272
	 * in the non-Java resources of the outer folder.
272
	 * in the non-Java resources of the outer folder.
273
	 * </p><p>
274
	 * Since 3.3, if this package fragment root is an archive, the non-Java resources
275
	 * are a tree of {@link IJarEntryResource}s. One can navigate this tree using
276
	 * the {@link IJarEntryResource#getChildren()} and 
277
	 * {@link IJarEntryResource#getParent()} methods.
273
	 * </p>
278
	 * </p>
279
	 * 
274
	 * @return an array of non-Java resources (<code>IFile</code>s, 
280
	 * @return an array of non-Java resources (<code>IFile</code>s, 
275
	 *              <code>IFolder</code>s, or <code>IStorage</code>s if the
281
	 *              <code>IFolder</code>s, or <code>IStorage</code>s if the
276
	 *              package fragment root is in archive) contained in this package 
282
	 *              package fragment root is in archive) contained in this package 
(-)model/org/eclipse/jdt/internal/core/JarEntryFile.java (-7 / +29 lines)
Lines 19-42 Link Here
19
import org.eclipse.core.runtime.CoreException;
19
import org.eclipse.core.runtime.CoreException;
20
import org.eclipse.core.runtime.IPath;
20
import org.eclipse.core.runtime.IPath;
21
import org.eclipse.core.runtime.PlatformObject;
21
import org.eclipse.core.runtime.PlatformObject;
22
import org.eclipse.jdt.core.IJarEntryResource;
22
import org.eclipse.jdt.core.IJavaModelStatusConstants;
23
import org.eclipse.jdt.core.IJavaModelStatusConstants;
23
import org.eclipse.jdt.core.JavaModelException;
24
import org.eclipse.jdt.core.JavaModelException;
24
25
25
/**
26
/**
26
 * A jar entry that represents a non-java resource found in a JAR.
27
 * A jar entry that represents a non-java file found in a JAR.
27
 *
28
 *
28
 * @see IStorage
29
 * @see IStorage
29
 */
30
 */
30
public class JarEntryFile extends PlatformObject implements IStorage {
31
public class JarEntryFile extends PlatformObject implements IJarEntryResource {
32
	private static final IJarEntryResource[] NO_CHILDREN = new IJarEntryResource[0];
33
	private Object parent;
31
	private String entryName;
34
	private String entryName;
32
	private String zipName;
35
	private String zipName;
33
	private IPath path;
36
	private IPath path;
34
	
37
	
35
	public JarEntryFile(String entryName, String zipName, IPath parentRelativePath) {
38
public JarEntryFile(String entryName, String zipName, IPath parentRelativePath) {
36
		this.entryName = entryName;
39
	this.entryName = entryName;
37
		this.zipName = zipName;
40
	this.zipName = zipName;
38
		this.path = parentRelativePath;
41
	this.path = parentRelativePath;
39
	}
42
}
43
44
public JarEntryFile clone(Object newParent) {
45
	JarEntryFile file = new JarEntryFile(this.entryName, this.zipName, this.path);
46
	file.setParent(newParent);
47
	return file;
48
}
49
	
40
public InputStream getContents() throws CoreException {
50
public InputStream getContents() throws CoreException {
41
51
42
	try {
52
	try {
Lines 53-58 Link Here
53
		throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
63
		throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION);
54
	}
64
	}
55
}
65
}
66
public IJarEntryResource[] getChildren() {
67
	return NO_CHILDREN;
68
}
56
/**
69
/**
57
 * @see IStorage#getFullPath
70
 * @see IStorage#getFullPath
58
 */
71
 */
Lines 65-76 Link Here
65
public String getName() {
78
public String getName() {
66
	return this.path.lastSegment();
79
	return this.path.lastSegment();
67
}
80
}
81
public Object getParent() {
82
	return this.parent;
83
}
84
public boolean isFile() {
85
	return true;
86
}
68
/**
87
/**
69
 * @see IStorage#isReadOnly()
88
 * @see IStorage#isReadOnly()
70
 */
89
 */
71
public boolean isReadOnly() {
90
public boolean isReadOnly() {
72
	return true;
91
	return true;
73
}
92
}
93
public void setParent(Object parent) {
94
	this.parent = parent;
95
}
74
/**
96
/**
75
 * @see IStorage#isReadOnly()
97
 * @see IStorage#isReadOnly()
76
 */
98
 */
(-)model/org/eclipse/jdt/internal/core/JarPackageFragment.java (-8 / +56 lines)
Lines 13-18 Link Here
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.HashMap;
14
import java.util.HashMap;
15
import java.util.Iterator;
15
import java.util.Iterator;
16
import java.util.Map;
16
17
17
import org.eclipse.core.resources.IResource;
18
import org.eclipse.core.resources.IResource;
18
import org.eclipse.core.runtime.IPath;
19
import org.eclipse.core.runtime.IPath;
Lines 20-25 Link Here
20
import org.eclipse.core.runtime.Path;
21
import org.eclipse.core.runtime.Path;
21
import org.eclipse.jdt.core.IClassFile;
22
import org.eclipse.jdt.core.IClassFile;
22
import org.eclipse.jdt.core.ICompilationUnit;
23
import org.eclipse.jdt.core.ICompilationUnit;
24
import org.eclipse.jdt.core.IJarEntryResource;
23
import org.eclipse.jdt.core.IJavaElement;
25
import org.eclipse.jdt.core.IJavaElement;
24
import org.eclipse.jdt.core.IJavaModelStatusConstants;
26
import org.eclipse.jdt.core.IJavaModelStatusConstants;
25
import org.eclipse.jdt.core.JavaModelException;
27
import org.eclipse.jdt.core.JavaModelException;
Lines 61-67 Link Here
61
/**
63
/**
62
 * Compute all the non-java resources according to the entry name found in the jar file.
64
 * Compute all the non-java resources according to the entry name found in the jar file.
63
 */
65
 */
64
/* package */ void computeNonJavaResources(String[] resNames, JarPackageFragmentInfo info, String zipName) {
66
/* package */ void computeNonJavaResources(String[] resNames, JarPackageFragment pkg, JarPackageFragmentInfo info, String zipName) {
65
	if (resNames == null) {
67
	if (resNames == null) {
66
		info.setNonJavaResources(null);
68
		info.setNonJavaResources(null);
67
		return;
69
		return;
Lines 70-88 Link Here
70
	if (max == 0) {
72
	if (max == 0) {
71
	    info.setNonJavaResources(JavaElementInfo.NO_NON_JAVA_RESOURCES);
73
	    info.setNonJavaResources(JavaElementInfo.NO_NON_JAVA_RESOURCES);
72
	} else {
74
	} else {
73
		Object[] res = new Object[max];
75
		HashMap jarEntries = new HashMap(); // map from IPath to IJarEntryResource
74
		int index = 0;
76
		HashMap childrenMap = new HashMap(); // map from IPath to ArrayList<IJarEntryResource>
77
		ArrayList topJarEntries = new ArrayList();
75
		for (int i = 0; i < max; i++) {
78
		for (int i = 0; i < max; i++) {
76
			String resName = resNames[i];
79
			String resName = resNames[i];
77
			// 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)
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)
78
			if (!Util.isJavaLikeFileName(resName)) {
81
			if (!Util.isJavaLikeFileName(resName)) {
79
				IPath parentRelativePath = new Path(resName).removeFirstSegments(this.names.length);
82
				IPath childPath = new Path(resName).removeFirstSegments(this.names.length);
80
				res[index++] = new JarEntryFile(resName, zipName, parentRelativePath);
83
				JarEntryFile file = new JarEntryFile(resName, zipName, childPath);
84
				jarEntries.put(childPath, file);
85
				if (childPath.segmentCount() == 1) {
86
					file.setParent(pkg);
87
					topJarEntries.add(file);
88
				} else {
89
					IPath parentPath = childPath.removeLastSegments(1);
90
					while (parentPath.segmentCount() > 0) {
91
						ArrayList parentChildren = (ArrayList) childrenMap.get(parentPath);
92
						if (parentChildren == null) {
93
							Object dir = new JarEntryDirectory(parentPath);
94
							jarEntries.put(parentPath, dir);
95
							childrenMap.put(parentPath, parentChildren = new ArrayList());
96
							parentChildren.add(childPath);
97
							if (parentPath.segmentCount() == 1) {
98
								topJarEntries.add(dir);
99
								break;
100
							}
101
							childPath = parentPath;
102
							parentPath = childPath.removeLastSegments(1);
103
						} else {
104
							parentChildren.add(childPath);
105
							break; // all parents are already registered
106
						}
107
					}
108
				}
81
			}
109
			}
82
		} 
83
		if (index != max) {
84
			System.arraycopy(res, 0, res = new Object[index], 0, index);
85
		}
110
		}
111
		Iterator entries = childrenMap.entrySet().iterator();
112
		while (entries.hasNext()) {
113
			Map.Entry entry = (Map.Entry) entries.next();
114
			IPath entryPath = (IPath) entry.getKey();
115
			ArrayList entryValue =  (ArrayList) entry.getValue();
116
			JarEntryDirectory jarEntryDirectory = (JarEntryDirectory) jarEntries.get(entryPath);
117
			int size = entryValue.size();
118
			IJarEntryResource[] children = new IJarEntryResource[size];
119
			for (int i = 0; i < size; i++) {
120
				Object child = jarEntries.get(entryValue.get(i));
121
				if (child instanceof JarEntryFile) {
122
					((JarEntryFile) child).setParent(jarEntryDirectory);
123
				} else {
124
					((JarEntryDirectory) child).setParent(jarEntryDirectory);
125
				}
126
				children[i] = (IJarEntryResource) child;
127
			}
128
			jarEntryDirectory.setChildren(children);
129
			if (entryPath.segmentCount() == 1) {
130
				jarEntryDirectory.setParent(pkg);
131
			}
132
		}
133
		Object[] res = topJarEntries.toArray(new Object[topJarEntries.size()]);
86
		info.setNonJavaResources(res);
134
		info.setNonJavaResources(res);
87
	}
135
	}
88
}
136
}
(-)model/org/eclipse/jdt/internal/core/JarPackageFragmentRoot.java (-3 / +15 lines)
Lines 98-108 Link Here
98
				JarPackageFragmentInfo fragInfo= new JarPackageFragmentInfo();
98
				JarPackageFragmentInfo fragInfo= new JarPackageFragmentInfo();
99
				int resLength= entries[NON_JAVA].size();
99
				int resLength= entries[NON_JAVA].size();
100
				if (resLength == 0) {
100
				if (resLength == 0) {
101
					packFrag.computeNonJavaResources(CharOperation.NO_STRINGS, fragInfo, jar.getName());
101
					packFrag.computeNonJavaResources(CharOperation.NO_STRINGS, packFrag, fragInfo, jar.getName());
102
				} else {
102
				} else {
103
					String[] resNames= new String[resLength];
103
					String[] resNames= new String[resLength];
104
					entries[NON_JAVA].toArray(resNames);
104
					entries[NON_JAVA].toArray(resNames);
105
					packFrag.computeNonJavaResources(resNames, fragInfo, jar.getName());
105
					packFrag.computeNonJavaResources(resNames, packFrag, fragInfo, jar.getName());
106
				}
106
				}
107
				packFrag.computeChildren(fragInfo, entries[JAVA]);
107
				packFrag.computeChildren(fragInfo, entries[JAVA]);
108
				newElements.put(packFrag, fragInfo);
108
				newElements.put(packFrag, fragInfo);
Lines 171-177 Link Here
171
	 */
171
	 */
172
	public Object[] getNonJavaResources() throws JavaModelException {
172
	public Object[] getNonJavaResources() throws JavaModelException {
173
		// We want to show non java resources of the default package at the root (see PR #1G58NB8)
173
		// We want to show non java resources of the default package at the root (see PR #1G58NB8)
174
		return ((JarPackageFragment) getPackageFragment(CharOperation.NO_STRINGS)).storedNonJavaResources();
174
		Object[] defaultPkgResources =  ((JarPackageFragment) getPackageFragment(CharOperation.NO_STRINGS)).storedNonJavaResources();
175
		int length = defaultPkgResources.length;
176
		if (length == 0)
177
			return defaultPkgResources;
178
		Object[] nonJavaResources = new Object[length];
179
		for (int i = 0; i < length; i++) {
180
			Object nonJavaResource = defaultPkgResources[i];
181
			if (nonJavaResource instanceof JarEntryFile)
182
				nonJavaResources[i] = ((JarEntryFile) nonJavaResource).clone(this);
183
			else
184
				nonJavaResources[i] = ((JarEntryDirectory) nonJavaResource).clone(this);
185
		}
186
		return nonJavaResources;
175
	}
187
	}
176
	public PackageFragment getPackageFragment(String[] pkgName) {
188
	public PackageFragment getPackageFragment(String[] pkgName) {
177
		return new JarPackageFragment(this, pkgName);
189
		return new JarPackageFragment(this, pkgName);
(-)model/org/eclipse/jdt/core/IJarEntryResource.java (+46 lines)
Added Link Here
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.core;
12
13
import org.eclipse.core.resources.IStorage;
14
15
/**
16
 * A jar entry corresponding to a non-Java resource in an archive {@link IPackageFragment}.
17
 * 
18
 * @since 3.3
19
 */
20
public interface IJarEntryResource extends IStorage {
21
	
22
	/**
23
	 * Returns the list of children of this jar entry resource.
24
	 * Returns an empty array if this jar entry is a file, or if this jar entry is a directory and it has no children.
25
	 * 
26
	 * @return the children of this jar entry resource
27
	 */
28
	IJarEntryResource[] getChildren();
29
	
30
	/**
31
	 * Returns the parent of this jar entry resource. This is either an {@link IJarEntryResource}, an {@link IPackageFragment}
32
	 * or an {@link IPackageFragmentRoot}.
33
	 * 
34
	 * @return the parent of this jar entry resource
35
	 */
36
	Object getParent();
37
	
38
	/**
39
	 * Returns <code>true</code> if this jar entry represents a file.
40
	 * Returns <code>false</code> if it is a directory.
41
	 * 
42
	 * @return whether this jar entry is a file
43
	 */
44
	boolean isFile();
45
46
}
(-)model/org/eclipse/jdt/internal/core/JarEntryDirectory.java (+87 lines)
Added Link Here
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.io.ByteArrayInputStream;
14
import java.io.InputStream;
15
16
import org.eclipse.core.runtime.CoreException;
17
import org.eclipse.core.runtime.IPath;
18
import org.eclipse.core.runtime.PlatformObject;
19
import org.eclipse.jdt.core.IJarEntryResource;
20
21
public class JarEntryDirectory extends PlatformObject implements IJarEntryResource {
22
	private Object parent;
23
	private IPath path;
24
	private IJarEntryResource[] children;
25
	
26
	public JarEntryDirectory(IPath parentRelativePath) {
27
		this.path = parentRelativePath;
28
	}
29
	
30
	public JarEntryDirectory clone(Object newParent) {
31
		JarEntryDirectory dir = new JarEntryDirectory(this.path);
32
		dir.setParent(newParent);
33
		int length = this.children.length;
34
		if (length > 0) {
35
			IJarEntryResource[] newChildren = new IJarEntryResource[length];
36
			for (int i = 0; i < length; i++) {
37
				IJarEntryResource child = this.children[i];
38
				if (child instanceof JarEntryFile)
39
					newChildren[i] = ((JarEntryFile) child).clone(dir);
40
				else
41
					newChildren[i] = ((JarEntryDirectory) child).clone(dir);
42
			}
43
			dir.setChildren(newChildren);
44
		}
45
		return dir;
46
	}
47
	
48
	public IJarEntryResource[] getChildren() {
49
		return this.children;
50
	}
51
52
	public InputStream getContents() throws CoreException {
53
		return new ByteArrayInputStream(new byte[0]);
54
	}
55
56
	public IPath getFullPath() {
57
		return this.path;
58
	}
59
60
	public String getName() {
61
		return this.path.lastSegment();
62
	}
63
64
	public Object getParent() {
65
		return this.parent;
66
	}
67
	
68
	public boolean isFile() {
69
		return false;
70
	}
71
72
	public boolean isReadOnly() {
73
		return true;
74
	}
75
76
	public void setChildren(IJarEntryResource[] children) {
77
		this.children = children;
78
	}
79
80
	public void setParent(Object parent) {
81
		this.parent = parent;
82
	}
83
	
84
	public String toString() {
85
		return "JarEntryDirectory["+this.path+"]"; //$NON-NLS-1$ //$NON-NLS-2$ 
86
	}
87
}
(-)src/org/eclipse/jdt/core/tests/model/JavaProjectTests.java (-2 / +2 lines)
Lines 760-766 Link Here
760
	Object[] resources = pkg.getNonJavaResources();
760
	Object[] resources = pkg.getNonJavaResources();
761
	assertResourcesEqual(
761
	assertResourcesEqual(
762
		"Unexpected resources", 
762
		"Unexpected resources", 
763
		"x.y/Test.txt",
763
		"x.y",
764
		resources);
764
		resources);
765
}
765
}
766
766
Lines 862-868 Link Here
862
	resources = root.getNonJavaResources();
862
	resources = root.getNonJavaResources();
863
	assertResourceNamesEqual(
863
	assertResourceNamesEqual(
864
		"unexpected non java resoures (test case 4)", 
864
		"unexpected non java resoures (test case 4)", 
865
		"MANIFEST.MF",
865
		"META-INF",
866
		resources);
866
		resources);
867
}
867
}
868
/**
868
/**

Return to bug 148944