1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import java.io.IOException ; 23 import java.net.URL ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.tools.FileObject; 30 import javax.tools.JavaFileManager; 31 import javax.tools.JavaFileObject; 32 import org.netbeans.api.java.classpath.ClassPath; 33 import org.netbeans.modules.java.preprocessorbridge.spi.JavaFileFilterImplementation; 34 import org.netbeans.modules.java.source.util.Iterators; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.util.Exceptions; 37 38 39 43 public class CachingFileManager implements JavaFileManager { 44 45 protected final CachingArchiveProvider provider; 46 protected final JavaFileFilterImplementation filter; 47 protected final ClassPath cp; 48 protected final boolean cacheFile; 49 protected final boolean ignoreExcludes; 50 51 52 public CachingFileManager( CachingArchiveProvider provider, final ClassPath cp, boolean cacheFile, boolean ignoreExcludes) { 53 this (provider, cp, null, cacheFile, ignoreExcludes); 54 } 55 56 57 public CachingFileManager( CachingArchiveProvider provider, final ClassPath cp, final JavaFileFilterImplementation filter, boolean cacheFile, boolean ignoreExcludes) { 58 this.provider = provider; 59 this.cp = cp; 60 this.cacheFile = cacheFile; 61 this.filter = filter; 62 this.ignoreExcludes = ignoreExcludes; 63 } 64 65 67 public Iterable <JavaFileObject> list( Location l, String packageName, Set <JavaFileObject.Kind> kinds, boolean recursive ) { 69 70 if (recursive) { 71 throw new UnsupportedOperationException ("Recursive listing is not supported in archives"); 72 } 73 74 76 String folderName = FileObjects.convertPackage2Folder( packageName ); 77 78 List <Iterator <JavaFileObject>> idxs = new LinkedList <Iterator <JavaFileObject>>(); 79 for(ClassPath.Entry entry : this.cp.entries()) { 80 try { 81 Archive archive = provider.getArchive( entry.getURL(), cacheFile ); 82 if (archive != null) { 83 Iterable <JavaFileObject> entries = archive.getFiles( folderName, ignoreExcludes?null:entry, filter); 84 idxs.add( entries.iterator() ); 85 } 86 } catch (IOException e) { 87 Exceptions.printStackTrace(e); 88 } 89 } 90 return Iterators.toIterable( Iterators.chained( idxs ) ); 92 } 93 94 public javax.tools.FileObject getFileForInput( Location l, String pkgName, String relativeName ) { 95 96 for( ClassPath.Entry root : this.cp.entries()) { 97 try { 98 Archive archive = provider.getArchive (root.getURL(), cacheFile); 99 if (archive != null) { 100 Iterable <JavaFileObject> files = archive.getFiles(FileObjects.convertPackage2Folder(pkgName), ignoreExcludes?null:root, filter); 101 for (JavaFileObject e : files) { 102 if (relativeName.equals(e.getName())) { 103 return e; 104 } 105 } 106 } 107 } catch (IOException e) { 108 Exceptions.printStackTrace(e); 109 } 110 } 111 return null; 112 } 113 114 public JavaFileObject getJavaFileForInput (Location l, String className, JavaFileObject.Kind kind) { 115 String [] namePair = FileObjects.getParentRelativePathAndName(className); 116 if (namePair == null) { 117 return null; 118 } 119 namePair[1] = namePair[1] + kind.extension; 120 for( ClassPath.Entry root : this.cp.entries()) { 121 try { 122 Archive archive = provider.getArchive (root.getURL(), cacheFile); 123 if (archive != null) { 124 Iterable <JavaFileObject> files = archive.getFiles(namePair[0], ignoreExcludes?null:root, filter); 125 for (JavaFileObject e : files) { 126 if (namePair[1].equals(e.getName())) { 127 return e; 128 } 129 } 130 } 131 } catch (IOException e) { 132 Exceptions.printStackTrace(e); 133 } 134 } 135 return null; 136 } 137 138 139 public javax.tools.FileObject getFileForOutput( Location l, String pkgName, String relativeName, javax.tools.FileObject sibling ) 140 throws IOException , UnsupportedOperationException , IllegalArgumentException { 141 throw new UnsupportedOperationException (); 142 } 143 144 public JavaFileObject getJavaFileForOutput( Location l, String className, JavaFileObject.Kind kind, javax.tools.FileObject sibling ) 145 throws IOException , UnsupportedOperationException , IllegalArgumentException { 146 throw new UnsupportedOperationException (); 147 } 148 149 public void flush() throws IOException { 150 } 152 153 public void close() throws IOException { 154 } 156 157 public int isSupportedOption(String string) { 158 return -1; 159 } 160 161 public boolean handleOption (final String head, final Iterator <String > tail) { 162 return false; 163 } 164 165 public boolean hasLocation(Location location) { 166 return true; 167 } 168 169 public ClassLoader getClassLoader (final Location l) { 170 return null; 171 } 172 173 public String inferBinaryName (Location l, JavaFileObject javaFileObject) { 174 if (javaFileObject instanceof FileObjects.Base) { 175 final FileObjects.Base base = (FileObjects.Base) javaFileObject; 176 final StringBuilder sb = new StringBuilder (); 177 sb.append (base.getPackage()); 178 sb.append('.'); sb.append(base.getNameWithoutExtension()); 180 return sb.toString(); 181 } 182 else if (javaFileObject instanceof SourceFileObject) { 183 org.openide.filesystems.FileObject fo = ((SourceFileObject)javaFileObject).file; 184 for (org.openide.filesystems.FileObject root : this.cp.getRoots()) { 185 if (FileUtil.isParentOf(root,fo)) { 186 String relativePath = FileUtil.getRelativePath(root,fo); 187 int index = relativePath.lastIndexOf('.'); assert index > 0; 189 final String result = relativePath.substring(0,index).replace('/','.'); return result; 191 } 192 } 193 } 194 return null; 195 } 196 197 199 public static URL [] getClassPathRoots (final ClassPath cp) { 200 assert cp != null; 201 final List <ClassPath.Entry> entries = cp.entries(); 202 final List <URL > result = new ArrayList <URL >(entries.size()); 203 for (ClassPath.Entry entry : entries) { 204 result.add (entry.getURL()); 205 } 206 return result.toArray(new URL [result.size()]); 207 } 208 209 public boolean isSameFile(FileObject fileObject, FileObject fileObject0) { 210 return fileObject instanceof FileObjects.FileBase 211 && fileObject0 instanceof FileObjects.FileBase 212 && ((FileObjects.FileBase)fileObject).getFile().equals(((FileObjects.FileBase)fileObject).getFile()); 213 } 214 } 215 | Popular Tags |