Thu, 31 Aug 2017 15:17:03 +0800
merge
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. Oracle designates this |
aoqi@0 | 8 | * particular file as subject to the "Classpath" exception as provided |
aoqi@0 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
aoqi@0 | 10 | * |
aoqi@0 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 15 | * accompanied this code). |
aoqi@0 | 16 | * |
aoqi@0 | 17 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 20 | * |
aoqi@0 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 22 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 23 | * questions. |
aoqi@0 | 24 | */ |
aoqi@0 | 25 | |
aoqi@0 | 26 | package com.sun.tools.doclets.internal.toolkit.util; |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.BufferedReader; |
aoqi@0 | 29 | import java.io.BufferedWriter; |
aoqi@0 | 30 | import java.io.FileNotFoundException; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.io.InputStream; |
aoqi@0 | 33 | import java.io.InputStreamReader; |
aoqi@0 | 34 | import java.io.OutputStream; |
aoqi@0 | 35 | import java.io.OutputStreamWriter; |
aoqi@0 | 36 | import java.io.UnsupportedEncodingException; |
aoqi@0 | 37 | import java.io.Writer; |
aoqi@0 | 38 | |
aoqi@0 | 39 | import javax.tools.JavaFileManager.Location; |
aoqi@0 | 40 | import javax.tools.StandardLocation; |
aoqi@0 | 41 | |
aoqi@0 | 42 | import com.sun.tools.doclets.internal.toolkit.Configuration; |
aoqi@0 | 43 | |
aoqi@0 | 44 | /** |
aoqi@0 | 45 | * Abstraction for handling files, which may be specified directly |
aoqi@0 | 46 | * (e.g. via a path on the command line) or relative to a Location. |
aoqi@0 | 47 | * |
aoqi@0 | 48 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 49 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 50 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 51 | * deletion without notice.</b> |
aoqi@0 | 52 | * |
aoqi@0 | 53 | * @since 8 |
aoqi@0 | 54 | */ |
aoqi@0 | 55 | public abstract class DocFile { |
aoqi@0 | 56 | |
aoqi@0 | 57 | /** Create a DocFile for a directory. */ |
aoqi@0 | 58 | public static DocFile createFileForDirectory(Configuration configuration, String file) { |
aoqi@0 | 59 | return DocFileFactory.getFactory(configuration).createFileForDirectory(file); |
aoqi@0 | 60 | } |
aoqi@0 | 61 | |
aoqi@0 | 62 | /** Create a DocFile for a file that will be opened for reading. */ |
aoqi@0 | 63 | public static DocFile createFileForInput(Configuration configuration, String file) { |
aoqi@0 | 64 | return DocFileFactory.getFactory(configuration).createFileForInput(file); |
aoqi@0 | 65 | } |
aoqi@0 | 66 | |
aoqi@0 | 67 | /** Create a DocFile for a file that will be opened for writing. */ |
aoqi@0 | 68 | public static DocFile createFileForOutput(Configuration configuration, DocPath path) { |
aoqi@0 | 69 | return DocFileFactory.getFactory(configuration).createFileForOutput(path); |
aoqi@0 | 70 | } |
aoqi@0 | 71 | |
aoqi@0 | 72 | private final Configuration configuration; |
aoqi@0 | 73 | |
aoqi@0 | 74 | /** |
aoqi@0 | 75 | * The location for this file. Maybe null if the file was created without |
aoqi@0 | 76 | * a location or path. |
aoqi@0 | 77 | */ |
aoqi@0 | 78 | protected final Location location; |
aoqi@0 | 79 | |
aoqi@0 | 80 | /** |
aoqi@0 | 81 | * The path relative to the (output) location. Maybe null if the file was |
aoqi@0 | 82 | * created without a location or path. |
aoqi@0 | 83 | */ |
aoqi@0 | 84 | protected final DocPath path; |
aoqi@0 | 85 | |
aoqi@0 | 86 | /** |
aoqi@0 | 87 | * List the directories and files found in subdirectories along the |
aoqi@0 | 88 | * elements of the given location. |
aoqi@0 | 89 | * @param configuration the doclet configuration |
aoqi@0 | 90 | * @param location currently, only {@link StandardLocation#SOURCE_PATH} is supported. |
aoqi@0 | 91 | * @param path the subdirectory of the directories of the location for which to |
aoqi@0 | 92 | * list files |
aoqi@0 | 93 | */ |
aoqi@0 | 94 | public static Iterable<DocFile> list(Configuration configuration, Location location, DocPath path) { |
aoqi@0 | 95 | return DocFileFactory.getFactory(configuration).list(location, path); |
aoqi@0 | 96 | } |
aoqi@0 | 97 | |
aoqi@0 | 98 | /** Create a DocFile without a location or path */ |
aoqi@0 | 99 | protected DocFile(Configuration configuration) { |
aoqi@0 | 100 | this.configuration = configuration; |
aoqi@0 | 101 | this.location = null; |
aoqi@0 | 102 | this.path = null; |
aoqi@0 | 103 | } |
aoqi@0 | 104 | |
aoqi@0 | 105 | /** Create a DocFile for a given location and relative path. */ |
aoqi@0 | 106 | protected DocFile(Configuration configuration, Location location, DocPath path) { |
aoqi@0 | 107 | this.configuration = configuration; |
aoqi@0 | 108 | this.location = location; |
aoqi@0 | 109 | this.path = path; |
aoqi@0 | 110 | } |
aoqi@0 | 111 | |
aoqi@0 | 112 | /** Open an input stream for the file. */ |
aoqi@0 | 113 | public abstract InputStream openInputStream() throws IOException; |
aoqi@0 | 114 | |
aoqi@0 | 115 | /** |
aoqi@0 | 116 | * Open an output stream for the file. |
aoqi@0 | 117 | * The file must have been created with a location of |
aoqi@0 | 118 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} |
aoqi@0 | 119 | * and a corresponding relative path. |
aoqi@0 | 120 | */ |
aoqi@0 | 121 | public abstract OutputStream openOutputStream() throws IOException, UnsupportedEncodingException; |
aoqi@0 | 122 | |
aoqi@0 | 123 | /** |
aoqi@0 | 124 | * Open an writer for the file, using the encoding (if any) given in the |
aoqi@0 | 125 | * doclet configuration. |
aoqi@0 | 126 | * The file must have been created with a location of |
aoqi@0 | 127 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
aoqi@0 | 128 | */ |
aoqi@0 | 129 | public abstract Writer openWriter() throws IOException, UnsupportedEncodingException; |
aoqi@0 | 130 | |
aoqi@0 | 131 | /** |
aoqi@0 | 132 | * Copy the contents of another file directly to this file. |
aoqi@0 | 133 | */ |
aoqi@0 | 134 | public void copyFile(DocFile fromFile) throws IOException { |
aoqi@0 | 135 | InputStream input = fromFile.openInputStream(); |
aoqi@0 | 136 | OutputStream output = openOutputStream(); |
aoqi@0 | 137 | try { |
aoqi@0 | 138 | byte[] bytearr = new byte[1024]; |
aoqi@0 | 139 | int len; |
aoqi@0 | 140 | while ((len = input.read(bytearr)) != -1) { |
aoqi@0 | 141 | output.write(bytearr, 0, len); |
aoqi@0 | 142 | } |
aoqi@0 | 143 | } catch (FileNotFoundException exc) { |
aoqi@0 | 144 | } catch (SecurityException exc) { |
aoqi@0 | 145 | } finally { |
aoqi@0 | 146 | input.close(); |
aoqi@0 | 147 | output.close(); |
aoqi@0 | 148 | } |
aoqi@0 | 149 | } |
aoqi@0 | 150 | |
aoqi@0 | 151 | /** |
aoqi@0 | 152 | * Copy the contents of a resource file to this file. |
aoqi@0 | 153 | * @param resource the path of the resource, relative to the package of this class |
aoqi@0 | 154 | * @param overwrite whether or not to overwrite the file if it already exists |
aoqi@0 | 155 | * @param replaceNewLine if false, the file is copied as a binary file; |
aoqi@0 | 156 | * if true, the file is written line by line, using the platform line |
aoqi@0 | 157 | * separator |
aoqi@0 | 158 | */ |
aoqi@0 | 159 | public void copyResource(DocPath resource, boolean overwrite, boolean replaceNewLine) { |
aoqi@0 | 160 | if (exists() && !overwrite) |
aoqi@0 | 161 | return; |
aoqi@0 | 162 | |
aoqi@0 | 163 | try { |
aoqi@0 | 164 | InputStream in = Configuration.class.getResourceAsStream(resource.getPath()); |
aoqi@0 | 165 | if (in == null) |
aoqi@0 | 166 | return; |
aoqi@0 | 167 | |
aoqi@0 | 168 | OutputStream out = openOutputStream(); |
aoqi@0 | 169 | try { |
aoqi@0 | 170 | if (!replaceNewLine) { |
aoqi@0 | 171 | byte[] buf = new byte[2048]; |
aoqi@0 | 172 | int n; |
aoqi@0 | 173 | while((n = in.read(buf))>0) out.write(buf,0,n); |
aoqi@0 | 174 | } else { |
aoqi@0 | 175 | BufferedReader reader = new BufferedReader(new InputStreamReader(in)); |
aoqi@0 | 176 | BufferedWriter writer; |
aoqi@0 | 177 | if (configuration.docencoding == null) { |
aoqi@0 | 178 | writer = new BufferedWriter(new OutputStreamWriter(out)); |
aoqi@0 | 179 | } else { |
aoqi@0 | 180 | writer = new BufferedWriter(new OutputStreamWriter(out, |
aoqi@0 | 181 | configuration.docencoding)); |
aoqi@0 | 182 | } |
aoqi@0 | 183 | try { |
aoqi@0 | 184 | String line; |
aoqi@0 | 185 | while ((line = reader.readLine()) != null) { |
aoqi@0 | 186 | writer.write(line); |
aoqi@0 | 187 | writer.write(DocletConstants.NL); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | } finally { |
aoqi@0 | 190 | reader.close(); |
aoqi@0 | 191 | writer.close(); |
aoqi@0 | 192 | } |
aoqi@0 | 193 | } |
aoqi@0 | 194 | } finally { |
aoqi@0 | 195 | in.close(); |
aoqi@0 | 196 | out.close(); |
aoqi@0 | 197 | } |
aoqi@0 | 198 | } catch (IOException e) { |
aoqi@0 | 199 | e.printStackTrace(System.err); |
aoqi@0 | 200 | throw new DocletAbortException(e); |
aoqi@0 | 201 | } |
aoqi@0 | 202 | } |
aoqi@0 | 203 | |
aoqi@0 | 204 | /** Return true if the file can be read. */ |
aoqi@0 | 205 | public abstract boolean canRead(); |
aoqi@0 | 206 | |
aoqi@0 | 207 | /** Return true if the file can be written. */ |
aoqi@0 | 208 | public abstract boolean canWrite(); |
aoqi@0 | 209 | |
aoqi@0 | 210 | /** Return true if the file exists. */ |
aoqi@0 | 211 | public abstract boolean exists(); |
aoqi@0 | 212 | |
aoqi@0 | 213 | /** Return the base name (last component) of the file name. */ |
aoqi@0 | 214 | public abstract String getName(); |
aoqi@0 | 215 | |
aoqi@0 | 216 | /** Return the file system path for this file. */ |
aoqi@0 | 217 | public abstract String getPath(); |
aoqi@0 | 218 | |
aoqi@0 | 219 | /** Return true if file has an absolute path name. */ |
aoqi@0 | 220 | public abstract boolean isAbsolute(); |
aoqi@0 | 221 | |
aoqi@0 | 222 | /** Return true if file identifies a directory. */ |
aoqi@0 | 223 | public abstract boolean isDirectory(); |
aoqi@0 | 224 | |
aoqi@0 | 225 | /** Return true if file identifies a file. */ |
aoqi@0 | 226 | public abstract boolean isFile(); |
aoqi@0 | 227 | |
aoqi@0 | 228 | /** Return true if this file is the same as another. */ |
aoqi@0 | 229 | public abstract boolean isSameFile(DocFile other); |
aoqi@0 | 230 | |
aoqi@0 | 231 | /** If the file is a directory, list its contents. */ |
aoqi@0 | 232 | public abstract Iterable<DocFile> list() throws IOException; |
aoqi@0 | 233 | |
aoqi@0 | 234 | /** Create the file as a directory, including any parent directories. */ |
aoqi@0 | 235 | public abstract boolean mkdirs(); |
aoqi@0 | 236 | |
aoqi@0 | 237 | /** |
aoqi@0 | 238 | * Derive a new file by resolving a relative path against this file. |
aoqi@0 | 239 | * The new file will inherit the configuration and location of this file |
aoqi@0 | 240 | * If this file has a path set, the new file will have a corresponding |
aoqi@0 | 241 | * new path. |
aoqi@0 | 242 | */ |
aoqi@0 | 243 | public abstract DocFile resolve(DocPath p); |
aoqi@0 | 244 | |
aoqi@0 | 245 | /** |
aoqi@0 | 246 | * Derive a new file by resolving a relative path against this file. |
aoqi@0 | 247 | * The new file will inherit the configuration and location of this file |
aoqi@0 | 248 | * If this file has a path set, the new file will have a corresponding |
aoqi@0 | 249 | * new path. |
aoqi@0 | 250 | */ |
aoqi@0 | 251 | public abstract DocFile resolve(String p); |
aoqi@0 | 252 | |
aoqi@0 | 253 | /** |
aoqi@0 | 254 | * Resolve a relative file against the given output location. |
aoqi@0 | 255 | * @param locn Currently, only |
aoqi@0 | 256 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. |
aoqi@0 | 257 | */ |
aoqi@0 | 258 | public abstract DocFile resolveAgainst(Location locn); |
aoqi@0 | 259 | } |