Wed, 31 Oct 2012 13:48:15 -0700
8001664: refactor javadoc to use abstraction to handle files
Reviewed-by: darcy
jjg@1383 | 1 | /* |
jjg@1383 | 2 | * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved. |
jjg@1383 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
jjg@1383 | 4 | * |
jjg@1383 | 5 | * This code is free software; you can redistribute it and/or modify it |
jjg@1383 | 6 | * under the terms of the GNU General Public License version 2 only, as |
jjg@1383 | 7 | * published by the Free Software Foundation. Oracle designates this |
jjg@1383 | 8 | * particular file as subject to the "Classpath" exception as provided |
jjg@1383 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
jjg@1383 | 10 | * |
jjg@1383 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
jjg@1383 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
jjg@1383 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
jjg@1383 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
jjg@1383 | 15 | * accompanied this code). |
jjg@1383 | 16 | * |
jjg@1383 | 17 | * You should have received a copy of the GNU General Public License version |
jjg@1383 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
jjg@1383 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
jjg@1383 | 20 | * |
jjg@1383 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
jjg@1383 | 22 | * or visit www.oracle.com if you need additional information or have any |
jjg@1383 | 23 | * questions. |
jjg@1383 | 24 | */ |
jjg@1383 | 25 | |
jjg@1383 | 26 | package com.sun.tools.doclets.internal.toolkit.util; |
jjg@1383 | 27 | |
jjg@1383 | 28 | import java.io.BufferedInputStream; |
jjg@1383 | 29 | import java.io.BufferedOutputStream; |
jjg@1383 | 30 | import java.io.BufferedReader; |
jjg@1383 | 31 | import java.io.BufferedWriter; |
jjg@1383 | 32 | import java.io.File; |
jjg@1383 | 33 | import java.io.FileInputStream; |
jjg@1383 | 34 | import java.io.FileNotFoundException; |
jjg@1383 | 35 | import java.io.FileOutputStream; |
jjg@1383 | 36 | import java.io.IOException; |
jjg@1383 | 37 | import java.io.InputStream; |
jjg@1383 | 38 | import java.io.InputStreamReader; |
jjg@1383 | 39 | import java.io.OutputStream; |
jjg@1383 | 40 | import java.io.OutputStreamWriter; |
jjg@1383 | 41 | import java.io.UnsupportedEncodingException; |
jjg@1383 | 42 | import java.io.Writer; |
jjg@1383 | 43 | import java.util.ArrayList; |
jjg@1383 | 44 | import java.util.LinkedHashSet; |
jjg@1383 | 45 | import java.util.List; |
jjg@1383 | 46 | import java.util.Set; |
jjg@1383 | 47 | |
jjg@1383 | 48 | import javax.tools.JavaFileManager.Location; |
jjg@1383 | 49 | import javax.tools.StandardLocation; |
jjg@1383 | 50 | |
jjg@1383 | 51 | import com.sun.tools.doclets.internal.toolkit.Configuration; |
jjg@1383 | 52 | |
jjg@1383 | 53 | /** |
jjg@1383 | 54 | * Abstraction for handling files, which may be specified directly |
jjg@1383 | 55 | * (e.g. via a path on the command line) or relative to a Location. |
jjg@1383 | 56 | * |
jjg@1383 | 57 | * <p><b>This is NOT part of any supported API. |
jjg@1383 | 58 | * If you write code that depends on this, you do so at your own risk. |
jjg@1383 | 59 | * This code and its internal interfaces are subject to change or |
jjg@1383 | 60 | * deletion without notice.</b> |
jjg@1383 | 61 | * |
jjg@1383 | 62 | * @since 8 |
jjg@1383 | 63 | */ |
jjg@1383 | 64 | public class DocFile { |
jjg@1383 | 65 | |
jjg@1383 | 66 | /** |
jjg@1383 | 67 | * The doclet configuration. |
jjg@1383 | 68 | * Provides access to options such as docencoding, output directory, etc. |
jjg@1383 | 69 | */ |
jjg@1383 | 70 | private final Configuration configuration; |
jjg@1383 | 71 | |
jjg@1383 | 72 | /** |
jjg@1383 | 73 | * The location for this file. Maybe null if the file was created without |
jjg@1383 | 74 | * a location or path. |
jjg@1383 | 75 | */ |
jjg@1383 | 76 | private final Location location; |
jjg@1383 | 77 | |
jjg@1383 | 78 | /** |
jjg@1383 | 79 | * The path relative to the (output) location. Maybe null if the file was |
jjg@1383 | 80 | * created without a location or path. |
jjg@1383 | 81 | */ |
jjg@1383 | 82 | private final DocPath path; |
jjg@1383 | 83 | |
jjg@1383 | 84 | /** |
jjg@1383 | 85 | * The file object itself. |
jjg@1383 | 86 | * This is temporary, until we create different subtypes of DocFile. |
jjg@1383 | 87 | */ |
jjg@1383 | 88 | private final File file; |
jjg@1383 | 89 | |
jjg@1383 | 90 | /** Create a DocFile for a directory. */ |
jjg@1383 | 91 | public static DocFile createFileForDirectory(Configuration configuration, String file) { |
jjg@1383 | 92 | return new DocFile(configuration, new File(file)); |
jjg@1383 | 93 | } |
jjg@1383 | 94 | |
jjg@1383 | 95 | /** Create a DocFile for a file that will be opened for reading. */ |
jjg@1383 | 96 | public static DocFile createFileForInput(Configuration configuration, String file) { |
jjg@1383 | 97 | return new DocFile(configuration, new File(file)); |
jjg@1383 | 98 | } |
jjg@1383 | 99 | |
jjg@1383 | 100 | /** Create a DocFile for a file that will be opened for writing. */ |
jjg@1383 | 101 | public static DocFile createFileForOutput(Configuration configuration, DocPath path) { |
jjg@1383 | 102 | return new DocFile(configuration, StandardLocation.CLASS_OUTPUT, path); |
jjg@1383 | 103 | } |
jjg@1383 | 104 | |
jjg@1383 | 105 | /** |
jjg@1383 | 106 | * List the directories and files found in subdirectories along the |
jjg@1383 | 107 | * elements of the given location. |
jjg@1383 | 108 | * @param configuration the doclet configuration |
jjg@1383 | 109 | * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported. |
jjg@1383 | 110 | * @param path the subdirectory of the directories of the location for which to |
jjg@1383 | 111 | * list files |
jjg@1383 | 112 | */ |
jjg@1383 | 113 | public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) { |
jjg@1383 | 114 | if (location != StandardLocation.SOURCE_PATH) |
jjg@1383 | 115 | throw new IllegalArgumentException(); |
jjg@1383 | 116 | |
jjg@1383 | 117 | Set<DocFile> files = new LinkedHashSet<DocFile>(); |
jjg@1383 | 118 | for (String s : configuration.sourcepath.split(File.pathSeparator)) { |
jjg@1383 | 119 | if (s.isEmpty()) |
jjg@1383 | 120 | continue; |
jjg@1383 | 121 | File f = new File(s); |
jjg@1383 | 122 | if (f.isDirectory()) { |
jjg@1383 | 123 | f = new File(f, path.getPath()); |
jjg@1383 | 124 | if (f.exists()) |
jjg@1383 | 125 | files.add(new DocFile(configuration, f)); |
jjg@1383 | 126 | } |
jjg@1383 | 127 | } |
jjg@1383 | 128 | return files; |
jjg@1383 | 129 | } |
jjg@1383 | 130 | |
jjg@1383 | 131 | /** Create a DocFile for a given file. */ |
jjg@1383 | 132 | private DocFile(Configuration configuration, File file) { |
jjg@1383 | 133 | this.configuration = configuration; |
jjg@1383 | 134 | this.location = null; |
jjg@1383 | 135 | this.path = null; |
jjg@1383 | 136 | this.file = file; |
jjg@1383 | 137 | } |
jjg@1383 | 138 | |
jjg@1383 | 139 | /** Create a DocFile for a given location and relative path. */ |
jjg@1383 | 140 | private DocFile(Configuration configuration, Location location, DocPath path) { |
jjg@1383 | 141 | this.configuration = configuration; |
jjg@1383 | 142 | this.location = location; |
jjg@1383 | 143 | this.path = path; |
jjg@1383 | 144 | this.file = path.resolveAgainst(configuration.destDirName); |
jjg@1383 | 145 | } |
jjg@1383 | 146 | |
jjg@1383 | 147 | /** Open an input stream for the file. */ |
jjg@1383 | 148 | public InputStream openInputStream() throws FileNotFoundException { |
jjg@1383 | 149 | return new BufferedInputStream(new FileInputStream(file)); |
jjg@1383 | 150 | } |
jjg@1383 | 151 | |
jjg@1383 | 152 | /** |
jjg@1383 | 153 | * Open an output stream for the file. |
jjg@1383 | 154 | * The file must have been created with a location of |
jjg@1383 | 155 | * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
jjg@1383 | 156 | */ |
jjg@1383 | 157 | public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
jjg@1383 | 158 | if (location != StandardLocation.CLASS_OUTPUT) |
jjg@1383 | 159 | throw new IllegalStateException(); |
jjg@1383 | 160 | |
jjg@1383 | 161 | createDirectoryForFile(file); |
jjg@1383 | 162 | return new BufferedOutputStream(new FileOutputStream(file)); |
jjg@1383 | 163 | } |
jjg@1383 | 164 | |
jjg@1383 | 165 | /** |
jjg@1383 | 166 | * Open an writer for the file, using the encoding (if any) given in the |
jjg@1383 | 167 | * doclet configuration. |
jjg@1383 | 168 | * The file must have been created with a location of |
jjg@1383 | 169 | * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. |
jjg@1383 | 170 | */ |
jjg@1383 | 171 | public Writer openWriter() throws IOException, UnsupportedEncodingException { |
jjg@1383 | 172 | if (location != StandardLocation.CLASS_OUTPUT) |
jjg@1383 | 173 | throw new IllegalStateException(); |
jjg@1383 | 174 | |
jjg@1383 | 175 | createDirectoryForFile(file); |
jjg@1383 | 176 | FileOutputStream fos = new FileOutputStream(file); |
jjg@1383 | 177 | if (configuration.docencoding == null) { |
jjg@1383 | 178 | return new BufferedWriter(new OutputStreamWriter(fos)); |
jjg@1383 | 179 | } else { |
jjg@1383 | 180 | return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding)); |
jjg@1383 | 181 | } |
jjg@1383 | 182 | } |
jjg@1383 | 183 | |
jjg@1383 | 184 | /** |
jjg@1383 | 185 | * Copy the contents of another file directly to this file. |
jjg@1383 | 186 | */ |
jjg@1383 | 187 | public void copyFile(DocFile fromFile) throws IOException { |
jjg@1383 | 188 | if (location != StandardLocation.CLASS_OUTPUT) |
jjg@1383 | 189 | throw new IllegalStateException(); |
jjg@1383 | 190 | |
jjg@1383 | 191 | createDirectoryForFile(file); |
jjg@1383 | 192 | |
jjg@1383 | 193 | InputStream input = fromFile.openInputStream(); |
jjg@1383 | 194 | OutputStream output = openOutputStream(); |
jjg@1383 | 195 | try { |
jjg@1383 | 196 | byte[] bytearr = new byte[1024]; |
jjg@1383 | 197 | int len; |
jjg@1383 | 198 | while ((len = input.read(bytearr)) != -1) { |
jjg@1383 | 199 | output.write(bytearr, 0, len); |
jjg@1383 | 200 | } |
jjg@1383 | 201 | } catch (FileNotFoundException exc) { |
jjg@1383 | 202 | } catch (SecurityException exc) { |
jjg@1383 | 203 | } finally { |
jjg@1383 | 204 | input.close(); |
jjg@1383 | 205 | output.close(); |
jjg@1383 | 206 | } |
jjg@1383 | 207 | } |
jjg@1383 | 208 | |
jjg@1383 | 209 | /** |
jjg@1383 | 210 | * Copy the contents of a resource file to this file. |
jjg@1383 | 211 | * @param resource the path of the resource, relative to the package of this class |
jjg@1383 | 212 | * @param overwrite whether or not to overwrite the file if it already exists |
jjg@1383 | 213 | * @param replaceNewLine if false, the file is copied as a binary file; |
jjg@1383 | 214 | * if true, the file is written line by line, using the platform line |
jjg@1383 | 215 | * separator |
jjg@1383 | 216 | */ |
jjg@1383 | 217 | public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) { |
jjg@1383 | 218 | if (location != StandardLocation.CLASS_OUTPUT) |
jjg@1383 | 219 | throw new IllegalStateException(); |
jjg@1383 | 220 | |
jjg@1383 | 221 | if (file.exists() && !overwrite) |
jjg@1383 | 222 | return; |
jjg@1383 | 223 | |
jjg@1383 | 224 | createDirectoryForFile(file); |
jjg@1383 | 225 | |
jjg@1383 | 226 | try { |
jjg@1383 | 227 | InputStream in = Configuration.class.getResourceAsStream(resource.getPath()); |
jjg@1383 | 228 | if (in == null) |
jjg@1383 | 229 | return; |
jjg@1383 | 230 | |
jjg@1383 | 231 | OutputStream out = new FileOutputStream(file); |
jjg@1383 | 232 | try { |
jjg@1383 | 233 | if (!replaceNewLine) { |
jjg@1383 | 234 | byte[] buf = new byte[2048]; |
jjg@1383 | 235 | int n; |
jjg@1383 | 236 | while((n = in.read(buf))>0) out.write(buf,0,n); |
jjg@1383 | 237 | } else { |
jjg@1383 | 238 | BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
jjg@1383 | 239 | BufferedWriter writer; |
jjg@1383 | 240 | if (configuration.docencoding == null) { |
jjg@1383 | 241 | writer = new BufferedWriter(new OutputStreamWriter(out)); |
jjg@1383 | 242 | } else { |
jjg@1383 | 243 | writer = new BufferedWriter(new OutputStreamWriter(out, |
jjg@1383 | 244 | configuration.docencoding)); |
jjg@1383 | 245 | } |
jjg@1383 | 246 | try { |
jjg@1383 | 247 | String line; |
jjg@1383 | 248 | while ((line = reader.readLine()) != null) { |
jjg@1383 | 249 | writer.write(line); |
jjg@1383 | 250 | writer.write(DocletConstants.NL); |
jjg@1383 | 251 | } |
jjg@1383 | 252 | } finally { |
jjg@1383 | 253 | reader.close(); |
jjg@1383 | 254 | writer.close(); |
jjg@1383 | 255 | } |
jjg@1383 | 256 | } |
jjg@1383 | 257 | } finally { |
jjg@1383 | 258 | in.close(); |
jjg@1383 | 259 | out.close(); |
jjg@1383 | 260 | } |
jjg@1383 | 261 | } catch (IOException e) { |
jjg@1383 | 262 | e.printStackTrace(System.err); |
jjg@1383 | 263 | throw new DocletAbortException(); |
jjg@1383 | 264 | } |
jjg@1383 | 265 | } |
jjg@1383 | 266 | |
jjg@1383 | 267 | /** Return true if the file can be read. */ |
jjg@1383 | 268 | public boolean canRead() { |
jjg@1383 | 269 | return file.canRead(); |
jjg@1383 | 270 | } |
jjg@1383 | 271 | |
jjg@1383 | 272 | /** Return true if the file can be written. */ |
jjg@1383 | 273 | public boolean canWrite() { |
jjg@1383 | 274 | return file.canRead(); |
jjg@1383 | 275 | } |
jjg@1383 | 276 | |
jjg@1383 | 277 | /** Return true if the file exists. */ |
jjg@1383 | 278 | public boolean exists() { |
jjg@1383 | 279 | return file.exists(); |
jjg@1383 | 280 | } |
jjg@1383 | 281 | |
jjg@1383 | 282 | /** Return the base name (last component) of the file name. */ |
jjg@1383 | 283 | public String getName() { |
jjg@1383 | 284 | return file.getName(); |
jjg@1383 | 285 | } |
jjg@1383 | 286 | |
jjg@1383 | 287 | /** Return the file system path for this file. */ |
jjg@1383 | 288 | public String getPath() { |
jjg@1383 | 289 | return file.getPath(); |
jjg@1383 | 290 | } |
jjg@1383 | 291 | |
jjg@1383 | 292 | /** Return true is file has an absolute path name. */ |
jjg@1383 | 293 | boolean isAbsolute() { |
jjg@1383 | 294 | return file.isAbsolute(); |
jjg@1383 | 295 | } |
jjg@1383 | 296 | |
jjg@1383 | 297 | /** Return true is file identifies a directory. */ |
jjg@1383 | 298 | public boolean isDirectory() { |
jjg@1383 | 299 | return file.isDirectory(); |
jjg@1383 | 300 | } |
jjg@1383 | 301 | |
jjg@1383 | 302 | /** Return true is file identifies a file. */ |
jjg@1383 | 303 | public boolean isFile() { |
jjg@1383 | 304 | return file.isFile(); |
jjg@1383 | 305 | } |
jjg@1383 | 306 | |
jjg@1383 | 307 | /** Return true if this file is the same as another. */ |
jjg@1383 | 308 | public boolean isSameFile(DocFile other) { |
jjg@1383 | 309 | try { |
jjg@1383 | 310 | return file.exists() |
jjg@1383 | 311 | && file.getCanonicalFile().equals(other.file.getCanonicalFile()); |
jjg@1383 | 312 | } catch (IOException e) { |
jjg@1383 | 313 | return false; |
jjg@1383 | 314 | } |
jjg@1383 | 315 | } |
jjg@1383 | 316 | |
jjg@1383 | 317 | /** If the file is a directory, list its contents. */ |
jjg@1383 | 318 | public Iterable<DocFile> list() { |
jjg@1383 | 319 | List<DocFile> files = new ArrayList<DocFile>(); |
jjg@1383 | 320 | for (File f: file.listFiles()) { |
jjg@1383 | 321 | files.add(new DocFile(configuration, f)); |
jjg@1383 | 322 | } |
jjg@1383 | 323 | return files; |
jjg@1383 | 324 | } |
jjg@1383 | 325 | |
jjg@1383 | 326 | /** Create the file as a directory, including any parent directories. */ |
jjg@1383 | 327 | public boolean mkdirs() { |
jjg@1383 | 328 | return file.mkdirs(); |
jjg@1383 | 329 | } |
jjg@1383 | 330 | |
jjg@1383 | 331 | /** |
jjg@1383 | 332 | * Derive a new file by resolving a relative path against this file. |
jjg@1383 | 333 | * The new file will inherit the configuration and location of this file |
jjg@1383 | 334 | * If this file has a path set, the new file will have a corresponding |
jjg@1383 | 335 | * new path. |
jjg@1383 | 336 | */ |
jjg@1383 | 337 | public DocFile resolve(DocPath p) { |
jjg@1383 | 338 | return resolve(p.getPath()); |
jjg@1383 | 339 | } |
jjg@1383 | 340 | |
jjg@1383 | 341 | /** |
jjg@1383 | 342 | * Derive a new file by resolving a relative path against this file. |
jjg@1383 | 343 | * The new file will inherit the configuration and location of this file |
jjg@1383 | 344 | * If this file has a path set, the new file will have a corresponding |
jjg@1383 | 345 | * new path. |
jjg@1383 | 346 | */ |
jjg@1383 | 347 | public DocFile resolve(String p) { |
jjg@1383 | 348 | if (location == null && path == null) { |
jjg@1383 | 349 | return new DocFile(configuration, new File(file, p)); |
jjg@1383 | 350 | } else { |
jjg@1383 | 351 | return new DocFile(configuration, location, path.resolve(p)); |
jjg@1383 | 352 | } |
jjg@1383 | 353 | } |
jjg@1383 | 354 | |
jjg@1383 | 355 | /** |
jjg@1383 | 356 | * Resolve a relative file against the given output location. |
jjg@1383 | 357 | * @param locn Currently, only SOURCE_OUTPUT is supported. |
jjg@1383 | 358 | */ |
jjg@1383 | 359 | public DocFile resolveAgainst(StandardLocation locn) { |
jjg@1383 | 360 | if (locn != StandardLocation.CLASS_OUTPUT) |
jjg@1383 | 361 | throw new IllegalArgumentException(); |
jjg@1383 | 362 | return new DocFile(configuration, |
jjg@1383 | 363 | new File(configuration.destDirName, file.getPath())); |
jjg@1383 | 364 | } |
jjg@1383 | 365 | |
jjg@1383 | 366 | /** |
jjg@1383 | 367 | * Given a path string create all the directories in the path. For example, |
jjg@1383 | 368 | * if the path string is "java/applet", the method will create directory |
jjg@1383 | 369 | * "java" and then "java/applet" if they don't exist. The file separator |
jjg@1383 | 370 | * string "/" is platform dependent system property. |
jjg@1383 | 371 | * |
jjg@1383 | 372 | * @param path Directory path string. |
jjg@1383 | 373 | */ |
jjg@1383 | 374 | private void createDirectoryForFile(File file) { |
jjg@1383 | 375 | File dir = file.getParentFile(); |
jjg@1383 | 376 | if (dir == null || dir.exists() || dir.mkdirs()) |
jjg@1383 | 377 | return; |
jjg@1383 | 378 | |
jjg@1383 | 379 | configuration.message.error( |
jjg@1383 | 380 | "doclet.Unable_to_create_directory_0", dir.getPath()); |
jjg@1383 | 381 | throw new DocletAbortException(); |
jjg@1383 | 382 | } |
jjg@1383 | 383 | |
jjg@1383 | 384 | /** Return a string to identify the contents of this object, |
jjg@1383 | 385 | * for debugging purposes. |
jjg@1383 | 386 | */ |
jjg@1383 | 387 | @Override |
jjg@1383 | 388 | public String toString() { |
jjg@1383 | 389 | StringBuilder sb = new StringBuilder(); |
jjg@1383 | 390 | sb.append("DocFile["); |
jjg@1383 | 391 | if (location != null) |
jjg@1383 | 392 | sb.append("locn:").append(location).append(","); |
jjg@1383 | 393 | if (path != null) |
jjg@1383 | 394 | sb.append("path:").append(path.getPath()).append(","); |
jjg@1383 | 395 | sb.append("file:").append(file); |
jjg@1383 | 396 | sb.append("]"); |
jjg@1383 | 397 | return sb.toString(); |
jjg@1383 | 398 | } |
jjg@1383 | 399 | } |