Tue, 09 Oct 2012 19:10:00 -0700
8000663: clean up langtools imports
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 | |
duke@1 | 33 | import com.sun.javadoc.*; |
jjg@1357 | 34 | import com.sun.tools.doclets.internal.toolkit.*; |
duke@1 | 35 | |
duke@1 | 36 | /** |
duke@1 | 37 | * Process and manage "-link" and "-linkoffline" to external packages. The |
duke@1 | 38 | * options "-link" and "-linkoffline" both depend on the fact that Javadoc now |
duke@1 | 39 | * generates "package-list"(lists all the packages which are getting |
duke@1 | 40 | * documented) file in the current or the destination directory, while |
duke@1 | 41 | * generating the documentation. |
duke@1 | 42 | * |
duke@1 | 43 | * This code is not part of an API. |
duke@1 | 44 | * It is implementation that is subject to change. |
duke@1 | 45 | * Do not use it as an API |
duke@1 | 46 | * |
duke@1 | 47 | * @author Atul M Dambalkar |
duke@1 | 48 | * @author Robert Field |
duke@1 | 49 | */ |
duke@1 | 50 | public class Extern { |
duke@1 | 51 | |
duke@1 | 52 | /** |
duke@1 | 53 | * Map package names onto Extern Item objects. |
duke@1 | 54 | * Lazily initialized. |
duke@1 | 55 | */ |
jjg@74 | 56 | private Map<String,Item> packageToItemMap; |
duke@1 | 57 | |
duke@1 | 58 | /** |
duke@1 | 59 | * The global configuration information for this run. |
duke@1 | 60 | */ |
duke@1 | 61 | private final Configuration configuration; |
duke@1 | 62 | |
duke@1 | 63 | /** |
duke@1 | 64 | * True if we are using -linkoffline and false if -link is used instead. |
duke@1 | 65 | */ |
duke@1 | 66 | private boolean linkoffline = false; |
duke@1 | 67 | |
duke@1 | 68 | /** |
duke@1 | 69 | * Stores the info for one external doc set |
duke@1 | 70 | */ |
duke@1 | 71 | private class Item { |
duke@1 | 72 | |
duke@1 | 73 | /** |
duke@1 | 74 | * Package name, found in the "package-list" file in the {@link path}. |
duke@1 | 75 | */ |
duke@1 | 76 | final String packageName; |
duke@1 | 77 | |
duke@1 | 78 | /** |
duke@1 | 79 | * The URL or the directory path at which the package documentation will be |
duke@1 | 80 | * avaliable. |
duke@1 | 81 | */ |
duke@1 | 82 | final String path; |
duke@1 | 83 | |
duke@1 | 84 | /** |
duke@1 | 85 | * If given path is directory path then true else if it is a URL then false. |
duke@1 | 86 | */ |
duke@1 | 87 | final boolean relative; |
duke@1 | 88 | |
duke@1 | 89 | /** |
duke@1 | 90 | * Constructor to build a Extern Item object and map it with the package name. |
duke@1 | 91 | * If the same package name is found in the map, then the first mapped |
duke@1 | 92 | * Item object or offline location will be retained. |
duke@1 | 93 | * |
duke@1 | 94 | * @param packagename Package name found in the "package-list" file. |
duke@1 | 95 | * @param path URL or Directory path from where the "package-list" |
duke@1 | 96 | * file is picked. |
duke@1 | 97 | * @param relative True if path is URL, false if directory path. |
duke@1 | 98 | */ |
duke@1 | 99 | Item(String packageName, String path, boolean relative) { |
duke@1 | 100 | this.packageName = packageName; |
duke@1 | 101 | this.path = path; |
duke@1 | 102 | this.relative = relative; |
duke@1 | 103 | if (packageToItemMap == null) { |
jjg@74 | 104 | packageToItemMap = new HashMap<String,Item>(); |
duke@1 | 105 | } |
duke@1 | 106 | if (!packageToItemMap.containsKey(packageName)) { // save the previous |
duke@1 | 107 | packageToItemMap.put(packageName, this); // mapped location |
duke@1 | 108 | } |
duke@1 | 109 | } |
duke@1 | 110 | |
duke@1 | 111 | /** |
duke@1 | 112 | * String representation of "this" with packagename and the path. |
duke@1 | 113 | */ |
duke@1 | 114 | public String toString() { |
duke@1 | 115 | return packageName + (relative? " -> " : " => ") + path; |
duke@1 | 116 | } |
duke@1 | 117 | } |
duke@1 | 118 | |
duke@1 | 119 | public Extern(Configuration configuration) { |
duke@1 | 120 | this.configuration = configuration; |
duke@1 | 121 | } |
duke@1 | 122 | |
duke@1 | 123 | /** |
duke@1 | 124 | * Determine if a doc item is externally documented. |
duke@1 | 125 | * |
duke@1 | 126 | * @param doc A ProgramElementDoc. |
duke@1 | 127 | */ |
duke@1 | 128 | public boolean isExternal(ProgramElementDoc doc) { |
duke@1 | 129 | if (packageToItemMap == null) { |
duke@1 | 130 | return false; |
duke@1 | 131 | } |
duke@1 | 132 | return packageToItemMap.get(doc.containingPackage().name()) != null; |
duke@1 | 133 | } |
duke@1 | 134 | |
duke@1 | 135 | /** |
duke@1 | 136 | * Convert a link to be an external link if appropriate. |
duke@1 | 137 | * |
duke@1 | 138 | * @param pkgName The package name. |
duke@1 | 139 | * @param relativepath The relative path. |
duke@1 | 140 | * @param link The link to convert. |
duke@1 | 141 | * @return if external return converted link else return null |
duke@1 | 142 | */ |
duke@1 | 143 | public String getExternalLink(String pkgName, |
duke@1 | 144 | String relativepath, String link) { |
duke@1 | 145 | Item fnd = findPackageItem(pkgName); |
duke@1 | 146 | if (fnd != null) { |
duke@1 | 147 | String externlink = fnd.path + link; |
duke@1 | 148 | if (fnd.relative) { // it's a relative path. |
duke@1 | 149 | return relativepath + externlink; |
duke@1 | 150 | } else { |
duke@1 | 151 | return externlink; |
duke@1 | 152 | } |
duke@1 | 153 | } |
duke@1 | 154 | return null; |
duke@1 | 155 | } |
duke@1 | 156 | |
duke@1 | 157 | /** |
duke@1 | 158 | * Build the extern package list from given URL or the directory path. |
duke@1 | 159 | * Flag error if the "-link" or "-linkoffline" option is already used. |
duke@1 | 160 | * |
duke@1 | 161 | * @param url URL or Directory path. |
duke@1 | 162 | * @param pkglisturl This can be another URL for "package-list" or ordinary |
duke@1 | 163 | * file. |
duke@1 | 164 | * @param reporter The <code>DocErrorReporter</code> used to report errors. |
duke@1 | 165 | * @param linkoffline True if -linkoffline isused and false if -link is used. |
duke@1 | 166 | */ |
duke@1 | 167 | public boolean url(String url, String pkglisturl, |
duke@1 | 168 | DocErrorReporter reporter, boolean linkoffline) { |
duke@1 | 169 | this.linkoffline = linkoffline; |
duke@1 | 170 | String errMsg = composeExternPackageList(url, pkglisturl); |
duke@1 | 171 | if (errMsg != null) { |
duke@1 | 172 | reporter.printWarning(errMsg); |
duke@1 | 173 | return false; |
duke@1 | 174 | } else { |
duke@1 | 175 | return true; |
duke@1 | 176 | } |
duke@1 | 177 | } |
duke@1 | 178 | |
duke@1 | 179 | /** |
duke@1 | 180 | * Get the Extern Item object associated with this package name. |
duke@1 | 181 | * |
duke@1 | 182 | * @param pkgname Package name. |
duke@1 | 183 | */ |
duke@1 | 184 | private Item findPackageItem(String pkgName) { |
duke@1 | 185 | if (packageToItemMap == null) { |
duke@1 | 186 | return null; |
duke@1 | 187 | } |
jjg@74 | 188 | return packageToItemMap.get(pkgName); |
duke@1 | 189 | } |
duke@1 | 190 | |
duke@1 | 191 | /** |
duke@1 | 192 | * Adjusts the end file separator if it is missing from the URL or the |
duke@1 | 193 | * directory path and depending upon the URL or file path, fetch or |
duke@1 | 194 | * read the "package-list" file. |
duke@1 | 195 | * |
duke@1 | 196 | * @param urlOrDirPath URL or the directory path. |
duke@1 | 197 | * @param pkgListUrlOrDirPath URL or directory path for the "package-list" file or the "package-list" |
duke@1 | 198 | * file itself. |
duke@1 | 199 | */ |
duke@1 | 200 | private String composeExternPackageList(String urlOrDirPath, String pkgListUrlOrDirPath) { |
duke@1 | 201 | urlOrDirPath = adjustEndFileSeparator(urlOrDirPath); |
duke@1 | 202 | pkgListUrlOrDirPath = adjustEndFileSeparator(pkgListUrlOrDirPath); |
duke@1 | 203 | return isUrl(pkgListUrlOrDirPath) ? |
duke@1 | 204 | fetchURLComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath) : |
duke@1 | 205 | readFileComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath); |
duke@1 | 206 | } |
duke@1 | 207 | |
duke@1 | 208 | /** |
duke@1 | 209 | * If the URL or Directory path is missing end file separator, add that. |
duke@1 | 210 | */ |
duke@1 | 211 | private String adjustEndFileSeparator(String url) { |
duke@1 | 212 | String filesep = "/"; |
duke@1 | 213 | if (!url.endsWith(filesep)) { |
duke@1 | 214 | url += filesep; |
duke@1 | 215 | } |
duke@1 | 216 | return url; |
duke@1 | 217 | } |
duke@1 | 218 | |
duke@1 | 219 | /** |
duke@1 | 220 | * Fetch the URL and read the "package-list" file. |
duke@1 | 221 | * |
duke@1 | 222 | * @param urlpath Path to the packages. |
duke@1 | 223 | * @param pkglisturlpath URL or the path to the "package-list" file. |
duke@1 | 224 | */ |
duke@1 | 225 | private String fetchURLComposeExternPackageList(String urlpath, |
duke@1 | 226 | String pkglisturlpath) { |
duke@1 | 227 | String link = pkglisturlpath + "package-list"; |
duke@1 | 228 | try { |
duke@1 | 229 | readPackageList((new URL(link)).openStream(), urlpath, false); |
duke@1 | 230 | } catch (MalformedURLException exc) { |
duke@1 | 231 | return configuration.getText("doclet.MalformedURL", link); |
duke@1 | 232 | } catch (IOException exc) { |
duke@1 | 233 | return configuration.getText("doclet.URL_error", link); |
duke@1 | 234 | } |
duke@1 | 235 | return null; |
duke@1 | 236 | } |
duke@1 | 237 | |
duke@1 | 238 | /** |
duke@1 | 239 | * Read the "package-list" file which is available locally. |
duke@1 | 240 | * |
duke@1 | 241 | * @param path URL or directory path to the packages. |
duke@1 | 242 | * @param pkgListPath Path to the local "package-list" file. |
duke@1 | 243 | */ |
duke@1 | 244 | private String readFileComposeExternPackageList(String path, |
duke@1 | 245 | String pkgListPath) { |
duke@1 | 246 | |
duke@1 | 247 | String link = pkgListPath + "package-list"; |
duke@1 | 248 | if (! ((new File(pkgListPath)).isAbsolute() || linkoffline)){ |
duke@1 | 249 | link = configuration.destDirName + link; |
duke@1 | 250 | } |
duke@1 | 251 | try { |
duke@1 | 252 | File file = new File(link); |
duke@1 | 253 | if (file.exists() && file.canRead()) { |
duke@1 | 254 | readPackageList(new FileInputStream(file), path, |
duke@1 | 255 | ! ((new File(path)).isAbsolute() || isUrl(path))); |
duke@1 | 256 | } else { |
duke@1 | 257 | return configuration.getText("doclet.File_error", link); |
duke@1 | 258 | } |
duke@1 | 259 | } catch (FileNotFoundException exc) { |
duke@1 | 260 | return configuration.getText("doclet.File_error", link); |
duke@1 | 261 | } catch (IOException exc) { |
duke@1 | 262 | return configuration.getText("doclet.File_error", link); |
duke@1 | 263 | } |
duke@1 | 264 | return null; |
duke@1 | 265 | } |
duke@1 | 266 | |
duke@1 | 267 | /** |
duke@1 | 268 | * Read the file "package-list" and for each package name found, create |
duke@1 | 269 | * Extern object and associate it with the package name in the map. |
duke@1 | 270 | * |
duke@1 | 271 | * @param input InputStream from the "package-list" file. |
duke@1 | 272 | * @param path URL or the directory path to the packages. |
duke@1 | 273 | * @param relative Is path relative? |
duke@1 | 274 | */ |
duke@1 | 275 | private void readPackageList(InputStream input, String path, |
duke@1 | 276 | boolean relative) |
duke@1 | 277 | throws IOException { |
duke@1 | 278 | BufferedReader in = new BufferedReader(new InputStreamReader(input)); |
duke@1 | 279 | StringBuffer strbuf = new StringBuffer(); |
duke@1 | 280 | try { |
duke@1 | 281 | int c; |
duke@1 | 282 | while ((c = in.read()) >= 0) { |
duke@1 | 283 | char ch = (char)c; |
duke@1 | 284 | if (ch == '\n' || ch == '\r') { |
duke@1 | 285 | if (strbuf.length() > 0) { |
duke@1 | 286 | String packname = strbuf.toString(); |
duke@1 | 287 | String packpath = path + |
duke@1 | 288 | packname.replace('.', '/') + '/'; |
duke@1 | 289 | new Item(packname, packpath, relative); |
duke@1 | 290 | strbuf.setLength(0); |
duke@1 | 291 | } |
duke@1 | 292 | } else { |
duke@1 | 293 | strbuf.append(ch); |
duke@1 | 294 | } |
duke@1 | 295 | } |
duke@1 | 296 | } finally { |
duke@1 | 297 | input.close(); |
duke@1 | 298 | } |
duke@1 | 299 | } |
duke@1 | 300 | |
duke@1 | 301 | public boolean isUrl (String urlCandidate) { |
duke@1 | 302 | try { |
duke@1 | 303 | new URL(urlCandidate); |
duke@1 | 304 | //No exception was thrown, so this must really be a URL. |
duke@1 | 305 | return true; |
duke@1 | 306 | } catch (MalformedURLException e) { |
duke@1 | 307 | //Since exception is thrown, this must be a directory path. |
duke@1 | 308 | return false; |
duke@1 | 309 | } |
duke@1 | 310 | } |
duke@1 | 311 | } |