KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > freeform > JavadocQuery


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.freeform;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URL JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.List JavaDoc;
28 import javax.swing.event.ChangeListener JavaDoc;
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 JavaDoc;
37
38 /**
39  * Handles Javadoc information.
40  * @author Jesse Glick
41  */

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 JavaDoc binaryRoot) {
55         Element JavaDoc data = aux.getConfigurationFragment(JavaProjectNature.EL_JAVA, JavaProjectNature.NS_JAVA_2, true);
56         if (data != null) {
57             for (Element JavaDoc cu : Util.findSubElements(data)) {
58                 assert cu.getLocalName().equals("compilation-unit") : cu;
59                 boolean rightCU = false;
60                 for (Element JavaDoc builtTo : Util.findSubElements(cu)) {
61                     if (builtTo.getLocalName().equals("built-to")) { // NOI18N
62
String JavaDoc rawtext = Util.findText(builtTo);
63                         assert rawtext != null;
64                         String JavaDoc 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 JavaDoc<URL JavaDoc> resultURLs = new ArrayList JavaDoc<URL JavaDoc>();
75                     for (Element JavaDoc javadocTo : Util.findSubElements(cu)) {
76                         if (javadocTo.getLocalName().equals("javadoc-built-to")) { // NOI18N
77
String JavaDoc rawtext = Util.findText(javadocTo);
78                             assert rawtext != null;
79                             String JavaDoc 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 JavaDoc evalTextToURL(String JavaDoc evaltext) {
93         File JavaDoc location = helper.resolveFile(evaltext);
94         URL JavaDoc u;
95         try {
96             u = location.toURI().toURL();
97         } catch (MalformedURLException JavaDoc e) {
98             throw new AssertionError JavaDoc(e);
99         }
100         if (FileUtil.isArchiveFile(u)) {
101             return FileUtil.getArchiveRoot(u);
102         } else {
103             String JavaDoc us = u.toExternalForm();
104             if (us.endsWith("/")) {
105                 return u;
106             } else {
107                 try {
108                     return new URL JavaDoc(us + '/');
109                 } catch (MalformedURLException JavaDoc e) {
110                     throw new AssertionError JavaDoc(e);
111                 }
112             }
113         }
114     }
115     
116     private static final class FixedResult implements JavadocForBinaryQuery.Result {
117         
118         private final List JavaDoc<URL JavaDoc> urls;
119         
120         public FixedResult(List JavaDoc<URL JavaDoc> urls) {
121             this.urls = urls;
122         }
123
124         public URL JavaDoc[] getRoots() {
125             return urls.toArray(new URL JavaDoc[urls.size()]);
126         }
127         
128         public void addChangeListener(ChangeListener JavaDoc l) {}
129
130         public void removeChangeListener(ChangeListener JavaDoc l) {}
131
132     }
133     
134 }
135
Popular Tags