Mon, 15 Oct 2012 17:07:55 -0700
8000666: javadoc should write directly to Writer instead of composing strings
Reviewed-by: bpatel
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 link The link to convert.
142 * @return if external return converted link else return null
143 */
144 public String getExternalLink(String pkgName,
145 String relativepath, String link) {
146 Item fnd = findPackageItem(pkgName);
147 if (fnd != null) {
148 String externlink = fnd.path + link;
149 if (fnd.relative) { // it's a relative path.
150 return relativepath + externlink;
151 } else {
152 return externlink;
153 }
154 }
155 return null;
156 }
158 /**
159 * Build the extern package list from given URL or the directory path.
160 * Flag error if the "-link" or "-linkoffline" option is already used.
161 *
162 * @param url URL or Directory path.
163 * @param pkglisturl This can be another URL for "package-list" or ordinary
164 * file.
165 * @param reporter The <code>DocErrorReporter</code> used to report errors.
166 * @param linkoffline True if -linkoffline isused and false if -link is used.
167 */
168 public boolean url(String url, String pkglisturl,
169 DocErrorReporter reporter, boolean linkoffline) {
170 this.linkoffline = linkoffline;
171 String errMsg = composeExternPackageList(url, pkglisturl);
172 if (errMsg != null) {
173 reporter.printWarning(errMsg);
174 return false;
175 } else {
176 return true;
177 }
178 }
180 /**
181 * Get the Extern Item object associated with this package name.
182 *
183 * @param pkgName Package name.
184 */
185 private Item findPackageItem(String pkgName) {
186 if (packageToItemMap == null) {
187 return null;
188 }
189 return packageToItemMap.get(pkgName);
190 }
192 /**
193 * Adjusts the end file separator if it is missing from the URL or the
194 * directory path and depending upon the URL or file path, fetch or
195 * read the "package-list" file.
196 *
197 * @param urlOrDirPath URL or the directory path.
198 * @param pkgListUrlOrDirPath URL or directory path for the "package-list" file or the "package-list"
199 * file itself.
200 */
201 private String composeExternPackageList(String urlOrDirPath, String pkgListUrlOrDirPath) {
202 urlOrDirPath = adjustEndFileSeparator(urlOrDirPath);
203 pkgListUrlOrDirPath = adjustEndFileSeparator(pkgListUrlOrDirPath);
204 return isUrl(pkgListUrlOrDirPath) ?
205 fetchURLComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath) :
206 readFileComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath);
207 }
209 /**
210 * If the URL or Directory path is missing end file separator, add that.
211 */
212 private String adjustEndFileSeparator(String url) {
213 String filesep = "/";
214 if (!url.endsWith(filesep)) {
215 url += filesep;
216 }
217 return url;
218 }
220 /**
221 * Fetch the URL and read the "package-list" file.
222 *
223 * @param urlpath Path to the packages.
224 * @param pkglisturlpath URL or the path to the "package-list" file.
225 */
226 private String fetchURLComposeExternPackageList(String urlpath,
227 String pkglisturlpath) {
228 String link = pkglisturlpath + "package-list";
229 try {
230 readPackageList((new URL(link)).openStream(), urlpath, false);
231 } catch (MalformedURLException exc) {
232 return configuration.getText("doclet.MalformedURL", link);
233 } catch (IOException exc) {
234 return configuration.getText("doclet.URL_error", link);
235 }
236 return null;
237 }
239 /**
240 * Read the "package-list" file which is available locally.
241 *
242 * @param path URL or directory path to the packages.
243 * @param pkgListPath Path to the local "package-list" file.
244 */
245 private String readFileComposeExternPackageList(String path,
246 String pkgListPath) {
248 String link = pkgListPath + "package-list";
249 if (! ((new File(pkgListPath)).isAbsolute() || linkoffline)){
250 link = configuration.destDirName + link;
251 }
252 try {
253 File file = new File(link);
254 if (file.exists() && file.canRead()) {
255 readPackageList(new FileInputStream(file), path,
256 ! ((new File(path)).isAbsolute() || isUrl(path)));
257 } else {
258 return configuration.getText("doclet.File_error", link);
259 }
260 } catch (FileNotFoundException exc) {
261 return configuration.getText("doclet.File_error", link);
262 } catch (IOException exc) {
263 return configuration.getText("doclet.File_error", link);
264 }
265 return null;
266 }
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));
280 StringBuilder strbuf = new StringBuilder();
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 }
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 }