1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 23 24 import java.io.File ; 25 import java.net.URI ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import org.netbeans.api.java.classpath.ClassPath; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.filesystems.URLMapper; 34 35 36 43 public class CachingArchiveProvider { 44 45 private static CachingArchiveProvider instance; 46 47 HashMap <URL ,Archive> archives; 50 51 public static synchronized CachingArchiveProvider getDefault () { 52 if (instance == null) { 53 instance = new CachingArchiveProvider (); 54 } 55 return instance; 56 } 57 58 61 CachingArchiveProvider() { 62 archives = new HashMap <URL ,Archive>(); 63 } 64 65 67 public synchronized Archive getArchive( URL root, boolean cacheFile) { 68 69 Archive archive = archives.get(root); 70 71 if (archive == null) { 72 archive = create(root, cacheFile); 73 if (archive != null) { 74 archives.put(root, archive ); 75 } 76 } 77 return archive; 78 79 } 80 81 83 public synchronized Iterable <Archive> getArchives( URL [] roots, boolean cacheFile) { 84 85 List <Archive> archives = new ArrayList <Archive>(roots.length); 86 for( int i = 0; i < roots.length; i++ ) { 87 Archive a = getArchive( roots[i], cacheFile ); 88 if (a != null) { 89 archives.add(a); 90 } 91 } 92 return archives; 93 } 94 95 96 98 public synchronized Iterable <Archive> getArchives( ClassPath cp, boolean cacheFile) { 99 final List <ClassPath.Entry> entries = cp.entries(); 100 final List <Archive> archives = new ArrayList <Archive> (entries.size()); 101 for (ClassPath.Entry entry : entries) { 102 Archive a = getArchive(entry.getURL(), cacheFile); 103 if (a != null) { 104 archives.add (a); 105 } 106 } 107 return archives; 108 } 109 110 public synchronized void removeArchive (final URL root) { 111 final Archive archive = archives.remove(root); 112 if (archive != null) { 113 archive.clear(); 114 } 115 } 116 117 public synchronized void clearArchive (final URL root) { 118 Archive archive = archives.get(root); 119 if (archive != null) { 120 archive.clear(); 121 } 122 } 123 124 126 128 private static Archive create( URL root, boolean cacheFile ) { 129 String protocol = root.getProtocol(); 130 if ("file".equals(protocol)) { 131 File f = new File (URI.create(root.toExternalForm())); 132 if (f.isDirectory()) { 133 return new FolderArchive (f); 134 } 135 else { 136 return null; 137 } 138 } 139 if ("jar".equals(protocol)) { 140 URL inner = FileUtil.getArchiveFile(root); 141 protocol = inner.getProtocol(); 142 if ("file".equals(protocol)) { 143 File f = new File (URI.create(inner.toExternalForm())); 144 if (f.isFile()) { 145 return new CachingArchive( f, cacheFile ); 146 } 147 else { 148 return null; 149 } 150 } 151 } 152 FileObject fo = URLMapper.findFileObject(root); 154 if (fo != null) { 155 return new FileObjectArchive (fo); 156 } 157 else { 158 return null; 159 } 160 } 161 162 163 } 164 | Popular Tags |