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.gsfpath.api.queries;
043:
044: import java.net.URL;
045: import java.util.Arrays;
046: import javax.swing.event.ChangeListener;
047: import org.netbeans.modules.gsfpath.spi.queries.JavadocForBinaryQueryImplementation;
048: import org.openide.ErrorManager;
049: import org.openide.filesystems.FileUtil;
050: import org.openide.util.Lookup;
051:
052: /**
053: * A query to find Javadoc root for the given classpath root.
054: * @author David Konecny, Jesse Glick
055: * @since org.netbeans.modules.gsfpath.api/1 1.4
056: */
057: public class JavadocForBinaryQuery {
058:
059: private static final ErrorManager ERR = ErrorManager.getDefault()
060: .getInstance(JavadocForBinaryQuery.class.getName());
061:
062: private static final Lookup.Result<? extends JavadocForBinaryQueryImplementation> implementations = Lookup
063: .getDefault().lookupResult(
064: JavadocForBinaryQueryImplementation.class);
065:
066: private JavadocForBinaryQuery() {
067: }
068:
069: /**
070: * Find Javadoc information for a classpath root containing Java classes.
071: * <p>
072: * These methods calls findJavadoc method on the JavadocForBinaryQueryImplementation
073: * instances registered in the lookup until null result is returned for given binaryRoot. The
074: * non null result is returned.
075: * </p>
076: * @param binary URL of a classpath root
077: * @return a result object encapsulating the answer (never null)
078: */
079: public static Result findJavadoc(URL binary) {
080: if (FileUtil.isArchiveFile(binary)) {
081: throw new IllegalArgumentException("File URL pointing to "
082: + // NOI18N
083: "JAR is not valid classpath entry. Use jar: URL. Was: "
084: + binary); // NOI18N
085: }
086: boolean log = ERR.isLoggable(ErrorManager.INFORMATIONAL);
087: if (log)
088: ERR.log("JFBQ.findJavadoc: " + binary);
089: for (JavadocForBinaryQueryImplementation impl : implementations
090: .allInstances()) {
091: Result r = impl.findJavadoc(binary);
092: if (r != null) {
093: if (log)
094: ERR.log(" got result "
095: + Arrays.asList(r.getRoots()) + " from "
096: + impl);
097: return r;
098: } else {
099: if (log)
100: ERR.log(" got no result from " + impl);
101: }
102: }
103: if (log)
104: ERR.log(" got no results from any impl");
105: return EMPTY_RESULT;
106: }
107:
108: /**
109: * Result of finding Javadoc, encapsulating the answer as well as the
110: * ability to listen to it.
111: */
112: public interface Result {
113:
114: /**
115: * Get the Javadoc roots.
116: * Each root should contain the main <code>index.html</code>, so that
117: * for a class <samp>pkg.Class</samp> the generated documentation would
118: * have a path <samp>pkg/Class.html</samp> relative to one of the roots.
119: * @return array of roots of Javadoc documentation (may be empty but not null)
120: */
121: URL[] getRoots();
122:
123: /**
124: * Add a listener to changes in the roots.
125: * @param l a listener to add
126: */
127: void addChangeListener(ChangeListener l);
128:
129: /**
130: * Remove a listener to changes in the roots.
131: * @param l a listener to remove
132: */
133: void removeChangeListener(ChangeListener l);
134:
135: }
136:
137: private static final Result EMPTY_RESULT = new EmptyResult();
138:
139: private static final class EmptyResult implements Result {
140: private static final URL[] NO_ROOTS = new URL[0];
141:
142: EmptyResult() {
143: }
144:
145: public URL[] getRoots() {
146: return NO_ROOTS;
147: }
148:
149: public void addChangeListener(ChangeListener l) {
150: }
151:
152: public void removeChangeListener(ChangeListener l) {
153: }
154: }
155:
156: }
|