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