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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 1985
0e6577980181
parent 0
959103a6100f
permissions
-rw-r--r--

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

mercurial