Sat, 07 Nov 2020 10:30:02 +0800
Added tag mips-jdk8u275-b01 for changeset eb6ee6a5f2fe
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 | package com.sun.tools.doclets.internal.toolkit.util; |
aoqi@0 | 26 | |
aoqi@0 | 27 | |
aoqi@0 | 28 | import java.io.BufferedInputStream; |
aoqi@0 | 29 | import java.io.BufferedOutputStream; |
aoqi@0 | 30 | import java.io.BufferedWriter; |
aoqi@0 | 31 | import java.io.IOException; |
aoqi@0 | 32 | import java.io.InputStream; |
aoqi@0 | 33 | import java.io.OutputStream; |
aoqi@0 | 34 | import java.io.OutputStreamWriter; |
aoqi@0 | 35 | import java.io.UnsupportedEncodingException; |
aoqi@0 | 36 | import java.io.Writer; |
aoqi@0 | 37 | import java.nio.file.DirectoryStream; |
aoqi@0 | 38 | import java.nio.file.Files; |
aoqi@0 | 39 | import java.nio.file.Path; |
aoqi@0 | 40 | import java.util.ArrayList; |
aoqi@0 | 41 | import java.util.Arrays; |
aoqi@0 | 42 | import java.util.LinkedHashSet; |
aoqi@0 | 43 | import java.util.List; |
aoqi@0 | 44 | import java.util.Set; |
aoqi@0 | 45 | |
aoqi@0 | 46 | import javax.tools.DocumentationTool; |
aoqi@0 | 47 | import javax.tools.FileObject; |
aoqi@0 | 48 | import javax.tools.JavaFileManager.Location; |
aoqi@0 | 49 | import javax.tools.JavaFileObject; |
aoqi@0 | 50 | import javax.tools.StandardLocation; |
aoqi@0 | 51 | |
aoqi@0 | 52 | import com.sun.tools.doclets.internal.toolkit.Configuration; |
aoqi@0 | 53 | import com.sun.tools.javac.nio.PathFileManager; |
aoqi@0 | 54 | |
aoqi@0 | 55 | |
aoqi@0 | 56 | /** |
aoqi@0 | 57 | * Implementation of DocFileFactory using a {@link PathFileManager}. |
aoqi@0 | 58 | * |
aoqi@0 | 59 | * <p><b>This is NOT part of any supported API. |
aoqi@0 | 60 | * If you write code that depends on this, you do so at your own risk. |
aoqi@0 | 61 | * This code and its internal interfaces are subject to change or |
aoqi@0 | 62 | * deletion without notice.</b> |
aoqi@0 | 63 | * |
aoqi@0 | 64 | * @since 1.8 |
aoqi@0 | 65 | */ |
aoqi@0 | 66 | class PathDocFileFactory extends DocFileFactory { |
aoqi@0 | 67 | private final PathFileManager fileManager; |
aoqi@0 | 68 | private final Path destDir; |
aoqi@0 | 69 | |
aoqi@0 | 70 | public PathDocFileFactory(Configuration configuration) { |
aoqi@0 | 71 | super(configuration); |
aoqi@0 | 72 | fileManager = (PathFileManager) configuration.getFileManager(); |
aoqi@0 | 73 | |
aoqi@0 | 74 | if (!configuration.destDirName.isEmpty() |
aoqi@0 | 75 | || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { |
aoqi@0 | 76 | try { |
aoqi@0 | 77 | String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; |
aoqi@0 | 78 | Path dir = fileManager.getDefaultFileSystem().getPath(dirName); |
aoqi@0 | 79 | fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); |
aoqi@0 | 80 | } catch (IOException e) { |
aoqi@0 | 81 | throw new DocletAbortException(e); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | } |
aoqi@0 | 84 | |
aoqi@0 | 85 | destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | public DocFile createFileForDirectory(String file) { |
aoqi@0 | 89 | return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file)); |
aoqi@0 | 90 | } |
aoqi@0 | 91 | |
aoqi@0 | 92 | public DocFile createFileForInput(String file) { |
aoqi@0 | 93 | return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file)); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | public DocFile createFileForOutput(DocPath path) { |
aoqi@0 | 97 | return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); |
aoqi@0 | 98 | } |
aoqi@0 | 99 | |
aoqi@0 | 100 | @Override |
aoqi@0 | 101 | Iterable<DocFile> list(Location location, DocPath path) { |
aoqi@0 | 102 | if (location != StandardLocation.SOURCE_PATH) |
aoqi@0 | 103 | throw new IllegalArgumentException(); |
aoqi@0 | 104 | |
aoqi@0 | 105 | Set<DocFile> files = new LinkedHashSet<DocFile>(); |
aoqi@0 | 106 | if (fileManager.hasLocation(location)) { |
aoqi@0 | 107 | for (Path f: fileManager.getLocation(location)) { |
aoqi@0 | 108 | if (Files.isDirectory(f)) { |
aoqi@0 | 109 | f = f.resolve(path.getPath()); |
aoqi@0 | 110 | if (Files.exists(f)) |
aoqi@0 | 111 | files.add(new StandardDocFile(f)); |
aoqi@0 | 112 | } |
aoqi@0 | 113 | } |
aoqi@0 | 114 | } |
aoqi@0 | 115 | return files; |
aoqi@0 | 116 | } |
aoqi@0 | 117 | |
aoqi@0 | 118 | class StandardDocFile extends DocFile { |
aoqi@0 | 119 | private Path file; |
aoqi@0 | 120 | |
aoqi@0 | 121 | /** Create a StandardDocFile for a given file. */ |
aoqi@0 | 122 | private StandardDocFile(Path file) { |
aoqi@0 | 123 | super(configuration); |
aoqi@0 | 124 | this.file = file; |
aoqi@0 | 125 | } |
aoqi@0 | 126 | |
aoqi@0 | 127 | /** Create a StandardDocFile for a given location and relative path. */ |
aoqi@0 | 128 | private StandardDocFile(Location location, DocPath path) { |
aoqi@0 | 129 | super(configuration, location, path); |
aoqi@0 | 130 | this.file = destDir.resolve(path.getPath()); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | |
aoqi@0 | 133 | /** Open an input stream for the file. */ |
aoqi@0 | 134 | public InputStream openInputStream() throws IOException { |
aoqi@0 | 135 | JavaFileObject fo = getJavaFileObjectForInput(file); |
aoqi@0 | 136 | return new BufferedInputStream(fo.openInputStream()); |
aoqi@0 | 137 | } |
aoqi@0 | 138 | |
aoqi@0 | 139 | /** |
aoqi@0 | 140 | * Open an output stream for the file. |
aoqi@0 | 141 | * The file must have been created with a location of |
aoqi@0 | 142 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
aoqi@0 | 143 | */ |
aoqi@0 | 144 | public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { |
aoqi@0 | 145 | if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
aoqi@0 | 146 | throw new IllegalStateException(); |
aoqi@0 | 147 | |
aoqi@0 | 148 | OutputStream out = getFileObjectForOutput(path).openOutputStream(); |
aoqi@0 | 149 | return new BufferedOutputStream(out); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | |
aoqi@0 | 152 | /** |
aoqi@0 | 153 | * Open an writer for the file, using the encoding (if any) given in the |
aoqi@0 | 154 | * doclet configuration. |
aoqi@0 | 155 | * The file must have been created with a location of |
aoqi@0 | 156 | * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. |
aoqi@0 | 157 | */ |
aoqi@0 | 158 | public Writer openWriter() throws IOException, UnsupportedEncodingException { |
aoqi@0 | 159 | if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
aoqi@0 | 160 | throw new IllegalStateException(); |
aoqi@0 | 161 | |
aoqi@0 | 162 | OutputStream out = getFileObjectForOutput(path).openOutputStream(); |
aoqi@0 | 163 | if (configuration.docencoding == null) { |
aoqi@0 | 164 | return new BufferedWriter(new OutputStreamWriter(out)); |
aoqi@0 | 165 | } else { |
aoqi@0 | 166 | return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding)); |
aoqi@0 | 167 | } |
aoqi@0 | 168 | } |
aoqi@0 | 169 | |
aoqi@0 | 170 | /** Return true if the file can be read. */ |
aoqi@0 | 171 | public boolean canRead() { |
aoqi@0 | 172 | return Files.isReadable(file); |
aoqi@0 | 173 | } |
aoqi@0 | 174 | |
aoqi@0 | 175 | /** Return true if the file can be written. */ |
aoqi@0 | 176 | public boolean canWrite() { |
aoqi@0 | 177 | return Files.isWritable(file); |
aoqi@0 | 178 | } |
aoqi@0 | 179 | |
aoqi@0 | 180 | /** Return true if the file exists. */ |
aoqi@0 | 181 | public boolean exists() { |
aoqi@0 | 182 | return Files.exists(file); |
aoqi@0 | 183 | } |
aoqi@0 | 184 | |
aoqi@0 | 185 | /** Return the base name (last component) of the file name. */ |
aoqi@0 | 186 | public String getName() { |
aoqi@0 | 187 | return file.getFileName().toString(); |
aoqi@0 | 188 | } |
aoqi@0 | 189 | |
aoqi@0 | 190 | /** Return the file system path for this file. */ |
aoqi@0 | 191 | public String getPath() { |
aoqi@0 | 192 | return file.toString(); |
aoqi@0 | 193 | } |
aoqi@0 | 194 | |
aoqi@0 | 195 | /** Return true is file has an absolute path name. */ |
aoqi@0 | 196 | public boolean isAbsolute() { |
aoqi@0 | 197 | return file.isAbsolute(); |
aoqi@0 | 198 | } |
aoqi@0 | 199 | |
aoqi@0 | 200 | /** Return true is file identifies a directory. */ |
aoqi@0 | 201 | public boolean isDirectory() { |
aoqi@0 | 202 | return Files.isDirectory(file); |
aoqi@0 | 203 | } |
aoqi@0 | 204 | |
aoqi@0 | 205 | /** Return true is file identifies a file. */ |
aoqi@0 | 206 | public boolean isFile() { |
aoqi@0 | 207 | return Files.isRegularFile(file); |
aoqi@0 | 208 | } |
aoqi@0 | 209 | |
aoqi@0 | 210 | /** Return true if this file is the same as another. */ |
aoqi@0 | 211 | public boolean isSameFile(DocFile other) { |
aoqi@0 | 212 | if (!(other instanceof StandardDocFile)) |
aoqi@0 | 213 | return false; |
aoqi@0 | 214 | |
aoqi@0 | 215 | try { |
aoqi@0 | 216 | return Files.isSameFile(file, ((StandardDocFile) other).file); |
aoqi@0 | 217 | } catch (IOException e) { |
aoqi@0 | 218 | return false; |
aoqi@0 | 219 | } |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | /** If the file is a directory, list its contents. */ |
aoqi@0 | 223 | public Iterable<DocFile> list() throws IOException { |
aoqi@0 | 224 | List<DocFile> files = new ArrayList<DocFile>(); |
aoqi@0 | 225 | try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) { |
aoqi@0 | 226 | for (Path f: ds) { |
aoqi@0 | 227 | files.add(new StandardDocFile(f)); |
aoqi@0 | 228 | } |
aoqi@0 | 229 | } |
aoqi@0 | 230 | return files; |
aoqi@0 | 231 | } |
aoqi@0 | 232 | |
aoqi@0 | 233 | /** Create the file as a directory, including any parent directories. */ |
aoqi@0 | 234 | public boolean mkdirs() { |
aoqi@0 | 235 | try { |
aoqi@0 | 236 | Files.createDirectories(file); |
aoqi@0 | 237 | return true; |
aoqi@0 | 238 | } catch (IOException e) { |
aoqi@0 | 239 | return false; |
aoqi@0 | 240 | } |
aoqi@0 | 241 | } |
aoqi@0 | 242 | |
aoqi@0 | 243 | /** |
aoqi@0 | 244 | * Derive a new file by resolving a relative path against this file. |
aoqi@0 | 245 | * The new file will inherit the configuration and location of this file |
aoqi@0 | 246 | * If this file has a path set, the new file will have a corresponding |
aoqi@0 | 247 | * new path. |
aoqi@0 | 248 | */ |
aoqi@0 | 249 | public DocFile resolve(DocPath p) { |
aoqi@0 | 250 | return resolve(p.getPath()); |
aoqi@0 | 251 | } |
aoqi@0 | 252 | |
aoqi@0 | 253 | /** |
aoqi@0 | 254 | * Derive a new file by resolving a relative path against this file. |
aoqi@0 | 255 | * The new file will inherit the configuration and location of this file |
aoqi@0 | 256 | * If this file has a path set, the new file will have a corresponding |
aoqi@0 | 257 | * new path. |
aoqi@0 | 258 | */ |
aoqi@0 | 259 | public DocFile resolve(String p) { |
aoqi@0 | 260 | if (location == null && path == null) { |
aoqi@0 | 261 | return new StandardDocFile(file.resolve(p)); |
aoqi@0 | 262 | } else { |
aoqi@0 | 263 | return new StandardDocFile(location, path.resolve(p)); |
aoqi@0 | 264 | } |
aoqi@0 | 265 | } |
aoqi@0 | 266 | |
aoqi@0 | 267 | /** |
aoqi@0 | 268 | * Resolve a relative file against the given output location. |
aoqi@0 | 269 | * @param locn Currently, only |
aoqi@0 | 270 | * {@link DocumentationTool.Location.DOCUMENTATION_OUTPUT} is supported. |
aoqi@0 | 271 | */ |
aoqi@0 | 272 | public DocFile resolveAgainst(Location locn) { |
aoqi@0 | 273 | if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) |
aoqi@0 | 274 | throw new IllegalArgumentException(); |
aoqi@0 | 275 | return new StandardDocFile(destDir.resolve(file)); |
aoqi@0 | 276 | } |
aoqi@0 | 277 | |
aoqi@0 | 278 | /** Return a string to identify the contents of this object, |
aoqi@0 | 279 | * for debugging purposes. |
aoqi@0 | 280 | */ |
aoqi@0 | 281 | @Override |
aoqi@0 | 282 | public String toString() { |
aoqi@0 | 283 | StringBuilder sb = new StringBuilder(); |
aoqi@0 | 284 | sb.append("PathDocFile["); |
aoqi@0 | 285 | if (location != null) |
aoqi@0 | 286 | sb.append("locn:").append(location).append(","); |
aoqi@0 | 287 | if (path != null) |
aoqi@0 | 288 | sb.append("path:").append(path.getPath()).append(","); |
aoqi@0 | 289 | sb.append("file:").append(file); |
aoqi@0 | 290 | sb.append("]"); |
aoqi@0 | 291 | return sb.toString(); |
aoqi@0 | 292 | } |
aoqi@0 | 293 | |
aoqi@0 | 294 | private JavaFileObject getJavaFileObjectForInput(Path file) { |
aoqi@0 | 295 | return fileManager.getJavaFileObjects(file).iterator().next(); |
aoqi@0 | 296 | } |
aoqi@0 | 297 | |
aoqi@0 | 298 | private FileObject getFileObjectForOutput(DocPath path) throws IOException { |
aoqi@0 | 299 | // break the path into a package-part and the rest, by finding |
aoqi@0 | 300 | // the position of the last '/' before an invalid character for a |
aoqi@0 | 301 | // package name, such as the "." before an extension or the "-" |
aoqi@0 | 302 | // in filenames like package-summary.html, doc-files or src-html. |
aoqi@0 | 303 | String p = path.getPath(); |
aoqi@0 | 304 | int lastSep = -1; |
aoqi@0 | 305 | for (int i = 0; i < p.length(); i++) { |
aoqi@0 | 306 | char ch = p.charAt(i); |
aoqi@0 | 307 | if (ch == '/') { |
aoqi@0 | 308 | lastSep = i; |
aoqi@0 | 309 | } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch) |
aoqi@0 | 310 | || !Character.isJavaIdentifierPart(ch)) { |
aoqi@0 | 311 | break; |
aoqi@0 | 312 | } |
aoqi@0 | 313 | } |
aoqi@0 | 314 | String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep); |
aoqi@0 | 315 | String rest = p.substring(lastSep + 1); |
aoqi@0 | 316 | return fileManager.getFileForOutput(location, pkg, rest, null); |
aoqi@0 | 317 | } |
aoqi@0 | 318 | } |
aoqi@0 | 319 | |
aoqi@0 | 320 | } |