|
1 /* |
|
2 * Copyright (c) 1998, 2016, 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 |
|
26 package jdk.javadoc.internal.doclets.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 |
|
44 import javax.tools.DocumentationTool; |
|
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 jdk.javadoc.internal.doclets.toolkit.Configuration; |
|
52 |
|
53 /** |
|
54 * Implementation of DocFileFactory using a {@link StandardJavaFileManager}. |
|
55 * |
|
56 * <p><b>This is NOT part of any supported API. |
|
57 * If you write code that depends on this, you do so at your own risk. |
|
58 * This code and its internal interfaces are subject to change or |
|
59 * deletion without notice.</b> |
|
60 * |
|
61 */ |
|
62 class StandardDocFileFactory extends DocFileFactory { |
|
63 private final StandardJavaFileManager fileManager; |
|
64 private File destDir; |
|
65 |
|
66 public StandardDocFileFactory(Configuration configuration) { |
|
67 super(configuration); |
|
68 fileManager = (StandardJavaFileManager) configuration.getFileManager(); |
|
69 } |
|
70 |
|
71 private File getDestDir() { |
|
72 if (destDir == null) { |
|
73 if (!configuration.destDirName.isEmpty() |
|
74 || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { |
|
75 try { |
|
76 String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; |
|
77 File dir = new File(dirName); |
|
78 fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); |
|
79 } catch (IOException e) { |
|
80 throw new DocletAbortException(e); |
|
81 } |
|
82 } |
|
83 |
|
84 destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); |
|
85 } |
|
86 return destDir; |
|
87 } |
|
88 |
|
89 public DocFile createFileForDirectory(String file) { |
|
90 return new StandardDocFile(new File(file)); |
|
91 } |
|
92 |
|
93 public DocFile createFileForInput(String file) { |
|
94 return new StandardDocFile(new File(file)); |
|
95 } |
|
96 |
|
97 public DocFile createFileForOutput(DocPath path) { |
|
98 return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); |
|
99 } |
|
100 |
|
101 @Override |
|
102 Iterable<DocFile> list(Location location, DocPath path) { |
|
103 if (location != StandardLocation.SOURCE_PATH) |
|
104 throw new IllegalArgumentException(); |
|
105 |
|
106 Set<DocFile> files = new LinkedHashSet<>(); |
|
107 Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH) |
|
108 ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; |
|
109 for (File f: fileManager.getLocation(l)) { |
|
110 if (f.isDirectory()) { |
|
111 f = new File(f, path.getPath()); |
|
112 if (f.exists()) |
|
113 files.add(new StandardDocFile(f)); |
|
114 } |
|
115 } |
|
116 return files; |
|
117 } |
|
118 |
|
119 private static File newFile(File dir, String path) { |
|
120 return (dir == null) ? new File(path) : new File(dir, path); |
|
121 } |
|
122 |
|
123 class StandardDocFile extends DocFile { |
|
124 private File file; |
|
125 |
|
126 |
|
127 /** Create a StandardDocFile for a given file. */ |
|
128 private StandardDocFile(File file) { |
|
129 super(configuration); |
|
130 this.file = file; |
|
131 } |
|
132 |
|
133 /** Create a StandardDocFile for a given location and relative path. */ |
|
134 private StandardDocFile(Location location, DocPath path) { |
|
135 super(configuration, location, path); |
|
136 if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) { |
|
137 throw new AssertionError("invalid location output"); |
|
138 } |
|
139 this.file = newFile(getDestDir(), path.getPath()); |
|
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 |
|
151 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
|
152 */ |
|
153 public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
|
154 if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
|
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 |
|
165 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
|
166 */ |
|
167 public Writer openWriter() throws IOException, UnsupportedEncodingException { |
|
168 if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
|
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() { |
|
234 List<DocFile> files = new ArrayList<>(); |
|
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. |
|
272 * @param locn Currently, only |
|
273 * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. |
|
274 */ |
|
275 public DocFile resolveAgainst(Location locn) { |
|
276 if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
|
277 throw new IllegalArgumentException(); |
|
278 return new StandardDocFile(newFile(getDestDir(), file.getPath())); |
|
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 } |