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