jjg@1412: /*
jjg@1412: * Copyright (c) 1998, 2012, Oracle and/or its affiliates. All rights reserved.
jjg@1412: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jjg@1412: *
jjg@1412: * This code is free software; you can redistribute it and/or modify it
jjg@1412: * under the terms of the GNU General Public License version 2 only, as
jjg@1412: * published by the Free Software Foundation. Oracle designates this
jjg@1412: * particular file as subject to the "Classpath" exception as provided
jjg@1412: * by Oracle in the LICENSE file that accompanied this code.
jjg@1412: *
jjg@1412: * This code is distributed in the hope that it will be useful, but WITHOUT
jjg@1412: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jjg@1412: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jjg@1412: * version 2 for more details (a copy is included in the LICENSE file that
jjg@1412: * accompanied this code).
jjg@1412: *
jjg@1412: * You should have received a copy of the GNU General Public License version
jjg@1412: * 2 along with this work; if not, write to the Free Software Foundation,
jjg@1412: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jjg@1412: *
jjg@1412: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jjg@1412: * or visit www.oracle.com if you need additional information or have any
jjg@1412: * questions.
jjg@1412: */
jjg@1412:
jjg@1412: package com.sun.tools.doclets.internal.toolkit.util;
jjg@1412:
jjg@1412: import java.io.BufferedInputStream;
jjg@1412: import java.io.BufferedOutputStream;
jjg@1412: import java.io.BufferedWriter;
jjg@1412: import java.io.File;
jjg@1412: import java.io.IOException;
jjg@1412: import java.io.InputStream;
jjg@1412: import java.io.OutputStream;
jjg@1412: import java.io.OutputStreamWriter;
jjg@1412: import java.io.UnsupportedEncodingException;
jjg@1412: import java.io.Writer;
jjg@1412: import java.util.ArrayList;
jjg@1412: import java.util.Arrays;
jjg@1412: import java.util.LinkedHashSet;
jjg@1412: import java.util.List;
jjg@1412: import java.util.Set;
jjg@1412:
jjg@1413: import javax.tools.DocumentationTool;
jjg@1412: import javax.tools.FileObject;
jjg@1412: import javax.tools.JavaFileManager.Location;
jjg@1412: import javax.tools.JavaFileObject;
jjg@1412: import javax.tools.StandardJavaFileManager;
jjg@1412: import javax.tools.StandardLocation;
jjg@1412:
jjg@1412: import com.sun.tools.doclets.internal.toolkit.Configuration;
jjg@1412: import com.sun.tools.javac.util.Assert;
jjg@1412:
jjg@1412: /**
jjg@1412: * Implementation of DocFileFactory using a {@link StandardJavaFileManager}.
jjg@1412: *
jjg@1412: *
This is NOT part of any supported API.
jjg@1412: * If you write code that depends on this, you do so at your own risk.
jjg@1412: * This code and its internal interfaces are subject to change or
jjg@1412: * deletion without notice.
jjg@1412: *
jjg@1412: * @since 1.8
jjg@1412: */
jjg@1412: class StandardDocFileFactory extends DocFileFactory {
jjg@1412: private final StandardJavaFileManager fileManager;
jjg@1413: private File destDir;
jjg@1412:
jjg@1412: public StandardDocFileFactory(Configuration configuration) {
jjg@1412: super(configuration);
jjg@1412: fileManager = (StandardJavaFileManager) configuration.getFileManager();
jjg@1413: }
jjg@1412:
jjg@1413: private File getDestDir() {
jjg@1413: if (destDir == null) {
jjg@1413: if (!configuration.destDirName.isEmpty()
jjg@1413: || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
jjg@1413: try {
jjg@1413: String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
jjg@1413: File dir = new File(dirName);
jjg@1413: fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
jjg@1413: } catch (IOException e) {
jjg@1413: throw new DocletAbortException();
jjg@1413: }
jjg@1412: }
jjg@1413:
jjg@1413: destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
jjg@1412: }
jjg@1413: return destDir;
jjg@1412: }
jjg@1412:
jjg@1412: public DocFile createFileForDirectory(String file) {
jjg@1412: return new StandardDocFile(new File(file));
jjg@1412: }
jjg@1412:
jjg@1412: public DocFile createFileForInput(String file) {
jjg@1412: return new StandardDocFile(new File(file));
jjg@1412: }
jjg@1412:
jjg@1412: public DocFile createFileForOutput(DocPath path) {
jjg@1413: return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
jjg@1412: }
jjg@1412:
jjg@1412: @Override
jjg@1412: Iterable list(Location location, DocPath path) {
jjg@1412: if (location != StandardLocation.SOURCE_PATH)
jjg@1412: throw new IllegalArgumentException();
jjg@1412:
jjg@1412: Set files = new LinkedHashSet();
jjg@1413: Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH)
jjg@1413: ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
jjg@1413: for (File f: fileManager.getLocation(l)) {
jjg@1413: if (f.isDirectory()) {
jjg@1413: f = new File(f, path.getPath());
jjg@1413: if (f.exists())
jjg@1413: files.add(new StandardDocFile(f));
jjg@1412: }
jjg@1412: }
jjg@1412: return files;
jjg@1412: }
jjg@1412:
jjg@1412: private static File newFile(File dir, String path) {
jjg@1412: return (dir == null) ? new File(path) : new File(dir, path);
jjg@1412: }
jjg@1412:
jjg@1412: class StandardDocFile extends DocFile {
jjg@1412: private File file;
jjg@1412:
jjg@1412:
jjg@1412: /** Create a StandardDocFile for a given file. */
jjg@1412: private StandardDocFile(File file) {
jjg@1412: super(configuration);
jjg@1412: this.file = file;
jjg@1412: }
jjg@1412:
jjg@1412: /** Create a StandardDocFile for a given location and relative path. */
jjg@1412: private StandardDocFile(Location location, DocPath path) {
jjg@1412: super(configuration, location, path);
jjg@1413: Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT);
jjg@1413: this.file = newFile(getDestDir(), path.getPath());
jjg@1412: }
jjg@1412:
jjg@1412: /** Open an input stream for the file. */
jjg@1412: public InputStream openInputStream() throws IOException {
jjg@1412: JavaFileObject fo = getJavaFileObjectForInput(file);
jjg@1412: return new BufferedInputStream(fo.openInputStream());
jjg@1412: }
jjg@1412:
jjg@1412: /**
jjg@1412: * Open an output stream for the file.
jjg@1412: * The file must have been created with a location of
jjg@1413: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
jjg@1412: */
jjg@1412: public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
jjg@1413: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
jjg@1412: throw new IllegalStateException();
jjg@1412:
jjg@1412: OutputStream out = getFileObjectForOutput(path).openOutputStream();
jjg@1412: return new BufferedOutputStream(out);
jjg@1412: }
jjg@1412:
jjg@1412: /**
jjg@1412: * Open an writer for the file, using the encoding (if any) given in the
jjg@1412: * doclet configuration.
jjg@1412: * The file must have been created with a location of
jjg@1413: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
jjg@1412: */
jjg@1412: public Writer openWriter() throws IOException, UnsupportedEncodingException {
jjg@1413: if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
jjg@1412: throw new IllegalStateException();
jjg@1412:
jjg@1412: OutputStream out = getFileObjectForOutput(path).openOutputStream();
jjg@1412: if (configuration.docencoding == null) {
jjg@1412: return new BufferedWriter(new OutputStreamWriter(out));
jjg@1412: } else {
jjg@1412: return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
jjg@1412: }
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true if the file can be read. */
jjg@1412: public boolean canRead() {
jjg@1412: return file.canRead();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true if the file can be written. */
jjg@1412: public boolean canWrite() {
jjg@1412: return file.canWrite();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true if the file exists. */
jjg@1412: public boolean exists() {
jjg@1412: return file.exists();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return the base name (last component) of the file name. */
jjg@1412: public String getName() {
jjg@1412: return file.getName();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return the file system path for this file. */
jjg@1412: public String getPath() {
jjg@1412: return file.getPath();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true is file has an absolute path name. */
jjg@1412: public boolean isAbsolute() {
jjg@1412: return file.isAbsolute();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true is file identifies a directory. */
jjg@1412: public boolean isDirectory() {
jjg@1412: return file.isDirectory();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true is file identifies a file. */
jjg@1412: public boolean isFile() {
jjg@1412: return file.isFile();
jjg@1412: }
jjg@1412:
jjg@1412: /** Return true if this file is the same as another. */
jjg@1412: public boolean isSameFile(DocFile other) {
jjg@1412: if (!(other instanceof StandardDocFile))
jjg@1412: return false;
jjg@1412:
jjg@1412: try {
jjg@1412: return file.exists()
jjg@1412: && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile());
jjg@1412: } catch (IOException e) {
jjg@1412: return false;
jjg@1412: }
jjg@1412: }
jjg@1412:
jjg@1412: /** If the file is a directory, list its contents. */
jjg@1412: public Iterable list() {
jjg@1412: List files = new ArrayList();
jjg@1412: for (File f: file.listFiles()) {
jjg@1412: files.add(new StandardDocFile(f));
jjg@1412: }
jjg@1412: return files;
jjg@1412: }
jjg@1412:
jjg@1412: /** Create the file as a directory, including any parent directories. */
jjg@1412: public boolean mkdirs() {
jjg@1412: return file.mkdirs();
jjg@1412: }
jjg@1412:
jjg@1412: /**
jjg@1412: * Derive a new file by resolving a relative path against this file.
jjg@1412: * The new file will inherit the configuration and location of this file
jjg@1412: * If this file has a path set, the new file will have a corresponding
jjg@1412: * new path.
jjg@1412: */
jjg@1412: public DocFile resolve(DocPath p) {
jjg@1412: return resolve(p.getPath());
jjg@1412: }
jjg@1412:
jjg@1412: /**
jjg@1412: * Derive a new file by resolving a relative path against this file.
jjg@1412: * The new file will inherit the configuration and location of this file
jjg@1412: * If this file has a path set, the new file will have a corresponding
jjg@1412: * new path.
jjg@1412: */
jjg@1412: public DocFile resolve(String p) {
jjg@1412: if (location == null && path == null) {
jjg@1412: return new StandardDocFile(new File(file, p));
jjg@1412: } else {
jjg@1412: return new StandardDocFile(location, path.resolve(p));
jjg@1412: }
jjg@1412: }
jjg@1412:
jjg@1412: /**
jjg@1412: * Resolve a relative file against the given output location.
jjg@1413: * @param locn Currently, only
jjg@1413: * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
jjg@1412: */
jjg@1413: public DocFile resolveAgainst(Location locn) {
jjg@1413: if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
jjg@1412: throw new IllegalArgumentException();
jjg@1413: return new StandardDocFile(newFile(getDestDir(), file.getPath()));
jjg@1412: }
jjg@1412:
jjg@1412: /** Return a string to identify the contents of this object,
jjg@1412: * for debugging purposes.
jjg@1412: */
jjg@1412: @Override
jjg@1412: public String toString() {
jjg@1412: StringBuilder sb = new StringBuilder();
jjg@1412: sb.append("StandardDocFile[");
jjg@1412: if (location != null)
jjg@1412: sb.append("locn:").append(location).append(",");
jjg@1412: if (path != null)
jjg@1412: sb.append("path:").append(path.getPath()).append(",");
jjg@1412: sb.append("file:").append(file);
jjg@1412: sb.append("]");
jjg@1412: return sb.toString();
jjg@1412: }
jjg@1412:
jjg@1412: private JavaFileObject getJavaFileObjectForInput(File file) {
jjg@1412: return fileManager.getJavaFileObjects(file).iterator().next();
jjg@1412: }
jjg@1412:
jjg@1412: private FileObject getFileObjectForOutput(DocPath path) throws IOException {
jjg@1412: // break the path into a package-part and the rest, by finding
jjg@1412: // the position of the last '/' before an invalid character for a
jjg@1412: // package name, such as the "." before an extension or the "-"
jjg@1412: // in filenames like package-summary.html, doc-files or src-html.
jjg@1412: String p = path.getPath();
jjg@1412: int lastSep = -1;
jjg@1412: for (int i = 0; i < p.length(); i++) {
jjg@1412: char ch = p.charAt(i);
jjg@1412: if (ch == '/') {
jjg@1412: lastSep = i;
jjg@1412: } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
jjg@1412: || !Character.isJavaIdentifierPart(ch)) {
jjg@1412: break;
jjg@1412: }
jjg@1412: }
jjg@1412: String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
jjg@1412: String rest = p.substring(lastSep + 1);
jjg@1412: return fileManager.getFileForOutput(location, pkg, rest, null);
jjg@1412: }
jjg@1412: }
jjg@1412: }