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.URL;
047: import java.util.ArrayList;
048: import java.util.Iterator;
049: import java.util.List;
050: import javax.swing.event.ChangeListener;
051: import org.netbeans.api.java.queries.JavadocForBinaryQuery;
052: import org.netbeans.modules.apisupport.project.NbModuleProject;
053: import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
054: import org.netbeans.modules.apisupport.project.Util;
055: import org.netbeans.modules.apisupport.project.universe.NbPlatform;
056: import org.netbeans.spi.java.queries.JavadocForBinaryQueryImplementation;
057: import org.openide.filesystems.URLMapper;
058:
059: /**
060: * Defines Javadoc locations for built modules.
061: * @author Jesse Glick
062: */
063: public final class JavadocForBinaryImpl implements
064: JavadocForBinaryQueryImplementation {
065:
066: private static final String NB_ALL_INFIX = "nbbuild"
067: + File.separatorChar + "build" + File.separatorChar
068: + "javadoc" + File.separatorChar; // NOI18N
069: private static final String EXT_INFIX = "build"
070: + File.separatorChar + "javadoc" + File.separatorChar; // NOI18N
071:
072: /** Configurable for the unit test, since it is too cumbersome to create fake Javadoc in all the right places. */
073: static boolean ignoreNonexistentRoots = true;
074:
075: private final NbModuleProject project;
076:
077: public JavadocForBinaryImpl(NbModuleProject project) {
078: this .project = project;
079: }
080:
081: public JavadocForBinaryQuery.Result findJavadoc(URL binaryRoot) {
082: if (!binaryRoot.equals(Util.urlForJar(project
083: .getModuleJarLocation()))) {
084: return null;
085: }
086: String cnb = project.getCodeNameBase();
087: if (cnb == null) { // #115521
088: return null;
089: }
090: String cnbdashes = cnb.replace('.', '-');
091: try {
092: final List<URL> candidates = new ArrayList<URL>();
093: NbPlatform platform = project.getPlatform(false);
094: if (platform == null) {
095: return null;
096: }
097: for (URL root : platform.getJavadocRoots()) {
098: candidates.add(new URL(root, cnbdashes + "/")); // NOI18N
099: }
100: File dir;
101: NbModuleProvider.NbModuleType type = Util
102: .getModuleType(project);
103: if (type == NbModuleProvider.NETBEANS_ORG) {
104: dir = project.getNbrootFile(NB_ALL_INFIX + cnbdashes);
105: } else {
106: dir = new File(project.getProjectDirectoryFile(),
107: EXT_INFIX + cnbdashes);
108: }
109: if (dir != null) { // #118491
110: candidates.add(Util.urlForDir(dir));
111: }
112: if (ignoreNonexistentRoots) {
113: Iterator<URL> it = candidates.iterator();
114: while (it.hasNext()) {
115: URL u = it.next();
116: if (URLMapper.findFileObject(u) == null) {
117: it.remove();
118: }
119: }
120: }
121: return new R(candidates.toArray(new URL[candidates.size()]));
122: } catch (MalformedURLException e) {
123: throw new AssertionError(e);
124: }
125: }
126:
127: private static final class R implements
128: JavadocForBinaryQuery.Result {
129: private final URL[] roots;
130:
131: public R(URL[] roots) {
132: this .roots = roots;
133: }
134:
135: public URL[] getRoots() {
136: return roots;
137: }
138:
139: public void addChangeListener(ChangeListener l) {
140: }
141:
142: public void removeChangeListener(ChangeListener l) {
143: }
144: }
145:
146: }
|