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