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 Thu Nov 15 19:54:20 2012 -0800 1.3 @@ -0,0 +1,315 @@ 1.4 +/* 1.5 + * Copyright (c) 1998, 2012, 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.FileObject; 1.48 +import javax.tools.JavaFileManager.Location; 1.49 +import javax.tools.JavaFileObject; 1.50 +import javax.tools.StandardJavaFileManager; 1.51 +import javax.tools.StandardLocation; 1.52 + 1.53 +import com.sun.tools.doclets.internal.toolkit.Configuration; 1.54 +import com.sun.tools.javac.util.Assert; 1.55 + 1.56 +/** 1.57 + * Implementation of DocFileFactory using a {@link StandardJavaFileManager}. 1.58 + * 1.59 + * <p><b>This is NOT part of any supported API. 1.60 + * If you write code that depends on this, you do so at your own risk. 1.61 + * This code and its internal interfaces are subject to change or 1.62 + * deletion without notice.</b> 1.63 + * 1.64 + * @since 1.8 1.65 + */ 1.66 +class StandardDocFileFactory extends DocFileFactory { 1.67 + private final StandardJavaFileManager fileManager; 1.68 + private final File destDir; 1.69 + 1.70 + public StandardDocFileFactory(Configuration configuration) { 1.71 + super(configuration); 1.72 + fileManager = (StandardJavaFileManager) configuration.getFileManager(); 1.73 + 1.74 + if (!configuration.destDirName.isEmpty() 1.75 + || !fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) { 1.76 + try { 1.77 + String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; 1.78 + File dir = new File(dirName); 1.79 + fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(dir)); 1.80 + } catch (IOException e) { 1.81 + throw new DocletAbortException(); 1.82 + } 1.83 + } 1.84 + 1.85 + destDir = fileManager.getLocation(StandardLocation.CLASS_OUTPUT).iterator().next(); 1.86 + } 1.87 + 1.88 + public DocFile createFileForDirectory(String file) { 1.89 + return new StandardDocFile(new File(file)); 1.90 + } 1.91 + 1.92 + public DocFile createFileForInput(String file) { 1.93 + return new StandardDocFile(new File(file)); 1.94 + } 1.95 + 1.96 + public DocFile createFileForOutput(DocPath path) { 1.97 + return new StandardDocFile(StandardLocation.CLASS_OUTPUT, path); 1.98 + } 1.99 + 1.100 + @Override 1.101 + Iterable<DocFile> list(Location location, DocPath path) { 1.102 + if (location != StandardLocation.SOURCE_PATH) 1.103 + throw new IllegalArgumentException(); 1.104 + 1.105 + Set<DocFile> files = new LinkedHashSet<DocFile>(); 1.106 + if (fileManager.hasLocation(location)) { 1.107 + for (File f: fileManager.getLocation(location)) { 1.108 + if (f.isDirectory()) { 1.109 + f = new File(f, path.getPath()); 1.110 + if (f.exists()) 1.111 + files.add(new StandardDocFile(f)); 1.112 + } 1.113 + } 1.114 + } 1.115 + return files; 1.116 + } 1.117 + 1.118 + private static File newFile(File dir, String path) { 1.119 + return (dir == null) ? new File(path) : new File(dir, path); 1.120 + } 1.121 + 1.122 + class StandardDocFile extends DocFile { 1.123 + private File file; 1.124 + 1.125 + 1.126 + /** Create a StandardDocFile for a given file. */ 1.127 + private StandardDocFile(File file) { 1.128 + super(configuration); 1.129 + this.file = file; 1.130 + } 1.131 + 1.132 + /** Create a StandardDocFile for a given location and relative path. */ 1.133 + private StandardDocFile(Location location, DocPath path) { 1.134 + super(configuration, location, path); 1.135 + Assert.check(location == StandardLocation.CLASS_OUTPUT); 1.136 + this.file = newFile(destDir, path.getPath()); 1.137 + } 1.138 + 1.139 + /** Open an input stream for the file. */ 1.140 + public InputStream openInputStream() throws IOException { 1.141 + JavaFileObject fo = getJavaFileObjectForInput(file); 1.142 + return new BufferedInputStream(fo.openInputStream()); 1.143 + } 1.144 + 1.145 + /** 1.146 + * Open an output stream for the file. 1.147 + * The file must have been created with a location of 1.148 + * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. 1.149 + */ 1.150 + public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { 1.151 + if (location != StandardLocation.CLASS_OUTPUT) 1.152 + throw new IllegalStateException(); 1.153 + 1.154 + OutputStream out = getFileObjectForOutput(path).openOutputStream(); 1.155 + return new BufferedOutputStream(out); 1.156 + } 1.157 + 1.158 + /** 1.159 + * Open an writer for the file, using the encoding (if any) given in the 1.160 + * doclet configuration. 1.161 + * The file must have been created with a location of 1.162 + * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path. 1.163 + */ 1.164 + public Writer openWriter() throws IOException, UnsupportedEncodingException { 1.165 + if (location != StandardLocation.CLASS_OUTPUT) 1.166 + throw new IllegalStateException(); 1.167 + 1.168 + OutputStream out = getFileObjectForOutput(path).openOutputStream(); 1.169 + if (configuration.docencoding == null) { 1.170 + return new BufferedWriter(new OutputStreamWriter(out)); 1.171 + } else { 1.172 + return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding)); 1.173 + } 1.174 + } 1.175 + 1.176 + /** Return true if the file can be read. */ 1.177 + public boolean canRead() { 1.178 + return file.canRead(); 1.179 + } 1.180 + 1.181 + /** Return true if the file can be written. */ 1.182 + public boolean canWrite() { 1.183 + return file.canWrite(); 1.184 + } 1.185 + 1.186 + /** Return true if the file exists. */ 1.187 + public boolean exists() { 1.188 + return file.exists(); 1.189 + } 1.190 + 1.191 + /** Return the base name (last component) of the file name. */ 1.192 + public String getName() { 1.193 + return file.getName(); 1.194 + } 1.195 + 1.196 + /** Return the file system path for this file. */ 1.197 + public String getPath() { 1.198 + return file.getPath(); 1.199 + } 1.200 + 1.201 + /** Return true is file has an absolute path name. */ 1.202 + public boolean isAbsolute() { 1.203 + return file.isAbsolute(); 1.204 + } 1.205 + 1.206 + /** Return true is file identifies a directory. */ 1.207 + public boolean isDirectory() { 1.208 + return file.isDirectory(); 1.209 + } 1.210 + 1.211 + /** Return true is file identifies a file. */ 1.212 + public boolean isFile() { 1.213 + return file.isFile(); 1.214 + } 1.215 + 1.216 + /** Return true if this file is the same as another. */ 1.217 + public boolean isSameFile(DocFile other) { 1.218 + if (!(other instanceof StandardDocFile)) 1.219 + return false; 1.220 + 1.221 + try { 1.222 + return file.exists() 1.223 + && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile()); 1.224 + } catch (IOException e) { 1.225 + return false; 1.226 + } 1.227 + } 1.228 + 1.229 + /** If the file is a directory, list its contents. */ 1.230 + public Iterable<DocFile> list() { 1.231 + List<DocFile> files = new ArrayList<DocFile>(); 1.232 + for (File f: file.listFiles()) { 1.233 + files.add(new StandardDocFile(f)); 1.234 + } 1.235 + return files; 1.236 + } 1.237 + 1.238 + /** Create the file as a directory, including any parent directories. */ 1.239 + public boolean mkdirs() { 1.240 + return file.mkdirs(); 1.241 + } 1.242 + 1.243 + /** 1.244 + * Derive a new file by resolving a relative path against this file. 1.245 + * The new file will inherit the configuration and location of this file 1.246 + * If this file has a path set, the new file will have a corresponding 1.247 + * new path. 1.248 + */ 1.249 + public DocFile resolve(DocPath p) { 1.250 + return resolve(p.getPath()); 1.251 + } 1.252 + 1.253 + /** 1.254 + * Derive a new file by resolving a relative path against this file. 1.255 + * The new file will inherit the configuration and location of this file 1.256 + * If this file has a path set, the new file will have a corresponding 1.257 + * new path. 1.258 + */ 1.259 + public DocFile resolve(String p) { 1.260 + if (location == null && path == null) { 1.261 + return new StandardDocFile(new File(file, p)); 1.262 + } else { 1.263 + return new StandardDocFile(location, path.resolve(p)); 1.264 + } 1.265 + } 1.266 + 1.267 + /** 1.268 + * Resolve a relative file against the given output location. 1.269 + * @param locn Currently, only SOURCE_OUTPUT is supported. 1.270 + */ 1.271 + public DocFile resolveAgainst(StandardLocation locn) { 1.272 + if (locn != StandardLocation.CLASS_OUTPUT) 1.273 + throw new IllegalArgumentException(); 1.274 + return new StandardDocFile(newFile(destDir, file.getPath())); 1.275 + } 1.276 + 1.277 + /** Return a string to identify the contents of this object, 1.278 + * for debugging purposes. 1.279 + */ 1.280 + @Override 1.281 + public String toString() { 1.282 + StringBuilder sb = new StringBuilder(); 1.283 + sb.append("StandardDocFile["); 1.284 + if (location != null) 1.285 + sb.append("locn:").append(location).append(","); 1.286 + if (path != null) 1.287 + sb.append("path:").append(path.getPath()).append(","); 1.288 + sb.append("file:").append(file); 1.289 + sb.append("]"); 1.290 + return sb.toString(); 1.291 + } 1.292 + 1.293 + private JavaFileObject getJavaFileObjectForInput(File file) { 1.294 + return fileManager.getJavaFileObjects(file).iterator().next(); 1.295 + } 1.296 + 1.297 + private FileObject getFileObjectForOutput(DocPath path) throws IOException { 1.298 + // break the path into a package-part and the rest, by finding 1.299 + // the position of the last '/' before an invalid character for a 1.300 + // package name, such as the "." before an extension or the "-" 1.301 + // in filenames like package-summary.html, doc-files or src-html. 1.302 + String p = path.getPath(); 1.303 + int lastSep = -1; 1.304 + for (int i = 0; i < p.length(); i++) { 1.305 + char ch = p.charAt(i); 1.306 + if (ch == '/') { 1.307 + lastSep = i; 1.308 + } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch) 1.309 + || !Character.isJavaIdentifierPart(ch)) { 1.310 + break; 1.311 + } 1.312 + } 1.313 + String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep); 1.314 + String rest = p.substring(lastSep + 1); 1.315 + return fileManager.getFileForOutput(location, pkg, rest, null); 1.316 + } 1.317 + } 1.318 +}