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

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

mercurial