1 11 package org.eclipse.jdt.internal.core; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.Map ; 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 36 class JarPackageFragment extends PackageFragment implements SuffixConstants { 37 40 protected JarPackageFragment(PackageFragmentRoot root, String [] names) { 41 super(root, names); 42 } 43 47 protected boolean computeChildren(OpenableElementInfo info, ArrayList entryNames) { 48 if (entryNames != null && entryNames.size() > 0) { 49 ArrayList vChildren = new ArrayList (); 50 for (Iterator iter = entryNames.iterator(); iter.hasNext();) { 51 String child = (String ) 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 66 void computeNonJavaResources(String [] resNames, JarPackageFragment pkg, JarPackageFragmentInfo info, String 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 jarEntries = new HashMap (); HashMap childrenMap = new HashMap (); ArrayList topJarEntries = new ArrayList (); 78 for (int i = 0; i < max; i++) { 79 String resName = resNames[i]; 80 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 parentChildren = (ArrayList ) childrenMap.get(parentPath); 93 if (parentChildren == null) { 94 Object dir = new JarEntryDirectory(parentPath.lastSegment()); 95 jarEntries.put(parentPath, dir); 96 childrenMap.put(parentPath, parentChildren = new ArrayList ()); 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; } 108 } 109 } 110 } 111 } 112 Iterator entries = childrenMap.entrySet().iterator(); 113 while (entries.hasNext()) { 114 Map.Entry entry = (Map.Entry ) entries.next(); 115 IPath entryPath = (IPath) entry.getKey(); 116 ArrayList entryValue = (ArrayList ) 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 [] res = topJarEntries.toArray(new Object [topJarEntries.size()]); 131 info.setNonJavaResources(res); 132 } 133 } 134 138 public boolean containsJavaResources() throws JavaModelException { 139 return ((JarPackageFragmentInfo) getElementInfo()).containsJavaResources(); 140 } 141 144 public ICompilationUnit createCompilationUnit(String cuName, String contents, boolean force, IProgressMonitor monitor) throws JavaModelException { 145 throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.READ_ONLY, this)); 146 } 147 150 protected Object createElementInfo() { 151 return null; } 153 156 protected void generateInfos(Object info, HashMap newElements, IProgressMonitor pm) throws JavaModelException { 157 Openable openableParent = (Openable)this.parent; 159 if (!openableParent.isOpen()) { 160 openableParent.generateInfos(openableParent.createElementInfo(), newElements, pm); 161 } 162 } 163 166 public IClassFile[] getClassFiles() throws JavaModelException { 167 ArrayList list = getChildrenOfType(CLASS_FILE); 168 IClassFile[] array= new IClassFile[list.size()]; 169 list.toArray(array); 170 return array; 171 } 172 176 public ICompilationUnit[] getCompilationUnits() { 177 return NO_COMPILATION_UNITS; 178 } 179 184 public IResource getCorrespondingResource() { 185 return null; 186 } 187 190 public Object [] getNonJavaResources() throws JavaModelException { 191 if (this.isDefaultPackage()) { 192 return JavaElementInfo.NO_NON_JAVA_RESOURCES; 194 } else { 195 return this.storedNonJavaResources(); 196 } 197 } 198 201 public boolean isReadOnly() { 202 return true; 203 } 204 protected Object [] storedNonJavaResources() throws JavaModelException { 205 return ((JarPackageFragmentInfo) getElementInfo()).getNonJavaResources(); 206 } 207 } 208 | Popular Tags |