8149773: StandardDocFileFactory should be converted to use java.nio.file.Path

Reviewed-by: ksrini
This commit is contained in:
Jonathan Gibbons 2016-02-15 14:02:57 -08:00
parent ca3da12f5a
commit 88a8d4719e
8 changed files with 90 additions and 713 deletions

View File

@ -212,13 +212,6 @@ public abstract class Configuration {
* used.Default is don't show version information.
*/
public boolean showversion = false;
/**
* Sourcepath from where to read the source files. Default is classpath.
*
*/
public String sourcepath = "";
/**
* Argument for command line option "-Xprofilespath".
*/
@ -492,11 +485,6 @@ public abstract class Configuration {
showversion = true;
} else if (opt.equals("-nodeprecated")) {
nodeprecated = true;
} else if (opt.equals("-sourcepath")) {
sourcepath = os[1];
} else if ((opt.equals("-classpath") || opt.equals("-cp")) &&
sourcepath.length() == 0) {
sourcepath = os[1];
} else if (opt.equals("-excludedocfilessubdir")) {
addToSet(excludedDocFileDirs, os[1]);
} else if (opt.equals("-noqualifier")) {
@ -541,10 +529,6 @@ public abstract class Configuration {
extern.link(url, pkglisturl, root, true);
}
}
if (sourcepath.length() == 0) {
sourcepath = System.getProperty("env.class.path") == null ? "" :
System.getProperty("env.class.path");
}
if (docencoding == null) {
docencoding = encoding;
}

View File

@ -23,7 +23,6 @@ doclet.Copy_Overwrite_warning=File {0} not copied to {1} due to existing file wi
doclet.Copying_File_0_To_Dir_1=Copying file {0} to directory {1}...
doclet.Copying_File_0_To_File_1=Copying file {0} to file {1}...
doclet.No_Public_Classes_To_Document=No public or protected classes found to document.
doclet.Unable_to_create_directory_0=Unable to create directory {0}
doclet.destination_directory_not_directory_0=Destination directory is not a directory {0}
doclet.destination_directory_not_writable_0=Destination directory not writable {0}
doclet.Encoding_not_supported=Encoding not supported: {0}

View File

@ -1,292 +0,0 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.doclets.internal.toolkit.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.tools.DocumentationTool;
import javax.tools.JavaFileManager.Location;
import javax.tools.StandardLocation;
import com.sun.tools.doclets.internal.toolkit.Configuration;
/**
* Implementation of DocFileFactory that just uses java.io.File API,
* and does not use a JavaFileManager..
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*
* @since 1.8
*/
class SimpleDocFileFactory extends DocFileFactory {
public SimpleDocFileFactory(Configuration configuration) {
super(configuration);
}
public DocFile createFileForDirectory(String file) {
return new SimpleDocFile(new File(file));
}
public DocFile createFileForInput(String file) {
return new SimpleDocFile(new File(file));
}
public DocFile createFileForOutput(DocPath path) {
return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
}
@Override
Iterable<DocFile> list(Location location, DocPath path) {
if (location != StandardLocation.SOURCE_PATH)
throw new IllegalArgumentException();
Set<DocFile> files = new LinkedHashSet<>();
for (String s : configuration.sourcepath.split(File.pathSeparator)) {
if (s.isEmpty())
continue;
File f = new File(s);
if (f.isDirectory()) {
f = new File(f, path.getPath());
if (f.exists())
files.add(new SimpleDocFile(f));
}
}
return files;
}
class SimpleDocFile extends DocFile {
private File file;
/** Create a DocFile for a given file. */
private SimpleDocFile(File file) {
super(configuration);
this.file = file;
}
/** Create a DocFile for a given location and relative path. */
private SimpleDocFile(Location location, DocPath path) {
super(configuration, location, path);
String destDirName = configuration.destDirName;
this.file = destDirName.isEmpty() ? new File(path.getPath())
: new File(destDirName, path.getPath());
}
/** Open an input stream for the file. */
public InputStream openInputStream() throws FileNotFoundException {
return new BufferedInputStream(new FileInputStream(file));
}
/**
* Open an output stream for the file.
* The file must have been created with a location of
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
*/
public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalStateException();
createDirectoryForFile(file);
return new BufferedOutputStream(new FileOutputStream(file));
}
/**
* Open an writer for the file, using the encoding (if any) given in the
* doclet configuration.
* The file must have been created with a location of
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
*/
public Writer openWriter() throws IOException, UnsupportedEncodingException {
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalStateException();
createDirectoryForFile(file);
FileOutputStream fos = new FileOutputStream(file);
if (configuration.docencoding == null) {
return new BufferedWriter(new OutputStreamWriter(fos));
} else {
return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
}
}
/** Return true if the file can be read. */
public boolean canRead() {
return file.canRead();
}
/** Return true if the file can be written. */
public boolean canWrite() {
return file.canRead();
}
/** Return true if the file exists. */
public boolean exists() {
return file.exists();
}
/** Return the base name (last component) of the file name. */
public String getName() {
return file.getName();
}
/** Return the file system path for this file. */
public String getPath() {
return file.getPath();
}
/** Return true is file has an absolute path name. */
public boolean isAbsolute() {
return file.isAbsolute();
}
/** Return true is file identifies a directory. */
public boolean isDirectory() {
return file.isDirectory();
}
/** Return true is file identifies a file. */
public boolean isFile() {
return file.isFile();
}
/** Return true if this file is the same as another. */
public boolean isSameFile(DocFile other) {
if (!(other instanceof SimpleDocFile))
return false;
try {
return file.exists()
&& file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile());
} catch (IOException e) {
return false;
}
}
/** If the file is a directory, list its contents. */
public Iterable<DocFile> list() {
List<DocFile> files = new ArrayList<>();
for (File f: file.listFiles()) {
files.add(new SimpleDocFile(f));
}
return files;
}
/** Create the file as a directory, including any parent directories. */
public boolean mkdirs() {
return file.mkdirs();
}
/**
* Derive a new file by resolving a relative path against this file.
* The new file will inherit the configuration and location of this file
* If this file has a path set, the new file will have a corresponding
* new path.
*/
public DocFile resolve(DocPath p) {
return resolve(p.getPath());
}
/**
* Derive a new file by resolving a relative path against this file.
* The new file will inherit the configuration and location of this file
* If this file has a path set, the new file will have a corresponding
* new path.
*/
public DocFile resolve(String p) {
if (location == null && path == null) {
return new SimpleDocFile(new File(file, p));
} else {
return new SimpleDocFile(location, path.resolve(p));
}
}
/**
* Resolve a relative file against the given output location.
* @param locn Currently, only
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
*/
public DocFile resolveAgainst(Location locn) {
if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalArgumentException();
return new SimpleDocFile(
new File(configuration.destDirName, file.getPath()));
}
/**
* Given a path string create all the directories in the path. For example,
* if the path string is "java/applet", the method will create directory
* "java" and then "java/applet" if they don't exist. The file separator
* string "/" is platform dependent system property.
*
* @param path Directory path string.
*/
private void createDirectoryForFile(File file) {
File dir = file.getParentFile();
if (dir == null || dir.exists() || dir.mkdirs())
return;
configuration.message.error(
"doclet.Unable_to_create_directory_0", dir.getPath());
throw new DocletAbortException("can't create directory");
}
/** Return a string to identify the contents of this object,
* for debugging purposes.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("DocFile[");
if (location != null)
sb.append("locn:").append(location).append(",");
if (path != null)
sb.append("path:").append(path.getPath()).append(",");
sb.append("file:").append(file);
sb.append("]");
return sb.toString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -28,13 +28,16 @@ package com.sun.tools.doclets.internal.toolkit.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
@ -63,37 +66,37 @@ import com.sun.tools.javac.util.Assert;
*/
class StandardDocFileFactory extends DocFileFactory {
private final StandardJavaFileManager fileManager;
private File destDir;
private Path destDir;
public StandardDocFileFactory(Configuration configuration) {
super(configuration);
fileManager = (StandardJavaFileManager) configuration.getFileManager();
}
private File getDestDir() {
private Path getDestDir() {
if (destDir == null) {
if (!configuration.destDirName.isEmpty()
|| !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
try {
String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
File dir = new File(dirName);
fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
Path dir = Paths.get(dirName);
fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
} catch (IOException e) {
throw new DocletAbortException(e);
}
}
destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
}
return destDir;
}
public DocFile createFileForDirectory(String file) {
return new StandardDocFile(new File(file));
return new StandardDocFile(Paths.get(file));
}
public DocFile createFileForInput(String file) {
return new StandardDocFile(new File(file));
return new StandardDocFile(Paths.get(file));
}
public DocFile createFileForOutput(DocPath path) {
@ -108,26 +111,25 @@ class StandardDocFileFactory extends DocFileFactory {
Set<DocFile> files = new LinkedHashSet<>();
Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH)
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
for (File f: fileManager.getLocation(l)) {
if (f.isDirectory()) {
f = new File(f, path.getPath());
if (f.exists())
for (Path f: fileManager.getLocationAsPaths(l)) {
if (Files.isDirectory(f)) {
f = f.resolve(path.getPath());
if (Files.exists(f))
files.add(new StandardDocFile(f));
}
}
return files;
}
private static File newFile(File dir, String path) {
return (dir == null) ? new File(path) : new File(dir, path);
private static Path newFile(Path dir, String path) {
return (dir == null) ? Paths.get(path) : dir.resolve(path);
}
class StandardDocFile extends DocFile {
private File file;
private Path file;
/** Create a StandardDocFile for a given file. */
private StandardDocFile(File file) {
private StandardDocFile(Path file) {
super(configuration);
this.file = file;
}
@ -178,27 +180,27 @@ class StandardDocFileFactory extends DocFileFactory {
/** Return true if the file can be read. */
public boolean canRead() {
return file.canRead();
return Files.isReadable(file);
}
/** Return true if the file can be written. */
public boolean canWrite() {
return file.canWrite();
return Files.isWritable(file);
}
/** Return true if the file exists. */
public boolean exists() {
return file.exists();
return Files.exists(file);
}
/** Return the base name (last component) of the file name. */
public String getName() {
return file.getName();
return file.getFileName().toString();
}
/** Return the file system path for this file. */
public String getPath() {
return file.getPath();
return file.toString();
}
/** Return true is file has an absolute path name. */
@ -208,12 +210,12 @@ class StandardDocFileFactory extends DocFileFactory {
/** Return true is file identifies a directory. */
public boolean isDirectory() {
return file.isDirectory();
return Files.isDirectory(file);
}
/** Return true is file identifies a file. */
public boolean isFile() {
return file.isFile();
return Files.isRegularFile(file);
}
/** Return true if this file is the same as another. */
@ -222,25 +224,31 @@ class StandardDocFileFactory extends DocFileFactory {
return false;
try {
return file.exists()
&& file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile());
return Files.isSameFile(file, ((StandardDocFile) other).file);
} catch (IOException e) {
return false;
}
}
/** If the file is a directory, list its contents. */
public Iterable<DocFile> list() {
List<DocFile> files = new ArrayList<>();
for (File f: file.listFiles()) {
files.add(new StandardDocFile(f));
public Iterable<DocFile> list() throws IOException {
List<DocFile> files = new ArrayList<DocFile>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) {
for (Path f: ds) {
files.add(new StandardDocFile(f));
}
}
return files;
}
/** Create the file as a directory, including any parent directories. */
public boolean mkdirs() {
return file.mkdirs();
try {
Files.createDirectories(file);
return true;
} catch (IOException e) {
return false;
}
}
/**
@ -261,7 +269,7 @@ class StandardDocFileFactory extends DocFileFactory {
*/
public DocFile resolve(String p) {
if (location == null && path == null) {
return new StandardDocFile(new File(file, p));
return new StandardDocFile(file.resolve(p));
} else {
return new StandardDocFile(location, path.resolve(p));
}
@ -270,12 +278,12 @@ class StandardDocFileFactory extends DocFileFactory {
/**
* Resolve a relative file against the given output location.
* @param locn Currently, only
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
* {@link DocumentationTool.Location.DOCUMENTATION_OUTPUT} is supported.
*/
public DocFile resolveAgainst(Location locn) {
if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalArgumentException();
return new StandardDocFile(newFile(getDestDir(), file.getPath()));
return new StandardDocFile(getDestDir().resolve(file));
}
/** Return a string to identify the contents of this object,
@ -284,7 +292,7 @@ class StandardDocFileFactory extends DocFileFactory {
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("StandardDocFile[");
sb.append("PathDocFile[");
if (location != null)
sb.append("locn:").append(location).append(",");
if (path != null)
@ -294,7 +302,7 @@ class StandardDocFileFactory extends DocFileFactory {
return sb.toString();
}
private JavaFileObject getJavaFileObjectForInput(File file) {
private JavaFileObject getJavaFileObjectForInput(Path file) {
return fileManager.getJavaFileObjects(file).iterator().next();
}

View File

@ -239,12 +239,6 @@ public abstract class Configuration {
*/
public boolean showversion = false;
/**
* Sourcepath from where to read the source files. Default is classpath.
*
*/
public String sourcepath = "";
/**
* Don't generate deprecated API information at all, if -nodeprecated
* option is used. <code>nodepracted</code> is set to true if
@ -389,26 +383,6 @@ public abstract class Configuration {
return true;
}
},
new Hidden(this, "classpath", 1) {
@Override
public boolean process(String opt, ListIterator<String> args) {
if (sourcepath.length() == 0) {
optionsProcessed.add(this);
sourcepath = args.next();
}
return true;
}
},
new Hidden(this, "cp", 1) {
@Override
public boolean process(String opt, ListIterator<String> args) {
if (sourcepath.length() == 0) {
optionsProcessed.add(this);
sourcepath = args.next();
}
return true;
}
},
new Option(this, "d", 1) {
@Override
public boolean process(String opt, ListIterator<String> args) {
@ -555,14 +529,6 @@ public abstract class Configuration {
return true;
}
},
new Hidden(this, "sourcepath", 1) {
@Override
public boolean process(String opt, ListIterator<String> args) {
optionsProcessed.add(this);
sourcepath = args.next();
return true;
}
},
new Option(this, "sourcetab", 1) {
@Override
public boolean process(String opt, ListIterator<String> args) {
@ -638,9 +604,6 @@ public abstract class Configuration {
extern.link(urlForLink, pkglistUrlForLink, reporter, false);
if (urlForLinkOffline != null && pkglistUrlForLinkOffline != null)
extern.link(urlForLinkOffline, pkglistUrlForLinkOffline, reporter, true);
if (sourcepath.length() == 0) {
sourcepath = System.getProperty("env.class.path", "");
}
if (docencoding == null) {
docencoding = encoding;
}

View File

@ -23,7 +23,6 @@ doclet.Copy_Overwrite_warning=File {0} not copied to {1} due to existing file wi
doclet.Copying_File_0_To_Dir_1=Copying file {0} to directory {1}...
doclet.Copying_File_0_To_File_1=Copying file {0} to file {1}...
doclet.No_Public_Classes_To_Document=No public or protected classes found to document.
doclet.Unable_to_create_directory_0=Unable to create directory {0}
doclet.destination_directory_not_directory_0=Destination directory is not a directory {0}
doclet.destination_directory_not_writable_0=Destination directory not writable {0}
doclet.Encoding_not_supported=Encoding not supported: {0}

View File

@ -1,291 +0,0 @@
/*
* Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package jdk.javadoc.internal.doclets.toolkit.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import javax.tools.DocumentationTool;
import javax.tools.JavaFileManager.Location;
import javax.tools.StandardLocation;
import jdk.javadoc.internal.doclets.toolkit.Configuration;
/**
* Implementation of DocFileFactory that just uses java.io.File API,
* and does not use a JavaFileManager..
*
* <p><b>This is NOT part of any supported API.
* If you write code that depends on this, you do so at your own risk.
* This code and its internal interfaces are subject to change or
* deletion without notice.</b>
*
*/
class SimpleDocFileFactory extends DocFileFactory {
public SimpleDocFileFactory(Configuration configuration) {
super(configuration);
}
public DocFile createFileForDirectory(String file) {
return new SimpleDocFile(new File(file));
}
public DocFile createFileForInput(String file) {
return new SimpleDocFile(new File(file));
}
public DocFile createFileForOutput(DocPath path) {
return new SimpleDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
}
@Override
Iterable<DocFile> list(Location location, DocPath path) {
if (location != StandardLocation.SOURCE_PATH)
throw new IllegalArgumentException();
Set<DocFile> files = new LinkedHashSet<>();
for (String s : configuration.sourcepath.split(File.pathSeparator)) {
if (s.isEmpty())
continue;
File f = new File(s);
if (f.isDirectory()) {
f = new File(f, path.getPath());
if (f.exists())
files.add(new SimpleDocFile(f));
}
}
return files;
}
class SimpleDocFile extends DocFile {
private File file;
/** Create a DocFile for a given file. */
private SimpleDocFile(File file) {
super(configuration);
this.file = file;
}
/** Create a DocFile for a given location and relative path. */
private SimpleDocFile(Location location, DocPath path) {
super(configuration, location, path);
String destDirName = configuration.destDirName;
this.file = destDirName.isEmpty() ? new File(path.getPath())
: new File(destDirName, path.getPath());
}
/** Open an input stream for the file. */
public InputStream openInputStream() throws FileNotFoundException {
return new BufferedInputStream(new FileInputStream(file));
}
/**
* Open an output stream for the file.
* The file must have been created with a location of
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
*/
public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalStateException();
createDirectoryForFile(file);
return new BufferedOutputStream(new FileOutputStream(file));
}
/**
* Open an writer for the file, using the encoding (if any) given in the
* doclet configuration.
* The file must have been created with a location of
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
*/
public Writer openWriter() throws IOException, UnsupportedEncodingException {
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalStateException();
createDirectoryForFile(file);
FileOutputStream fos = new FileOutputStream(file);
if (configuration.docencoding == null) {
return new BufferedWriter(new OutputStreamWriter(fos));
} else {
return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
}
}
/** Return true if the file can be read. */
public boolean canRead() {
return file.canRead();
}
/** Return true if the file can be written. */
public boolean canWrite() {
return file.canRead();
}
/** Return true if the file exists. */
public boolean exists() {
return file.exists();
}
/** Return the base name (last component) of the file name. */
public String getName() {
return file.getName();
}
/** Return the file system path for this file. */
public String getPath() {
return file.getPath();
}
/** Return true is file has an absolute path name. */
public boolean isAbsolute() {
return file.isAbsolute();
}
/** Return true is file identifies a directory. */
public boolean isDirectory() {
return file.isDirectory();
}
/** Return true is file identifies a file. */
public boolean isFile() {
return file.isFile();
}
/** Return true if this file is the same as another. */
public boolean isSameFile(DocFile other) {
if (!(other instanceof SimpleDocFile))
return false;
try {
return file.exists()
&& file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile());
} catch (IOException e) {
return false;
}
}
/** If the file is a directory, list its contents. */
public Iterable<DocFile> list() {
List<DocFile> files = new ArrayList<>();
for (File f: file.listFiles()) {
files.add(new SimpleDocFile(f));
}
return files;
}
/** Create the file as a directory, including any parent directories. */
public boolean mkdirs() {
return file.mkdirs();
}
/**
* Derive a new file by resolving a relative path against this file.
* The new file will inherit the configuration and location of this file
* If this file has a path set, the new file will have a corresponding
* new path.
*/
public DocFile resolve(DocPath p) {
return resolve(p.getPath());
}
/**
* Derive a new file by resolving a relative path against this file.
* The new file will inherit the configuration and location of this file
* If this file has a path set, the new file will have a corresponding
* new path.
*/
public DocFile resolve(String p) {
if (location == null && path == null) {
return new SimpleDocFile(new File(file, p));
} else {
return new SimpleDocFile(location, path.resolve(p));
}
}
/**
* Resolve a relative file against the given output location.
* @param locn Currently, only
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
*/
public DocFile resolveAgainst(Location locn) {
if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalArgumentException();
return new SimpleDocFile(
new File(configuration.destDirName, file.getPath()));
}
/**
* Given a path string create all the directories in the path. For example,
* if the path string is "java/applet", the method will create directory
* "java" and then "java/applet" if they don't exist. The file separator
* string "/" is platform dependent system property.
*
* @param path Directory path string.
*/
private void createDirectoryForFile(File file) {
File dir = file.getParentFile();
if (dir == null || dir.exists() || dir.mkdirs())
return;
configuration.message.error(
"doclet.Unable_to_create_directory_0", dir.getPath());
throw new DocletAbortException("can't create directory");
}
/** Return a string to identify the contents of this object,
* for debugging purposes.
*/
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("DocFile[");
if (location != null)
sb.append("locn:").append(location).append(",");
if (path != null)
sb.append("path:").append(path.getPath()).append(",");
sb.append("file:").append(file);
sb.append("]");
return sb.toString();
}
}
}

View File

@ -28,13 +28,16 @@ package jdk.javadoc.internal.doclets.toolkit.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
@ -48,6 +51,7 @@ import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import javax.tools.StandardLocation;
import com.sun.tools.javac.util.Assert;
import jdk.javadoc.internal.doclets.toolkit.Configuration;
/**
@ -61,37 +65,37 @@ import jdk.javadoc.internal.doclets.toolkit.Configuration;
*/
class StandardDocFileFactory extends DocFileFactory {
private final StandardJavaFileManager fileManager;
private File destDir;
private Path destDir;
public StandardDocFileFactory(Configuration configuration) {
super(configuration);
fileManager = (StandardJavaFileManager) configuration.getFileManager();
}
private File getDestDir() {
private Path getDestDir() {
if (destDir == null) {
if (!configuration.destDirName.isEmpty()
|| !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
try {
String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
File dir = new File(dirName);
fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
Path dir = Paths.get(dirName);
fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
} catch (IOException e) {
throw new DocletAbortException(e);
}
}
destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
}
return destDir;
}
public DocFile createFileForDirectory(String file) {
return new StandardDocFile(new File(file));
return new StandardDocFile(Paths.get(file));
}
public DocFile createFileForInput(String file) {
return new StandardDocFile(new File(file));
return new StandardDocFile(Paths.get(file));
}
public DocFile createFileForOutput(DocPath path) {
@ -106,26 +110,25 @@ class StandardDocFileFactory extends DocFileFactory {
Set<DocFile> files = new LinkedHashSet<>();
Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH)
? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
for (File f: fileManager.getLocation(l)) {
if (f.isDirectory()) {
f = new File(f, path.getPath());
if (f.exists())
for (Path f: fileManager.getLocationAsPaths(l)) {
if (Files.isDirectory(f)) {
f = f.resolve(path.getPath());
if (Files.exists(f))
files.add(new StandardDocFile(f));
}
}
return files;
}
private static File newFile(File dir, String path) {
return (dir == null) ? new File(path) : new File(dir, path);
private static Path newFile(Path dir, String path) {
return (dir == null) ? Paths.get(path) : dir.resolve(path);
}
class StandardDocFile extends DocFile {
private File file;
private Path file;
/** Create a StandardDocFile for a given file. */
private StandardDocFile(File file) {
private StandardDocFile(Path file) {
super(configuration);
this.file = file;
}
@ -133,9 +136,7 @@ class StandardDocFileFactory extends DocFileFactory {
/** Create a StandardDocFile for a given location and relative path. */
private StandardDocFile(Location location, DocPath path) {
super(configuration, location, path);
if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT) {
throw new AssertionError("invalid location output");
}
Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT);
this.file = newFile(getDestDir(), path.getPath());
}
@ -178,27 +179,27 @@ class StandardDocFileFactory extends DocFileFactory {
/** Return true if the file can be read. */
public boolean canRead() {
return file.canRead();
return Files.isReadable(file);
}
/** Return true if the file can be written. */
public boolean canWrite() {
return file.canWrite();
return Files.isWritable(file);
}
/** Return true if the file exists. */
public boolean exists() {
return file.exists();
return Files.exists(file);
}
/** Return the base name (last component) of the file name. */
public String getName() {
return file.getName();
return file.getFileName().toString();
}
/** Return the file system path for this file. */
public String getPath() {
return file.getPath();
return file.toString();
}
/** Return true is file has an absolute path name. */
@ -208,12 +209,12 @@ class StandardDocFileFactory extends DocFileFactory {
/** Return true is file identifies a directory. */
public boolean isDirectory() {
return file.isDirectory();
return Files.isDirectory(file);
}
/** Return true is file identifies a file. */
public boolean isFile() {
return file.isFile();
return Files.isRegularFile(file);
}
/** Return true if this file is the same as another. */
@ -222,25 +223,31 @@ class StandardDocFileFactory extends DocFileFactory {
return false;
try {
return file.exists()
&& file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile());
return Files.isSameFile(file, ((StandardDocFile) other).file);
} catch (IOException e) {
return false;
}
}
/** If the file is a directory, list its contents. */
public Iterable<DocFile> list() {
List<DocFile> files = new ArrayList<>();
for (File f: file.listFiles()) {
files.add(new StandardDocFile(f));
public Iterable<DocFile> list() throws IOException {
List<DocFile> files = new ArrayList<DocFile>();
try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) {
for (Path f: ds) {
files.add(new StandardDocFile(f));
}
}
return files;
}
/** Create the file as a directory, including any parent directories. */
public boolean mkdirs() {
return file.mkdirs();
try {
Files.createDirectories(file);
return true;
} catch (IOException e) {
return false;
}
}
/**
@ -261,7 +268,7 @@ class StandardDocFileFactory extends DocFileFactory {
*/
public DocFile resolve(String p) {
if (location == null && path == null) {
return new StandardDocFile(new File(file, p));
return new StandardDocFile(file.resolve(p));
} else {
return new StandardDocFile(location, path.resolve(p));
}
@ -270,12 +277,12 @@ class StandardDocFileFactory extends DocFileFactory {
/**
* Resolve a relative file against the given output location.
* @param locn Currently, only
* {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
* {@link DocumentationTool.Location.DOCUMENTATION_OUTPUT} is supported.
*/
public DocFile resolveAgainst(Location locn) {
if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
throw new IllegalArgumentException();
return new StandardDocFile(newFile(getDestDir(), file.getPath()));
return new StandardDocFile(getDestDir().resolve(file));
}
/** Return a string to identify the contents of this object,
@ -294,7 +301,7 @@ class StandardDocFileFactory extends DocFileFactory {
return sb.toString();
}
private JavaFileObject getJavaFileObjectForInput(File file) {
private JavaFileObject getJavaFileObjectForInput(Path file) {
return fileManager.getJavaFileObjects(file).iterator().next();
}