aoqi@0: /* aoqi@0: * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.tools.doclets.internal.toolkit.util; aoqi@0: aoqi@0: import java.io.BufferedInputStream; aoqi@0: import java.io.BufferedOutputStream; aoqi@0: import java.io.BufferedWriter; aoqi@0: import java.io.File; aoqi@0: import java.io.IOException; aoqi@0: import java.io.InputStream; aoqi@0: import java.io.OutputStream; aoqi@0: import java.io.OutputStreamWriter; aoqi@0: import java.io.UnsupportedEncodingException; aoqi@0: import java.io.Writer; aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.Arrays; aoqi@0: import java.util.LinkedHashSet; aoqi@0: import java.util.List; aoqi@0: import java.util.Set; aoqi@0: aoqi@0: import javax.tools.DocumentationTool; aoqi@0: import javax.tools.FileObject; aoqi@0: import javax.tools.JavaFileManager.Location; aoqi@0: import javax.tools.JavaFileObject; aoqi@0: import javax.tools.StandardJavaFileManager; aoqi@0: import javax.tools.StandardLocation; aoqi@0: aoqi@0: import com.sun.tools.doclets.internal.toolkit.Configuration; aoqi@0: import com.sun.tools.javac.util.Assert; aoqi@0: aoqi@0: /** aoqi@0: * Implementation of DocFileFactory using a {@link StandardJavaFileManager}. aoqi@0: * aoqi@0: *

This is NOT part of any supported API. aoqi@0: * If you write code that depends on this, you do so at your own risk. aoqi@0: * This code and its internal interfaces are subject to change or aoqi@0: * deletion without notice. aoqi@0: * aoqi@0: * @since 1.8 aoqi@0: */ aoqi@0: class StandardDocFileFactory extends DocFileFactory { aoqi@0: private final StandardJavaFileManager fileManager; aoqi@0: private File destDir; aoqi@0: aoqi@0: public StandardDocFileFactory(Configuration configuration) { aoqi@0: super(configuration); aoqi@0: fileManager = (StandardJavaFileManager) configuration.getFileManager(); aoqi@0: } aoqi@0: aoqi@0: private File getDestDir() { aoqi@0: if (destDir == null) { aoqi@0: if (!configuration.destDirName.isEmpty() aoqi@0: || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) { aoqi@0: try { aoqi@0: String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName; aoqi@0: File dir = new File(dirName); aoqi@0: fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir)); aoqi@0: } catch (IOException e) { aoqi@0: throw new DocletAbortException(e); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next(); aoqi@0: } aoqi@0: return destDir; aoqi@0: } aoqi@0: aoqi@0: public DocFile createFileForDirectory(String file) { aoqi@0: return new StandardDocFile(new File(file)); aoqi@0: } aoqi@0: aoqi@0: public DocFile createFileForInput(String file) { aoqi@0: return new StandardDocFile(new File(file)); aoqi@0: } aoqi@0: aoqi@0: public DocFile createFileForOutput(DocPath path) { aoqi@0: return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path); aoqi@0: } aoqi@0: aoqi@0: @Override aoqi@0: Iterable list(Location location, DocPath path) { aoqi@0: if (location != StandardLocation.SOURCE_PATH) aoqi@0: throw new IllegalArgumentException(); aoqi@0: aoqi@0: Set files = new LinkedHashSet(); aoqi@0: Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH) aoqi@0: ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH; aoqi@0: for (File f: fileManager.getLocation(l)) { aoqi@0: if (f.isDirectory()) { aoqi@0: f = new File(f, path.getPath()); aoqi@0: if (f.exists()) aoqi@0: files.add(new StandardDocFile(f)); aoqi@0: } aoqi@0: } aoqi@0: return files; aoqi@0: } aoqi@0: aoqi@0: private static File newFile(File dir, String path) { aoqi@0: return (dir == null) ? new File(path) : new File(dir, path); aoqi@0: } aoqi@0: aoqi@0: class StandardDocFile extends DocFile { aoqi@0: private File file; aoqi@0: aoqi@0: aoqi@0: /** Create a StandardDocFile for a given file. */ aoqi@0: private StandardDocFile(File file) { aoqi@0: super(configuration); aoqi@0: this.file = file; aoqi@0: } aoqi@0: aoqi@0: /** Create a StandardDocFile for a given location and relative path. */ aoqi@0: private StandardDocFile(Location location, DocPath path) { aoqi@0: super(configuration, location, path); aoqi@0: Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT); aoqi@0: this.file = newFile(getDestDir(), path.getPath()); aoqi@0: } aoqi@0: aoqi@0: /** Open an input stream for the file. */ aoqi@0: public InputStream openInputStream() throws IOException { aoqi@0: JavaFileObject fo = getJavaFileObjectForInput(file); aoqi@0: return new BufferedInputStream(fo.openInputStream()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Open an output stream for the file. aoqi@0: * The file must have been created with a location of aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. aoqi@0: */ aoqi@0: public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException { aoqi@0: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) aoqi@0: throw new IllegalStateException(); aoqi@0: aoqi@0: OutputStream out = getFileObjectForOutput(path).openOutputStream(); aoqi@0: return new BufferedOutputStream(out); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Open an writer for the file, using the encoding (if any) given in the aoqi@0: * doclet configuration. aoqi@0: * The file must have been created with a location of aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path. aoqi@0: */ aoqi@0: public Writer openWriter() throws IOException, UnsupportedEncodingException { aoqi@0: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) aoqi@0: throw new IllegalStateException(); aoqi@0: aoqi@0: OutputStream out = getFileObjectForOutput(path).openOutputStream(); aoqi@0: if (configuration.docencoding == null) { aoqi@0: return new BufferedWriter(new OutputStreamWriter(out)); aoqi@0: } else { aoqi@0: return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** Return true if the file can be read. */ aoqi@0: public boolean canRead() { aoqi@0: return file.canRead(); aoqi@0: } aoqi@0: aoqi@0: /** Return true if the file can be written. */ aoqi@0: public boolean canWrite() { aoqi@0: return file.canWrite(); aoqi@0: } aoqi@0: aoqi@0: /** Return true if the file exists. */ aoqi@0: public boolean exists() { aoqi@0: return file.exists(); aoqi@0: } aoqi@0: aoqi@0: /** Return the base name (last component) of the file name. */ aoqi@0: public String getName() { aoqi@0: return file.getName(); aoqi@0: } aoqi@0: aoqi@0: /** Return the file system path for this file. */ aoqi@0: public String getPath() { aoqi@0: return file.getPath(); aoqi@0: } aoqi@0: aoqi@0: /** Return true is file has an absolute path name. */ aoqi@0: public boolean isAbsolute() { aoqi@0: return file.isAbsolute(); aoqi@0: } aoqi@0: aoqi@0: /** Return true is file identifies a directory. */ aoqi@0: public boolean isDirectory() { aoqi@0: return file.isDirectory(); aoqi@0: } aoqi@0: aoqi@0: /** Return true is file identifies a file. */ aoqi@0: public boolean isFile() { aoqi@0: return file.isFile(); aoqi@0: } aoqi@0: aoqi@0: /** Return true if this file is the same as another. */ aoqi@0: public boolean isSameFile(DocFile other) { aoqi@0: if (!(other instanceof StandardDocFile)) aoqi@0: return false; aoqi@0: aoqi@0: try { aoqi@0: return file.exists() aoqi@0: && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile()); aoqi@0: } catch (IOException e) { aoqi@0: return false; aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** If the file is a directory, list its contents. */ aoqi@0: public Iterable list() { aoqi@0: List files = new ArrayList(); aoqi@0: for (File f: file.listFiles()) { aoqi@0: files.add(new StandardDocFile(f)); aoqi@0: } aoqi@0: return files; aoqi@0: } aoqi@0: aoqi@0: /** Create the file as a directory, including any parent directories. */ aoqi@0: public boolean mkdirs() { aoqi@0: return file.mkdirs(); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Derive a new file by resolving a relative path against this file. aoqi@0: * The new file will inherit the configuration and location of this file aoqi@0: * If this file has a path set, the new file will have a corresponding aoqi@0: * new path. aoqi@0: */ aoqi@0: public DocFile resolve(DocPath p) { aoqi@0: return resolve(p.getPath()); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Derive a new file by resolving a relative path against this file. aoqi@0: * The new file will inherit the configuration and location of this file aoqi@0: * If this file has a path set, the new file will have a corresponding aoqi@0: * new path. aoqi@0: */ aoqi@0: public DocFile resolve(String p) { aoqi@0: if (location == null && path == null) { aoqi@0: return new StandardDocFile(new File(file, p)); aoqi@0: } else { aoqi@0: return new StandardDocFile(location, path.resolve(p)); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Resolve a relative file against the given output location. aoqi@0: * @param locn Currently, only aoqi@0: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported. aoqi@0: */ aoqi@0: public DocFile resolveAgainst(Location locn) { aoqi@0: if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT) aoqi@0: throw new IllegalArgumentException(); aoqi@0: return new StandardDocFile(newFile(getDestDir(), file.getPath())); aoqi@0: } aoqi@0: aoqi@0: /** Return a string to identify the contents of this object, aoqi@0: * for debugging purposes. aoqi@0: */ aoqi@0: @Override aoqi@0: public String toString() { aoqi@0: StringBuilder sb = new StringBuilder(); aoqi@0: sb.append("StandardDocFile["); aoqi@0: if (location != null) aoqi@0: sb.append("locn:").append(location).append(","); aoqi@0: if (path != null) aoqi@0: sb.append("path:").append(path.getPath()).append(","); aoqi@0: sb.append("file:").append(file); aoqi@0: sb.append("]"); aoqi@0: return sb.toString(); aoqi@0: } aoqi@0: aoqi@0: private JavaFileObject getJavaFileObjectForInput(File file) { aoqi@0: return fileManager.getJavaFileObjects(file).iterator().next(); aoqi@0: } aoqi@0: aoqi@0: private FileObject getFileObjectForOutput(DocPath path) throws IOException { aoqi@0: // break the path into a package-part and the rest, by finding aoqi@0: // the position of the last '/' before an invalid character for a aoqi@0: // package name, such as the "." before an extension or the "-" aoqi@0: // in filenames like package-summary.html, doc-files or src-html. aoqi@0: String p = path.getPath(); aoqi@0: int lastSep = -1; aoqi@0: for (int i = 0; i < p.length(); i++) { aoqi@0: char ch = p.charAt(i); aoqi@0: if (ch == '/') { aoqi@0: lastSep = i; aoqi@0: } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch) aoqi@0: || !Character.isJavaIdentifierPart(ch)) { aoqi@0: break; aoqi@0: } aoqi@0: } aoqi@0: String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep); aoqi@0: String rest = p.substring(lastSep + 1); aoqi@0: return fileManager.getFileForOutput(location, pkg, rest, null); aoqi@0: } aoqi@0: } aoqi@0: }