23 * questions. |
23 * questions. |
24 */ |
24 */ |
25 |
25 |
26 package com.sun.tools.doclets.internal.toolkit.util; |
26 package com.sun.tools.doclets.internal.toolkit.util; |
27 |
27 |
28 import java.io.BufferedInputStream; |
28 import java.io.FileNotFoundException; |
29 import java.io.BufferedOutputStream; |
29 import java.io.IOException; |
|
30 import java.io.InputStream; |
|
31 import java.io.OutputStream; |
|
32 import java.io.UnsupportedEncodingException; |
|
33 import java.io.Writer; |
|
34 |
|
35 import javax.tools.JavaFileManager.Location; |
|
36 import javax.tools.StandardLocation; |
|
37 |
|
38 import com.sun.tools.doclets.internal.toolkit.Configuration; |
30 import java.io.BufferedReader; |
39 import java.io.BufferedReader; |
31 import java.io.BufferedWriter; |
40 import java.io.BufferedWriter; |
32 import java.io.File; |
|
33 import java.io.FileInputStream; |
|
34 import java.io.FileNotFoundException; |
|
35 import java.io.FileOutputStream; |
|
36 import java.io.IOException; |
|
37 import java.io.InputStream; |
|
38 import java.io.InputStreamReader; |
41 import java.io.InputStreamReader; |
39 import java.io.OutputStream; |
|
40 import java.io.OutputStreamWriter; |
42 import java.io.OutputStreamWriter; |
41 import java.io.UnsupportedEncodingException; |
|
42 import java.io.Writer; |
|
43 import java.util.ArrayList; |
|
44 import java.util.LinkedHashSet; |
|
45 import java.util.List; |
|
46 import java.util.Set; |
|
47 |
|
48 import javax.tools.JavaFileManager.Location; |
|
49 import javax.tools.StandardLocation; |
|
50 |
|
51 import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
52 |
43 |
53 /** |
44 /** |
54 * Abstraction for handling files, which may be specified directly |
45 * Abstraction for handling files, which may be specified directly |
55 * (e.g. via a path on the command line) or relative to a Location. |
46 * (e.g. via a path on the command line) or relative to a Location. |
56 * |
47 * |
59 * This code and its internal interfaces are subject to change or |
50 * This code and its internal interfaces are subject to change or |
60 * deletion without notice.</b> |
51 * deletion without notice.</b> |
61 * |
52 * |
62 * @since 8 |
53 * @since 8 |
63 */ |
54 */ |
64 public class DocFile { |
55 public abstract class DocFile { |
65 |
56 |
66 /** |
57 /** Create a DocFile for a directory. */ |
67 * The doclet configuration. |
58 public static DocFile createFileForDirectory(Configuration configuration, String file) { |
68 * Provides access to options such as docencoding, output directory, etc. |
59 return DocFileFactory.getFactory(configuration).createFileForDirectory(file); |
69 */ |
60 } |
|
61 |
|
62 /** Create a DocFile for a file that will be opened for reading. */ |
|
63 public static DocFile createFileForInput(Configuration configuration, String file) { |
|
64 return DocFileFactory.getFactory(configuration).createFileForInput(file); |
|
65 } |
|
66 |
|
67 /** Create a DocFile for a file that will be opened for writing. */ |
|
68 public static DocFile createFileForOutput(Configuration configuration, DocPath path) { |
|
69 return DocFileFactory.getFactory(configuration).createFileForOutput(path); |
|
70 } |
|
71 |
70 private final Configuration configuration; |
72 private final Configuration configuration; |
71 |
73 |
72 /** |
74 /** |
73 * The location for this file. Maybe null if the file was created without |
75 * The location for this file. Maybe null if the file was created without |
74 * a location or path. |
76 * a location or path. |
75 */ |
77 */ |
76 private final Location location; |
78 protected final Location location; |
77 |
79 |
78 /** |
80 /** |
79 * The path relative to the (output) location. Maybe null if the file was |
81 * The path relative to the (output) location. Maybe null if the file was |
80 * created without a location or path. |
82 * created without a location or path. |
81 */ |
83 */ |
82 private final DocPath path; |
84 protected final DocPath path; |
83 |
|
84 /** |
|
85 * The file object itself. |
|
86 * This is temporary, until we create different subtypes of DocFile. |
|
87 */ |
|
88 private final File file; |
|
89 |
|
90 /** Create a DocFile for a directory. */ |
|
91 public static DocFile createFileForDirectory(Configuration configuration, String file) { |
|
92 return new DocFile(configuration, new File(file)); |
|
93 } |
|
94 |
|
95 /** Create a DocFile for a file that will be opened for reading. */ |
|
96 public static DocFile createFileForInput(Configuration configuration, String file) { |
|
97 return new DocFile(configuration, new File(file)); |
|
98 } |
|
99 |
|
100 /** Create a DocFile for a file that will be opened for writing. */ |
|
101 public static DocFile createFileForOutput(Configuration configuration, DocPath path) { |
|
102 return new DocFile(configuration, StandardLocation.CLASS_OUTPUT, path); |
|
103 } |
|
104 |
85 |
105 /** |
86 /** |
106 * List the directories and files found in subdirectories along the |
87 * List the directories and files found in subdirectories along the |
107 * elements of the given location. |
88 * elements of the given location. |
108 * @param configuration the doclet configuration |
89 * @param configuration the doclet configuration |
109 * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported. |
90 * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported. |
110 * @param path the subdirectory of the directories of the location for which to |
91 * @param path the subdirectory of the directories of the location for which to |
111 * list files |
92 * list files |
112 */ |
93 */ |
113 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) { |
94 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) { |
114 if (location != StandardLocation.SOURCE_PATH) |
95 return DocFileFactory.getFactory(configuration).list(location, path); |
115 throw new IllegalArgumentException(); |
96 } |
116 |
97 |
117 Set<DocFile> files = new LinkedHashSet<DocFile>(); |
98 /** Create a DocFile without a location or path */ |
118 for (String s : configuration.sourcepath.split(File.pathSeparator)) { |
99 protected DocFile(Configuration configuration) { |
119 if (s.isEmpty()) |
|
120 continue; |
|
121 File f = new File(s); |
|
122 if (f.isDirectory()) { |
|
123 f = new File(f, path.getPath()); |
|
124 if (f.exists()) |
|
125 files.add(new DocFile(configuration, f)); |
|
126 } |
|
127 } |
|
128 return files; |
|
129 } |
|
130 |
|
131 /** Create a DocFile for a given file. */ |
|
132 private DocFile(Configuration configuration, File file) { |
|
133 this.configuration = configuration; |
100 this.configuration = configuration; |
134 this.location = null; |
101 this.location = null; |
135 this.path = null; |
102 this.path = null; |
136 this.file = file; |
|
137 } |
103 } |
138 |
104 |
139 /** Create a DocFile for a given location and relative path. */ |
105 /** Create a DocFile for a given location and relative path. */ |
140 private DocFile(Configuration configuration, Location location, DocPath path) { |
106 protected DocFile(Configuration configuration, Location location, DocPath path) { |
141 this.configuration = configuration; |
107 this.configuration = configuration; |
142 this.location = location; |
108 this.location = location; |
143 this.path = path; |
109 this.path = path; |
144 this.file = path.resolveAgainst(configuration.destDirName); |
|
145 } |
110 } |
146 |
111 |
147 /** Open an input stream for the file. */ |
112 /** Open an input stream for the file. */ |
148 public InputStream openInputStream() throws FileNotFoundException { |
113 public abstract InputStream openInputStream() throws IOException; |
149 return new BufferedInputStream(new FileInputStream(file)); |
|
150 } |
|
151 |
114 |
152 /** |
115 /** |
153 * Open an output stream for the file. |
116 * Open an output stream for the file. |
154 * The file must have been created with a location of |
117 * The file must have been created with a location of |
155 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
118 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
156 */ |
119 */ |
157 public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
120 public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException; |
158 if (location != StandardLocation.CLASS_OUTPUT) |
|
159 throw new IllegalStateException(); |
|
160 |
|
161 createDirectoryForFile(file); |
|
162 return new BufferedOutputStream(new FileOutputStream(file)); |
|
163 } |
|
164 |
121 |
165 /** |
122 /** |
166 * Open an writer for the file, using the encoding (if any) given in the |
123 * Open an writer for the file, using the encoding (if any) given in the |
167 * doclet configuration. |
124 * doclet configuration. |
168 * The file must have been created with a location of |
125 * The file must have been created with a location of |
169 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
126 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
170 */ |
127 */ |
171 public Writer openWriter() throws IOException, UnsupportedEncodingException { |
128 public abstract Writer openWriter() throws IOException, UnsupportedEncodingException; |
172 if (location != StandardLocation.CLASS_OUTPUT) |
|
173 throw new IllegalStateException(); |
|
174 |
|
175 createDirectoryForFile(file); |
|
176 FileOutputStream fos = new FileOutputStream(file); |
|
177 if (configuration.docencoding == null) { |
|
178 return new BufferedWriter(new OutputStreamWriter(fos)); |
|
179 } else { |
|
180 return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); |
|
181 } |
|
182 } |
|
183 |
129 |
184 /** |
130 /** |
185 * Copy the contents of another file directly to this file. |
131 * Copy the contents of another file directly to this file. |
186 */ |
132 */ |
187 public void copyFile(DocFile fromFile) throws IOException { |
133 public void copyFile(DocFile fromFile) throws IOException { |
188 if (location != StandardLocation.CLASS_OUTPUT) |
|
189 throw new IllegalStateException(); |
|
190 |
|
191 createDirectoryForFile(file); |
|
192 |
|
193 InputStream input = fromFile.openInputStream(); |
134 InputStream input = fromFile.openInputStream(); |
194 OutputStream output = openOutputStream(); |
135 OutputStream output = openOutputStream(); |
195 try { |
136 try { |
196 byte[] bytearr = new byte[1024]; |
137 byte[] bytearr = new byte[1024]; |
197 int len; |
138 int len; |
263 throw new DocletAbortException(); |
199 throw new DocletAbortException(); |
264 } |
200 } |
265 } |
201 } |
266 |
202 |
267 /** Return true if the file can be read. */ |
203 /** Return true if the file can be read. */ |
268 public boolean canRead() { |
204 public abstract boolean canRead(); |
269 return file.canRead(); |
|
270 } |
|
271 |
205 |
272 /** Return true if the file can be written. */ |
206 /** Return true if the file can be written. */ |
273 public boolean canWrite() { |
207 public abstract boolean canWrite(); |
274 return file.canRead(); |
|
275 } |
|
276 |
208 |
277 /** Return true if the file exists. */ |
209 /** Return true if the file exists. */ |
278 public boolean exists() { |
210 public abstract boolean exists(); |
279 return file.exists(); |
|
280 } |
|
281 |
211 |
282 /** Return the base name (last component) of the file name. */ |
212 /** Return the base name (last component) of the file name. */ |
283 public String getName() { |
213 public abstract String getName(); |
284 return file.getName(); |
|
285 } |
|
286 |
214 |
287 /** Return the file system path for this file. */ |
215 /** Return the file system path for this file. */ |
288 public String getPath() { |
216 public abstract String getPath(); |
289 return file.getPath(); |
217 |
290 } |
218 /** Return true if file has an absolute path name. */ |
291 |
219 public abstract boolean isAbsolute(); |
292 /** Return true is file has an absolute path name. */ |
220 |
293 boolean isAbsolute() { |
221 /** Return true if file identifies a directory. */ |
294 return file.isAbsolute(); |
222 public abstract boolean isDirectory(); |
295 } |
223 |
296 |
224 /** Return true if file identifies a file. */ |
297 /** Return true is file identifies a directory. */ |
225 public abstract boolean isFile(); |
298 public boolean isDirectory() { |
|
299 return file.isDirectory(); |
|
300 } |
|
301 |
|
302 /** Return true is file identifies a file. */ |
|
303 public boolean isFile() { |
|
304 return file.isFile(); |
|
305 } |
|
306 |
226 |
307 /** Return true if this file is the same as another. */ |
227 /** Return true if this file is the same as another. */ |
308 public boolean isSameFile(DocFile other) { |
228 public abstract boolean isSameFile(DocFile other); |
309 try { |
|
310 return file.exists() |
|
311 && file.getCanonicalFile().equals(other.file.getCanonicalFile()); |
|
312 } catch (IOException e) { |
|
313 return false; |
|
314 } |
|
315 } |
|
316 |
229 |
317 /** If the file is a directory, list its contents. */ |
230 /** If the file is a directory, list its contents. */ |
318 public Iterable<DocFile> list() { |
231 public abstract Iterable<DocFile> list() throws IOException; |
319 List<DocFile> files = new ArrayList<DocFile>(); |
|
320 for (File f: file.listFiles()) { |
|
321 files.add(new DocFile(configuration, f)); |
|
322 } |
|
323 return files; |
|
324 } |
|
325 |
232 |
326 /** Create the file as a directory, including any parent directories. */ |
233 /** Create the file as a directory, including any parent directories. */ |
327 public boolean mkdirs() { |
234 public abstract boolean mkdirs(); |
328 return file.mkdirs(); |
|
329 } |
|
330 |
235 |
331 /** |
236 /** |
332 * Derive a new file by resolving a relative path against this file. |
237 * Derive a new file by resolving a relative path against this file. |
333 * The new file will inherit the configuration and location of this file |
238 * The new file will inherit the configuration and location of this file |
334 * If this file has a path set, the new file will have a corresponding |
239 * If this file has a path set, the new file will have a corresponding |
335 * new path. |
240 * new path. |
336 */ |
241 */ |
337 public DocFile resolve(DocPath p) { |
242 public abstract DocFile resolve(DocPath p); |
338 return resolve(p.getPath()); |
|
339 } |
|
340 |
243 |
341 /** |
244 /** |
342 * Derive a new file by resolving a relative path against this file. |
245 * Derive a new file by resolving a relative path against this file. |
343 * The new file will inherit the configuration and location of this file |
246 * The new file will inherit the configuration and location of this file |
344 * If this file has a path set, the new file will have a corresponding |
247 * If this file has a path set, the new file will have a corresponding |
345 * new path. |
248 * new path. |
346 */ |
249 */ |
347 public DocFile resolve(String p) { |
250 public abstract DocFile resolve(String p); |
348 if (location == null && path == null) { |
|
349 return new DocFile(configuration, new File(file, p)); |
|
350 } else { |
|
351 return new DocFile(configuration, location, path.resolve(p)); |
|
352 } |
|
353 } |
|
354 |
251 |
355 /** |
252 /** |
356 * Resolve a relative file against the given output location. |
253 * Resolve a relative file against the given output location. |
357 * @param locn Currently, only SOURCE_OUTPUT is supported. |
254 * @param locn Currently, only SOURCE_OUTPUT is supported. |
358 */ |
255 */ |
359 public DocFile resolveAgainst(StandardLocation locn) { |
256 public abstract DocFile resolveAgainst(StandardLocation locn); |
360 if (locn != StandardLocation.CLASS_OUTPUT) |
|
361 throw new IllegalArgumentException(); |
|
362 return new DocFile(configuration, |
|
363 new File(configuration.destDirName, file.getPath())); |
|
364 } |
|
365 |
|
366 /** |
|
367 * Given a path string create all the directories in the path. For example, |
|
368 * if the path string is "java/applet", the method will create directory |
|
369 * "java" and then "java/applet" if they don't exist. The file separator |
|
370 * string "/" is platform dependent system property. |
|
371 * |
|
372 * @param path Directory path string. |
|
373 */ |
|
374 private void createDirectoryForFile(File file) { |
|
375 File dir = file.getParentFile(); |
|
376 if (dir == null || dir.exists() || dir.mkdirs()) |
|
377 return; |
|
378 |
|
379 configuration.message.error( |
|
380 "doclet.Unable_to_create_directory_0", dir.getPath()); |
|
381 throw new DocletAbortException(); |
|
382 } |
|
383 |
|
384 /** Return a string to identify the contents of this object, |
|
385 * for debugging purposes. |
|
386 */ |
|
387 @Override |
|
388 public String toString() { |
|
389 StringBuilder sb = new StringBuilder(); |
|
390 sb.append("DocFile["); |
|
391 if (location != null) |
|
392 sb.append("locn:").append(location).append(","); |
|
393 if (path != null) |
|
394 sb.append("path:").append(path.getPath()).append(","); |
|
395 sb.append("file:").append(file); |
|
396 sb.append("]"); |
|
397 return sb.toString(); |
|
398 } |
|
399 } |
257 } |