KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > wizards > datatransfer > ZipFileStructureProvider


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  *******************************************************************************/

11 package org.eclipse.ui.wizards.datatransfer;
12
13 import java.io.IOException JavaDoc;
14 import java.io.InputStream JavaDoc;
15 import java.util.ArrayList JavaDoc;
16 import java.util.Enumeration JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipFile JavaDoc;
22
23 import org.eclipse.core.runtime.IPath;
24 import org.eclipse.core.runtime.Path;
25 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
26
27 /**
28  * This class provides information regarding the context structure and
29  * content of specified zip file entry objects.
30  */

31 public class ZipFileStructureProvider implements IImportStructureProvider {
32     private ZipFile JavaDoc zipFile;
33
34     private ZipEntry JavaDoc root = new ZipEntry JavaDoc("/");//$NON-NLS-1$
35

36     private Map JavaDoc children;
37
38     private Map JavaDoc directoryEntryCache = new HashMap JavaDoc();
39
40     /**
41      * Creates a <code>ZipFileStructureProvider</code>, which will operate
42      * on the passed zip file.
43      *
44      * @param sourceFile the zip file used to create this structure provider
45      */

46     public ZipFileStructureProvider(ZipFile JavaDoc sourceFile) {
47         super();
48         zipFile = sourceFile;
49     }
50
51     /**
52      * Adds the specified child to the internal collection of the parent's children.
53      */

54     protected void addToChildren(ZipEntry JavaDoc parent, ZipEntry JavaDoc child) {
55         List JavaDoc childList = (List JavaDoc) children.get(parent);
56         if (childList == null) {
57             childList = new ArrayList JavaDoc();
58             children.put(parent, childList);
59         }
60
61         childList.add(child);
62     }
63
64     /**
65      * Creates a new container zip entry with the specified name, iff
66      * it has not already been created.
67      */

68     protected void createContainer(IPath pathname) {
69         if (directoryEntryCache.containsKey(pathname)) {
70             return;
71         }
72
73         ZipEntry JavaDoc parent;
74         if (pathname.segmentCount() == 1) {
75             parent = root;
76         } else {
77             parent = (ZipEntry JavaDoc) directoryEntryCache.get(pathname
78                     .removeLastSegments(1));
79         }
80
81         ZipEntry JavaDoc newEntry = new ZipEntry JavaDoc(pathname.toString());
82         directoryEntryCache.put(pathname, newEntry);
83         addToChildren(parent, newEntry);
84     }
85
86     /**
87      * Creates a new file zip entry with the specified name.
88      */

89     protected void createFile(ZipEntry JavaDoc entry) {
90         IPath pathname = new Path(entry.getName());
91         ZipEntry JavaDoc parent;
92         if (pathname.segmentCount() == 1) {
93             parent = root;
94         } else {
95             parent = (ZipEntry JavaDoc) directoryEntryCache.get(pathname
96                     .removeLastSegments(1));
97         }
98
99         addToChildren(parent, entry);
100     }
101
102     /* (non-Javadoc)
103      * Method declared on IImportStructureProvider
104      */

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

116     public InputStream JavaDoc getContents(Object JavaDoc element) {
117         try {
118             return zipFile.getInputStream((ZipEntry JavaDoc) element);
119         } catch (IOException JavaDoc e) {
120             IDEWorkbenchPlugin.log(e.getLocalizedMessage(), e);
121             return null;
122         }
123     }
124
125     /* (non-Javadoc)
126      * Method declared on IImportStructureProvider
127      */

128     public String JavaDoc getFullPath(Object JavaDoc element) {
129         return ((ZipEntry JavaDoc) element).getName();
130     }
131
132     /* (non-Javadoc)
133      * Method declared on IImportStructureProvider
134      */

135     public String JavaDoc getLabel(Object JavaDoc element) {
136         if (element.equals(root)) {
137             return ((ZipEntry JavaDoc) element).getName();
138         }
139
140         return new Path(((ZipEntry JavaDoc) element).getName()).lastSegment();
141     }
142
143     /**
144      * Returns the entry that this importer uses as the root sentinel.
145      *
146      * @return java.util.zip.ZipEntry
147      */

148     public ZipEntry JavaDoc getRoot() {
149         return root;
150     }
151
152     /**
153      * Returns the zip file that this provider provides structure for.
154      *
155      * @return the zip file this provider provides structure for
156      */

157     public ZipFile JavaDoc getZipFile() {
158         return zipFile;
159     }
160
161     /**
162      * Initializes this object's children table based on the contents of
163      * the specified source file.
164      */

165     protected void initialize() {
166         children = new HashMap JavaDoc(1000);
167
168         Enumeration JavaDoc entries = zipFile.entries();
169         while (entries.hasMoreElements()) {
170             ZipEntry JavaDoc entry = (ZipEntry JavaDoc) entries.nextElement();
171             if (!entry.isDirectory()) {
172                 IPath path = new Path(entry.getName()).addTrailingSeparator();
173                 int pathSegmentCount = path.segmentCount();
174
175                 for (int i = 1; i < pathSegmentCount; i++) {
176                     createContainer(path.uptoSegment(i));
177                 }
178                 createFile(entry);
179             }
180         }
181     }
182
183     /* (non-Javadoc)
184      * Method declared on IImportStructureProvider
185      */

186     public boolean isFolder(Object JavaDoc element) {
187         return ((ZipEntry JavaDoc) element).isDirectory();
188     }
189 }
190
Popular Tags