1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/StandardDocFileFactory.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,322 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.doclets.internal.toolkit.util; 1.30 + 1.31 +import java.io.BufferedInputStream; 1.32 +import java.io.BufferedOutputStream; 1.33 +import java.io.BufferedWriter; 1.34 +import java.io.File; 1.35 +import java.io.IOException; 1.36 +import java.io.InputStream; 1.37 +import java.io.OutputStream; 1.38 +import java.io.OutputStreamWriter; 1.39 +import java.io.UnsupportedEncodingException; 1.40 +import java.io.Writer; 1.41 +import java.util.ArrayList; 1.42 +import java.util.Arrays; 1.43 +import java.util.LinkedHashSet; 1.44 +import java.util.List; 1.45 +import java.util.Set; 1.46 + 1.47 +import javax.tools.DocumentationTool; 1.48 +import javax.tools.FileObject; 1.49 +import javax.tools.JavaFileManager.Location; 1.50 +import javax.tools.JavaFileObject; 1.51 +import javax.tools.StandardJavaFileManager; 1.52 +import javax.tools.StandardLocation; 1.53 + 1.54 +import com.sun.tools.doclets.internal.toolkit.Configuration; 1.55 +import com.sun.tools.javac.util.Assert; 1.56 + 1.57 +/** 1.58 + * Implementation of DocFileFactory using a {@link StandardJavaFileManager}. 1.59 + * 1.60 + * <p><b>This is NOT part of any supported API. 1.61 + * If you write code that depends on this, you do so at your own risk. 1.62 + * This code and its internal interfaces are subject to change or 1.63 + * deletion without notice.</b> 1.64 + * 1.65 + * @since 1.8 1.66 + */ 1.67 +class StandardDocFileFactory extends DocFileFactory { 1.68 + private final StandardJavaFileManager fileManager; 1.69 + private File destDir; 1.70 + 1.71 + public StandardDocFileFactory(Configuration configuration) { 1.72 + super(configuration); 1.73 + fileManager = (StandardJavaFileManager) configuration.getFileManager(); 1.74 + } 1.75 + 1.76 + private File getDestDir() { 1.77 + if (destDir == null) { 1.78 + if (!configuration.destDirName.isEmpty() 1.79 + || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { 1.80 + try { 1.81 + String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; 1.82 + File dir = new File(dirName); 1.83 + fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); 1.84 + } catch (IOException e) { 1.85 + throw new DocletAbortException(e); 1.86 + } 1.87 + } 1.88 + 1.89 + destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); 1.90 + } 1.91 + return destDir; 1.92 + } 1.93 + 1.94 + public DocFile createFileForDirectory(String file) { 1.95 + return new StandardDocFile(new File(file)); 1.96 + } 1.97 + 1.98 + public DocFile createFileForInput(String file) { 1.99 + return new StandardDocFile(new File(file)); 1.100 + } 1.101 + 1.102 + public DocFile createFileForOutput(DocPath path) { 1.103 + return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); 1.104 + } 1.105 + 1.106 + @Override 1.107 + Iterable<DocFile> list(Location location, DocPath path) { 1.108 + if (location != StandardLocation.SOURCE_PATH) 1.109 + throw new IllegalArgumentException(); 1.110 + 1.111 + Set<DocFile> files = new LinkedHashSet<DocFile>(); 1.112 + Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH) 1.113 + ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; 1.114 + for (File f: fileManager.getLocation(l)) { 1.115 + if (f.isDirectory()) { 1.116 + f = new File(f, path.getPath()); 1.117 + if (f.exists()) 1.118 + files.add(new StandardDocFile(f)); 1.119 + } 1.120 + } 1.121 + return files; 1.122 + } 1.123 + 1.124 + private static File newFile(File dir, String path) { 1.125 + return (dir == null) ? new File(path) : new File(dir, path); 1.126 + } 1.127 + 1.128 + class StandardDocFile extends DocFile { 1.129 + private File file; 1.130 + 1.131 + 1.132 + /** Create a StandardDocFile for a given file. */ 1.133 + private StandardDocFile(File file) { 1.134 + super(configuration); 1.135 + this.file = file; 1.136 + } 1.137 + 1.138 + /** Create a StandardDocFile for a given location and relative path. */ 1.139 + private StandardDocFile(Location location, DocPath path) { 1.140 + super(configuration, location, path); 1.141 + Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT); 1.142 + this.file = newFile(getDestDir(), path.getPath()); 1.143 + } 1.144 + 1.145 + /** Open an input stream for the file. */ 1.146 + public InputStream openInputStream() throws IOException { 1.147 + JavaFileObject fo = getJavaFileObjectForInput(file); 1.148 + return new BufferedInputStream(fo.openInputStream()); 1.149 + } 1.150 + 1.151 + /** 1.152 + * Open an output stream for the file. 1.153 + * The file must have been created with a location of 1.154 + * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. 1.155 + */ 1.156 + public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { 1.157 + if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) 1.158 + throw new IllegalStateException(); 1.159 + 1.160 + OutputStream out = getFileObjectForOutput(path).openOutputStream(); 1.161 + return new BufferedOutputStream(out); 1.162 + } 1.163 + 1.164 + /** 1.165 + * Open an writer for the file, using the encoding (if any) given in the 1.166 + * doclet configuration. 1.167 + * The file must have been created with a location of 1.168 + * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. 1.169 + */ 1.170 + public Writer openWriter() throws IOException, UnsupportedEncodingException { 1.171 + if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) 1.172 + throw new IllegalStateException(); 1.173 + 1.174 + OutputStream out = getFileObjectForOutput(path).openOutputStream(); 1.175 + if (configuration.docencoding == null) { 1.176 + return new BufferedWriter(new OutputStreamWriter(out)); 1.177 + } else { 1.178 + return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding)); 1.179 + } 1.180 + } 1.181 + 1.182 + /** Return true if the file can be read. */ 1.183 + public boolean canRead() { 1.184 + return file.canRead(); 1.185 + } 1.186 + 1.187 + /** Return true if the file can be written. */ 1.188 + public boolean canWrite() { 1.189 + return file.canWrite(); 1.190 + } 1.191 + 1.192 + /** Return true if the file exists. */ 1.193 + public boolean exists() { 1.194 + return file.exists(); 1.195 + } 1.196 + 1.197 + /** Return the base name (last component) of the file name. */ 1.198 + public String getName() { 1.199 + return file.getName(); 1.200 + } 1.201 + 1.202 + /** Return the file system path for this file. */ 1.203 + public String getPath() { 1.204 + return file.getPath(); 1.205 + } 1.206 + 1.207 + /** Return true is file has an absolute path name. */ 1.208 + public boolean isAbsolute() { 1.209 + return file.isAbsolute(); 1.210 + } 1.211 + 1.212 + /** Return true is file identifies a directory. */ 1.213 + public boolean isDirectory() { 1.214 + return file.isDirectory(); 1.215 + } 1.216 + 1.217 + /** Return true is file identifies a file. */ 1.218 + public boolean isFile() { 1.219 + return file.isFile(); 1.220 + } 1.221 + 1.222 + /** Return true if this file is the same as another. */ 1.223 + public boolean isSameFile(DocFile other) { 1.224 + if (!(other instanceof StandardDocFile)) 1.225 + return false; 1.226 + 1.227 + try { 1.228 + return file.exists() 1.229 + && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile()); 1.230 + } catch (IOException e) { 1.231 + return false; 1.232 + } 1.233 + } 1.234 + 1.235 + /** If the file is a directory, list its contents. */ 1.236 + public Iterable<DocFile> list() { 1.237 + List<DocFile> files = new ArrayList<DocFile>(); 1.238 + for (File f: file.listFiles()) { 1.239 + files.add(new StandardDocFile(f)); 1.240 + } 1.241 + return files; 1.242 + } 1.243 + 1.244 + /** Create the file as a directory, including any parent directories. */ 1.245 + public boolean mkdirs() { 1.246 + return file.mkdirs(); 1.247 + } 1.248 + 1.249 + /** 1.250 + * Derive a new file by resolving a relative path against this file. 1.251 + * The new file will inherit the configuration and location of this file 1.252 + * If this file has a path set, the new file will have a corresponding 1.253 + * new path. 1.254 + */ 1.255 + public DocFile resolve(DocPath p) { 1.256 + return resolve(p.getPath()); 1.257 + } 1.258 + 1.259 + /** 1.260 + * Derive a new file by resolving a relative path against this file. 1.261 + * The new file will inherit the configuration and location of this file 1.262 + * If this file has a path set, the new file will have a corresponding 1.263 + * new path. 1.264 + */ 1.265 + public DocFile resolve(String p) { 1.266 + if (location == null && path == null) { 1.267 + return new StandardDocFile(new File(file, p)); 1.268 + } else { 1.269 + return new StandardDocFile(location, path.resolve(p)); 1.270 + } 1.271 + } 1.272 + 1.273 + /** 1.274 + * Resolve a relative file against the given output location. 1.275 + * @param locn Currently, only 1.276 + * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. 1.277 + */ 1.278 + public DocFile resolveAgainst(Location locn) { 1.279 + if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) 1.280 + throw new IllegalArgumentException(); 1.281 + return new StandardDocFile(newFile(getDestDir(), file.getPath())); 1.282 + } 1.283 + 1.284 + /** Return a string to identify the contents of this object, 1.285 + * for debugging purposes. 1.286 + */ 1.287 + @Override 1.288 + public String toString() { 1.289 + StringBuilder sb = new StringBuilder(); 1.290 + sb.append("StandardDocFile["); 1.291 + if (location != null) 1.292 + sb.append("locn:").append(location).append(","); 1.293 + if (path != null) 1.294 + sb.append("path:").append(path.getPath()).append(","); 1.295 + sb.append("file:").append(file); 1.296 + sb.append("]"); 1.297 + return sb.toString(); 1.298 + } 1.299 + 1.300 + private JavaFileObject getJavaFileObjectForInput(File file) { 1.301 + return fileManager.getJavaFileObjects(file).iterator().next(); 1.302 + } 1.303 + 1.304 + private FileObject getFileObjectForOutput(DocPath path) throws IOException { 1.305 + // break the path into a package-part and the rest, by finding 1.306 + // the position of the last '/' before an invalid character for a 1.307 + // package name, such as the "." before an extension or the "-" 1.308 + // in filenames like package-summary.html, doc-files or src-html. 1.309 + String p = path.getPath(); 1.310 + int lastSep = -1; 1.311 + for (int i = 0; i < p.length(); i++) { 1.312 + char ch = p.charAt(i); 1.313 + if (ch == '/') { 1.314 + lastSep = i; 1.315 + } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch) 1.316 + || !Character.isJavaIdentifierPart(ch)) { 1.317 + break; 1.318 + } 1.319 + } 1.320 + String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep); 1.321 + String rest = p.substring(lastSep + 1); 1.322 + return fileManager.getFileForOutput(location, pkg, rest, null); 1.323 + } 1.324 + } 1.325 +}