001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.apisupport.project.queries;
043:
044: import java.io.File;
045: import java.net.MalformedURLException;
046: import java.net.URI;
047: import java.net.URISyntaxException;
048: import java.net.URL;
049: import java.util.ArrayList;
050: import java.util.Arrays;
051: import java.util.Iterator;
052: import java.util.List;
053: import javax.swing.event.ChangeListener;
054: import org.netbeans.api.java.queries.JavadocForBinaryQuery;
055: import org.netbeans.api.java.queries.JavadocForBinaryQuery.Result;
056: import org.netbeans.api.project.FileOwnerQuery;
057: import org.netbeans.api.project.Project;
058: import org.netbeans.modules.apisupport.project.NbModuleProject;
059: import org.netbeans.modules.apisupport.project.Util;
060: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
061: import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
062: import org.openide.filesystems.FileUtil;
063: import org.openide.filesystems.URLMapper;
064: import org.openide.modules.InstalledFileLocator;
065:
066: /**
067: * Able to find Javadoc in the appropriate NbPlatform for the given URL.
068: *
069: * @author Jesse Glick, Martin Krauskopf
070: */
071: public final class GlobalJavadocForBinaryImpl implements
072: JavadocForBinaryQueryImplementation {
073:
074: public JavadocForBinaryQuery.Result findJavadoc(final URL root) {
075: try {
076: if (root.getProtocol().equals("jar")) { // NOI18N
077: return findForBinaryRoot(root);
078: } else {
079: return findForSourceRoot(root);
080: }
081: } catch (MalformedURLException e) {
082: throw new AssertionError(e);
083: }
084: }
085:
086: private Result findForBinaryRoot(final URL binaryRoot)
087: throws MalformedURLException, MalformedURLException {
088: URL jar = FileUtil.getArchiveFile(binaryRoot);
089: if (!jar.getProtocol().equals("file")) { // NOI18N
090: Util.err.log(binaryRoot + " is not an archive file."); // NOI18N
091: return null;
092: }
093: if (jar.toExternalForm().endsWith("/xtest/lib/junit.jar")) { // NOI18N
094: // #68685 hack - associate reasonable Javadoc with XTest's version of junit
095: File f = InstalledFileLocator.getDefault().locate(
096: "modules/ext/junit-3.8.2.jar",
097: "org.netbeans.modules.junit", false); // NOI18N
098: if (f == null) {
099: // For compat with NB 5.0.
100: f = InstalledFileLocator.getDefault().locate(
101: "modules/ext/junit-3.8.1.jar",
102: "org.netbeans.modules.junit", false); // NOI18N
103: }
104: if (f != null) {
105: return JavadocForBinaryQuery.findJavadoc(FileUtil
106: .getArchiveRoot(f.toURI().toURL()));
107: }
108: }
109: File binaryRootF = new File(URI.create(jar.toExternalForm()));
110: // XXX this will only work for modules following regular naming conventions:
111: String n = binaryRootF.getName();
112: if (!n.endsWith(".jar")) { // NOI18N
113: Util.err.log(binaryRootF + " is not a *.jar"); // NOI18N
114: return null;
115: }
116: String cnbdashes = n.substring(0, n.length() - 4);
117: NbPlatform supposedPlaf = null;
118: for (NbPlatform plaf : NbPlatform.getPlatforms()) {
119: if (binaryRootF.getAbsolutePath().startsWith(
120: plaf.getDestDir().getAbsolutePath())) {
121: supposedPlaf = plaf;
122: break;
123: }
124: }
125: if (supposedPlaf == null) {
126: Util.err.log(binaryRootF
127: + " does not correspond to a known platform"); // NOI18N
128: return null;
129: }
130: return findByDashedCNB(cnbdashes, supposedPlaf);
131: }
132:
133: /**
134: * Go through all registered platforms and tries to find Javadoc for the
135: * given URL.
136: * <p>
137: * <em>TODO</em>: ideally should check module, or at least cluster, version.
138: */
139: private Result findForSourceRoot(final URL root)
140: throws MalformedURLException {
141: Project p;
142: try {
143: p = FileOwnerQuery.getOwner(root.toURI());
144: } catch (URISyntaxException e) {
145: throw new AssertionError(e);
146: }
147: if (p != null) {
148: NbModuleProject module = p.getLookup().lookup(
149: NbModuleProject.class);
150: if (module != null) {
151: String cnb = module.getCodeNameBase();
152: for (NbPlatform plaf : NbPlatform.getPlatforms()) {
153: Result r = findByDashedCNB(cnb.replace('.', '-'),
154: plaf);
155: if (r != null) {
156: return r;
157: }
158: }
159: }
160: }
161: return null;
162: }
163:
164: private Result findByDashedCNB(final String cnbdashes,
165: final NbPlatform plaf) throws MalformedURLException {
166: final List<URL> candidates = new ArrayList<URL>();
167: URL[] roots = plaf.getJavadocRoots();
168: Util.err.log("Platform in " + plaf.getDestDir()
169: + " claimed to have Javadoc roots "
170: + Arrays.asList(roots));
171: for (URL root : roots) {
172: // XXX: so should be checked, instead of always adding both?
173: // 1. user may insert directly e.g ...nbbuild/build/javadoc/org-openide-actions[.zip]
174: candidates.add(root);
175: // 2. or whole bunch of javadocs e.g. ...nbbuild/build/javadoc/
176: candidates.add(new URL(root, cnbdashes + '/'));
177: }
178: Iterator<URL> it = candidates.iterator();
179: while (it.hasNext()) {
180: URL u = it.next();
181: if (URLMapper.findFileObject(u) == null) {
182: Util.err.log("No such Javadoc candidate URL " + u);
183: it.remove();
184: }
185: }
186: if (candidates.isEmpty()) {
187: return null;
188: }
189: return new JavadocForBinaryQuery.Result() {
190: public URL[] getRoots() {
191: return candidates.toArray(new URL[candidates.size()]);
192: }
193:
194: public void addChangeListener(ChangeListener l) {
195: }
196:
197: public void removeChangeListener(ChangeListener l) {
198: }
199: };
200: }
201:
202: }
|