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

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
parent 1383
b980e8e6aabf
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
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.FileNotFoundException;
jjg@1383 29 import java.io.IOException;
jjg@1383 30 import java.io.InputStream;
jjg@1383 31 import java.io.OutputStream;
jjg@1383 32 import java.io.UnsupportedEncodingException;
jjg@1383 33 import java.io.Writer;
jjg@1383 34
jjg@1383 35 import javax.tools.JavaFileManager.Location;
jjg@1383 36 import javax.tools.StandardLocation;
jjg@1383 37
jjg@1383 38 import com.sun.tools.doclets.internal.toolkit.Configuration;
jjg@1412 39 import java.io.BufferedReader;
jjg@1412 40 import java.io.BufferedWriter;
jjg@1412 41 import java.io.InputStreamReader;
jjg@1412 42 import java.io.OutputStreamWriter;
jjg@1383 43
jjg@1383 44 /**
jjg@1383 45 * Abstraction for handling files, which may be specified directly
jjg@1383 46 * (e.g. via a path on the command line) or relative to a Location.
jjg@1383 47 *
jjg@1383 48 * <p><b>This is NOT part of any supported API.
jjg@1383 49 * If you write code that depends on this, you do so at your own risk.
jjg@1383 50 * This code and its internal interfaces are subject to change or
jjg@1383 51 * deletion without notice.</b>
jjg@1383 52 *
jjg@1383 53 * @since 8
jjg@1383 54 */
jjg@1412 55 public abstract class DocFile {
jjg@1383 56
jjg@1412 57 /** Create a DocFile for a directory. */
jjg@1412 58 public static DocFile createFileForDirectory(Configuration configuration, String file) {
jjg@1412 59 return DocFileFactory.getFactory(configuration).createFileForDirectory(file);
jjg@1412 60 }
jjg@1412 61
jjg@1412 62 /** Create a DocFile for a file that will be opened for reading. */
jjg@1412 63 public static DocFile createFileForInput(Configuration configuration, String file) {
jjg@1412 64 return DocFileFactory.getFactory(configuration).createFileForInput(file);
jjg@1412 65 }
jjg@1412 66
jjg@1412 67 /** Create a DocFile for a file that will be opened for writing. */
jjg@1412 68 public static DocFile createFileForOutput(Configuration configuration, DocPath path) {
jjg@1412 69 return DocFileFactory.getFactory(configuration).createFileForOutput(path);
jjg@1412 70 }
jjg@1412 71
jjg@1383 72 private final Configuration configuration;
jjg@1383 73
jjg@1383 74 /**
jjg@1383 75 * The location for this file. Maybe null if the file was created without
jjg@1383 76 * a location or path.
jjg@1383 77 */
jjg@1412 78 protected final Location location;
jjg@1383 79
jjg@1383 80 /**
jjg@1383 81 * The path relative to the (output) location. Maybe null if the file was
jjg@1383 82 * created without a location or path.
jjg@1383 83 */
jjg@1412 84 protected final DocPath path;
jjg@1383 85
jjg@1383 86 /**
jjg@1383 87 * List the directories and files found in subdirectories along the
jjg@1383 88 * elements of the given location.
jjg@1383 89 * @param configuration the doclet configuration
jjg@1383 90 * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported.
jjg@1383 91 * @param path the subdirectory of the directories of the location for which to
jjg@1383 92 * list files
jjg@1383 93 */
jjg@1383 94 public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) {
jjg@1412 95 return DocFileFactory.getFactory(configuration).list(location, path);
jjg@1383 96 }
jjg@1383 97
jjg@1412 98 /** Create a DocFile without a location or path */
jjg@1412 99 protected DocFile(Configuration configuration) {
jjg@1383 100 this.configuration = configuration;
jjg@1383 101 this.location = null;
jjg@1383 102 this.path = null;
jjg@1383 103 }
jjg@1383 104
jjg@1383 105 /** Create a DocFile for a given location and relative path. */
jjg@1412 106 protected DocFile(Configuration configuration, Location location, DocPath path) {
jjg@1383 107 this.configuration = configuration;
jjg@1383 108 this.location = location;
jjg@1383 109 this.path = path;
jjg@1383 110 }
jjg@1383 111
jjg@1383 112 /** Open an input stream for the file. */
jjg@1412 113 public abstract InputStream openInputStream() throws IOException;
jjg@1383 114
jjg@1383 115 /**
jjg@1383 116 * Open an output stream for the file.
jjg@1383 117 * The file must have been created with a location of
jjg@1383 118 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
jjg@1383 119 */
jjg@1412 120 public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException;
jjg@1383 121
jjg@1383 122 /**
jjg@1383 123 * Open an writer for the file, using the encoding (if any) given in the
jjg@1383 124 * doclet configuration.
jjg@1383 125 * The file must have been created with a location of
jjg@1383 126 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
jjg@1383 127 */
jjg@1412 128 public abstract Writer openWriter() throws IOException, UnsupportedEncodingException;
jjg@1383 129
jjg@1383 130 /**
jjg@1383 131 * Copy the contents of another file directly to this file.
jjg@1383 132 */
jjg@1383 133 public void copyFile(DocFile fromFile) throws IOException {
jjg@1383 134 InputStream input = fromFile.openInputStream();
jjg@1383 135 OutputStream output = openOutputStream();
jjg@1383 136 try {
jjg@1383 137 byte[] bytearr = new byte[1024];
jjg@1383 138 int len;
jjg@1383 139 while ((len = input.read(bytearr)) != -1) {
jjg@1383 140 output.write(bytearr, 0, len);
jjg@1383 141 }
jjg@1383 142 } catch (FileNotFoundException exc) {
jjg@1383 143 } catch (SecurityException exc) {
jjg@1383 144 } finally {
jjg@1383 145 input.close();
jjg@1383 146 output.close();
jjg@1383 147 }
jjg@1383 148 }
jjg@1383 149
jjg@1383 150 /**
jjg@1383 151 * Copy the contents of a resource file to this file.
jjg@1383 152 * @param resource the path of the resource, relative to the package of this class
jjg@1383 153 * @param overwrite whether or not to overwrite the file if it already exists
jjg@1383 154 * @param replaceNewLine if false, the file is copied as a binary file;
jjg@1383 155 * if true, the file is written line by line, using the platform line
jjg@1383 156 * separator
jjg@1383 157 */
jjg@1383 158 public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) {
jjg@1412 159 if (exists() && !overwrite)
jjg@1383 160 return;
jjg@1383 161
jjg@1383 162 try {
jjg@1383 163 InputStream in = Configuration.class.getResourceAsStream(resource.getPath());
jjg@1383 164 if (in == null)
jjg@1383 165 return;
jjg@1383 166
jjg@1412 167 OutputStream out = openOutputStream();
jjg@1383 168 try {
jjg@1383 169 if (!replaceNewLine) {
jjg@1383 170 byte[] buf = new byte[2048];
jjg@1383 171 int n;
jjg@1383 172 while((n = in.read(buf))>0) out.write(buf,0,n);
jjg@1383 173 } else {
jjg@1383 174 BufferedReader reader = new BufferedReader(new InputStreamReader(in));
jjg@1383 175 BufferedWriter writer;
jjg@1383 176 if (configuration.docencoding == null) {
jjg@1383 177 writer = new BufferedWriter(new OutputStreamWriter(out));
jjg@1383 178 } else {
jjg@1383 179 writer = new BufferedWriter(new OutputStreamWriter(out,
jjg@1383 180 configuration.docencoding));
jjg@1383 181 }
jjg@1383 182 try {
jjg@1383 183 String line;
jjg@1383 184 while ((line = reader.readLine()) != null) {
jjg@1383 185 writer.write(line);
jjg@1383 186 writer.write(DocletConstants.NL);
jjg@1383 187 }
jjg@1383 188 } finally {
jjg@1383 189 reader.close();
jjg@1383 190 writer.close();
jjg@1383 191 }
jjg@1383 192 }
jjg@1383 193 } finally {
jjg@1383 194 in.close();
jjg@1383 195 out.close();
jjg@1383 196 }
jjg@1383 197 } catch (IOException e) {
jjg@1383 198 e.printStackTrace(System.err);
jjg@1383 199 throw new DocletAbortException();
jjg@1383 200 }
jjg@1383 201 }
jjg@1383 202
jjg@1383 203 /** Return true if the file can be read. */
jjg@1412 204 public abstract boolean canRead();
jjg@1383 205
jjg@1383 206 /** Return true if the file can be written. */
jjg@1412 207 public abstract boolean canWrite();
jjg@1383 208
jjg@1383 209 /** Return true if the file exists. */
jjg@1412 210 public abstract boolean exists();
jjg@1383 211
jjg@1383 212 /** Return the base name (last component) of the file name. */
jjg@1412 213 public abstract String getName();
jjg@1383 214
jjg@1383 215 /** Return the file system path for this file. */
jjg@1412 216 public abstract String getPath();
jjg@1383 217
jjg@1412 218 /** Return true if file has an absolute path name. */
jjg@1412 219 public abstract boolean isAbsolute();
jjg@1383 220
jjg@1412 221 /** Return true if file identifies a directory. */
jjg@1412 222 public abstract boolean isDirectory();
jjg@1383 223
jjg@1412 224 /** Return true if file identifies a file. */
jjg@1412 225 public abstract boolean isFile();
jjg@1383 226
jjg@1383 227 /** Return true if this file is the same as another. */
jjg@1412 228 public abstract boolean isSameFile(DocFile other);
jjg@1383 229
jjg@1383 230 /** If the file is a directory, list its contents. */
jjg@1412 231 public abstract Iterable<DocFile> list() throws IOException;
jjg@1383 232
jjg@1383 233 /** Create the file as a directory, including any parent directories. */
jjg@1412 234 public abstract boolean mkdirs();
jjg@1383 235
jjg@1383 236 /**
jjg@1383 237 * Derive a new file by resolving a relative path against this file.
jjg@1383 238 * The new file will inherit the configuration and location of this file
jjg@1383 239 * If this file has a path set, the new file will have a corresponding
jjg@1383 240 * new path.
jjg@1383 241 */
jjg@1412 242 public abstract DocFile resolve(DocPath p);
jjg@1383 243
jjg@1383 244 /**
jjg@1383 245 * Derive a new file by resolving a relative path against this file.
jjg@1383 246 * The new file will inherit the configuration and location of this file
jjg@1383 247 * If this file has a path set, the new file will have a corresponding
jjg@1383 248 * new path.
jjg@1383 249 */
jjg@1412 250 public abstract DocFile resolve(String p);
jjg@1383 251
jjg@1383 252 /**
jjg@1383 253 * Resolve a relative file against the given output location.
jjg@1383 254 * @param locn Currently, only SOURCE_OUTPUT is supported.
jjg@1383 255 */
jjg@1412 256 public abstract DocFile resolveAgainst(StandardLocation locn);
jjg@1383 257 }

mercurial