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

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy

jjg@1412 1 /*
jjg@1412 2 * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1412 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1412 4 *
jjg@1412 5 * This code is free software; you can redistribute it and/or modify it
jjg@1412 6 * under the terms of the GNU General Public License version 2 only, as
jjg@1412 7 * published by the Free Software Foundation. Oracle designates this
jjg@1412 8 * particular file as subject to the "Classpath" exception as provided
jjg@1412 9 * by Oracle in the LICENSE file that accompanied this code.
jjg@1412 10 *
jjg@1412 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1412 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1412 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1412 14 * version 2 for more details (a copy is included in the LICENSE file that
jjg@1412 15 * accompanied this code).
jjg@1412 16 *
jjg@1412 17 * You should have received a copy of the GNU General Public License version
jjg@1412 18 * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1412 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1412 20 *
jjg@1412 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1412 22 * or visit www.oracle.com if you need additional information or have any
jjg@1412 23 * questions.
jjg@1412 24 */
jjg@1412 25
jjg@1412 26 package com.sun.tools.doclets.internal.toolkit.util;
jjg@1412 27
jjg@1412 28 import java.io.BufferedInputStream;
jjg@1412 29 import java.io.BufferedOutputStream;
jjg@1412 30 import java.io.BufferedWriter;
jjg@1412 31 import java.io.File;
jjg@1412 32 import java.io.IOException;
jjg@1412 33 import java.io.InputStream;
jjg@1412 34 import java.io.OutputStream;
jjg@1412 35 import java.io.OutputStreamWriter;
jjg@1412 36 import java.io.UnsupportedEncodingException;
jjg@1412 37 import java.io.Writer;
jjg@1412 38 import java.util.ArrayList;
jjg@1412 39 import java.util.Arrays;
jjg@1412 40 import java.util.LinkedHashSet;
jjg@1412 41 import java.util.List;
jjg@1412 42 import java.util.Set;
jjg@1412 43
jjg@1412 44 import javax.tools.FileObject;
jjg@1412 45 import javax.tools.JavaFileManager.Location;
jjg@1412 46 import javax.tools.JavaFileObject;
jjg@1412 47 import javax.tools.StandardJavaFileManager;
jjg@1412 48 import javax.tools.StandardLocation;
jjg@1412 49
jjg@1412 50 import com.sun.tools.doclets.internal.toolkit.Configuration;
jjg@1412 51 import com.sun.tools.javac.util.Assert;
jjg@1412 52
jjg@1412 53 /**
jjg@1412 54 * Implementation of DocFileFactory using a {@link StandardJavaFileManager}.
jjg@1412 55 *
jjg@1412 56 * <p><b>This is NOT part of any supported API.
jjg@1412 57 * If you write code that depends on this, you do so at your own risk.
jjg@1412 58 * This code and its internal interfaces are subject to change or
jjg@1412 59 * deletion without notice.</b>
jjg@1412 60 *
jjg@1412 61 * @since 1.8
jjg@1412 62 */
jjg@1412 63 class StandardDocFileFactory extends DocFileFactory {
jjg@1412 64 private final StandardJavaFileManager fileManager;
jjg@1412 65 private final File destDir;
jjg@1412 66
jjg@1412 67 public StandardDocFileFactory(Configuration configuration) {
jjg@1412 68 super(configuration);
jjg@1412 69 fileManager = (StandardJavaFileManager) configuration.getFileManager();
jjg@1412 70
jjg@1412 71 if (!configuration.destDirName.isEmpty()
jjg@1412 72 || !fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
jjg@1412 73 try {
jjg@1412 74 String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
jjg@1412 75 File dir = new File(dirName);
jjg@1412 76 fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
jjg@1412 77 } catch (IOException e) {
jjg@1412 78 throw new DocletAbortException();
jjg@1412 79 }
jjg@1412 80 }
jjg@1412 81
jjg@1412 82 destDir = fileManager.getLocation(StandardLocation.CLASS_OUTPUT).iterator().next();
jjg@1412 83 }
jjg@1412 84
jjg@1412 85 public DocFile createFileForDirectory(String file) {
jjg@1412 86 return new StandardDocFile(new File(file));
jjg@1412 87 }
jjg@1412 88
jjg@1412 89 public DocFile createFileForInput(String file) {
jjg@1412 90 return new StandardDocFile(new File(file));
jjg@1412 91 }
jjg@1412 92
jjg@1412 93 public DocFile createFileForOutput(DocPath path) {
jjg@1412 94 return new StandardDocFile(StandardLocation.CLASS_OUTPUT, path);
jjg@1412 95 }
jjg@1412 96
jjg@1412 97 @Override
jjg@1412 98 Iterable<DocFile> list(Location location, DocPath path) {
jjg@1412 99 if (location != StandardLocation.SOURCE_PATH)
jjg@1412 100 throw new IllegalArgumentException();
jjg@1412 101
jjg@1412 102 Set<DocFile> files = new LinkedHashSet<DocFile>();
jjg@1412 103 if (fileManager.hasLocation(location)) {
jjg@1412 104 for (File f: fileManager.getLocation(location)) {
jjg@1412 105 if (f.isDirectory()) {
jjg@1412 106 f = new File(f, path.getPath());
jjg@1412 107 if (f.exists())
jjg@1412 108 files.add(new StandardDocFile(f));
jjg@1412 109 }
jjg@1412 110 }
jjg@1412 111 }
jjg@1412 112 return files;
jjg@1412 113 }
jjg@1412 114
jjg@1412 115 private static File newFile(File dir, String path) {
jjg@1412 116 return (dir == null) ? new File(path) : new File(dir, path);
jjg@1412 117 }
jjg@1412 118
jjg@1412 119 class StandardDocFile extends DocFile {
jjg@1412 120 private File file;
jjg@1412 121
jjg@1412 122
jjg@1412 123 /** Create a StandardDocFile for a given file. */
jjg@1412 124 private StandardDocFile(File file) {
jjg@1412 125 super(configuration);
jjg@1412 126 this.file = file;
jjg@1412 127 }
jjg@1412 128
jjg@1412 129 /** Create a StandardDocFile for a given location and relative path. */
jjg@1412 130 private StandardDocFile(Location location, DocPath path) {
jjg@1412 131 super(configuration, location, path);
jjg@1412 132 Assert.check(location == StandardLocation.CLASS_OUTPUT);
jjg@1412 133 this.file = newFile(destDir, path.getPath());
jjg@1412 134 }
jjg@1412 135
jjg@1412 136 /** Open an input stream for the file. */
jjg@1412 137 public InputStream openInputStream() throws IOException {
jjg@1412 138 JavaFileObject fo = getJavaFileObjectForInput(file);
jjg@1412 139 return new BufferedInputStream(fo.openInputStream());
jjg@1412 140 }
jjg@1412 141
jjg@1412 142 /**
jjg@1412 143 * Open an output stream for the file.
jjg@1412 144 * The file must have been created with a location of
jjg@1412 145 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
jjg@1412 146 */
jjg@1412 147 public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
jjg@1412 148 if (location != StandardLocation.CLASS_OUTPUT)
jjg@1412 149 throw new IllegalStateException();
jjg@1412 150
jjg@1412 151 OutputStream out = getFileObjectForOutput(path).openOutputStream();
jjg@1412 152 return new BufferedOutputStream(out);
jjg@1412 153 }
jjg@1412 154
jjg@1412 155 /**
jjg@1412 156 * Open an writer for the file, using the encoding (if any) given in the
jjg@1412 157 * doclet configuration.
jjg@1412 158 * The file must have been created with a location of
jjg@1412 159 * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
jjg@1412 160 */
jjg@1412 161 public Writer openWriter() throws IOException, UnsupportedEncodingException {
jjg@1412 162 if (location != StandardLocation.CLASS_OUTPUT)
jjg@1412 163 throw new IllegalStateException();
jjg@1412 164
jjg@1412 165 OutputStream out = getFileObjectForOutput(path).openOutputStream();
jjg@1412 166 if (configuration.docencoding == null) {
jjg@1412 167 return new BufferedWriter(new OutputStreamWriter(out));
jjg@1412 168 } else {
jjg@1412 169 return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
jjg@1412 170 }
jjg@1412 171 }
jjg@1412 172
jjg@1412 173 /** Return true if the file can be read. */
jjg@1412 174 public boolean canRead() {
jjg@1412 175 return file.canRead();
jjg@1412 176 }
jjg@1412 177
jjg@1412 178 /** Return true if the file can be written. */
jjg@1412 179 public boolean canWrite() {
jjg@1412 180 return file.canWrite();
jjg@1412 181 }
jjg@1412 182
jjg@1412 183 /** Return true if the file exists. */
jjg@1412 184 public boolean exists() {
jjg@1412 185 return file.exists();
jjg@1412 186 }
jjg@1412 187
jjg@1412 188 /** Return the base name (last component) of the file name. */
jjg@1412 189 public String getName() {
jjg@1412 190 return file.getName();
jjg@1412 191 }
jjg@1412 192
jjg@1412 193 /** Return the file system path for this file. */
jjg@1412 194 public String getPath() {
jjg@1412 195 return file.getPath();
jjg@1412 196 }
jjg@1412 197
jjg@1412 198 /** Return true is file has an absolute path name. */
jjg@1412 199 public boolean isAbsolute() {
jjg@1412 200 return file.isAbsolute();
jjg@1412 201 }
jjg@1412 202
jjg@1412 203 /** Return true is file identifies a directory. */
jjg@1412 204 public boolean isDirectory() {
jjg@1412 205 return file.isDirectory();
jjg@1412 206 }
jjg@1412 207
jjg@1412 208 /** Return true is file identifies a file. */
jjg@1412 209 public boolean isFile() {
jjg@1412 210 return file.isFile();
jjg@1412 211 }
jjg@1412 212
jjg@1412 213 /** Return true if this file is the same as another. */
jjg@1412 214 public boolean isSameFile(DocFile other) {
jjg@1412 215 if (!(other instanceof StandardDocFile))
jjg@1412 216 return false;
jjg@1412 217
jjg@1412 218 try {
jjg@1412 219 return file.exists()
jjg@1412 220 && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile());
jjg@1412 221 } catch (IOException e) {
jjg@1412 222 return false;
jjg@1412 223 }
jjg@1412 224 }
jjg@1412 225
jjg@1412 226 /** If the file is a directory, list its contents. */
jjg@1412 227 public Iterable<DocFile> list() {
jjg@1412 228 List<DocFile> files = new ArrayList<DocFile>();
jjg@1412 229 for (File f: file.listFiles()) {
jjg@1412 230 files.add(new StandardDocFile(f));
jjg@1412 231 }
jjg@1412 232 return files;
jjg@1412 233 }
jjg@1412 234
jjg@1412 235 /** Create the file as a directory, including any parent directories. */
jjg@1412 236 public boolean mkdirs() {
jjg@1412 237 return file.mkdirs();
jjg@1412 238 }
jjg@1412 239
jjg@1412 240 /**
jjg@1412 241 * Derive a new file by resolving a relative path against this file.
jjg@1412 242 * The new file will inherit the configuration and location of this file
jjg@1412 243 * If this file has a path set, the new file will have a corresponding
jjg@1412 244 * new path.
jjg@1412 245 */
jjg@1412 246 public DocFile resolve(DocPath p) {
jjg@1412 247 return resolve(p.getPath());
jjg@1412 248 }
jjg@1412 249
jjg@1412 250 /**
jjg@1412 251 * Derive a new file by resolving a relative path against this file.
jjg@1412 252 * The new file will inherit the configuration and location of this file
jjg@1412 253 * If this file has a path set, the new file will have a corresponding
jjg@1412 254 * new path.
jjg@1412 255 */
jjg@1412 256 public DocFile resolve(String p) {
jjg@1412 257 if (location == null && path == null) {
jjg@1412 258 return new StandardDocFile(new File(file, p));
jjg@1412 259 } else {
jjg@1412 260 return new StandardDocFile(location, path.resolve(p));
jjg@1412 261 }
jjg@1412 262 }
jjg@1412 263
jjg@1412 264 /**
jjg@1412 265 * Resolve a relative file against the given output location.
jjg@1412 266 * @param locn Currently, only SOURCE_OUTPUT is supported.
jjg@1412 267 */
jjg@1412 268 public DocFile resolveAgainst(StandardLocation locn) {
jjg@1412 269 if (locn != StandardLocation.CLASS_OUTPUT)
jjg@1412 270 throw new IllegalArgumentException();
jjg@1412 271 return new StandardDocFile(newFile(destDir, file.getPath()));
jjg@1412 272 }
jjg@1412 273
jjg@1412 274 /** Return a string to identify the contents of this object,
jjg@1412 275 * for debugging purposes.
jjg@1412 276 */
jjg@1412 277 @Override
jjg@1412 278 public String toString() {
jjg@1412 279 StringBuilder sb = new StringBuilder();
jjg@1412 280 sb.append("StandardDocFile[");
jjg@1412 281 if (location != null)
jjg@1412 282 sb.append("locn:").append(location).append(",");
jjg@1412 283 if (path != null)
jjg@1412 284 sb.append("path:").append(path.getPath()).append(",");
jjg@1412 285 sb.append("file:").append(file);
jjg@1412 286 sb.append("]");
jjg@1412 287 return sb.toString();
jjg@1412 288 }
jjg@1412 289
jjg@1412 290 private JavaFileObject getJavaFileObjectForInput(File file) {
jjg@1412 291 return fileManager.getJavaFileObjects(file).iterator().next();
jjg@1412 292 }
jjg@1412 293
jjg@1412 294 private FileObject getFileObjectForOutput(DocPath path) throws IOException {
jjg@1412 295 // break the path into a package-part and the rest, by finding
jjg@1412 296 // the position of the last '/' before an invalid character for a
jjg@1412 297 // package name, such as the "." before an extension or the "-"
jjg@1412 298 // in filenames like package-summary.html, doc-files or src-html.
jjg@1412 299 String p = path.getPath();
jjg@1412 300 int lastSep = -1;
jjg@1412 301 for (int i = 0; i < p.length(); i++) {
jjg@1412 302 char ch = p.charAt(i);
jjg@1412 303 if (ch == '/') {
jjg@1412 304 lastSep = i;
jjg@1412 305 } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
jjg@1412 306 || !Character.isJavaIdentifierPart(ch)) {
jjg@1412 307 break;
jjg@1412 308 }
jjg@1412 309 }
jjg@1412 310 String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
jjg@1412 311 String rest = p.substring(lastSep + 1);
jjg@1412 312 return fileManager.getFileForOutput(location, pkg, rest, null);
jjg@1412 313 }
jjg@1412 314 }
jjg@1412 315 }

mercurial