KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > wizards > datatransfer > TarLeveledStructureProvider


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  * Red Hat, Inc - Was TarFileStructureProvider, performed changes from
11  * IImportStructureProvider to ILeveledImportStructureProvider
12  *******************************************************************************/

13 package org.eclipse.ui.internal.wizards.datatransfer;
14
15 import java.io.IOException JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.Enumeration JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
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 /**
29  * This class provides information regarding the context structure and content
30  * of specified tar file entry objects.
31  *
32  * @since 3.1
33  */

34 public class TarLeveledStructureProvider implements
35         ILeveledImportStructureProvider {
36     private TarFile tarFile;
37
38     private TarEntry root = new TarEntry("/");//$NON-NLS-1$
39

40     private Map JavaDoc children;
41
42     private Map JavaDoc directoryEntryCache = new HashMap JavaDoc();
43
44     private int stripLevel;
45
46     /**
47      * Creates a <code>TarFileStructureProvider</code>, which will operate on
48      * the passed tar file.
49      *
50      * @param sourceFile
51      * the source TarFile
52      */

53     public TarLeveledStructureProvider(TarFile sourceFile) {
54         super();
55         tarFile = sourceFile;
56         root.setFileType(TarEntry.DIRECTORY);
57     }
58
59     /**
60      * Creates a new container tar entry with the specified name, iff it has
61      * not already been created. If the parent of the given element does not
62      * already exist it will be recursively created as well.
63      * @param pathname The path representing the container
64      * @return The element represented by this pathname (it may have already existed)
65      */

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 JavaDoc childList = new ArrayList JavaDoc();
82         children.put(newEntry, childList);
83
84         List JavaDoc parentChildList = (List JavaDoc) children.get(parent);
85         parentChildList.add(newEntry);
86         return newEntry;
87     }
88
89     /**
90      * Creates a new tar file entry with the specified name.
91      */

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 JavaDoc childList = (List JavaDoc) children.get(parent);
103         childList.add(entry);
104     }
105
106     /*
107      * (non-Javadoc) Method declared on IImportStructureProvider
108      */

109     public List JavaDoc getChildren(Object JavaDoc element) {
110         if (children == null) {
111             initialize();
112         }
113
114         return ((List JavaDoc) children.get(element));
115     }
116
117     /*
118      * (non-Javadoc) Method declared on IImportStructureProvider
119      */

120     public InputStream JavaDoc getContents(Object JavaDoc 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 JavaDoc e) {
127             IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
128             return null;
129         }
130     }
131
132     /**
133      * Returns the resource attributes for this file.
134      *
135      * @param element
136      * @return the attributes of the file
137      */

138     public ResourceAttributes getResourceAttributes(Object JavaDoc 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     /*
147      * (non-Javadoc) Method declared on IImportStructureProvider
148      */

149     public String JavaDoc getFullPath(Object JavaDoc element) {
150         return stripPath(((TarEntry) element).getName());
151     }
152
153     /*
154      * (non-Javadoc) Method declared on IImportStructureProvider
155      */

156     public String JavaDoc getLabel(Object JavaDoc 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     /**
165      * Returns the entry that this importer uses as the root sentinel.
166      *
167      * @return TarEntry entry
168      */

169     public Object JavaDoc getRoot() {
170         return root;
171     }
172
173     /**
174      * Returns the tar file that this provider provides structure for.
175      *
176      * @return TarFile file
177      */

178     public TarFile getTarFile() {
179         return tarFile;
180     }
181
182     /*
183      * (non-Javadoc)
184      * @see org.eclipse.ui.internal.wizards.datatransfer.ILeveledImportStructureProvider#closeArchive()
185      */

186     public boolean closeArchive(){
187         try {
188             getTarFile().close();
189         } catch (IOException JavaDoc e) {
190             IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
191                     + getTarFile().getName(), e);
192             return false;
193         }
194         return true;
195     }
196     
197     /**
198      * Initializes this object's children table based on the contents of the
199      * specified source file.
200      */

201     protected void initialize() {
202         children = new HashMap JavaDoc(1000);
203         
204         children.put(root, new ArrayList JavaDoc());
205         Enumeration JavaDoc 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                 // Ensure the container structure for all levels above this is initialized
215
// Once we hit a higher-level container that's already added we need go no further
216
int pathSegmentCount = path.segmentCount();
217                 if (pathSegmentCount > 1) {
218                     createContainer(path.uptoSegment(pathSegmentCount - 1));
219                 }
220                 createFile(entry);
221             }
222         }
223     }
224     
225     /*
226      * (non-Javadoc) Method declared on IImportStructureProvider
227      */

228     public boolean isFolder(Object JavaDoc element) {
229         return (((TarEntry) element).getFileType() == TarEntry.DIRECTORY);
230     }
231
232     /*
233      * Strip the leading directories from the path
234      */

235     private String JavaDoc stripPath(String JavaDoc path) {
236         String JavaDoc pathOrig = new String JavaDoc(path);
237         for (int i = 0; i < stripLevel; i++) {
238             int firstSep = path.indexOf('/');
239             // If the first character was a seperator we must strip to the next
240
// seperator as well
241
if (firstSep == 0) {
242                 path = path.substring(1);
243                 firstSep = path.indexOf('/');
244             }
245             // No seperator wasw present so we're in a higher directory right
246
// now
247
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