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.Iterator ; 27 import java.util.List ; 28 import javax.swing.event.ChangeListener ; 29 import org.netbeans.api.java.queries.JavadocForBinaryQuery; 30 import org.netbeans.modules.ant.freeform.spi.support.Util; 31 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation; 32 import org.netbeans.spi.project.AuxiliaryConfiguration; 33 import org.netbeans.spi.project.support.ant.AntProjectHelper; 34 import org.netbeans.spi.project.support.ant.PropertyEvaluator; 35 import org.openide.filesystems.FileUtil; 36 import org.w3c.dom.Element ; 37 38 42 final class JavadocQuery implements JavadocForBinaryQueryImplementation { 43 44 private final AntProjectHelper helper; 45 private final PropertyEvaluator eval; 46 private final AuxiliaryConfiguration aux; 47 48 public JavadocQuery(AntProjectHelper helper, PropertyEvaluator eval, AuxiliaryConfiguration aux) { 49 this.helper = helper; 50 this.eval = eval; 51 this.aux = aux; 52 } 53 54 public JavadocForBinaryQuery.Result findJavadoc(URL binaryRoot) { 55 Element data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true); 56 if (data != null) { 57 for (Element cu : Util.findSubElements(data)) { 58 assert cu.getLocalName().equals("compilation-unit") : cu; 59 boolean rightCU = false; 60 for (Element builtTo : Util.findSubElements(cu)) { 61 if (builtTo.getLocalName().equals("built-to")) { String rawtext = Util.findText(builtTo); 63 assert rawtext != null; 64 String evaltext = eval.evaluate(rawtext); 65 if (evaltext != null) { 66 if (evalTextToURL(evaltext).equals(binaryRoot)) { 67 rightCU = true; 68 break; 69 } 70 } 71 } 72 } 73 if (rightCU) { 74 List <URL > resultURLs = new ArrayList <URL >(); 75 for (Element javadocTo : Util.findSubElements(cu)) { 76 if (javadocTo.getLocalName().equals("javadoc-built-to")) { String rawtext = Util.findText(javadocTo); 78 assert rawtext != null; 79 String evaltext = eval.evaluate(rawtext); 80 if (evaltext != null) { 81 resultURLs.add(evalTextToURL(evaltext)); 82 } 83 } 84 } 85 return new FixedResult(resultURLs); 86 } 87 } 88 } 89 return null; 90 } 91 92 private URL evalTextToURL(String evaltext) { 93 File location = helper.resolveFile(evaltext); 94 URL u; 95 try { 96 u = location.toURI().toURL(); 97 } catch (MalformedURLException e) { 98 throw new AssertionError (e); 99 } 100 if (FileUtil.isArchiveFile(u)) { 101 return FileUtil.getArchiveRoot(u); 102 } else { 103 String us = u.toExternalForm(); 104 if (us.endsWith("/")) { 105 return u; 106 } else { 107 try { 108 return new URL (us + '/'); 109 } catch (MalformedURLException e) { 110 throw new AssertionError (e); 111 } 112 } 113 } 114 } 115 116 private static final class FixedResult implements JavadocForBinaryQuery.Result { 117 118 private final List <URL > urls; 119 120 public FixedResult(List <URL > urls) { 121 this.urls = urls; 122 } 123 124 public URL [] getRoots() { 125 return urls.toArray(new URL [urls.size()]); 126 } 127 128 public void addChangeListener(ChangeListener l) {} 129 130 public void removeChangeListener(ChangeListener l) {} 131 132 } 133 134 } 135 | Popular Tags |