1 19 20 package org.netbeans.modules.java.freeform; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URL ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 import java.util.Map ; 30 import javax.swing.event.ChangeListener ; 31 import org.netbeans.api.java.queries.SourceForBinaryQuery; 32 import org.netbeans.modules.ant.freeform.spi.support.Util; 33 import org.netbeans.spi.java.queries.SourceForBinaryQueryImplementation; 34 import org.netbeans.spi.project.AuxiliaryConfiguration; 35 import org.netbeans.spi.project.support.ant.AntProjectEvent; 36 import org.netbeans.spi.project.support.ant.AntProjectHelper; 37 import org.netbeans.spi.project.support.ant.AntProjectListener; 38 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 39 import org.openide.filesystems.FileObject; 40 import org.openide.filesystems.FileUtil; 41 import org.w3c.dom.Element ; 42 43 48 final class SourceForBinaryQueryImpl implements SourceForBinaryQueryImplementation, AntProjectListener { 49 50 private AntProjectHelper helper; 51 private PropertyEvaluator evaluator; 52 private AuxiliaryConfiguration aux; 53 54 57 private Map <URL ,FileObject[]> roots = null; 58 59 public SourceForBinaryQueryImpl(AntProjectHelper helper, PropertyEvaluator evaluator, AuxiliaryConfiguration aux) { 60 this.helper = helper; 61 this.evaluator = evaluator; 62 this.aux = aux; 63 helper.addAntProjectListener(this); 64 } 65 66 private void refresh () { 67 roots = null; 68 } 69 70 public synchronized SourceForBinaryQuery.Result findSourceRoots(URL binaryRoot) { 71 if (roots == null) { 72 roots = new HashMap <URL ,FileObject[]>(); 74 Element java = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 75 if (java == null) { 76 return null; 77 } 78 for (Element compilationUnit : Util.findSubElements(java)) { 79 assert compilationUnit.getLocalName().equals("compilation-unit") : compilationUnit; 80 List <URL > binaries = findBinaries(compilationUnit); 81 if (!binaries.isEmpty()) { 82 List <FileObject> packageRoots = Classpaths.findPackageRoots(helper, evaluator, compilationUnit); 83 FileObject[] sources = packageRoots.toArray(new FileObject[packageRoots.size()]); 84 for (URL u : binaries) { 85 FileObject[] orig = roots.get(u); 86 if (orig != null) { 91 FileObject[] merged = new FileObject[orig.length+sources.length]; 92 System.arraycopy(orig, 0, merged, 0, orig.length); 93 System.arraycopy(sources, 0, merged, orig.length, sources.length); 94 sources = merged; 95 } 96 roots.put(u, sources); 97 } 98 } 99 } 100 } 101 assert roots != null; 102 FileObject[] sources = roots.get(binaryRoot); 103 return sources == null ? null : new Result (sources); } 105 106 110 private List <URL > findBinaries(Element compilationUnitEl) { 111 List <URL > binaries = new ArrayList <URL >(); 112 for (Element builtToEl : Util.findSubElements(compilationUnitEl)) { 113 if (!builtToEl.getLocalName().equals("built-to")) { continue; 115 } 116 String text = Util.findText(builtToEl); 117 String textEval = evaluator.evaluate(text); 118 if (textEval == null) { 119 continue; 120 } 121 File buildProduct = helper.resolveFile(textEval); 122 URL buildProductURL; 123 try { 124 buildProductURL = buildProduct.toURI().toURL(); 125 } catch (MalformedURLException e) { 126 assert false : e; 127 continue; 128 } 129 if (FileUtil.isArchiveFile(buildProductURL)) { 130 buildProductURL = FileUtil.getArchiveRoot(buildProductURL); 131 } else { 132 if (!buildProduct.exists() && !buildProductURL.toExternalForm().endsWith("/")) { 136 try { 137 buildProductURL = new URL (buildProductURL.toExternalForm()+"/"); 138 } catch (MalformedURLException e) { 139 assert false : e; 140 } 141 } 142 } 143 binaries.add(buildProductURL); 144 } 145 return binaries; 146 } 147 148 public void configurationXmlChanged(AntProjectEvent ev) { 149 refresh(); 150 } 151 152 public void propertiesChanged(AntProjectEvent ev) { 153 } 155 156 private static class Result implements SourceForBinaryQuery.Result { 157 158 private FileObject[] ret; 159 160 public Result (FileObject[] ret) { 161 this.ret = ret; 162 } 163 164 public FileObject[] getRoots () { 165 return ret; 166 } 167 168 public void addChangeListener (ChangeListener l) { 169 } 171 172 public void removeChangeListener (ChangeListener l) { 173 } 175 176 } 177 178 } 179 | Popular Tags |