KickJava   Java API By Example, From Geeks To Geeks.

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


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 ZipFileStructureProvider, 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 import java.util.zip.ZipEntry JavaDoc;
23 import java.util.zip.ZipFile JavaDoc;
24
25 import org.eclipse.core.runtime.IPath;
26 import org.eclipse.core.runtime.Path;
27 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
28
29 /**
30  * This class provides information regarding the context structure and content
31  * of specified zip file entry objects.
32  *
33  * @since 3.1
34  */

35 public class ZipLeveledStructureProvider implements
36         ILeveledImportStructureProvider {
37     private ZipFile JavaDoc zipFile;
38
39     private ZipEntry JavaDoc root = new ZipEntry JavaDoc("/");//$NON-NLS-1$
40

41     private Map JavaDoc children;
42
43     private Map JavaDoc directoryEntryCache = new HashMap JavaDoc();
44
45     private int stripLevel;
46
47     /**
48      * Creates a <code>ZipFileStructureProvider</code>, which will operate on
49      * the passed zip file.
50      *
51      * @param sourceFile
52      * The source file to create the ZipLeveledStructureProvider
53      * around
54      */

55     public ZipLeveledStructureProvider(ZipFile JavaDoc sourceFile) {
56         super();
57         zipFile = sourceFile;
58         stripLevel = 0;
59     }
60
61     /**
62      * Creates a new container zip entry with the specified name, iff it has
63      * not already been created. If the parent of the given element does not
64      * already exist it will be recursively created as well.
65      * @param pathname The path representing the container
66      * @return The element represented by this pathname (it may have already existed)
67      */

68     protected ZipEntry JavaDoc createContainer(IPath pathname) {
69         ZipEntry JavaDoc existingEntry = (ZipEntry JavaDoc) directoryEntryCache.get(pathname);
70         if (existingEntry != null) {
71             return existingEntry;
72         }
73
74         ZipEntry JavaDoc parent;
75         if (pathname.segmentCount() == 1) {
76             parent = root;
77         } else {
78             parent = createContainer(pathname.removeLastSegments(1));
79         }
80         ZipEntry JavaDoc newEntry = new ZipEntry JavaDoc(pathname.toString());
81         directoryEntryCache.put(pathname, newEntry);
82         List JavaDoc childList = new ArrayList JavaDoc();
83         children.put(newEntry, childList);
84
85         List JavaDoc parentChildList = (List JavaDoc) children.get(parent);
86         parentChildList.add(newEntry);
87         return newEntry;
88     }
89
90     /**
91      * Creates a new file zip entry with the specified name.
92      */

93     protected void createFile(ZipEntry JavaDoc entry) {
94         IPath pathname = new Path(entry.getName());
95         ZipEntry JavaDoc parent;
96         if (pathname.segmentCount() == 1) {
97             parent = root;
98         } else {
99             parent = (ZipEntry JavaDoc) directoryEntryCache.get(pathname
100                     .removeLastSegments(1));
101         }
102
103         List JavaDoc childList = (List JavaDoc) children.get(parent);
104         childList.add(entry);
105     }
106
107     /*
108      * (non-Javadoc) Method declared on IImportStructureProvider
109      */

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

121     public InputStream JavaDoc getContents(Object JavaDoc element) {
122         try {
123             return zipFile.getInputStream((ZipEntry JavaDoc) element);
124         } catch (IOException JavaDoc e) {
125             IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
126             return null;
127         }
128     }
129
130     /*
131      * Strip the leading directories from the path
132      */

133     private String JavaDoc stripPath(String JavaDoc path) {
134         String JavaDoc pathOrig = new String JavaDoc(path);
135         for (int i = 0; i < stripLevel; i++) {
136             int firstSep = path.indexOf('/');
137             // If the first character was a seperator we must strip to the next
138
// seperator as well
139
if (firstSep == 0) {
140                 path = path.substring(1);
141                 firstSep = path.indexOf('/');
142             }
143             // No seperator wasw present so we're in a higher directory right
144
// now
145
if (firstSep == -1) {
146                 return pathOrig;
147             }
148             path = path.substring(firstSep);
149         }
150         return path;
151     }
152
153     /*
154      * (non-Javadoc) Method declared on IImportStructureProvider
155      */

156     public String JavaDoc getFullPath(Object JavaDoc element) {
157         return stripPath(((ZipEntry JavaDoc) element).getName());
158     }
159
160     /*
161      * (non-Javadoc) Method declared on IImportStructureProvider
162      */

163     public String JavaDoc getLabel(Object JavaDoc element) {
164         if (element.equals(root)) {
165             return ((ZipEntry JavaDoc) element).getName();
166         }
167
168         return stripPath(new Path(((ZipEntry JavaDoc) element).getName()).lastSegment());
169     }
170
171     /**
172      * Returns the entry that this importer uses as the root sentinel.
173      *
174      * @return java.util.zip.ZipEntry
175      */

176     public Object JavaDoc getRoot() {
177         return root;
178     }
179
180     /**
181      * Returns the zip file that this provider provides structure for.
182      *
183      * @return The zip file
184      */

185     public ZipFile JavaDoc getZipFile() {
186         return zipFile;
187     }
188
189
190     /*
191      * (non-Javadoc)
192      * @see org.eclipse.ui.internal.wizards.datatransfer.ILeveledImportStructureProvider#closeArchive()
193      */

194     public boolean closeArchive(){
195         try {
196             getZipFile().close();
197         } catch (IOException JavaDoc e) {
198             IDEWorkbenchPlugin.log(DataTransferMessages.ZipImport_couldNotClose
199                     + getZipFile().getName(), e);
200             return false;
201         }
202         return true;
203     }
204     
205     /**
206      * Initializes this object's children table based on the contents of the
207      * specified source file.
208      */

209     protected void initialize() {
210         children = new HashMap JavaDoc(1000);
211
212         children.put(root, new ArrayList JavaDoc());
213         Enumeration JavaDoc entries = zipFile.entries();
214         while (entries.hasMoreElements()) {
215             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
216             IPath path = new Path(entry.getName()).addTrailingSeparator();
217
218             if (entry.isDirectory()) {
219                 createContainer(path);
220             } else
221             {
222                 // Ensure the container structure for all levels above this is initialized
223
// Once we hit a higher-level container that's already added we need go no further
224
int pathSegmentCount = path.segmentCount();
225                 if (pathSegmentCount > 1) {
226                     createContainer(path.uptoSegment(pathSegmentCount - 1));
227                 }
228                 createFile(entry);
229             }
230         }
231     }
232
233     /*
234      * (non-Javadoc) Method declared on IImportStructureProvider
235      */

236     public boolean isFolder(Object JavaDoc element) {
237         return ((ZipEntry JavaDoc) element).isDirectory();
238     }
239
240     public void setStrip(int level) {
241         stripLevel = level;
242     }
243
244     public int getStrip() {
245         return stripLevel;
246     }
247 }
248
Popular Tags