src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DocFile.java

changeset 1383
b980e8e6aabf
child 1412
400a4e8accd3
equal deleted inserted replaced
1382:27f7952eea3c 1383:b980e8e6aabf
1 /*
2 * Copyright (c) 1998, 2012, 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 com.sun.tools.doclets.internal.toolkit.util;
27
28 import java.io.BufferedInputStream;
29 import java.io.BufferedOutputStream;
30 import java.io.BufferedReader;
31 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;
39 import java.io.OutputStream;
40 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
53 /**
54 * Abstraction for handling files, which may be specified directly
55 * (e.g. via a path on the command line) or relative to a Location.
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 8
63 */
64 public class DocFile {
65
66 /**
67 * The doclet configuration.
68 * Provides access to options such as docencoding, output directory, etc.
69 */
70 private final Configuration configuration;
71
72 /**
73 * The location for this file. Maybe null if the file was created without
74 * a location or path.
75 */
76 private final Location location;
77
78 /**
79 * The path relative to the (output) location. Maybe null if the file was
80 * created without a location or path.
81 */
82 private 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
105 /**
106 * List the directories and files found in subdirectories along the
107 * elements of the given location.
108 * @param configuration the doclet configuration
109 * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported.
110 * @param path the subdirectory of the directories of the location for which to
111 * list files
112 */
113 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) {
114 if (location != StandardLocation.SOURCE_PATH)
115 throw new IllegalArgumentException();
116
117 Set<DocFile> files = new LinkedHashSet<DocFile>();
118 for (String s : configuration.sourcepath.split(File.pathSeparator)) {
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;
134 this.location = null;
135 this.path = null;
136 this.file = file;
137 }
138
139 /** Create a DocFile for a given location and relative path. */
140 private DocFile(Configuration configuration, Location location, DocPath path) {
141 this.configuration = configuration;
142 this.location = location;
143 this.path = path;
144 this.file = path.resolveAgainst(configuration.destDirName);
145 }
146
147 /** Open an input stream for the file. */
148 public InputStream openInputStream() throws FileNotFoundException {
149 return new BufferedInputStream(new FileInputStream(file));
150 }
151
152 /**
153 * Open an output stream for the file.
154 * The file must have been created with a location of
155 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
156 */
157 public 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
165 /**
166 * Open an writer for the file, using the encoding (if any) given in the
167 * doclet configuration.
168 * The file must have been created with a location of
169 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
170 */
171 public 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
184 /**
185 * Copy the contents of another file directly to this file.
186 */
187 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();
194 OutputStream output = openOutputStream();
195 try {
196 byte[] bytearr = new byte[1024];
197 int len;
198 while ((len = input.read(bytearr)) != -1) {
199 output.write(bytearr, 0, len);
200 }
201 } catch (FileNotFoundException exc) {
202 } catch (SecurityException exc) {
203 } finally {
204 input.close();
205 output.close();
206 }
207 }
208
209 /**
210 * Copy the contents of a resource file to this file.
211 * @param resource the path of the resource, relative to the package of this class
212 * @param overwrite whether or not to overwrite the file if it already exists
213 * @param replaceNewLine if false, the file is copied as a binary file;
214 * if true, the file is written line by line, using the platform line
215 * separator
216 */
217 public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) {
218 if (location != StandardLocation.CLASS_OUTPUT)
219 throw new IllegalStateException();
220
221 if (file.exists() && !overwrite)
222 return;
223
224 createDirectoryForFile(file);
225
226 try {
227 InputStream in = Configuration.class.getResourceAsStream(resource.getPath());
228 if (in == null)
229 return;
230
231 OutputStream out = new FileOutputStream(file);
232 try {
233 if (!replaceNewLine) {
234 byte[] buf = new byte[2048];
235 int n;
236 while((n = in.read(buf))>0) out.write(buf,0,n);
237 } else {
238 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
239 BufferedWriter writer;
240 if (configuration.docencoding == null) {
241 writer = new BufferedWriter(new OutputStreamWriter(out));
242 } else {
243 writer = new BufferedWriter(new OutputStreamWriter(out,
244 configuration.docencoding));
245 }
246 try {
247 String line;
248 while ((line = reader.readLine()) != null) {
249 writer.write(line);
250 writer.write(DocletConstants.NL);
251 }
252 } finally {
253 reader.close();
254 writer.close();
255 }
256 }
257 } finally {
258 in.close();
259 out.close();
260 }
261 } catch (IOException e) {
262 e.printStackTrace(System.err);
263 throw new DocletAbortException();
264 }
265 }
266
267 /** Return true if the file can be read. */
268 public boolean canRead() {
269 return file.canRead();
270 }
271
272 /** Return true if the file can be written. */
273 public boolean canWrite() {
274 return file.canRead();
275 }
276
277 /** Return true if the file exists. */
278 public boolean exists() {
279 return file.exists();
280 }
281
282 /** Return the base name (last component) of the file name. */
283 public String getName() {
284 return file.getName();
285 }
286
287 /** Return the file system path for this file. */
288 public String getPath() {
289 return file.getPath();
290 }
291
292 /** Return true is file has an absolute path name. */
293 boolean isAbsolute() {
294 return file.isAbsolute();
295 }
296
297 /** Return true is file identifies a directory. */
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
307 /** Return true if this file is the same as another. */
308 public 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
317 /** If the file is a directory, list its contents. */
318 public Iterable<DocFile> list() {
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
326 /** Create the file as a directory, including any parent directories. */
327 public boolean mkdirs() {
328 return file.mkdirs();
329 }
330
331 /**
332 * 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
334 * If this file has a path set, the new file will have a corresponding
335 * new path.
336 */
337 public DocFile resolve(DocPath p) {
338 return resolve(p.getPath());
339 }
340
341 /**
342 * 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
344 * If this file has a path set, the new file will have a corresponding
345 * new path.
346 */
347 public 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
355 /**
356 * Resolve a relative file against the given output location.
357 * @param locn Currently, only SOURCE_OUTPUT is supported.
358 */
359 public 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 }

mercurial