1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.Enumeration ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.zip.ZipEntry ; 32 import java.util.zip.ZipFile ; 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 archiveFile; 45 private final boolean keepOpened; 46 private ZipFile zipFile; 47 48 private Map <String ,List <ZipRecord>> folders2files; 50 51 53 54 public CachingArchive( File archiveFile, boolean keepOpened) { 55 this.archiveFile = archiveFile; 56 this.keepOpened = keepOpened; 57 } 58 59 61 62 64 public Iterable <JavaFileObject> getFiles( String folderName, ClassPath.Entry entry, JavaFileFilterImplementation filter) throws IOException { 65 doInit(); 66 List <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 83 public synchronized boolean isInitialized() { 84 return folders2files != null; 85 } 86 87 public synchronized void initialize() { 88 folders2files = createMap( archiveFile ); 89 } 90 91 93 private synchronized void doInit() { 94 if ( !isInitialized() ) { 95 initialize(); 96 } 97 } 98 99 private Map <String ,List <ZipRecord>> createMap(File file ) { 100 if (file.canRead()) { 101 try { 102 ZipFile zip = new ZipFile (file); 103 try { 104 final Map <String ,List <ZipRecord>> map = new HashMap <String ,List <ZipRecord>>(); 105 106 for ( Enumeration <? extends ZipEntry > e = zip.entries(); e.hasMoreElements(); ) { 107 ZipEntry entry = e.nextElement(); 108 String name = entry.getName(); 109 int i = name.lastIndexOf('/'); 110 String dirname = i == -1 ? "" : name.substring(0, i ); 111 String basename = name.substring(i+1); 112 if (basename.length() == 0) { 113 basename = null; 114 } 115 List <ZipRecord> list = map.get(dirname); 116 if (list == null) { 117 list = new ArrayList <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 ioe) { 135 Exceptions.printStackTrace(ioe); 136 } 137 } 138 } 139 } catch (IOException ioe) { 140 141 } 142 } 143 return Collections.<String ,List <ZipRecord>>emptyMap(); 144 } 145 146 148 private static class JFOFactory implements Factory<JavaFileObject,ZipRecord> { 149 150 private final String pkg; 151 private final File archiveFile; 152 private final ZipFile zipFile; 153 154 JFOFactory( String pkg, File archiveFile, ZipFile 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 baseName; 174 175 public ZipRecord (final String baseName, final long mtime) { 176 assert baseName != null; 177 this.mtime = mtime; 178 this.baseName = baseName; 179 } 180 } 181 182 183 } 184 | Popular Tags |