1 19 20 package org.netbeans.modules.java.source.parsing; 21 22 import java.io.File ; 23 import java.io.IOException ; 24 import java.net.URI ; 25 import java.net.URL ; 26 import java.util.Iterator ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 import javax.tools.JavaFileObject; 30 import org.netbeans.api.java.classpath.ClassPath; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 34 38 public class OutputFileManager extends CachingFileManager { 39 40 private static boolean debug = Boolean.getBoolean("org.netbeans.modules.java.source.parsing.OutputFileManager.debug"); 42 private ClassPath scp; 43 44 45 public OutputFileManager(CachingArchiveProvider provider, final ClassPath outputClassPath, final ClassPath sourcePath) { 46 super (provider, outputClassPath, false, true); 47 assert sourcePath != null && outputClassPath != null; 48 this.scp = sourcePath; 49 } 50 51 52 public @Override JavaFileObject getJavaFileForOutput( Location l, String className, JavaFileObject.Kind kind, javax.tools.FileObject sibling ) 53 throws IOException , UnsupportedOperationException , IllegalArgumentException { 54 55 56 if (kind != JavaFileObject.Kind.CLASS) { 57 throw new IllegalArgumentException (); 58 } 59 else { 60 int index; 61 if (sibling != null) { 62 index = getActiveRoot (sibling); 63 } 64 else { 65 index = getActiveRoot (FileObjects.convertPackage2Folder(className)); 66 } 67 assert index >= 0 : "class: " + className +" sibling: " + sibling +" srcRoots: " + this.scp + " cacheRoots: " + this.cp; 68 assert index < this.cp.entries().size() : "index "+ index +" class: " + className +" sibling: " + sibling +" srcRoots: " + this.scp + " cacheRoots: " + this.cp; 69 File activeRoot = new File (URI.create(this.cp.entries().get(index).getURL().toExternalForm())); 70 String baseName = className.replace('.', File.separatorChar); String nameStr = baseName + '.' + FileObjects.SIG; 72 int nameComponentIndex = nameStr.lastIndexOf(File.separatorChar); 73 if (nameComponentIndex != -1) { 74 String pathComponent = nameStr.substring(0, nameComponentIndex); 75 new File (activeRoot, pathComponent).mkdirs(); 76 } 77 else { 78 activeRoot.mkdirs(); 79 } 80 File f = FileUtil.normalizeFile(new File (activeRoot, nameStr)); 81 return OutputFileObject.create (activeRoot, f); 82 } 83 } 84 85 public @Override javax.tools.FileObject getFileForOutput( Location l, String pkgName, String relativeName, javax.tools.FileObject sibling ) 86 throws IOException , UnsupportedOperationException , IllegalArgumentException { 87 assert pkgName != null; 88 assert relativeName != null; 89 if (sibling == null) { 90 throw new IllegalArgumentException ("sibling == null"); 91 } 92 final int index = getActiveRoot (sibling); 93 assert index >= 0 && index < this.cp.entries().size(); 94 File activeRoot = new File (URI.create(this.cp.entries().get(index).getURL().toExternalForm())); 95 File folder; 96 if (pkgName.length() == 0) { 97 folder = activeRoot; 98 } 99 else { 100 folder = new File (activeRoot,FileObjects.convertPackage2Folder(pkgName)); 101 } 102 if (!folder.exists()) { 103 if (!folder.mkdirs()) { 104 throw new IOException (); 105 } 106 } 107 File file = new File (folder,relativeName); 108 return OutputFileObject.create (activeRoot,file); 109 } 110 111 112 113 private int getActiveRoot (final javax.tools.FileObject file) throws IOException { 114 if (this.scp.entries().size() == 1) { 115 return 0; 116 } 117 Iterator <ClassPath.Entry> it = this.scp.entries().iterator(); 118 for (int i = 0; it.hasNext(); i++) { 119 URL rootUrl = it.next().getURL(); 120 if (isParentOf(rootUrl, file.toUri().toURL())) { 121 return i; 122 } 123 } 124 return -1; 125 } 126 127 private boolean isParentOf (URL folder, final URL file) throws IOException { 128 assert folder != null && file != null; 129 return file.toExternalForm().startsWith(folder.toExternalForm()); 130 } 131 132 private int getActiveRoot (String baseName) { 133 if (this.scp.entries().size() == 1) { 134 return 0; 135 } 136 String name, parent = null; 137 int index = baseName.lastIndexOf('/'); if (index<0) { 139 name = baseName; 140 } 141 else { 142 parent = baseName.substring(0, index); 143 name = baseName.substring(index+1); 144 } 145 index = name.indexOf('$'); if (index > 0) { 147 name = name.substring(0,index); 148 } 149 Iterator <ClassPath.Entry> it = this.scp.entries().iterator(); 150 for (int i=0; it.hasNext(); i++) { 151 FileObject root = it.next().getRoot(); 152 if (root != null) { 153 FileObject parentFile = root.getFileObject(parent); 154 if (parentFile != null) { 155 if (parentFile.getFileObject(name, FileObjects.JAVA) != null) { 156 return i; 157 } 158 } 159 } 160 } 161 return -1; 162 } 163 164 private static boolean debug (String message) { 165 if (debug) { 166 Logger.getLogger("global").log(Level.INFO, message); 167 } 168 return true; 169 } 170 171 } 172 | Popular Tags |