1 19 20 package org.netbeans.modules.apisupport.project.queries; 21 22 import java.io.File ; 23 import java.net.URI ; 24 import java.net.URL ; 25 import java.util.Arrays ; 26 import java.util.Map ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import javax.swing.event.ChangeListener ; 30 import org.netbeans.api.java.queries.SourceForBinaryQuery; 31 import org.netbeans.modules.apisupport.project.NbModuleProject; 32 import org.netbeans.modules.apisupport.project.Util; 33 import org.netbeans.modules.apisupport.project.universe.TestEntry; 34 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 35 import org.netbeans.spi.project.support.ant.PropertyUtils; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileUtil; 38 import org.w3c.dom.Element ; 39 40 44 public final class SourceForBinaryImpl implements SourceForBinaryQueryImplementation { 45 46 private final NbModuleProject project; 47 private String clusterPath; 48 private URL classesUrl; 49 private URL testClassesUrl; 50 private Map <URL , SourceForBinaryQuery.Result> cache = new HashMap (); 51 52 public SourceForBinaryImpl(NbModuleProject project) { 53 this.project = project; 54 } 55 56 public SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 57 SourceForBinaryQuery.Result res = (SourceForBinaryQuery.Result) cache.get(binaryRoot); 59 if (res == null) { 60 URL binaryJar = FileUtil.getArchiveFile(binaryRoot); 61 if (binaryJar != null) { 62 File binaryJarF = new File (URI.create(binaryJar.toExternalForm())); 63 FileObject srcDir = null; 64 if (binaryJarF.getAbsolutePath().endsWith(getModuleJarClusterPath())) { 65 srcDir = project.getSourceDirectory(); 66 } else { 67 TestEntry entry = TestEntry.get(binaryJarF); 69 if (entry != null && project.getCodeNameBase().equals(entry.getCodeNameBase())) { 70 srcDir = ( entry.isUnit() ) ? project.getTestSourceDirectory() : 71 project.getFunctionalTestSourceDirectory(); 72 } 73 } 74 if (srcDir != null) { 75 res = new Result(new FileObject[] {srcDir}); 76 return res; 77 } 78 } 79 if (binaryRoot.equals(getClassesUrl())) { 80 FileObject srcDir = project.getSourceDirectory(); 81 if (srcDir != null) { 82 res = new Result(new FileObject[] {srcDir}); 83 } 84 } else if (binaryRoot.equals(getTestClassesUrl())) { 85 FileObject testSrcDir = project.getTestSourceDirectory(); 86 if (testSrcDir != null) { 87 res = new Result (new FileObject[] {testSrcDir}); 88 } 89 } else { 90 Iterator <Map.Entry <FileObject,Element >> ecus = project.getExtraCompilationUnits().entrySet().iterator(); 92 ECUS: while (ecus.hasNext()) { 93 Map.Entry entry = (Map.Entry ) ecus.next(); 94 Element pkgrootEl = (Element ) entry.getValue(); 95 Iterator <Element > pkgrootKids = Util.findSubElements(pkgrootEl).iterator(); 96 while (pkgrootKids.hasNext()) { 97 Element kid = (Element ) pkgrootKids.next(); 98 if (!kid.getLocalName().equals("built-to")) { continue; 100 } 101 String rawtext = Util.findText(kid); 102 assert rawtext != null : "Null content for <built-to> in " + project; 103 String text = project.evaluator().evaluate(rawtext); 104 if (text == null) { 105 continue; 106 } 107 File loc = project.getHelper().resolveFile(text); 108 URL u = Util.urlForDirOrJar(loc); 109 if (u.equals(binaryRoot)) { 110 res = new Result(new FileObject[] {(FileObject) entry.getKey()}); 111 break ECUS; 112 } 113 } 114 } 115 } 116 if (res != null) { 117 this.cache.put(binaryRoot,res); 118 } 119 } 120 return res; 121 } 122 123 private String getModuleJarClusterPath() { 124 if (clusterPath == null) { File cluster = project.getHelper().resolveFile(project.evaluator().evaluate("${cluster}")); clusterPath = PropertyUtils.relativizeFile(cluster.getParentFile(), 127 project.getModuleJarLocation()).replace('/', File.separatorChar); 128 } 129 return clusterPath; 130 } 131 132 private URL getClassesUrl() { 133 if (classesUrl == null) { 134 File classesDir = project.getClassesDirectory(); 135 classesUrl = Util.urlForDir(classesDir); 136 } 137 return classesUrl; 138 } 139 140 private URL getTestClassesUrl() { 141 if (testClassesUrl == null && project.supportsUnitTests()) { 142 File testClassesDir = project.getTestClassesDirectory(); 143 testClassesUrl = Util.urlForDir(testClassesDir); 144 } 145 return testClassesUrl; 146 } 147 148 149 private static class Result implements SourceForBinaryQuery.Result { 150 151 private FileObject[] res; 152 153 public Result (FileObject[] res) { 154 this.res = res; 155 assert res != null && !Arrays.asList(res).contains(null); 156 } 157 158 public FileObject[] getRoots () { 159 return this.res; 160 } 161 162 public void addChangeListener (ChangeListener l) { 163 } 165 166 public void removeChangeListener (ChangeListener l) { 167 } 169 170 } 171 172 } 173 | Popular Tags |