|
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.BufferedWriter; |
|
31 import java.io.File; |
|
32 import java.io.FileInputStream; |
|
33 import java.io.FileNotFoundException; |
|
34 import java.io.FileOutputStream; |
|
35 import java.io.IOException; |
|
36 import java.io.InputStream; |
|
37 import java.io.OutputStream; |
|
38 import java.io.OutputStreamWriter; |
|
39 import java.io.UnsupportedEncodingException; |
|
40 import java.io.Writer; |
|
41 import java.util.ArrayList; |
|
42 import java.util.LinkedHashSet; |
|
43 import java.util.List; |
|
44 import java.util.Set; |
|
45 |
|
46 import javax.tools.JavaFileManager.Location; |
|
47 import javax.tools.StandardLocation; |
|
48 |
|
49 import com.sun.tools.doclets.internal.toolkit.Configuration; |
|
50 |
|
51 /** |
|
52 * Implementation of DocFileFactory that just uses java.io.File API, |
|
53 * and does not use a JavaFileManager.. |
|
54 * |
|
55 * <p><b>This is NOT part of any supported API. |
|
56 * If you write code that depends on this, you do so at your own risk. |
|
57 * This code and its internal interfaces are subject to change or |
|
58 * deletion without notice.</b> |
|
59 * |
|
60 * @since 1.8 |
|
61 */ |
|
62 class SimpleDocFileFactory extends DocFileFactory { |
|
63 |
|
64 public SimpleDocFileFactory(Configuration configuration) { |
|
65 super(configuration); |
|
66 } |
|
67 |
|
68 public DocFile createFileForDirectory(String file) { |
|
69 return new SimpleDocFile(new File(file)); |
|
70 } |
|
71 |
|
72 public DocFile createFileForInput(String file) { |
|
73 return new SimpleDocFile(new File(file)); |
|
74 } |
|
75 |
|
76 public DocFile createFileForOutput(DocPath path) { |
|
77 return new SimpleDocFile(StandardLocation.CLASS_OUTPUT, path); |
|
78 } |
|
79 |
|
80 @Override |
|
81 Iterable<DocFile> list(Location location, DocPath path) { |
|
82 if (location != StandardLocation.SOURCE_PATH) |
|
83 throw new IllegalArgumentException(); |
|
84 |
|
85 Set<DocFile> files = new LinkedHashSet<DocFile>(); |
|
86 for (String s : configuration.sourcepath.split(File.pathSeparator)) { |
|
87 if (s.isEmpty()) |
|
88 continue; |
|
89 File f = new File(s); |
|
90 if (f.isDirectory()) { |
|
91 f = new File(f, path.getPath()); |
|
92 if (f.exists()) |
|
93 files.add(new SimpleDocFile(f)); |
|
94 } |
|
95 } |
|
96 return files; |
|
97 } |
|
98 |
|
99 class SimpleDocFile extends DocFile { |
|
100 private File file; |
|
101 |
|
102 /** Create a DocFile for a given file. */ |
|
103 private SimpleDocFile(File file) { |
|
104 super(configuration); |
|
105 this.file = file; |
|
106 } |
|
107 |
|
108 /** Create a DocFile for a given location and relative path. */ |
|
109 private SimpleDocFile(Location location, DocPath path) { |
|
110 super(configuration, location, path); |
|
111 String destDirName = configuration.destDirName; |
|
112 this.file = destDirName.isEmpty() ? new File(path.getPath()) |
|
113 : new File(destDirName, path.getPath()); |
|
114 } |
|
115 |
|
116 /** Open an input stream for the file. */ |
|
117 public InputStream openInputStream() throws FileNotFoundException { |
|
118 return new BufferedInputStream(new FileInputStream(file)); |
|
119 } |
|
120 |
|
121 /** |
|
122 * Open an output stream for the file. |
|
123 * The file must have been created with a location of |
|
124 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
|
125 */ |
|
126 public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
|
127 if (location != StandardLocation.CLASS_OUTPUT) |
|
128 throw new IllegalStateException(); |
|
129 |
|
130 createDirectoryForFile(file); |
|
131 return new BufferedOutputStream(new FileOutputStream(file)); |
|
132 } |
|
133 |
|
134 /** |
|
135 * Open an writer for the file, using the encoding (if any) given in the |
|
136 * doclet configuration. |
|
137 * The file must have been created with a location of |
|
138 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
|
139 */ |
|
140 public Writer openWriter() throws IOException, UnsupportedEncodingException { |
|
141 if (location != StandardLocation.CLASS_OUTPUT) |
|
142 throw new IllegalStateException(); |
|
143 |
|
144 createDirectoryForFile(file); |
|
145 FileOutputStream fos = new FileOutputStream(file); |
|
146 if (configuration.docencoding == null) { |
|
147 return new BufferedWriter(new OutputStreamWriter(fos)); |
|
148 } else { |
|
149 return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); |
|
150 } |
|
151 } |
|
152 |
|
153 /** Return true if the file can be read. */ |
|
154 public boolean canRead() { |
|
155 return file.canRead(); |
|
156 } |
|
157 |
|
158 /** Return true if the file can be written. */ |
|
159 public boolean canWrite() { |
|
160 return file.canRead(); |
|
161 } |
|
162 |
|
163 /** Return true if the file exists. */ |
|
164 public boolean exists() { |
|
165 return file.exists(); |
|
166 } |
|
167 |
|
168 /** Return the base name (last component) of the file name. */ |
|
169 public String getName() { |
|
170 return file.getName(); |
|
171 } |
|
172 |
|
173 /** Return the file system path for this file. */ |
|
174 public String getPath() { |
|
175 return file.getPath(); |
|
176 } |
|
177 |
|
178 /** Return true is file has an absolute path name. */ |
|
179 public boolean isAbsolute() { |
|
180 return file.isAbsolute(); |
|
181 } |
|
182 |
|
183 /** Return true is file identifies a directory. */ |
|
184 public boolean isDirectory() { |
|
185 return file.isDirectory(); |
|
186 } |
|
187 |
|
188 /** Return true is file identifies a file. */ |
|
189 public boolean isFile() { |
|
190 return file.isFile(); |
|
191 } |
|
192 |
|
193 /** Return true if this file is the same as another. */ |
|
194 public boolean isSameFile(DocFile other) { |
|
195 if (!(other instanceof SimpleDocFile)) |
|
196 return false; |
|
197 |
|
198 try { |
|
199 return file.exists() |
|
200 && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile()); |
|
201 } catch (IOException e) { |
|
202 return false; |
|
203 } |
|
204 } |
|
205 |
|
206 /** If the file is a directory, list its contents. */ |
|
207 public Iterable<DocFile> list() { |
|
208 List<DocFile> files = new ArrayList<DocFile>(); |
|
209 for (File f: file.listFiles()) { |
|
210 files.add(new SimpleDocFile(f)); |
|
211 } |
|
212 return files; |
|
213 } |
|
214 |
|
215 /** Create the file as a directory, including any parent directories. */ |
|
216 public boolean mkdirs() { |
|
217 return file.mkdirs(); |
|
218 } |
|
219 |
|
220 /** |
|
221 * Derive a new file by resolving a relative path against this file. |
|
222 * The new file will inherit the configuration and location of this file |
|
223 * If this file has a path set, the new file will have a corresponding |
|
224 * new path. |
|
225 */ |
|
226 public DocFile resolve(DocPath p) { |
|
227 return resolve(p.getPath()); |
|
228 } |
|
229 |
|
230 /** |
|
231 * Derive a new file by resolving a relative path against this file. |
|
232 * The new file will inherit the configuration and location of this file |
|
233 * If this file has a path set, the new file will have a corresponding |
|
234 * new path. |
|
235 */ |
|
236 public DocFile resolve(String p) { |
|
237 if (location == null && path == null) { |
|
238 return new SimpleDocFile(new File(file, p)); |
|
239 } else { |
|
240 return new SimpleDocFile(location, path.resolve(p)); |
|
241 } |
|
242 } |
|
243 |
|
244 /** |
|
245 * Resolve a relative file against the given output location. |
|
246 * @param locn Currently, only SOURCE_OUTPUT is supported. |
|
247 */ |
|
248 public DocFile resolveAgainst(StandardLocation locn) { |
|
249 if (locn != StandardLocation.CLASS_OUTPUT) |
|
250 throw new IllegalArgumentException(); |
|
251 return new SimpleDocFile( |
|
252 new File(configuration.destDirName, file.getPath())); |
|
253 } |
|
254 |
|
255 /** |
|
256 * Given a path string create all the directories in the path. For example, |
|
257 * if the path string is "java/applet", the method will create directory |
|
258 * "java" and then "java/applet" if they don't exist. The file separator |
|
259 * string "/" is platform dependent system property. |
|
260 * |
|
261 * @param path Directory path string. |
|
262 */ |
|
263 private void createDirectoryForFile(File file) { |
|
264 File dir = file.getParentFile(); |
|
265 if (dir == null || dir.exists() || dir.mkdirs()) |
|
266 return; |
|
267 |
|
268 configuration.message.error( |
|
269 "doclet.Unable_to_create_directory_0", dir.getPath()); |
|
270 throw new DocletAbortException(); |
|
271 } |
|
272 |
|
273 /** Return a string to identify the contents of this object, |
|
274 * for debugging purposes. |
|
275 */ |
|
276 @Override |
|
277 public String toString() { |
|
278 StringBuilder sb = new StringBuilder(); |
|
279 sb.append("DocFile["); |
|
280 if (location != null) |
|
281 sb.append("locn:").append(location).append(","); |
|
282 if (path != null) |
|
283 sb.append("path:").append(path.getPath()).append(","); |
|
284 sb.append("file:").append(file); |
|
285 sb.append("]"); |
|
286 return sb.toString(); |
|
287 } |
|
288 |
|
289 } |
|
290 } |