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