0N/A/*
553N/A * Copyright (c) 1998, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.doclets.internal.toolkit.util;
0N/A
0N/Aimport com.sun.tools.doclets.internal.toolkit.*;
0N/A
0N/Aimport com.sun.javadoc.*;
0N/Aimport java.util.Map;
0N/Aimport java.util.HashMap;
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/A
0N/A/**
0N/A * Process and manage "-link" and "-linkoffline" to external packages. The
0N/A * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
0N/A * generates "package-list"(lists all the packages which are getting
0N/A * documented) file in the current or the destination directory, while
0N/A * generating the documentation.
0N/A *
0N/A * This code is not part of an API.
0N/A * It is implementation that is subject to change.
0N/A * Do not use it as an API
0N/A *
0N/A * @author Atul M Dambalkar
0N/A * @author Robert Field
0N/A */
0N/Apublic class Extern {
0N/A
0N/A /**
0N/A * Map package names onto Extern Item objects.
0N/A * Lazily initialized.
0N/A */
73N/A private Map<String,Item> packageToItemMap;
0N/A
0N/A /**
0N/A * The global configuration information for this run.
0N/A */
0N/A private final Configuration configuration;
0N/A
0N/A /**
0N/A * True if we are using -linkoffline and false if -link is used instead.
0N/A */
0N/A private boolean linkoffline = false;
0N/A
0N/A /**
0N/A * Stores the info for one external doc set
0N/A */
0N/A private class Item {
0N/A
0N/A /**
0N/A * Package name, found in the "package-list" file in the {@link path}.
0N/A */
0N/A final String packageName;
0N/A
0N/A /**
0N/A * The URL or the directory path at which the package documentation will be
0N/A * avaliable.
0N/A */
0N/A final String path;
0N/A
0N/A /**
0N/A * If given path is directory path then true else if it is a URL then false.
0N/A */
0N/A final boolean relative;
0N/A
0N/A /**
0N/A * Constructor to build a Extern Item object and map it with the package name.
0N/A * If the same package name is found in the map, then the first mapped
0N/A * Item object or offline location will be retained.
0N/A *
0N/A * @param packagename Package name found in the "package-list" file.
0N/A * @param path URL or Directory path from where the "package-list"
0N/A * file is picked.
0N/A * @param relative True if path is URL, false if directory path.
0N/A */
0N/A Item(String packageName, String path, boolean relative) {
0N/A this.packageName = packageName;
0N/A this.path = path;
0N/A this.relative = relative;
0N/A if (packageToItemMap == null) {
73N/A packageToItemMap = new HashMap<String,Item>();
0N/A }
0N/A if (!packageToItemMap.containsKey(packageName)) { // save the previous
0N/A packageToItemMap.put(packageName, this); // mapped location
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * String representation of "this" with packagename and the path.
0N/A */
0N/A public String toString() {
0N/A return packageName + (relative? " -> " : " => ") + path;
0N/A }
0N/A }
0N/A
0N/A public Extern(Configuration configuration) {
0N/A this.configuration = configuration;
0N/A }
0N/A
0N/A /**
0N/A * Determine if a doc item is externally documented.
0N/A *
0N/A * @param doc A ProgramElementDoc.
0N/A */
0N/A public boolean isExternal(ProgramElementDoc doc) {
0N/A if (packageToItemMap == null) {
0N/A return false;
0N/A }
0N/A return packageToItemMap.get(doc.containingPackage().name()) != null;
0N/A }
0N/A
0N/A /**
0N/A * Convert a link to be an external link if appropriate.
0N/A *
0N/A * @param pkgName The package name.
0N/A * @param relativepath The relative path.
0N/A * @param link The link to convert.
0N/A * @return if external return converted link else return null
0N/A */
0N/A public String getExternalLink(String pkgName,
0N/A String relativepath, String link) {
0N/A Item fnd = findPackageItem(pkgName);
0N/A if (fnd != null) {
0N/A String externlink = fnd.path + link;
0N/A if (fnd.relative) { // it's a relative path.
0N/A return relativepath + externlink;
0N/A } else {
0N/A return externlink;
0N/A }
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Build the extern package list from given URL or the directory path.
0N/A * Flag error if the "-link" or "-linkoffline" option is already used.
0N/A *
0N/A * @param url URL or Directory path.
0N/A * @param pkglisturl This can be another URL for "package-list" or ordinary
0N/A * file.
0N/A * @param reporter The <code>DocErrorReporter</code> used to report errors.
0N/A * @param linkoffline True if -linkoffline isused and false if -link is used.
0N/A */
0N/A public boolean url(String url, String pkglisturl,
0N/A DocErrorReporter reporter, boolean linkoffline) {
0N/A this.linkoffline = linkoffline;
0N/A String errMsg = composeExternPackageList(url, pkglisturl);
0N/A if (errMsg != null) {
0N/A reporter.printWarning(errMsg);
0N/A return false;
0N/A } else {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Get the Extern Item object associated with this package name.
0N/A *
0N/A * @param pkgname Package name.
0N/A */
0N/A private Item findPackageItem(String pkgName) {
0N/A if (packageToItemMap == null) {
0N/A return null;
0N/A }
73N/A return packageToItemMap.get(pkgName);
0N/A }
0N/A
0N/A /**
0N/A * Adjusts the end file separator if it is missing from the URL or the
0N/A * directory path and depending upon the URL or file path, fetch or
0N/A * read the "package-list" file.
0N/A *
0N/A * @param urlOrDirPath URL or the directory path.
0N/A * @param pkgListUrlOrDirPath URL or directory path for the "package-list" file or the "package-list"
0N/A * file itself.
0N/A */
0N/A private String composeExternPackageList(String urlOrDirPath, String pkgListUrlOrDirPath) {
0N/A urlOrDirPath = adjustEndFileSeparator(urlOrDirPath);
0N/A pkgListUrlOrDirPath = adjustEndFileSeparator(pkgListUrlOrDirPath);
0N/A return isUrl(pkgListUrlOrDirPath) ?
0N/A fetchURLComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath) :
0N/A readFileComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath);
0N/A }
0N/A
0N/A /**
0N/A * If the URL or Directory path is missing end file separator, add that.
0N/A */
0N/A private String adjustEndFileSeparator(String url) {
0N/A String filesep = "/";
0N/A if (!url.endsWith(filesep)) {
0N/A url += filesep;
0N/A }
0N/A return url;
0N/A }
0N/A
0N/A /**
0N/A * Fetch the URL and read the "package-list" file.
0N/A *
0N/A * @param urlpath Path to the packages.
0N/A * @param pkglisturlpath URL or the path to the "package-list" file.
0N/A */
0N/A private String fetchURLComposeExternPackageList(String urlpath,
0N/A String pkglisturlpath) {
0N/A String link = pkglisturlpath + "package-list";
0N/A try {
0N/A readPackageList((new URL(link)).openStream(), urlpath, false);
0N/A } catch (MalformedURLException exc) {
0N/A return configuration.getText("doclet.MalformedURL", link);
0N/A } catch (IOException exc) {
0N/A return configuration.getText("doclet.URL_error", link);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Read the "package-list" file which is available locally.
0N/A *
0N/A * @param path URL or directory path to the packages.
0N/A * @param pkgListPath Path to the local "package-list" file.
0N/A */
0N/A private String readFileComposeExternPackageList(String path,
0N/A String pkgListPath) {
0N/A
0N/A String link = pkgListPath + "package-list";
0N/A if (! ((new File(pkgListPath)).isAbsolute() || linkoffline)){
0N/A link = configuration.destDirName + link;
0N/A }
0N/A try {
0N/A File file = new File(link);
0N/A if (file.exists() && file.canRead()) {
0N/A readPackageList(new FileInputStream(file), path,
0N/A ! ((new File(path)).isAbsolute() || isUrl(path)));
0N/A } else {
0N/A return configuration.getText("doclet.File_error", link);
0N/A }
0N/A } catch (FileNotFoundException exc) {
0N/A return configuration.getText("doclet.File_error", link);
0N/A } catch (IOException exc) {
0N/A return configuration.getText("doclet.File_error", link);
0N/A }
0N/A return null;
0N/A }
0N/A
0N/A /**
0N/A * Read the file "package-list" and for each package name found, create
0N/A * Extern object and associate it with the package name in the map.
0N/A *
0N/A * @param input InputStream from the "package-list" file.
0N/A * @param path URL or the directory path to the packages.
0N/A * @param relative Is path relative?
0N/A */
0N/A private void readPackageList(InputStream input, String path,
0N/A boolean relative)
0N/A throws IOException {
0N/A BufferedReader in = new BufferedReader(new InputStreamReader(input));
0N/A StringBuffer strbuf = new StringBuffer();
0N/A try {
0N/A int c;
0N/A while ((c = in.read()) >= 0) {
0N/A char ch = (char)c;
0N/A if (ch == '\n' || ch == '\r') {
0N/A if (strbuf.length() > 0) {
0N/A String packname = strbuf.toString();
0N/A String packpath = path +
0N/A packname.replace('.', '/') + '/';
0N/A new Item(packname, packpath, relative);
0N/A strbuf.setLength(0);
0N/A }
0N/A } else {
0N/A strbuf.append(ch);
0N/A }
0N/A }
0N/A } finally {
0N/A input.close();
0N/A }
0N/A }
0N/A
0N/A public boolean isUrl (String urlCandidate) {
0N/A try {
0N/A new URL(urlCandidate);
0N/A //No exception was thrown, so this must really be a URL.
0N/A return true;
0N/A } catch (MalformedURLException e) {
0N/A //Since exception is thrown, this must be a directory path.
0N/A return false;
0N/A }
0N/A }
0N/A}