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

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
parent 1383
b980e8e6aabf
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy

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

mercurial