001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.ui.wizards.datatransfer;
011:
012: import java.io.IOException;
013: import java.io.InputStream;
014: import java.util.ArrayList;
015: import java.util.Enumeration;
016: import java.util.HashMap;
017: import java.util.List;
018: import java.util.Map;
019: import java.util.zip.ZipEntry;
020: import java.util.zip.ZipFile;
021:
022: import org.eclipse.core.runtime.IPath;
023: import org.eclipse.core.runtime.Path;
024: import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
025:
026: /**
027: * This class provides information regarding the context structure and
028: * content of specified zip file entry objects.
029: */
030: public class ZipFileStructureProvider implements
031: IImportStructureProvider {
032: private ZipFile zipFile;
033:
034: private ZipEntry root = new ZipEntry("/");//$NON-NLS-1$
035:
036: private Map children;
037:
038: private Map directoryEntryCache = new HashMap();
039:
040: /**
041: * Creates a <code>ZipFileStructureProvider</code>, which will operate
042: * on the passed zip file.
043: *
044: * @param sourceFile the zip file used to create this structure provider
045: */
046: public ZipFileStructureProvider(ZipFile sourceFile) {
047: super ();
048: zipFile = sourceFile;
049: }
050:
051: /**
052: * Adds the specified child to the internal collection of the parent's children.
053: */
054: protected void addToChildren(ZipEntry parent, ZipEntry child) {
055: List childList = (List) children.get(parent);
056: if (childList == null) {
057: childList = new ArrayList();
058: children.put(parent, childList);
059: }
060:
061: childList.add(child);
062: }
063:
064: /**
065: * Creates a new container zip entry with the specified name, iff
066: * it has not already been created.
067: */
068: protected void createContainer(IPath pathname) {
069: if (directoryEntryCache.containsKey(pathname)) {
070: return;
071: }
072:
073: ZipEntry parent;
074: if (pathname.segmentCount() == 1) {
075: parent = root;
076: } else {
077: parent = (ZipEntry) directoryEntryCache.get(pathname
078: .removeLastSegments(1));
079: }
080:
081: ZipEntry newEntry = new ZipEntry(pathname.toString());
082: directoryEntryCache.put(pathname, newEntry);
083: addToChildren(parent, newEntry);
084: }
085:
086: /**
087: * Creates a new file zip entry with the specified name.
088: */
089: protected void createFile(ZipEntry entry) {
090: IPath pathname = new Path(entry.getName());
091: ZipEntry parent;
092: if (pathname.segmentCount() == 1) {
093: parent = root;
094: } else {
095: parent = (ZipEntry) directoryEntryCache.get(pathname
096: .removeLastSegments(1));
097: }
098:
099: addToChildren(parent, entry);
100: }
101:
102: /* (non-Javadoc)
103: * Method declared on IImportStructureProvider
104: */
105: public List getChildren(Object element) {
106: if (children == null) {
107: initialize();
108: }
109:
110: return ((List) children.get(element));
111: }
112:
113: /* (non-Javadoc)
114: * Method declared on IImportStructureProvider
115: */
116: public InputStream getContents(Object element) {
117: try {
118: return zipFile.getInputStream((ZipEntry) element);
119: } catch (IOException 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 getFullPath(Object element) {
129: return ((ZipEntry) element).getName();
130: }
131:
132: /* (non-Javadoc)
133: * Method declared on IImportStructureProvider
134: */
135: public String getLabel(Object element) {
136: if (element.equals(root)) {
137: return ((ZipEntry) element).getName();
138: }
139:
140: return new Path(((ZipEntry) 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 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 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(1000);
167:
168: Enumeration entries = zipFile.entries();
169: while (entries.hasMoreElements()) {
170: ZipEntry entry = (ZipEntry) entries.nextElement();
171: if (!entry.isDirectory()) {
172: IPath path = new Path(entry.getName())
173: .addTrailingSeparator();
174: int pathSegmentCount = path.segmentCount();
175:
176: for (int i = 1; i < pathSegmentCount; i++) {
177: createContainer(path.uptoSegment(i));
178: }
179: createFile(entry);
180: }
181: }
182: }
183:
184: /* (non-Javadoc)
185: * Method declared on IImportStructureProvider
186: */
187: public boolean isFolder(Object element) {
188: return ((ZipEntry) element).isDirectory();
189: }
190: }
|