KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > core > sourcelookup > containers > ZipEntryStorage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.core.sourcelookup.containers;
12
13
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.util.zip.ZipEntry JavaDoc;
17 import java.util.zip.ZipFile JavaDoc;
18
19 import org.eclipse.core.resources.IStorage;
20 import org.eclipse.core.runtime.CoreException;
21 import org.eclipse.core.runtime.IPath;
22 import org.eclipse.core.runtime.IStatus;
23 import org.eclipse.core.runtime.Path;
24 import org.eclipse.core.runtime.PlatformObject;
25 import org.eclipse.core.runtime.Status;
26 import org.eclipse.debug.core.DebugPlugin;
27 import org.eclipse.debug.internal.core.sourcelookup.SourceLookupMessages;
28  
29 /**
30  * Storage implementation for zip entries.
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.
33  * </p>
34  * @see IStorage
35  * @since 3.0
36  */

37 public class ZipEntryStorage extends PlatformObject implements IStorage {
38     
39     /**
40      * Zip file associated with zip entry
41      */

42     private ZipFile JavaDoc fArchive;
43     
44     /**
45      * Zip entry
46      */

47     private ZipEntry JavaDoc fZipEntry;
48     
49     /**
50      * Constructs a new storage implementation for the
51      * given zip entry in the specified zip file
52      *
53      * @param archive zip file
54      * @param entry zip entry
55      */

56     public ZipEntryStorage(ZipFile JavaDoc archive, ZipEntry JavaDoc entry) {
57         setArchive(archive);
58         setZipEntry(entry);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.core.resources.IStorage#getContents()
63      */

64     public InputStream JavaDoc getContents() throws CoreException {
65         try {
66             return getArchive().getInputStream(getZipEntry());
67         } catch (IOException JavaDoc e) {
68             throw new CoreException(new Status(IStatus.ERROR, DebugPlugin.getUniqueIdentifier(), DebugPlugin.INTERNAL_ERROR, SourceLookupMessages.ZipEntryStorage_0, e));
69         }
70     }
71
72     /* (non-Javadoc)
73      * @see org.eclipse.core.resources.IStorage#getFullPath()
74      */

75     public IPath getFullPath() {
76         return new Path(getArchive().getName()).append(getZipEntry().getName());
77     }
78
79     /* (non-Javadoc)
80      * @see org.eclipse.core.resources.IStorage#getName()
81      */

82     public String JavaDoc getName() {
83         int index = getZipEntry().getName().lastIndexOf('\\');
84         if (index == -1) {
85             index = getZipEntry().getName().lastIndexOf('/');
86         }
87         if (index == -1) {
88             return getZipEntry().getName();
89         }
90         return getZipEntry().getName().substring(index + 1);
91     }
92
93     /* (non-Javadoc)
94      * @see org.eclipse.core.resources.IStorage#isReadOnly()
95      */

96     public boolean isReadOnly() {
97         return true;
98     }
99     
100     /**
101      * Sets the archive containing the zip entry.
102      *
103      * @param archive a zip file
104      */

105     private void setArchive(ZipFile JavaDoc archive) {
106         fArchive = archive;
107     }
108     
109     /**
110      * Returns the archive containing the zip entry.
111      *
112      * @return zip file
113      */

114     public ZipFile JavaDoc getArchive() {
115         return fArchive;
116     }
117     
118     /**
119      * Sets the entry that contains the source.
120      *
121      * @param entry the entry that contains the source
122      */

123     private void setZipEntry(ZipEntry JavaDoc entry) {
124         fZipEntry = entry;
125     }
126     
127     /**
128      * Returns the entry that contains the source
129      *
130      * @return zip entry
131      */

132     public ZipEntry JavaDoc getZipEntry() {
133         return fZipEntry;
134     }
135
136     /* (non-Javadoc)
137      * @see java.lang.Object#equals(java.lang.Object)
138      */

139     public boolean equals(Object JavaDoc object) {
140         return object instanceof ZipEntryStorage &&
141              getArchive().equals(((ZipEntryStorage)object).getArchive()) &&
142              getZipEntry().getName().equals(((ZipEntryStorage)object).getZipEntry().getName());
143     }
144     
145     /* (non-Javadoc)
146      * @see java.lang.Object#hashCode()
147      */

148     public int hashCode() {
149         return getZipEntry().getName().hashCode();
150     }
151 }
152
Popular Tags