1 13 package org.eclipse.ui.internal.wizards.datatransfer; 14 15 import java.io.IOException ; 16 import java.io.InputStream ; 17 import java.util.ArrayList ; 18 import java.util.Enumeration ; 19 import java.util.HashMap ; 20 import java.util.List ; 21 import java.util.Map ; 22 23 import org.eclipse.core.resources.ResourceAttributes; 24 import org.eclipse.core.runtime.IPath; 25 import org.eclipse.core.runtime.Path; 26 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 27 28 34 public class TarLeveledStructureProvider implements 35 ILeveledImportStructureProvider { 36 private TarFile tarFile; 37 38 private TarEntry root = new TarEntry("/"); 40 private Map children; 41 42 private Map directoryEntryCache = new HashMap (); 43 44 private int stripLevel; 45 46 53 public TarLeveledStructureProvider(TarFile sourceFile) { 54 super(); 55 tarFile = sourceFile; 56 root.setFileType(TarEntry.DIRECTORY); 57 } 58 59 66 protected TarEntry createContainer(IPath pathname) { 67 TarEntry existingEntry = (TarEntry) directoryEntryCache.get(pathname); 68 if (existingEntry != null) { 69 return existingEntry; 70 } 71 72 TarEntry parent; 73 if (pathname.segmentCount() == 1) { 74 parent = root; 75 } else { 76 parent = createContainer(pathname.removeLastSegments(1)); 77 } 78 TarEntry newEntry = new TarEntry(pathname.toString()); 79 newEntry.setFileType(TarEntry.DIRECTORY); 80 directoryEntryCache.put(pathname, newEntry); 81 List childList = new ArrayList (); 82 children.put(newEntry, childList); 83 84 List parentChildList = (List ) children.get(parent); 85 parentChildList.add(newEntry); 86 return newEntry; 87 } 88 89 92 protected void createFile(TarEntry entry) { 93 IPath pathname = new Path(entry.getName()); 94 TarEntry parent; 95 if (pathname.segmentCount() == 1) { 96 parent = root; 97 } else { 98 parent = (TarEntry) directoryEntryCache.get(pathname 99 .removeLastSegments(1)); 100 } 101 102 List childList = (List ) children.get(parent); 103 childList.add(entry); 104 } 105 106 109 public List getChildren(Object element) { 110 if (children == null) { 111 initialize(); 112 } 113 114 return ((List ) children.get(element)); 115 } 116 117 120 public InputStream getContents(Object element) { 121 try { 122 return tarFile.getInputStream((TarEntry) element); 123 } catch (TarException e) { 124 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e); 125 return null; 126 } catch (IOException e) { 127 IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e); 128 return null; 129 } 130 } 131 132 138 public ResourceAttributes getResourceAttributes(Object element) { 139 ResourceAttributes attributes = new ResourceAttributes(); 140 TarEntry entry = (TarEntry) element; 141 attributes.setExecutable((entry.getMode() & 0100) != 0); 142 attributes.setReadOnly((entry.getMode() & 0200) == 0); 143 return attributes; 144 } 145 146 149 public String getFullPath(Object element) { 150 return stripPath(((TarEntry) element).getName()); 151 } 152 153 156 public String getLabel(Object element) { 157 if (element.equals(root)) { 158 return ((TarEntry) element).getName(); 159 } 160 161 return stripPath(new Path(((TarEntry) element).getName()).lastSegment()); 162 } 163 164 169 public Object getRoot() { 170 return root; 171 } 172 173 178 public TarFile getTarFile() { 179 return tarFile; 180 } 181 182 186 public boolean closeArchive(){ 187 try { 188 getTarFile().close(); 189 } catch (IOException e) { 190 IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose 191 + getTarFile().getName(), e); 192 return false; 193 } 194 return true; 195 } 196 197 201 protected void initialize() { 202 children = new HashMap (1000); 203 204 children.put(root, new ArrayList ()); 205 Enumeration entries = tarFile.entries(); 206 while (entries.hasMoreElements()) { 207 TarEntry entry = (TarEntry) entries.nextElement(); 208 IPath path = new Path(entry.getName()).addTrailingSeparator(); 209 210 if (entry.getFileType() == TarEntry.DIRECTORY) { 211 createContainer(path); 212 } else 213 { 214 int pathSegmentCount = path.segmentCount(); 217 if (pathSegmentCount > 1) { 218 createContainer(path.uptoSegment(pathSegmentCount - 1)); 219 } 220 createFile(entry); 221 } 222 } 223 } 224 225 228 public boolean isFolder(Object element) { 229 return (((TarEntry) element).getFileType() == TarEntry.DIRECTORY); 230 } 231 232 235 private String stripPath(String path) { 236 String pathOrig = new String (path); 237 for (int i = 0; i < stripLevel; i++) { 238 int firstSep = path.indexOf('/'); 239 if (firstSep == 0) { 242 path = path.substring(1); 243 firstSep = path.indexOf('/'); 244 } 245 if (firstSep == -1) { 248 return pathOrig; 249 } 250 path = path.substring(firstSep); 251 } 252 return path; 253 } 254 255 public void setStrip(int level) { 256 stripLevel = level; 257 } 258 259 public int getStrip() { 260 return stripLevel; 261 } 262 } 263 | Popular Tags |