src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Extern.java

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2366
5b8151ede720
parent 0
959103a6100f
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation. Oracle designates this
aoqi@0 8 * particular file as subject to the "Classpath" exception as provided
aoqi@0 9 * by Oracle in the LICENSE file that accompanied this code.
aoqi@0 10 *
aoqi@0 11 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 14 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 15 * accompanied this code).
aoqi@0 16 *
aoqi@0 17 * You should have received a copy of the GNU General Public License version
aoqi@0 18 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 20 *
aoqi@0 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 22 * or visit www.oracle.com if you need additional information or have any
aoqi@0 23 * questions.
aoqi@0 24 */
aoqi@0 25
aoqi@0 26 package com.sun.tools.doclets.internal.toolkit.util;
aoqi@0 27
aoqi@0 28 import java.io.*;
aoqi@0 29 import java.net.*;
aoqi@0 30 import java.util.HashMap;
aoqi@0 31 import java.util.Map;
aoqi@0 32
aoqi@0 33 import javax.tools.DocumentationTool;
aoqi@0 34
aoqi@0 35 import com.sun.javadoc.*;
aoqi@0 36 import com.sun.tools.doclets.internal.toolkit.*;
aoqi@0 37
aoqi@0 38 /**
aoqi@0 39 * Process and manage "-link" and "-linkoffline" to external packages. The
aoqi@0 40 * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
aoqi@0 41 * generates "package-list"(lists all the packages which are getting
aoqi@0 42 * documented) file in the current or the destination directory, while
aoqi@0 43 * generating the documentation.
aoqi@0 44 *
aoqi@0 45 * <p><b>This is NOT part of any supported API.
aoqi@0 46 * If you write code that depends on this, you do so at your own risk.
aoqi@0 47 * This code and its internal interfaces are subject to change or
aoqi@0 48 * deletion without notice.</b>
aoqi@0 49 *
aoqi@0 50 * @author Atul M Dambalkar
aoqi@0 51 * @author Robert Field
aoqi@0 52 */
aoqi@0 53 public class Extern {
aoqi@0 54
aoqi@0 55 /**
aoqi@0 56 * Map package names onto Extern Item objects.
aoqi@0 57 * Lazily initialized.
aoqi@0 58 */
aoqi@0 59 private Map<String,Item> packageToItemMap;
aoqi@0 60
aoqi@0 61 /**
aoqi@0 62 * The global configuration information for this run.
aoqi@0 63 */
aoqi@0 64 private final Configuration configuration;
aoqi@0 65
aoqi@0 66 /**
aoqi@0 67 * True if we are using -linkoffline and false if -link is used instead.
aoqi@0 68 */
aoqi@0 69 private boolean linkoffline = false;
aoqi@0 70
aoqi@0 71 /**
aoqi@0 72 * Stores the info for one external doc set
aoqi@0 73 */
aoqi@0 74 private class Item {
aoqi@0 75
aoqi@0 76 /**
aoqi@0 77 * Package name, found in the "package-list" file in the {@link path}.
aoqi@0 78 */
aoqi@0 79 final String packageName;
aoqi@0 80
aoqi@0 81 /**
aoqi@0 82 * The URL or the directory path at which the package documentation will be
aoqi@0 83 * avaliable.
aoqi@0 84 */
aoqi@0 85 final String path;
aoqi@0 86
aoqi@0 87 /**
aoqi@0 88 * If given path is directory path then true else if it is a URL then false.
aoqi@0 89 */
aoqi@0 90 final boolean relative;
aoqi@0 91
aoqi@0 92 /**
aoqi@0 93 * Constructor to build a Extern Item object and map it with the package name.
aoqi@0 94 * If the same package name is found in the map, then the first mapped
aoqi@0 95 * Item object or offline location will be retained.
aoqi@0 96 *
aoqi@0 97 * @param packageName Package name found in the "package-list" file.
aoqi@0 98 * @param path URL or Directory path from where the "package-list"
aoqi@0 99 * file is picked.
aoqi@0 100 * @param relative True if path is URL, false if directory path.
aoqi@0 101 */
aoqi@0 102 Item(String packageName, String path, boolean relative) {
aoqi@0 103 this.packageName = packageName;
aoqi@0 104 this.path = path;
aoqi@0 105 this.relative = relative;
aoqi@0 106 if (packageToItemMap == null) {
aoqi@0 107 packageToItemMap = new HashMap<String,Item>();
aoqi@0 108 }
aoqi@0 109 if (!packageToItemMap.containsKey(packageName)) { // save the previous
aoqi@0 110 packageToItemMap.put(packageName, this); // mapped location
aoqi@0 111 }
aoqi@0 112 }
aoqi@0 113
aoqi@0 114 /**
aoqi@0 115 * String representation of "this" with packagename and the path.
aoqi@0 116 */
aoqi@0 117 public String toString() {
aoqi@0 118 return packageName + (relative? " -> " : " => ") + path;
aoqi@0 119 }
aoqi@0 120 }
aoqi@0 121
aoqi@0 122 public Extern(Configuration configuration) {
aoqi@0 123 this.configuration = configuration;
aoqi@0 124 }
aoqi@0 125
aoqi@0 126 /**
aoqi@0 127 * Determine if a doc item is externally documented.
aoqi@0 128 *
aoqi@0 129 * @param doc A ProgramElementDoc.
aoqi@0 130 */
aoqi@0 131 public boolean isExternal(ProgramElementDoc doc) {
aoqi@0 132 if (packageToItemMap == null) {
aoqi@0 133 return false;
aoqi@0 134 }
aoqi@0 135 return packageToItemMap.get(doc.containingPackage().name()) != null;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 /**
aoqi@0 139 * Convert a link to be an external link if appropriate.
aoqi@0 140 *
aoqi@0 141 * @param pkgName The package name.
aoqi@0 142 * @param relativepath The relative path.
aoqi@0 143 * @param filename The link to convert.
aoqi@0 144 * @return if external return converted link else return null
aoqi@0 145 */
aoqi@0 146 public DocLink getExternalLink(String pkgName,
aoqi@0 147 DocPath relativepath, String filename) {
aoqi@0 148 return getExternalLink(pkgName, relativepath, filename, null);
aoqi@0 149 }
aoqi@0 150
aoqi@0 151 public DocLink getExternalLink(String pkgName,
aoqi@0 152 DocPath relativepath, String filename, String memberName) {
aoqi@0 153 Item fnd = findPackageItem(pkgName);
aoqi@0 154 if (fnd == null)
aoqi@0 155 return null;
aoqi@0 156
aoqi@0 157 DocPath p = fnd.relative ?
aoqi@0 158 relativepath.resolve(fnd.path).resolve(filename) :
aoqi@0 159 DocPath.create(fnd.path).resolve(filename);
aoqi@0 160
aoqi@0 161 return new DocLink(p, "is-external=true", memberName);
aoqi@0 162 }
aoqi@0 163
aoqi@0 164 /**
aoqi@0 165 * Build the extern package list from given URL or the directory path.
aoqi@0 166 * Flag error if the "-link" or "-linkoffline" option is already used.
aoqi@0 167 *
aoqi@0 168 * @param url URL or Directory path.
aoqi@0 169 * @param pkglisturl This can be another URL for "package-list" or ordinary
aoqi@0 170 * file.
aoqi@0 171 * @param reporter The <code>DocErrorReporter</code> used to report errors.
aoqi@0 172 * @param linkoffline True if -linkoffline is used and false if -link is used.
aoqi@0 173 */
aoqi@0 174 public boolean link(String url, String pkglisturl,
aoqi@0 175 DocErrorReporter reporter, boolean linkoffline) {
aoqi@0 176 this.linkoffline = linkoffline;
aoqi@0 177 try {
aoqi@0 178 url = adjustEndFileSeparator(url);
aoqi@0 179 if (isUrl(pkglisturl)) {
aoqi@0 180 readPackageListFromURL(url, toURL(adjustEndFileSeparator(pkglisturl)));
aoqi@0 181 } else {
aoqi@0 182 readPackageListFromFile(url, DocFile.createFileForInput(configuration, pkglisturl));
aoqi@0 183 }
aoqi@0 184 return true;
aoqi@0 185 } catch (Fault f) {
aoqi@0 186 reporter.printWarning(f.getMessage());
aoqi@0 187 return false;
aoqi@0 188 }
aoqi@0 189 }
aoqi@0 190
aoqi@0 191 private URL toURL(String url) throws Fault {
aoqi@0 192 try {
aoqi@0 193 return new URL(url);
aoqi@0 194 } catch (MalformedURLException e) {
aoqi@0 195 throw new Fault(configuration.getText("doclet.MalformedURL", url), e);
aoqi@0 196 }
aoqi@0 197 }
aoqi@0 198
aoqi@0 199 private class Fault extends Exception {
aoqi@0 200 private static final long serialVersionUID = 0;
aoqi@0 201
aoqi@0 202 Fault(String msg, Exception cause) {
aoqi@0 203 super(msg, cause);
aoqi@0 204 }
aoqi@0 205 }
aoqi@0 206
aoqi@0 207 /**
aoqi@0 208 * Get the Extern Item object associated with this package name.
aoqi@0 209 *
aoqi@0 210 * @param pkgName Package name.
aoqi@0 211 */
aoqi@0 212 private Item findPackageItem(String pkgName) {
aoqi@0 213 if (packageToItemMap == null) {
aoqi@0 214 return null;
aoqi@0 215 }
aoqi@0 216 return packageToItemMap.get(pkgName);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 /**
aoqi@0 220 * If the URL or Directory path is missing end file separator, add that.
aoqi@0 221 */
aoqi@0 222 private String adjustEndFileSeparator(String url) {
aoqi@0 223 return url.endsWith("/") ? url : url + '/';
aoqi@0 224 }
aoqi@0 225
aoqi@0 226 /**
aoqi@0 227 * Fetch the URL and read the "package-list" file.
aoqi@0 228 *
aoqi@0 229 * @param urlpath Path to the packages.
aoqi@0 230 * @param pkglisturlpath URL or the path to the "package-list" file.
aoqi@0 231 */
aoqi@0 232 private void readPackageListFromURL(String urlpath, URL pkglisturlpath)
aoqi@0 233 throws Fault {
aoqi@0 234 try {
aoqi@0 235 URL link = pkglisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL();
aoqi@0 236 readPackageList(link.openStream(), urlpath, false);
aoqi@0 237 } catch (URISyntaxException exc) {
aoqi@0 238 throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc);
aoqi@0 239 } catch (MalformedURLException exc) {
aoqi@0 240 throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc);
aoqi@0 241 } catch (IOException exc) {
aoqi@0 242 throw new Fault(configuration.getText("doclet.URL_error", pkglisturlpath.toString()), exc);
aoqi@0 243 }
aoqi@0 244 }
aoqi@0 245
aoqi@0 246 /**
aoqi@0 247 * Read the "package-list" file which is available locally.
aoqi@0 248 *
aoqi@0 249 * @param path URL or directory path to the packages.
aoqi@0 250 * @param pkgListPath Path to the local "package-list" file.
aoqi@0 251 */
aoqi@0 252 private void readPackageListFromFile(String path, DocFile pkgListPath)
aoqi@0 253 throws Fault {
aoqi@0 254 DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST);
aoqi@0 255 if (! (file.isAbsolute() || linkoffline)){
aoqi@0 256 file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT);
aoqi@0 257 }
aoqi@0 258 try {
aoqi@0 259 if (file.exists() && file.canRead()) {
aoqi@0 260 boolean pathIsRelative =
aoqi@0 261 !DocFile.createFileForInput(configuration, path).isAbsolute()
aoqi@0 262 && !isUrl(path);
aoqi@0 263 readPackageList(file.openInputStream(), path, pathIsRelative);
aoqi@0 264 } else {
aoqi@0 265 throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
aoqi@0 266 }
aoqi@0 267 } catch (IOException exc) {
aoqi@0 268 throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc);
aoqi@0 269 }
aoqi@0 270 }
aoqi@0 271
aoqi@0 272 /**
aoqi@0 273 * Read the file "package-list" and for each package name found, create
aoqi@0 274 * Extern object and associate it with the package name in the map.
aoqi@0 275 *
aoqi@0 276 * @param input InputStream from the "package-list" file.
aoqi@0 277 * @param path URL or the directory path to the packages.
aoqi@0 278 * @param relative Is path relative?
aoqi@0 279 */
aoqi@0 280 private void readPackageList(InputStream input, String path,
aoqi@0 281 boolean relative)
aoqi@0 282 throws IOException {
aoqi@0 283 BufferedReader in = new BufferedReader(new InputStreamReader(input));
aoqi@0 284 StringBuilder strbuf = new StringBuilder();
aoqi@0 285 try {
aoqi@0 286 int c;
aoqi@0 287 while ((c = in.read()) >= 0) {
aoqi@0 288 char ch = (char)c;
aoqi@0 289 if (ch == '\n' || ch == '\r') {
aoqi@0 290 if (strbuf.length() > 0) {
aoqi@0 291 String packname = strbuf.toString();
aoqi@0 292 String packpath = path +
aoqi@0 293 packname.replace('.', '/') + '/';
aoqi@0 294 new Item(packname, packpath, relative);
aoqi@0 295 strbuf.setLength(0);
aoqi@0 296 }
aoqi@0 297 } else {
aoqi@0 298 strbuf.append(ch);
aoqi@0 299 }
aoqi@0 300 }
aoqi@0 301 } finally {
aoqi@0 302 input.close();
aoqi@0 303 }
aoqi@0 304 }
aoqi@0 305
aoqi@0 306 public boolean isUrl (String urlCandidate) {
aoqi@0 307 try {
aoqi@0 308 new URL(urlCandidate);
aoqi@0 309 //No exception was thrown, so this must really be a URL.
aoqi@0 310 return true;
aoqi@0 311 } catch (MalformedURLException e) {
aoqi@0 312 //Since exception is thrown, this must be a directory path.
aoqi@0 313 return false;
aoqi@0 314 }
aoqi@0 315 }
aoqi@0 316 }

mercurial