author | briangoetz |
Wed, 18 Dec 2013 16:05:18 -0500 | |
changeset 22163 | 3651128c74eb |
parent 19667 | fdfce85627a9 |
permissions | -rw-r--r-- |
14544 | 1 |
/* |
19667
fdfce85627a9
8001669: javadoc internal DocletAbortException should set cause when appropriate
jjg
parents:
14545
diff
changeset
|
2 |
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
14544 | 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 |
*/ |
|
25 |
||
26 |
package com.sun.tools.doclets.internal.toolkit.util; |
|
27 |
||
28 |
import java.io.BufferedInputStream; |
|
29 |
import java.io.BufferedOutputStream; |
|
30 |
import java.io.BufferedWriter; |
|
31 |
import java.io.File; |
|
32 |
import java.io.IOException; |
|
33 |
import java.io.InputStream; |
|
34 |
import java.io.OutputStream; |
|
35 |
import java.io.OutputStreamWriter; |
|
36 |
import java.io.UnsupportedEncodingException; |
|
37 |
import java.io.Writer; |
|
38 |
import java.util.ArrayList; |
|
39 |
import java.util.Arrays; |
|
40 |
import java.util.LinkedHashSet; |
|
41 |
import java.util.List; |
|
42 |
import java.util.Set; |
|
43 |
||
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
44 |
import javax.tools.DocumentationTool; |
14544 | 45 |
import javax.tools.FileObject; |
46 |
import javax.tools.JavaFileManager.Location; |
|
47 |
import javax.tools.JavaFileObject; |
|
48 |
import javax.tools.StandardJavaFileManager; |
|
49 |
import javax.tools.StandardLocation; |
|
50 |
||
51 |
import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
52 |
import com.sun.tools.javac.util.Assert; |
|
53 |
||
54 |
/** |
|
55 |
* Implementation of DocFileFactory using a {@link StandardJavaFileManager}. |
|
56 |
* |
|
57 |
* <p><b>This is NOT part of any supported API. |
|
58 |
* If you write code that depends on this, you do so at your own risk. |
|
59 |
* This code and its internal interfaces are subject to change or |
|
60 |
* deletion without notice.</b> |
|
61 |
* |
|
62 |
* @since 1.8 |
|
63 |
*/ |
|
64 |
class StandardDocFileFactory extends DocFileFactory { |
|
65 |
private final StandardJavaFileManager fileManager; |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
66 |
private File destDir; |
14544 | 67 |
|
68 |
public StandardDocFileFactory(Configuration configuration) { |
|
69 |
super(configuration); |
|
70 |
fileManager = (StandardJavaFileManager) configuration.getFileManager(); |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
71 |
} |
14544 | 72 |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
73 |
private File getDestDir() { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
74 |
if (destDir == null) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
75 |
if (!configuration.destDirName.isEmpty() |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
76 |
|| !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
77 |
try { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
78 |
String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
79 |
File dir = new File(dirName); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
80 |
fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
81 |
} catch (IOException e) { |
19667
fdfce85627a9
8001669: javadoc internal DocletAbortException should set cause when appropriate
jjg
parents:
14545
diff
changeset
|
82 |
throw new DocletAbortException(e); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
83 |
} |
14544 | 84 |
} |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
85 |
|
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
86 |
destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); |
14544 | 87 |
} |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
88 |
return destDir; |
14544 | 89 |
} |
90 |
||
91 |
public DocFile createFileForDirectory(String file) { |
|
92 |
return new StandardDocFile(new File(file)); |
|
93 |
} |
|
94 |
||
95 |
public DocFile createFileForInput(String file) { |
|
96 |
return new StandardDocFile(new File(file)); |
|
97 |
} |
|
98 |
||
99 |
public DocFile createFileForOutput(DocPath path) { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
100 |
return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); |
14544 | 101 |
} |
102 |
||
103 |
@Override |
|
104 |
Iterable<DocFile> list(Location location, DocPath path) { |
|
105 |
if (location != StandardLocation.SOURCE_PATH) |
|
106 |
throw new IllegalArgumentException(); |
|
107 |
||
22163 | 108 |
Set<DocFile> files = new LinkedHashSet<>(); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
109 |
Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
110 |
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
111 |
for (File f: fileManager.getLocation(l)) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
112 |
if (f.isDirectory()) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
113 |
f = new File(f, path.getPath()); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
114 |
if (f.exists()) |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
115 |
files.add(new StandardDocFile(f)); |
14544 | 116 |
} |
117 |
} |
|
118 |
return files; |
|
119 |
} |
|
120 |
||
121 |
private static File newFile(File dir, String path) { |
|
122 |
return (dir == null) ? new File(path) : new File(dir, path); |
|
123 |
} |
|
124 |
||
125 |
class StandardDocFile extends DocFile { |
|
126 |
private File file; |
|
127 |
||
128 |
||
129 |
/** Create a StandardDocFile for a given file. */ |
|
130 |
private StandardDocFile(File file) { |
|
131 |
super(configuration); |
|
132 |
this.file = file; |
|
133 |
} |
|
134 |
||
135 |
/** Create a StandardDocFile for a given location and relative path. */ |
|
136 |
private StandardDocFile(Location location, DocPath path) { |
|
137 |
super(configuration, location, path); |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
138 |
Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT); |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
139 |
this.file = newFile(getDestDir(), path.getPath()); |
14544 | 140 |
} |
141 |
||
142 |
/** Open an input stream for the file. */ |
|
143 |
public InputStream openInputStream() throws IOException { |
|
144 |
JavaFileObject fo = getJavaFileObjectForInput(file); |
|
145 |
return new BufferedInputStream(fo.openInputStream()); |
|
146 |
} |
|
147 |
||
148 |
/** |
|
149 |
* Open an output stream for the file. |
|
150 |
* The file must have been created with a location of |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
151 |
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
14544 | 152 |
*/ |
153 |
public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
154 |
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
14544 | 155 |
throw new IllegalStateException(); |
156 |
||
157 |
OutputStream out = getFileObjectForOutput(path).openOutputStream(); |
|
158 |
return new BufferedOutputStream(out); |
|
159 |
} |
|
160 |
||
161 |
/** |
|
162 |
* Open an writer for the file, using the encoding (if any) given in the |
|
163 |
* doclet configuration. |
|
164 |
* The file must have been created with a location of |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
165 |
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
14544 | 166 |
*/ |
167 |
public Writer openWriter() throws IOException, UnsupportedEncodingException { |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
168 |
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
14544 | 169 |
throw new IllegalStateException(); |
170 |
||
171 |
OutputStream out = getFileObjectForOutput(path).openOutputStream(); |
|
172 |
if (configuration.docencoding == null) { |
|
173 |
return new BufferedWriter(new OutputStreamWriter(out)); |
|
174 |
} else { |
|
175 |
return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding)); |
|
176 |
} |
|
177 |
} |
|
178 |
||
179 |
/** Return true if the file can be read. */ |
|
180 |
public boolean canRead() { |
|
181 |
return file.canRead(); |
|
182 |
} |
|
183 |
||
184 |
/** Return true if the file can be written. */ |
|
185 |
public boolean canWrite() { |
|
186 |
return file.canWrite(); |
|
187 |
} |
|
188 |
||
189 |
/** Return true if the file exists. */ |
|
190 |
public boolean exists() { |
|
191 |
return file.exists(); |
|
192 |
} |
|
193 |
||
194 |
/** Return the base name (last component) of the file name. */ |
|
195 |
public String getName() { |
|
196 |
return file.getName(); |
|
197 |
} |
|
198 |
||
199 |
/** Return the file system path for this file. */ |
|
200 |
public String getPath() { |
|
201 |
return file.getPath(); |
|
202 |
} |
|
203 |
||
204 |
/** Return true is file has an absolute path name. */ |
|
205 |
public boolean isAbsolute() { |
|
206 |
return file.isAbsolute(); |
|
207 |
} |
|
208 |
||
209 |
/** Return true is file identifies a directory. */ |
|
210 |
public boolean isDirectory() { |
|
211 |
return file.isDirectory(); |
|
212 |
} |
|
213 |
||
214 |
/** Return true is file identifies a file. */ |
|
215 |
public boolean isFile() { |
|
216 |
return file.isFile(); |
|
217 |
} |
|
218 |
||
219 |
/** Return true if this file is the same as another. */ |
|
220 |
public boolean isSameFile(DocFile other) { |
|
221 |
if (!(other instanceof StandardDocFile)) |
|
222 |
return false; |
|
223 |
||
224 |
try { |
|
225 |
return file.exists() |
|
226 |
&& file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile()); |
|
227 |
} catch (IOException e) { |
|
228 |
return false; |
|
229 |
} |
|
230 |
} |
|
231 |
||
232 |
/** If the file is a directory, list its contents. */ |
|
233 |
public Iterable<DocFile> list() { |
|
22163 | 234 |
List<DocFile> files = new ArrayList<>(); |
14544 | 235 |
for (File f: file.listFiles()) { |
236 |
files.add(new StandardDocFile(f)); |
|
237 |
} |
|
238 |
return files; |
|
239 |
} |
|
240 |
||
241 |
/** Create the file as a directory, including any parent directories. */ |
|
242 |
public boolean mkdirs() { |
|
243 |
return file.mkdirs(); |
|
244 |
} |
|
245 |
||
246 |
/** |
|
247 |
* Derive a new file by resolving a relative path against this file. |
|
248 |
* The new file will inherit the configuration and location of this file |
|
249 |
* If this file has a path set, the new file will have a corresponding |
|
250 |
* new path. |
|
251 |
*/ |
|
252 |
public DocFile resolve(DocPath p) { |
|
253 |
return resolve(p.getPath()); |
|
254 |
} |
|
255 |
||
256 |
/** |
|
257 |
* Derive a new file by resolving a relative path against this file. |
|
258 |
* The new file will inherit the configuration and location of this file |
|
259 |
* If this file has a path set, the new file will have a corresponding |
|
260 |
* new path. |
|
261 |
*/ |
|
262 |
public DocFile resolve(String p) { |
|
263 |
if (location == null && path == null) { |
|
264 |
return new StandardDocFile(new File(file, p)); |
|
265 |
} else { |
|
266 |
return new StandardDocFile(location, path.resolve(p)); |
|
267 |
} |
|
268 |
} |
|
269 |
||
270 |
/** |
|
271 |
* Resolve a relative file against the given output location. |
|
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
272 |
* @param locn Currently, only |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
273 |
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. |
14544 | 274 |
*/ |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
275 |
public DocFile resolveAgainst(Location locn) { |
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
276 |
if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
14544 | 277 |
throw new IllegalArgumentException(); |
14545
2e7bab0639b8
6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents:
14544
diff
changeset
|
278 |
return new StandardDocFile(newFile(getDestDir(), file.getPath())); |
14544 | 279 |
} |
280 |
||
281 |
/** Return a string to identify the contents of this object, |
|
282 |
* for debugging purposes. |
|
283 |
*/ |
|
284 |
@Override |
|
285 |
public String toString() { |
|
286 |
StringBuilder sb = new StringBuilder(); |
|
287 |
sb.append("StandardDocFile["); |
|
288 |
if (location != null) |
|
289 |
sb.append("locn:").append(location).append(","); |
|
290 |
if (path != null) |
|
291 |
sb.append("path:").append(path.getPath()).append(","); |
|
292 |
sb.append("file:").append(file); |
|
293 |
sb.append("]"); |
|
294 |
return sb.toString(); |
|
295 |
} |
|
296 |
||
297 |
private JavaFileObject getJavaFileObjectForInput(File file) { |
|
298 |
return fileManager.getJavaFileObjects(file).iterator().next(); |
|
299 |
} |
|
300 |
||
301 |
private FileObject getFileObjectForOutput(DocPath path) throws IOException { |
|
302 |
// break the path into a package-part and the rest, by finding |
|
303 |
// the position of the last '/' before an invalid character for a |
|
304 |
// package name, such as the "." before an extension or the "-" |
|
305 |
// in filenames like package-summary.html, doc-files or src-html. |
|
306 |
String p = path.getPath(); |
|
307 |
int lastSep = -1; |
|
308 |
for (int i = 0; i < p.length(); i++) { |
|
309 |
char ch = p.charAt(i); |
|
310 |
if (ch == '/') { |
|
311 |
lastSep = i; |
|
312 |
} else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch) |
|
313 |
|| !Character.isJavaIdentifierPart(ch)) { |
|
314 |
break; |
|
315 |
} |
|
316 |
} |
|
317 |
String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep); |
|
318 |
String rest = p.substring(lastSep + 1); |
|
319 |
return fileManager.getFileForOutput(location, pkg, rest, null); |
|
320 |
} |
|
321 |
} |
|
322 |
} |