duke@1: /* jjg@1357: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.doclets.internal.toolkit.util; duke@1: jjg@1357: import java.io.*; jjg@1357: import java.net.*; jjg@1357: import java.util.HashMap; jjg@1357: import java.util.Map; duke@1: duke@1: import com.sun.javadoc.*; jjg@1357: import com.sun.tools.doclets.internal.toolkit.*; duke@1: duke@1: /** duke@1: * Process and manage "-link" and "-linkoffline" to external packages. The duke@1: * options "-link" and "-linkoffline" both depend on the fact that Javadoc now duke@1: * generates "package-list"(lists all the packages which are getting duke@1: * documented) file in the current or the destination directory, while duke@1: * generating the documentation. duke@1: * jjg@1359: *

This is NOT part of any supported API. jjg@1359: * If you write code that depends on this, you do so at your own risk. jjg@1359: * This code and its internal interfaces are subject to change or jjg@1359: * deletion without notice. duke@1: * duke@1: * @author Atul M Dambalkar duke@1: * @author Robert Field duke@1: */ duke@1: public class Extern { duke@1: duke@1: /** duke@1: * Map package names onto Extern Item objects. duke@1: * Lazily initialized. duke@1: */ jjg@74: private Map packageToItemMap; duke@1: duke@1: /** duke@1: * The global configuration information for this run. duke@1: */ duke@1: private final Configuration configuration; duke@1: duke@1: /** duke@1: * True if we are using -linkoffline and false if -link is used instead. duke@1: */ duke@1: private boolean linkoffline = false; duke@1: duke@1: /** duke@1: * Stores the info for one external doc set duke@1: */ duke@1: private class Item { duke@1: duke@1: /** duke@1: * Package name, found in the "package-list" file in the {@link path}. duke@1: */ duke@1: final String packageName; duke@1: duke@1: /** duke@1: * The URL or the directory path at which the package documentation will be duke@1: * avaliable. duke@1: */ duke@1: final String path; duke@1: duke@1: /** duke@1: * If given path is directory path then true else if it is a URL then false. duke@1: */ duke@1: final boolean relative; duke@1: duke@1: /** duke@1: * Constructor to build a Extern Item object and map it with the package name. duke@1: * If the same package name is found in the map, then the first mapped duke@1: * Item object or offline location will be retained. duke@1: * jjg@1358: * @param packageName Package name found in the "package-list" file. duke@1: * @param path URL or Directory path from where the "package-list" duke@1: * file is picked. duke@1: * @param relative True if path is URL, false if directory path. duke@1: */ duke@1: Item(String packageName, String path, boolean relative) { duke@1: this.packageName = packageName; duke@1: this.path = path; duke@1: this.relative = relative; duke@1: if (packageToItemMap == null) { jjg@74: packageToItemMap = new HashMap(); duke@1: } duke@1: if (!packageToItemMap.containsKey(packageName)) { // save the previous duke@1: packageToItemMap.put(packageName, this); // mapped location duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * String representation of "this" with packagename and the path. duke@1: */ duke@1: public String toString() { duke@1: return packageName + (relative? " -> " : " => ") + path; duke@1: } duke@1: } duke@1: duke@1: public Extern(Configuration configuration) { duke@1: this.configuration = configuration; duke@1: } duke@1: duke@1: /** duke@1: * Determine if a doc item is externally documented. duke@1: * duke@1: * @param doc A ProgramElementDoc. duke@1: */ duke@1: public boolean isExternal(ProgramElementDoc doc) { duke@1: if (packageToItemMap == null) { duke@1: return false; duke@1: } duke@1: return packageToItemMap.get(doc.containingPackage().name()) != null; duke@1: } duke@1: duke@1: /** duke@1: * Convert a link to be an external link if appropriate. duke@1: * duke@1: * @param pkgName The package name. duke@1: * @param relativepath The relative path. duke@1: * @param link The link to convert. duke@1: * @return if external return converted link else return null duke@1: */ duke@1: public String getExternalLink(String pkgName, duke@1: String relativepath, String link) { duke@1: Item fnd = findPackageItem(pkgName); duke@1: if (fnd != null) { duke@1: String externlink = fnd.path + link; duke@1: if (fnd.relative) { // it's a relative path. duke@1: return relativepath + externlink; duke@1: } else { duke@1: return externlink; duke@1: } duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Build the extern package list from given URL or the directory path. duke@1: * Flag error if the "-link" or "-linkoffline" option is already used. duke@1: * duke@1: * @param url URL or Directory path. duke@1: * @param pkglisturl This can be another URL for "package-list" or ordinary duke@1: * file. duke@1: * @param reporter The DocErrorReporter used to report errors. duke@1: * @param linkoffline True if -linkoffline isused and false if -link is used. duke@1: */ duke@1: public boolean url(String url, String pkglisturl, duke@1: DocErrorReporter reporter, boolean linkoffline) { duke@1: this.linkoffline = linkoffline; duke@1: String errMsg = composeExternPackageList(url, pkglisturl); duke@1: if (errMsg != null) { duke@1: reporter.printWarning(errMsg); duke@1: return false; duke@1: } else { duke@1: return true; duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Get the Extern Item object associated with this package name. duke@1: * jjg@1358: * @param pkgName Package name. duke@1: */ duke@1: private Item findPackageItem(String pkgName) { duke@1: if (packageToItemMap == null) { duke@1: return null; duke@1: } jjg@74: return packageToItemMap.get(pkgName); duke@1: } duke@1: duke@1: /** duke@1: * Adjusts the end file separator if it is missing from the URL or the duke@1: * directory path and depending upon the URL or file path, fetch or duke@1: * read the "package-list" file. duke@1: * duke@1: * @param urlOrDirPath URL or the directory path. duke@1: * @param pkgListUrlOrDirPath URL or directory path for the "package-list" file or the "package-list" duke@1: * file itself. duke@1: */ duke@1: private String composeExternPackageList(String urlOrDirPath, String pkgListUrlOrDirPath) { duke@1: urlOrDirPath = adjustEndFileSeparator(urlOrDirPath); duke@1: pkgListUrlOrDirPath = adjustEndFileSeparator(pkgListUrlOrDirPath); duke@1: return isUrl(pkgListUrlOrDirPath) ? duke@1: fetchURLComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath) : duke@1: readFileComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath); duke@1: } duke@1: duke@1: /** duke@1: * If the URL or Directory path is missing end file separator, add that. duke@1: */ duke@1: private String adjustEndFileSeparator(String url) { duke@1: String filesep = "/"; duke@1: if (!url.endsWith(filesep)) { duke@1: url += filesep; duke@1: } duke@1: return url; duke@1: } duke@1: duke@1: /** duke@1: * Fetch the URL and read the "package-list" file. duke@1: * duke@1: * @param urlpath Path to the packages. duke@1: * @param pkglisturlpath URL or the path to the "package-list" file. duke@1: */ duke@1: private String fetchURLComposeExternPackageList(String urlpath, duke@1: String pkglisturlpath) { duke@1: String link = pkglisturlpath + "package-list"; duke@1: try { duke@1: readPackageList((new URL(link)).openStream(), urlpath, false); duke@1: } catch (MalformedURLException exc) { duke@1: return configuration.getText("doclet.MalformedURL", link); duke@1: } catch (IOException exc) { duke@1: return configuration.getText("doclet.URL_error", link); duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Read the "package-list" file which is available locally. duke@1: * duke@1: * @param path URL or directory path to the packages. duke@1: * @param pkgListPath Path to the local "package-list" file. duke@1: */ duke@1: private String readFileComposeExternPackageList(String path, duke@1: String pkgListPath) { duke@1: duke@1: String link = pkgListPath + "package-list"; duke@1: if (! ((new File(pkgListPath)).isAbsolute() || linkoffline)){ duke@1: link = configuration.destDirName + link; duke@1: } duke@1: try { duke@1: File file = new File(link); duke@1: if (file.exists() && file.canRead()) { duke@1: readPackageList(new FileInputStream(file), path, duke@1: ! ((new File(path)).isAbsolute() || isUrl(path))); duke@1: } else { duke@1: return configuration.getText("doclet.File_error", link); duke@1: } duke@1: } catch (FileNotFoundException exc) { duke@1: return configuration.getText("doclet.File_error", link); duke@1: } catch (IOException exc) { duke@1: return configuration.getText("doclet.File_error", link); duke@1: } duke@1: return null; duke@1: } duke@1: duke@1: /** duke@1: * Read the file "package-list" and for each package name found, create duke@1: * Extern object and associate it with the package name in the map. duke@1: * duke@1: * @param input InputStream from the "package-list" file. duke@1: * @param path URL or the directory path to the packages. duke@1: * @param relative Is path relative? duke@1: */ duke@1: private void readPackageList(InputStream input, String path, duke@1: boolean relative) duke@1: throws IOException { duke@1: BufferedReader in = new BufferedReader(new InputStreamReader(input)); jjg@1362: StringBuilder strbuf = new StringBuilder(); duke@1: try { duke@1: int c; duke@1: while ((c = in.read()) >= 0) { duke@1: char ch = (char)c; duke@1: if (ch == '\n' || ch == '\r') { duke@1: if (strbuf.length() > 0) { duke@1: String packname = strbuf.toString(); duke@1: String packpath = path + duke@1: packname.replace('.', '/') + '/'; duke@1: new Item(packname, packpath, relative); duke@1: strbuf.setLength(0); duke@1: } duke@1: } else { duke@1: strbuf.append(ch); duke@1: } duke@1: } duke@1: } finally { duke@1: input.close(); duke@1: } duke@1: } duke@1: duke@1: public boolean isUrl (String urlCandidate) { duke@1: try { duke@1: new URL(urlCandidate); duke@1: //No exception was thrown, so this must really be a URL. duke@1: return true; duke@1: } catch (MalformedURLException e) { duke@1: //Since exception is thrown, this must be a directory path. duke@1: return false; duke@1: } duke@1: } duke@1: }