author | jjg |
Fri, 26 Oct 2012 13:10:56 -0700 | |
changeset 14361 | 2814fa6977ac |
parent 14358 | 9eda9239cba0 |
child 14368 | 6f4c62de6985 |
permissions | -rw-r--r-- |
10 | 1 |
/* |
14258 | 2 |
* Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
10 | 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 |
|
5520 | 7 |
* published by the Free Software Foundation. Oracle designates this |
10 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5520 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
10 | 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 |
* |
|
5520 | 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. |
|
10 | 24 |
*/ |
25 |
||
26 |
package com.sun.tools.doclets.internal.toolkit.util; |
|
27 |
||
14258 | 28 |
import java.io.*; |
29 |
import java.net.*; |
|
30 |
import java.util.HashMap; |
|
31 |
import java.util.Map; |
|
10 | 32 |
|
33 |
import com.sun.javadoc.*; |
|
14258 | 34 |
import com.sun.tools.doclets.internal.toolkit.*; |
10 | 35 |
|
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 |
* |
|
14260
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
43 |
* <p><b>This is NOT part of any supported API. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
44 |
* If you write code that depends on this, you do so at your own risk. |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
45 |
* This code and its internal interfaces are subject to change or |
727a84636f12
8000665: fix "internal API" comments on javadoc files
jjg
parents:
14259
diff
changeset
|
46 |
* deletion without notice.</b> |
10 | 47 |
* |
48 |
* @author Atul M Dambalkar |
|
49 |
* @author Robert Field |
|
50 |
*/ |
|
51 |
public class Extern { |
|
52 |
||
53 |
/** |
|
54 |
* Map package names onto Extern Item objects. |
|
55 |
* Lazily initialized. |
|
56 |
*/ |
|
868 | 57 |
private Map<String,Item> packageToItemMap; |
10 | 58 |
|
59 |
/** |
|
60 |
* The global configuration information for this run. |
|
61 |
*/ |
|
62 |
private final Configuration configuration; |
|
63 |
||
64 |
/** |
|
65 |
* True if we are using -linkoffline and false if -link is used instead. |
|
66 |
*/ |
|
67 |
private boolean linkoffline = false; |
|
68 |
||
69 |
/** |
|
70 |
* Stores the info for one external doc set |
|
71 |
*/ |
|
72 |
private class Item { |
|
73 |
||
74 |
/** |
|
75 |
* Package name, found in the "package-list" file in the {@link path}. |
|
76 |
*/ |
|
77 |
final String packageName; |
|
78 |
||
79 |
/** |
|
80 |
* The URL or the directory path at which the package documentation will be |
|
81 |
* avaliable. |
|
82 |
*/ |
|
83 |
final String path; |
|
84 |
||
85 |
/** |
|
86 |
* If given path is directory path then true else if it is a URL then false. |
|
87 |
*/ |
|
88 |
final boolean relative; |
|
89 |
||
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 |
* |
|
14259 | 95 |
* @param packageName Package name found in the "package-list" file. |
10 | 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) { |
|
868 | 105 |
packageToItemMap = new HashMap<String,Item>(); |
10 | 106 |
} |
107 |
if (!packageToItemMap.containsKey(packageName)) { // save the previous |
|
108 |
packageToItemMap.put(packageName, this); // mapped location |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* String representation of "this" with packagename and the path. |
|
114 |
*/ |
|
115 |
public String toString() { |
|
116 |
return packageName + (relative? " -> " : " => ") + path; |
|
117 |
} |
|
118 |
} |
|
119 |
||
120 |
public Extern(Configuration configuration) { |
|
121 |
this.configuration = configuration; |
|
122 |
} |
|
123 |
||
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 |
} |
|
135 |
||
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. |
|
14358
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
141 |
* @param filename The link to convert. |
10 | 142 |
* @return if external return converted link else return null |
143 |
*/ |
|
14358
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
144 |
public DocLink getExternalLink(String pkgName, |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
145 |
DocPath relativepath, String filename) { |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
146 |
return getExternalLink(pkgName, relativepath, filename, null); |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
147 |
} |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
148 |
|
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
149 |
public DocLink getExternalLink(String pkgName, |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
150 |
DocPath relativepath, String filename, String memberName) { |
10 | 151 |
Item fnd = findPackageItem(pkgName); |
14358
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
152 |
if (fnd == null) |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
153 |
return null; |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
154 |
|
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
155 |
DocPath p = fnd.relative ? |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
156 |
relativepath.resolve(fnd.path).resolve(filename) : |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
157 |
DocPath.create(fnd.path).resolve(filename); |
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
158 |
|
9eda9239cba0
8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents:
14357
diff
changeset
|
159 |
return new DocLink(p, "is-external=true", memberName); |
10 | 160 |
} |
161 |
||
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. |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
170 |
* @param linkoffline True if -linkoffline is used and false if -link is used. |
10 | 171 |
*/ |
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
172 |
public boolean link(String url, String pkglisturl, |
10 | 173 |
DocErrorReporter reporter, boolean linkoffline) { |
174 |
this.linkoffline = linkoffline; |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
175 |
try { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
176 |
url = adjustEndFileSeparator(url); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
177 |
if (isUrl(pkglisturl)) { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
178 |
readPackageListFromURL(url, toURL(pkglisturl)); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
179 |
} else { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
180 |
readPackageListFromFile(url, new File(pkglisturl)); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
181 |
} |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
182 |
return true; |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
183 |
} catch (Fault f) { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
184 |
reporter.printWarning(f.getMessage()); |
10 | 185 |
return false; |
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
186 |
} |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
187 |
} |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
188 |
|
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
189 |
private URL toURL(String url) throws Fault { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
190 |
try { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
191 |
return new URL(url); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
192 |
} catch (MalformedURLException e) { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
193 |
throw new Fault(configuration.getText("doclet.MalformedURL", url), e); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
194 |
} |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
195 |
} |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
196 |
|
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
197 |
private class Fault extends Exception { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
198 |
private static final long serialVersionUID = 0; |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
199 |
|
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
200 |
Fault(String msg, Exception cause) { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
201 |
super(msg, cause); |
10 | 202 |
} |
203 |
} |
|
204 |
||
205 |
/** |
|
206 |
* Get the Extern Item object associated with this package name. |
|
207 |
* |
|
14259 | 208 |
* @param pkgName Package name. |
10 | 209 |
*/ |
210 |
private Item findPackageItem(String pkgName) { |
|
211 |
if (packageToItemMap == null) { |
|
212 |
return null; |
|
213 |
} |
|
868 | 214 |
return packageToItemMap.get(pkgName); |
10 | 215 |
} |
216 |
||
217 |
/** |
|
218 |
* If the URL or Directory path is missing end file separator, add that. |
|
219 |
*/ |
|
220 |
private String adjustEndFileSeparator(String url) { |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
221 |
return url.endsWith("/") ? url : url + '/'; |
10 | 222 |
} |
223 |
||
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 |
*/ |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
230 |
private void readPackageListFromURL(String urlpath, URL pkglisturlpath) |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
231 |
throws Fault { |
10 | 232 |
try { |
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
233 |
URL link = pkglisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL(); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
234 |
readPackageList(link.openStream(), urlpath, false); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
235 |
} catch (URISyntaxException exc) { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
236 |
throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc); |
10 | 237 |
} catch (MalformedURLException exc) { |
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
238 |
throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc); |
10 | 239 |
} catch (IOException exc) { |
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
240 |
throw new Fault(configuration.getText("doclet.URL_error", pkglisturlpath.toString()), exc); |
10 | 241 |
} |
242 |
} |
|
243 |
||
244 |
/** |
|
245 |
* Read the "package-list" file which is available locally. |
|
246 |
* |
|
247 |
* @param path URL or directory path to the packages. |
|
248 |
* @param pkgListPath Path to the local "package-list" file. |
|
249 |
*/ |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
250 |
private void readPackageListFromFile(String path, File pkgListPath) |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
251 |
throws Fault { |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
252 |
File file = new File(pkgListPath, "package-list"); |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
253 |
if (! (file.isAbsolute() || linkoffline)){ |
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
254 |
file = new File(configuration.destDirName, file.getPath()); |
10 | 255 |
} |
256 |
try { |
|
257 |
if (file.exists() && file.canRead()) { |
|
258 |
readPackageList(new FileInputStream(file), path, |
|
259 |
! ((new File(path)).isAbsolute() || isUrl(path))); |
|
260 |
} else { |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
261 |
throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null); |
10 | 262 |
} |
263 |
} catch (IOException exc) { |
|
14361
2814fa6977ac
8001219: Clean up use of URLs in javadoc Extern class
jjg
parents:
14358
diff
changeset
|
264 |
throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc); |
10 | 265 |
} |
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* Read the file "package-list" and for each package name found, create |
|
270 |
* Extern object and associate it with the package name in the map. |
|
271 |
* |
|
272 |
* @param input InputStream from the "package-list" file. |
|
273 |
* @param path URL or the directory path to the packages. |
|
274 |
* @param relative Is path relative? |
|
275 |
*/ |
|
276 |
private void readPackageList(InputStream input, String path, |
|
277 |
boolean relative) |
|
278 |
throws IOException { |
|
279 |
BufferedReader in = new BufferedReader(new InputStreamReader(input)); |
|
14263 | 280 |
StringBuilder strbuf = new StringBuilder(); |
10 | 281 |
try { |
282 |
int c; |
|
283 |
while ((c = in.read()) >= 0) { |
|
284 |
char ch = (char)c; |
|
285 |
if (ch == '\n' || ch == '\r') { |
|
286 |
if (strbuf.length() > 0) { |
|
287 |
String packname = strbuf.toString(); |
|
288 |
String packpath = path + |
|
289 |
packname.replace('.', '/') + '/'; |
|
290 |
new Item(packname, packpath, relative); |
|
291 |
strbuf.setLength(0); |
|
292 |
} |
|
293 |
} else { |
|
294 |
strbuf.append(ch); |
|
295 |
} |
|
296 |
} |
|
297 |
} finally { |
|
298 |
input.close(); |
|
299 |
} |
|
300 |
} |
|
301 |
||
302 |
public boolean isUrl (String urlCandidate) { |
|
303 |
try { |
|
304 |
new URL(urlCandidate); |
|
305 |
//No exception was thrown, so this must really be a URL. |
|
306 |
return true; |
|
307 |
} catch (MalformedURLException e) { |
|
308 |
//Since exception is thrown, this must be a directory path. |
|
309 |
return false; |
|
310 |
} |
|
311 |
} |
|
312 |
} |