KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > parsing > CachingArchive


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.source.parsing;
21
22
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.Enumeration JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.zip.ZipEntry JavaDoc;
32 import java.util.zip.ZipFile JavaDoc;
33 import javax.tools.JavaFileObject;
34 import org.netbeans.api.java.classpath.ClassPath;
35 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation;
36 import org.netbeans.modules.java.source.util.Factory;
37 import org.netbeans.modules.java.source.util.Iterators;
38 import org.openide.util.Exceptions;
39
40 public class CachingArchive implements Archive {
41
42
43
44     private final File JavaDoc archiveFile;
45     private final boolean keepOpened;
46     private ZipFile JavaDoc zipFile;
47         
48     // Cache
49
private Map JavaDoc<String JavaDoc,List JavaDoc<ZipRecord>> folders2files;
50     
51     // Constructors ------------------------------------------------------------
52

53     /** Creates a new instance of archive from zip file */
54     public CachingArchive( File JavaDoc archiveFile, boolean keepOpened) {
55         this.archiveFile = archiveFile;
56         this.keepOpened = keepOpened;
57     }
58         
59     // Archive implementation --------------------------------------------------
60

61     
62     /** Gets all files in given folder
63      */

64     public Iterable JavaDoc<JavaFileObject> getFiles( String JavaDoc folderName, ClassPath.Entry entry, JavaFileFilterImplementation filter) throws IOException JavaDoc {
65         doInit();
66         List JavaDoc<ZipRecord> files = folders2files.get( folderName );
67         if (files == null) {
68             return Collections.<JavaFileObject>emptyList();
69         }
70         else {
71             assert !keepOpened || zipFile != null;
72             return Iterators.translating(files, new JFOFactory(folderName, archiveFile, zipFile));
73         }
74     }
75     
76     
77     public synchronized void clear () {
78         this.folders2files = null;
79     }
80                       
81     // ILazzy implementation ---------------------------------------------------
82

83     public synchronized boolean isInitialized() {
84         return folders2files != null;
85     }
86     
87     public synchronized void initialize() {
88         folders2files = createMap( archiveFile );
89     }
90     
91     // Private methods ---------------------------------------------------------
92

93     private synchronized void doInit() {
94         if ( !isInitialized() ) {
95             initialize();
96         }
97     }
98     
99     private Map JavaDoc<String JavaDoc,List JavaDoc<ZipRecord>> createMap(File JavaDoc file ) {
100         if (file.canRead()) {
101             try {
102                 ZipFile JavaDoc zip = new ZipFile JavaDoc (file);
103                 try {
104                     final Map JavaDoc<String JavaDoc,List JavaDoc<ZipRecord>> map = new HashMap JavaDoc<String JavaDoc,List JavaDoc<ZipRecord>>();
105
106                     for ( Enumeration JavaDoc<? extends ZipEntry JavaDoc> e = zip.entries(); e.hasMoreElements(); ) {
107                         ZipEntry JavaDoc entry = e.nextElement();
108                         String JavaDoc name = entry.getName();
109                         int i = name.lastIndexOf('/');
110                         String JavaDoc dirname = i == -1 ? "" : name.substring(0, i /* +1 */);
111                         String JavaDoc basename = name.substring(i+1);
112                         if (basename.length() == 0) {
113                             basename = null;
114                         }
115                         List JavaDoc<ZipRecord> list = map.get(dirname);
116                         if (list == null) {
117                             list = new ArrayList JavaDoc<ZipRecord>();
118                             map.put(dirname, list);
119                         }
120
121                         if ( basename != null ) {
122                             list.add( new ZipRecord (basename, entry.getTime()));
123                         }
124
125                     }
126                     return map;
127                 } finally {
128                     if (keepOpened) {
129                         this.zipFile = zip;
130                     }
131                     else {
132                         try {
133                             zip.close();
134                         } catch (IOException JavaDoc ioe) {
135                             Exceptions.printStackTrace(ioe);
136                         }
137                     }
138                 }
139             } catch (IOException JavaDoc ioe) {
140                 
141             }
142         }
143         return Collections.<String JavaDoc,List JavaDoc<ZipRecord>>emptyMap();
144     }
145     
146     // Innerclasses ------------------------------------------------------------
147

148     private static class JFOFactory implements Factory<JavaFileObject,ZipRecord> {
149         
150         private final String JavaDoc pkg;
151         private final File JavaDoc archiveFile;
152         private final ZipFile JavaDoc zipFile;
153         
154         JFOFactory( String JavaDoc pkg, File JavaDoc archiveFile, ZipFile JavaDoc zipFile ) {
155             this.pkg = pkg;
156             this.archiveFile = archiveFile;
157             this.zipFile = zipFile;
158         }
159         
160         public JavaFileObject create(final ZipRecord parameter) {
161             if (zipFile == null) {
162                 return FileObjects.zipFileObject(archiveFile, pkg, parameter.baseName, parameter.mtime);
163             }
164             else {
165                 return FileObjects.zipFileObject( zipFile, pkg, parameter.baseName, parameter.mtime);
166             }
167         }
168     };
169     
170     
171     private static class ZipRecord {
172         private final long mtime;
173         private final String JavaDoc baseName;
174         
175         public ZipRecord (final String JavaDoc baseName, final long mtime) {
176             assert baseName != null;
177             this.mtime = mtime;
178             this.baseName = baseName;
179         }
180     }
181     
182         
183 }
184
Popular Tags