KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > apisupport > project > queries > GlobalJavadocForBinaryImpl


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.apisupport.project.queries;
21
22 import java.io.File JavaDoc;
23 import java.net.MalformedURLException JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.Arrays JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import javax.swing.event.ChangeListener JavaDoc;
31 import org.netbeans.api.java.queries.JavadocForBinaryQuery;
32 import org.netbeans.modules.apisupport.project.Util;
33 import org.netbeans.modules.apisupport.project.universe.NbPlatform;
34 import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
35 import org.openide.filesystems.FileUtil;
36 import org.openide.filesystems.URLMapper;
37 import org.openide.modules.InstalledFileLocator;
38
39 /**
40  * Able to find javadoc in the appropriate NbPlatform for the given URL.
41  *
42  * @author Jesse Glick, Martin Krauskopf
43  */

44 public final class GlobalJavadocForBinaryImpl implements JavadocForBinaryQueryImplementation {
45     
46     public JavadocForBinaryQuery.Result findJavadoc(URL JavaDoc binaryRoot) {
47         try {
48             if (!binaryRoot.getProtocol().equals("jar")) { // NOI18N
49
// XXX probably shouldn't just return null in this case
50
Util.err.log(binaryRoot + " is not an archive file."); // NOI18N
51
return null;
52             }
53             URL JavaDoc jar = FileUtil.getArchiveFile(binaryRoot);
54             if (!jar.getProtocol().equals("file")) { // NOI18N
55
Util.err.log(binaryRoot + " is not an archive file."); // NOI18N
56
return null;
57             }
58             if (jar.toExternalForm().endsWith("/xtest/lib/junit.jar")) { // NOI18N
59
// #68685 hack - associate reasonable Javadoc with XTest's version of junit
60
File JavaDoc f = InstalledFileLocator.getDefault().locate("modules/ext/junit-3.8.2.jar", "org.netbeans.modules.junit", false); // NOI18N
61
if (f == null) {
62                     // For compat with NB 5.0.
63
f = InstalledFileLocator.getDefault().locate("modules/ext/junit-3.8.1.jar", "org.netbeans.modules.junit", false); // NOI18N
64
}
65                 if (f != null) {
66                     return JavadocForBinaryQuery.findJavadoc(FileUtil.getArchiveRoot(f.toURI().toURL()));
67                 }
68             }
69             File JavaDoc binaryRootF = new File JavaDoc(URI.create(jar.toExternalForm()));
70             NbPlatform supposedPlaf = null;
71             for (Iterator JavaDoc it = NbPlatform.getPlatforms().iterator(); it.hasNext(); ) {
72                 NbPlatform plaf = (NbPlatform) it.next();
73                 if (binaryRootF.getAbsolutePath().startsWith(plaf.getDestDir().getAbsolutePath())) {
74                     supposedPlaf = plaf;
75                     break;
76                 }
77             }
78             if (supposedPlaf == null) {
79                 Util.err.log(binaryRootF + " does not correspond to a known platform"); // NOI18N
80
return null;
81             }
82             // XXX this will only work for modules following regular naming conventions:
83
String JavaDoc n = binaryRootF.getName();
84             if (!n.endsWith(".jar")) { // NOI18N
85
Util.err.log(binaryRootF + " is not a *.jar"); // NOI18N
86
return null;
87             }
88             String JavaDoc cnbdashes = n.substring(0, n.length() - 4);
89             final List JavaDoc<URL JavaDoc> candidates = new ArrayList JavaDoc();
90             URL JavaDoc[] roots = supposedPlaf.getJavadocRoots();
91             Util.err.log("Platform in " + supposedPlaf.getDestDir() + " claimed to have Javadoc roots " + Arrays.asList(roots));
92             for (int i = 0; i < roots.length; i++) {
93                 // 1. user may insert directly e.g ...nbbuild/build/javadoc/org-openide-actions[.zip]
94
candidates.add(roots[i]);
95                 // 2. or whole bunch of javadocs e.g. ...nbbuild/build/javadoc/
96
candidates.add(new URL JavaDoc(roots[i], cnbdashes + '/'));
97             }
98             Iterator JavaDoc it = candidates.iterator();
99             while (it.hasNext()) {
100                 URL JavaDoc u = (URL JavaDoc) it.next();
101                 if (URLMapper.findFileObject(u) == null) {
102                     Util.err.log("No such Javadoc candidate URL " + u);
103                     it.remove();
104                 }
105             }
106             return new JavadocForBinaryQuery.Result() {
107                 public URL JavaDoc[] getRoots() {
108                     return (URL JavaDoc[]) candidates.toArray(new URL JavaDoc[candidates.size()]);
109                 }
110                 public void addChangeListener(ChangeListener JavaDoc l) {}
111                 public void removeChangeListener(ChangeListener JavaDoc l) {}
112             };
113         } catch (MalformedURLException JavaDoc e) {
114             throw new AssertionError JavaDoc(e);
115         }
116     }
117     
118 }
119
Popular Tags