KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > apt > util > Archive


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jdt.internal.compiler.apt.util;
12
13 import java.io.File JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.nio.charset.Charset JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.Enumeration JavaDoc;
18 import java.util.Hashtable JavaDoc;
19 import java.util.Set JavaDoc;
20 import java.util.zip.ZipEntry JavaDoc;
21 import java.util.zip.ZipException JavaDoc;
22 import java.util.zip.ZipFile JavaDoc;
23
24 /**
25  * Used as a zip file cache.
26  */

27 public class Archive {
28
29     public static final Archive UNKNOWN_ARCHIVE = new Archive();
30     
31     ZipFile JavaDoc zipFile;
32     File JavaDoc file;
33     protected Hashtable JavaDoc<String JavaDoc, ArrayList JavaDoc<String JavaDoc>> packagesCache;
34     
35     private Archive() {
36     }
37
38     public Archive(File JavaDoc file) throws ZipException JavaDoc, IOException JavaDoc {
39         this.file = file;
40         this.zipFile = new ZipFile JavaDoc(file);
41         initialize();
42     }
43
44     private void initialize() {
45         // initialize packages
46
this.packagesCache = new Hashtable JavaDoc<String JavaDoc, ArrayList JavaDoc<String JavaDoc>>();
47         nextEntry : for (Enumeration JavaDoc<? extends ZipEntry JavaDoc> e = this.zipFile.entries(); e.hasMoreElements(); ) {
48             String JavaDoc fileName = ((ZipEntry JavaDoc) e.nextElement()).getName();
49
50             // add the package name & all of its parent packages
51
int last = fileName.lastIndexOf('/');
52             // extract the package name
53
String JavaDoc packageName = fileName.substring(0, last + 1);
54             String JavaDoc typeName = fileName.substring(last + 1);
55             ArrayList JavaDoc<String JavaDoc> types = this.packagesCache.get(packageName);
56             if (types == null) {
57                 // might be empty if this is a directory entry
58
if (typeName.length() == 0) {
59                     continue nextEntry;
60                 }
61                 types = new ArrayList JavaDoc<String JavaDoc>();
62                 types.add(typeName);
63                 this.packagesCache.put(packageName, types);
64             } else {
65                 types.add(typeName);
66             }
67         }
68     }
69     
70     public ArchiveFileObject getArchiveFileObject(String JavaDoc entryName, Charset JavaDoc charset) {
71         return new ArchiveFileObject(this.file, this.zipFile, entryName, charset);
72     }
73     
74     public boolean contains(String JavaDoc entryName) {
75         return this.zipFile.getEntry(entryName) != null;
76     }
77     
78     public Set JavaDoc<String JavaDoc> allPackages() {
79         if (this.packagesCache == null) {
80             this.initialize();
81         }
82         return this.packagesCache.keySet();
83     }
84     
85     public ArrayList JavaDoc<String JavaDoc> getTypes(String JavaDoc packageName) {
86         // package name is expected to ends with '/'
87
return this.packagesCache.get(packageName);
88     }
89     
90     public void flush() {
91         this.packagesCache = null;
92     }
93
94     public void close() {
95         try {
96             if (this.zipFile != null) this.zipFile.close();
97             this.packagesCache = null;
98         } catch (IOException JavaDoc e) {
99             // ignore
100
}
101     }
102 }
103
Popular Tags