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