1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import java.io.IOException ; 23 import java.net.MalformedURLException ; 24 import java.util.ArrayList ; 25 import java.util.Enumeration ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import java.util.Set ; 29 import javax.tools.JavaFileManager; 30 import javax.tools.JavaFileManager.Location; 31 import javax.tools.JavaFileObject; 32 import org.netbeans.api.java.classpath.ClassPath; 33 import org.openide.ErrorManager; 34 import org.openide.filesystems.FileObject; 35 import org.openide.filesystems.FileUtil; 36 import org.openide.filesystems.URLMapper; 37 38 42 public class SourceFileManager implements JavaFileManager { 43 44 private final ClassPath sourceRoots; 45 private final boolean ignoreExcludes; 46 47 48 public SourceFileManager (final ClassPath sourceRoots, final boolean ignoreExcludes) { 49 this.sourceRoots = sourceRoots; 50 this.ignoreExcludes = ignoreExcludes; 51 } 52 53 public List <JavaFileObject> list(final Location l, final String packageName, final Set <JavaFileObject.Kind> kinds, final boolean recursive) { 54 List <JavaFileObject> result = new ArrayList <JavaFileObject> (); 56 String _name = packageName.replace('.','/'); if (_name.length() != 0) { 58 _name+='/'; } 60 for (ClassPath.Entry entry : this.sourceRoots.entries()) { 61 if (ignoreExcludes || entry.includes(_name)) { 62 FileObject root = entry.getRoot(); 63 if (root != null) { 64 FileObject tmpFile = root.getFileObject(_name); 65 if (tmpFile != null && tmpFile.isFolder()) { 66 Enumeration <? extends FileObject> files = tmpFile.getChildren (recursive); 67 while (files.hasMoreElements()) { 68 FileObject file = files.nextElement(); 69 if (ignoreExcludes || entry.includes(file)) { 70 JavaFileObject.Kind kind; 71 final String ext = file.getExt(); 72 if (FileObjects.JAVA.equalsIgnoreCase(ext)) { 73 kind = JavaFileObject.Kind.SOURCE; 74 } 75 else if (FileObjects.CLASS.equalsIgnoreCase(ext) || "sig".equalsIgnoreCase(ext)) { 76 kind = JavaFileObject.Kind.CLASS; 77 } 78 else if (FileObjects.HTML.equalsIgnoreCase(ext)) { 79 kind = JavaFileObject.Kind.HTML; 80 } 81 else { 82 kind = JavaFileObject.Kind.OTHER; 83 } 84 if (kinds.contains(kind)) { 85 result.add (SourceFileObject.create(file)); 86 } 87 } 88 } 89 } 90 } 91 } 92 } 93 return result; 94 } 95 96 public javax.tools.FileObject getFileForInput (final Location l, final String pkgName, final String relativeName) { 97 String rp = FileObjects.getRelativePath (pkgName, relativeName); 98 for (ClassPath.Entry entry : this.sourceRoots.entries()) { 99 if (ignoreExcludes || entry.includes(rp)) { 100 FileObject root = entry.getRoot(); 101 if (root != null) { 102 FileObject file = root.getFileObject(rp); 103 if (file != null) { 104 return SourceFileObject.create (file); 105 } 106 } 107 } 108 } 109 return null; 110 } 111 112 public JavaFileObject getJavaFileForInput (Location l, final String className, JavaFileObject.Kind kind) { 113 String [] namePair = FileObjects.getParentRelativePathAndName (className); 114 if (namePair == null) { 115 return null; 116 } 117 String ext = kind == JavaFileObject.Kind.CLASS ? "sig" : kind.extension.substring(1); for (ClassPath.Entry entry : this.sourceRoots.entries()) { 119 FileObject root = entry.getRoot(); 120 if (root != null) { 121 FileObject parent = root.getFileObject(namePair[0]); 122 if (parent != null) { 123 FileObject[] children = parent.getChildren(); 124 for (FileObject child : children) { 125 if (namePair[1].equals(child.getName()) && ext.equalsIgnoreCase(child.getExt()) && (ignoreExcludes || entry.includes(child))) { 126 return SourceFileObject.create (child); 127 } 128 } 129 } 130 } 131 } 132 return null; 133 } 134 135 public javax.tools.FileObject getFileForOutput(Location l, String pkgName, String relativeName, javax.tools.FileObject sibling) 136 throws IOException , UnsupportedOperationException , IllegalArgumentException { 137 throw new UnsupportedOperationException ("The SourceFileManager does not support write operations."); } 139 140 public JavaFileObject getJavaFileForOutput (Location l, String className, JavaFileObject.Kind kind, javax.tools.FileObject sibling) 141 throws IOException , UnsupportedOperationException , IllegalArgumentException { 142 throw new UnsupportedOperationException ("The SourceFileManager does not support write operations."); } 144 145 public void flush() throws java.io.IOException { 146 } 148 149 public void close() throws java.io.IOException { 150 } 152 153 public int isSupportedOption(String string) { 154 return -1; 155 } 156 157 public boolean handleOption (final String head, final Iterator <String > tail) { 158 return false; 159 } 160 161 public boolean hasLocation(Location location) { 162 return true; 163 } 164 165 public ClassLoader getClassLoader (Location l) { 166 return null; 167 } 168 169 public String inferBinaryName (final Location l, final JavaFileObject jfo) { 170 try { 171 FileObject fo; 172 if (jfo instanceof SourceFileObject) { 173 fo = ((SourceFileObject)jfo).file; 174 } 175 else { 176 fo = URLMapper.findFileObject(jfo.toUri().toURL()); 178 } 179 for (FileObject root : this.sourceRoots.getRoots()) { 180 if (FileUtil.isParentOf(root,fo)) { 181 String relativePath = FileUtil.getRelativePath(root,fo); 182 int index = relativePath.lastIndexOf('.'); 183 assert index > 0; 184 final String result = relativePath.substring(0,index).replace('/','.'); 185 return result; 186 } 187 } 188 } catch (MalformedURLException e) { 189 ErrorManager.getDefault().notify(e); 190 } 191 return null; 192 } 193 194 public boolean isSameFile(javax.tools.FileObject fileObject, javax.tools.FileObject fileObject0) { 195 return fileObject instanceof SourceFileObject 196 && fileObject0 instanceof SourceFileObject 197 && ((SourceFileObject)fileObject).file == ((SourceFileObject)fileObject0).file; 198 } 199 } 200 | Popular Tags |