1.1 --- a/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DocFile.java Thu Nov 15 14:41:31 2012 -0800 1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/DocFile.java Thu Nov 15 19:54:20 2012 -0800 1.3 @@ -25,30 +25,21 @@ 1.4 1.5 package com.sun.tools.doclets.internal.toolkit.util; 1.6 1.7 -import java.io.BufferedInputStream; 1.8 -import java.io.BufferedOutputStream; 1.9 -import java.io.BufferedReader; 1.10 -import java.io.BufferedWriter; 1.11 -import java.io.File; 1.12 -import java.io.FileInputStream; 1.13 import java.io.FileNotFoundException; 1.14 -import java.io.FileOutputStream; 1.15 import java.io.IOException; 1.16 import java.io.InputStream; 1.17 -import java.io.InputStreamReader; 1.18 import java.io.OutputStream; 1.19 -import java.io.OutputStreamWriter; 1.20 import java.io.UnsupportedEncodingException; 1.21 import java.io.Writer; 1.22 -import java.util.ArrayList; 1.23 -import java.util.LinkedHashSet; 1.24 -import java.util.List; 1.25 -import java.util.Set; 1.26 1.27 import javax.tools.JavaFileManager.Location; 1.28 import javax.tools.StandardLocation; 1.29 1.30 import com.sun.tools.doclets.internal.toolkit.Configuration; 1.31 +import java.io.BufferedReader; 1.32 +import java.io.BufferedWriter; 1.33 +import java.io.InputStreamReader; 1.34 +import java.io.OutputStreamWriter; 1.35 1.36 /** 1.37 * Abstraction for handling files, which may be specified directly 1.38 @@ -61,46 +52,36 @@ 1.39 * 1.40 * @since 8 1.41 */ 1.42 -public class DocFile { 1.43 +public abstract class DocFile { 1.44 1.45 - /** 1.46 - * The doclet configuration. 1.47 - * Provides access to options such as docencoding, output directory, etc. 1.48 - */ 1.49 + /** Create a DocFile for a directory. */ 1.50 + public static DocFile createFileForDirectory(Configuration configuration, String file) { 1.51 + return DocFileFactory.getFactory(configuration).createFileForDirectory(file); 1.52 + } 1.53 + 1.54 + /** Create a DocFile for a file that will be opened for reading. */ 1.55 + public static DocFile createFileForInput(Configuration configuration, String file) { 1.56 + return DocFileFactory.getFactory(configuration).createFileForInput(file); 1.57 + } 1.58 + 1.59 + /** Create a DocFile for a file that will be opened for writing. */ 1.60 + public static DocFile createFileForOutput(Configuration configuration, DocPath path) { 1.61 + return DocFileFactory.getFactory(configuration).createFileForOutput(path); 1.62 + } 1.63 + 1.64 private final Configuration configuration; 1.65 1.66 /** 1.67 * The location for this file. Maybe null if the file was created without 1.68 * a location or path. 1.69 */ 1.70 - private final Location location; 1.71 + protected final Location location; 1.72 1.73 /** 1.74 * The path relative to the (output) location. Maybe null if the file was 1.75 * created without a location or path. 1.76 */ 1.77 - private final DocPath path; 1.78 - 1.79 - /** 1.80 - * The file object itself. 1.81 - * This is temporary, until we create different subtypes of DocFile. 1.82 - */ 1.83 - private final File file; 1.84 - 1.85 - /** Create a DocFile for a directory. */ 1.86 - public static DocFile createFileForDirectory(Configuration configuration, String file) { 1.87 - return new DocFile(configuration, new File(file)); 1.88 - } 1.89 - 1.90 - /** Create a DocFile for a file that will be opened for reading. */ 1.91 - public static DocFile createFileForInput(Configuration configuration, String file) { 1.92 - return new DocFile(configuration, new File(file)); 1.93 - } 1.94 - 1.95 - /** Create a DocFile for a file that will be opened for writing. */ 1.96 - public static DocFile createFileForOutput(Configuration configuration, DocPath path) { 1.97 - return new DocFile(configuration, StandardLocation.CLASS_OUTPUT, path); 1.98 - } 1.99 + protected final DocPath path; 1.100 1.101 /** 1.102 * List the directories and files found in subdirectories along the 1.103 @@ -111,56 +92,32 @@ 1.104 * list files 1.105 */ 1.106 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) { 1.107 - if (location != StandardLocation.SOURCE_PATH) 1.108 - throw new IllegalArgumentException(); 1.109 - 1.110 - Set<DocFile> files = new LinkedHashSet<DocFile>(); 1.111 - for (String s : configuration.sourcepath.split(File.pathSeparator)) { 1.112 - if (s.isEmpty()) 1.113 - continue; 1.114 - File f = new File(s); 1.115 - if (f.isDirectory()) { 1.116 - f = new File(f, path.getPath()); 1.117 - if (f.exists()) 1.118 - files.add(new DocFile(configuration, f)); 1.119 - } 1.120 - } 1.121 - return files; 1.122 + return DocFileFactory.getFactory(configuration).list(location, path); 1.123 } 1.124 1.125 - /** Create a DocFile for a given file. */ 1.126 - private DocFile(Configuration configuration, File file) { 1.127 + /** Create a DocFile without a location or path */ 1.128 + protected DocFile(Configuration configuration) { 1.129 this.configuration = configuration; 1.130 this.location = null; 1.131 this.path = null; 1.132 - this.file = file; 1.133 } 1.134 1.135 /** Create a DocFile for a given location and relative path. */ 1.136 - private DocFile(Configuration configuration, Location location, DocPath path) { 1.137 + protected DocFile(Configuration configuration, Location location, DocPath path) { 1.138 this.configuration = configuration; 1.139 this.location = location; 1.140 this.path = path; 1.141 - this.file = path.resolveAgainst(configuration.destDirName); 1.142 } 1.143 1.144 /** Open an input stream for the file. */ 1.145 - public InputStream openInputStream() throws FileNotFoundException { 1.146 - return new BufferedInputStream(new FileInputStream(file)); 1.147 - } 1.148 + public abstract InputStream openInputStream() throws IOException; 1.149 1.150 /** 1.151 * Open an output stream for the file. 1.152 * The file must have been created with a location of 1.153 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. 1.154 */ 1.155 - public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { 1.156 - if (location != StandardLocation.CLASS_OUTPUT) 1.157 - throw new IllegalStateException(); 1.158 - 1.159 - createDirectoryForFile(file); 1.160 - return new BufferedOutputStream(new FileOutputStream(file)); 1.161 - } 1.162 + public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException; 1.163 1.164 /** 1.165 * Open an writer for the file, using the encoding (if any) given in the 1.166 @@ -168,28 +125,12 @@ 1.167 * The file must have been created with a location of 1.168 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. 1.169 */ 1.170 - public Writer openWriter() throws IOException, UnsupportedEncodingException { 1.171 - if (location != StandardLocation.CLASS_OUTPUT) 1.172 - throw new IllegalStateException(); 1.173 - 1.174 - createDirectoryForFile(file); 1.175 - FileOutputStream fos = new FileOutputStream(file); 1.176 - if (configuration.docencoding == null) { 1.177 - return new BufferedWriter(new OutputStreamWriter(fos)); 1.178 - } else { 1.179 - return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); 1.180 - } 1.181 - } 1.182 + public abstract Writer openWriter() throws IOException, UnsupportedEncodingException; 1.183 1.184 /** 1.185 * Copy the contents of another file directly to this file. 1.186 */ 1.187 public void copyFile(DocFile fromFile) throws IOException { 1.188 - if (location != StandardLocation.CLASS_OUTPUT) 1.189 - throw new IllegalStateException(); 1.190 - 1.191 - createDirectoryForFile(file); 1.192 - 1.193 InputStream input = fromFile.openInputStream(); 1.194 OutputStream output = openOutputStream(); 1.195 try { 1.196 @@ -215,20 +156,15 @@ 1.197 * separator 1.198 */ 1.199 public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) { 1.200 - if (location != StandardLocation.CLASS_OUTPUT) 1.201 - throw new IllegalStateException(); 1.202 - 1.203 - if (file.exists() && !overwrite) 1.204 + if (exists() && !overwrite) 1.205 return; 1.206 1.207 - createDirectoryForFile(file); 1.208 - 1.209 try { 1.210 InputStream in = Configuration.class.getResourceAsStream(resource.getPath()); 1.211 if (in == null) 1.212 return; 1.213 1.214 - OutputStream out = new FileOutputStream(file); 1.215 + OutputStream out = openOutputStream(); 1.216 try { 1.217 if (!replaceNewLine) { 1.218 byte[] buf = new byte[2048]; 1.219 @@ -265,68 +201,37 @@ 1.220 } 1.221 1.222 /** Return true if the file can be read. */ 1.223 - public boolean canRead() { 1.224 - return file.canRead(); 1.225 - } 1.226 + public abstract boolean canRead(); 1.227 1.228 /** Return true if the file can be written. */ 1.229 - public boolean canWrite() { 1.230 - return file.canRead(); 1.231 - } 1.232 + public abstract boolean canWrite(); 1.233 1.234 /** Return true if the file exists. */ 1.235 - public boolean exists() { 1.236 - return file.exists(); 1.237 - } 1.238 + public abstract boolean exists(); 1.239 1.240 /** Return the base name (last component) of the file name. */ 1.241 - public String getName() { 1.242 - return file.getName(); 1.243 - } 1.244 + public abstract String getName(); 1.245 1.246 /** Return the file system path for this file. */ 1.247 - public String getPath() { 1.248 - return file.getPath(); 1.249 - } 1.250 + public abstract String getPath(); 1.251 1.252 - /** Return true is file has an absolute path name. */ 1.253 - boolean isAbsolute() { 1.254 - return file.isAbsolute(); 1.255 - } 1.256 + /** Return true if file has an absolute path name. */ 1.257 + public abstract boolean isAbsolute(); 1.258 1.259 - /** Return true is file identifies a directory. */ 1.260 - public boolean isDirectory() { 1.261 - return file.isDirectory(); 1.262 - } 1.263 + /** Return true if file identifies a directory. */ 1.264 + public abstract boolean isDirectory(); 1.265 1.266 - /** Return true is file identifies a file. */ 1.267 - public boolean isFile() { 1.268 - return file.isFile(); 1.269 - } 1.270 + /** Return true if file identifies a file. */ 1.271 + public abstract boolean isFile(); 1.272 1.273 /** Return true if this file is the same as another. */ 1.274 - public boolean isSameFile(DocFile other) { 1.275 - try { 1.276 - return file.exists() 1.277 - && file.getCanonicalFile().equals(other.file.getCanonicalFile()); 1.278 - } catch (IOException e) { 1.279 - return false; 1.280 - } 1.281 - } 1.282 + public abstract boolean isSameFile(DocFile other); 1.283 1.284 /** If the file is a directory, list its contents. */ 1.285 - public Iterable<DocFile> list() { 1.286 - List<DocFile> files = new ArrayList<DocFile>(); 1.287 - for (File f: file.listFiles()) { 1.288 - files.add(new DocFile(configuration, f)); 1.289 - } 1.290 - return files; 1.291 - } 1.292 + public abstract Iterable<DocFile> list() throws IOException; 1.293 1.294 /** Create the file as a directory, including any parent directories. */ 1.295 - public boolean mkdirs() { 1.296 - return file.mkdirs(); 1.297 - } 1.298 + public abstract boolean mkdirs(); 1.299 1.300 /** 1.301 * Derive a new file by resolving a relative path against this file. 1.302 @@ -334,9 +239,7 @@ 1.303 * If this file has a path set, the new file will have a corresponding 1.304 * new path. 1.305 */ 1.306 - public DocFile resolve(DocPath p) { 1.307 - return resolve(p.getPath()); 1.308 - } 1.309 + public abstract DocFile resolve(DocPath p); 1.310 1.311 /** 1.312 * Derive a new file by resolving a relative path against this file. 1.313 @@ -344,56 +247,11 @@ 1.314 * If this file has a path set, the new file will have a corresponding 1.315 * new path. 1.316 */ 1.317 - public DocFile resolve(String p) { 1.318 - if (location == null && path == null) { 1.319 - return new DocFile(configuration, new File(file, p)); 1.320 - } else { 1.321 - return new DocFile(configuration, location, path.resolve(p)); 1.322 - } 1.323 - } 1.324 + public abstract DocFile resolve(String p); 1.325 1.326 /** 1.327 * Resolve a relative file against the given output location. 1.328 * @param locn Currently, only SOURCE_OUTPUT is supported. 1.329 */ 1.330 - public DocFile resolveAgainst(StandardLocation locn) { 1.331 - if (locn != StandardLocation.CLASS_OUTPUT) 1.332 - throw new IllegalArgumentException(); 1.333 - return new DocFile(configuration, 1.334 - new File(configuration.destDirName, file.getPath())); 1.335 - } 1.336 - 1.337 - /** 1.338 - * Given a path string create all the directories in the path. For example, 1.339 - * if the path string is "java/applet", the method will create directory 1.340 - * "java" and then "java/applet" if they don't exist. The file separator 1.341 - * string "/" is platform dependent system property. 1.342 - * 1.343 - * @param path Directory path string. 1.344 - */ 1.345 - private void createDirectoryForFile(File file) { 1.346 - File dir = file.getParentFile(); 1.347 - if (dir == null || dir.exists() || dir.mkdirs()) 1.348 - return; 1.349 - 1.350 - configuration.message.error( 1.351 - "doclet.Unable_to_create_directory_0", dir.getPath()); 1.352 - throw new DocletAbortException(); 1.353 - } 1.354 - 1.355 - /** Return a string to identify the contents of this object, 1.356 - * for debugging purposes. 1.357 - */ 1.358 - @Override 1.359 - public String toString() { 1.360 - StringBuilder sb = new StringBuilder(); 1.361 - sb.append("DocFile["); 1.362 - if (location != null) 1.363 - sb.append("locn:").append(location).append(","); 1.364 - if (path != null) 1.365 - sb.append("path:").append(path.getPath()).append(","); 1.366 - sb.append("file:").append(file); 1.367 - sb.append("]"); 1.368 - return sb.toString(); 1.369 - } 1.370 + public abstract DocFile resolveAgainst(StandardLocation locn); 1.371 }