langtools/src/jdk.javadoc/share/classes/com/sun/tools/doclets/internal/toolkit/util/StandardDocFileFactory.java
changeset 36037 688096b6bcc4
parent 25874 83c19f00452c
child 36526 3b41f1c69604
equal deleted inserted replaced
35814:6c644cca3f3f 36037:688096b6bcc4
     1 /*
     1 /*
     2  * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    26 package com.sun.tools.doclets.internal.toolkit.util;
    26 package com.sun.tools.doclets.internal.toolkit.util;
    27 
    27 
    28 import java.io.BufferedInputStream;
    28 import java.io.BufferedInputStream;
    29 import java.io.BufferedOutputStream;
    29 import java.io.BufferedOutputStream;
    30 import java.io.BufferedWriter;
    30 import java.io.BufferedWriter;
    31 import java.io.File;
       
    32 import java.io.IOException;
    31 import java.io.IOException;
    33 import java.io.InputStream;
    32 import java.io.InputStream;
    34 import java.io.OutputStream;
    33 import java.io.OutputStream;
    35 import java.io.OutputStreamWriter;
    34 import java.io.OutputStreamWriter;
    36 import java.io.UnsupportedEncodingException;
    35 import java.io.UnsupportedEncodingException;
    37 import java.io.Writer;
    36 import java.io.Writer;
       
    37 import java.nio.file.DirectoryStream;
       
    38 import java.nio.file.Files;
       
    39 import java.nio.file.Path;
       
    40 import java.nio.file.Paths;
    38 import java.util.ArrayList;
    41 import java.util.ArrayList;
    39 import java.util.Arrays;
    42 import java.util.Arrays;
    40 import java.util.LinkedHashSet;
    43 import java.util.LinkedHashSet;
    41 import java.util.List;
    44 import java.util.List;
    42 import java.util.Set;
    45 import java.util.Set;
    61  *
    64  *
    62  * @since 1.8
    65  * @since 1.8
    63  */
    66  */
    64 class StandardDocFileFactory extends DocFileFactory {
    67 class StandardDocFileFactory extends DocFileFactory {
    65     private final StandardJavaFileManager fileManager;
    68     private final StandardJavaFileManager fileManager;
    66     private File destDir;
    69     private Path destDir;
    67 
    70 
    68     public StandardDocFileFactory(Configuration configuration) {
    71     public StandardDocFileFactory(Configuration configuration) {
    69         super(configuration);
    72         super(configuration);
    70         fileManager = (StandardJavaFileManager) configuration.getFileManager();
    73         fileManager = (StandardJavaFileManager) configuration.getFileManager();
    71     }
    74     }
    72 
    75 
    73     private File getDestDir() {
    76     private Path getDestDir() {
    74         if (destDir == null) {
    77         if (destDir == null) {
    75             if (!configuration.destDirName.isEmpty()
    78             if (!configuration.destDirName.isEmpty()
    76                     || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
    79                     || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
    77                 try {
    80                 try {
    78                     String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
    81                     String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
    79                     File dir = new File(dirName);
    82                     Path dir = Paths.get(dirName);
    80                     fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
    83                     fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
    81                 } catch (IOException e) {
    84                 } catch (IOException e) {
    82                     throw new DocletAbortException(e);
    85                     throw new DocletAbortException(e);
    83                 }
    86                 }
    84             }
    87             }
    85 
    88 
    86             destDir = fileManager.getLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
    89             destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
    87         }
    90         }
    88         return destDir;
    91         return destDir;
    89     }
    92     }
    90 
    93 
    91     public DocFile createFileForDirectory(String file) {
    94     public DocFile createFileForDirectory(String file) {
    92         return new StandardDocFile(new File(file));
    95         return new StandardDocFile(Paths.get(file));
    93     }
    96     }
    94 
    97 
    95     public DocFile createFileForInput(String file) {
    98     public DocFile createFileForInput(String file) {
    96         return new StandardDocFile(new File(file));
    99         return new StandardDocFile(Paths.get(file));
    97     }
   100     }
    98 
   101 
    99     public DocFile createFileForOutput(DocPath path) {
   102     public DocFile createFileForOutput(DocPath path) {
   100         return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
   103         return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
   101     }
   104     }
   106             throw new IllegalArgumentException();
   109             throw new IllegalArgumentException();
   107 
   110 
   108         Set<DocFile> files = new LinkedHashSet<>();
   111         Set<DocFile> files = new LinkedHashSet<>();
   109         Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH)
   112         Location l = fileManager.hasLocation(StandardLocation.SOURCE_PATH)
   110                 ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
   113                 ? StandardLocation.SOURCE_PATH : StandardLocation.CLASS_PATH;
   111         for (File f: fileManager.getLocation(l)) {
   114         for (Path f: fileManager.getLocationAsPaths(l)) {
   112             if (f.isDirectory()) {
   115             if (Files.isDirectory(f)) {
   113                 f = new File(f, path.getPath());
   116                 f = f.resolve(path.getPath());
   114                 if (f.exists())
   117                 if (Files.exists(f))
   115                     files.add(new StandardDocFile(f));
   118                     files.add(new StandardDocFile(f));
   116             }
   119             }
   117         }
   120         }
   118         return files;
   121         return files;
   119     }
   122     }
   120 
   123 
   121     private static File newFile(File dir, String path) {
   124     private static Path newFile(Path dir, String path) {
   122         return (dir == null) ? new File(path) : new File(dir, path);
   125         return (dir == null) ? Paths.get(path) : dir.resolve(path);
   123     }
   126     }
   124 
   127 
   125     class StandardDocFile extends DocFile {
   128     class StandardDocFile extends DocFile {
   126         private File file;
   129         private Path file;
   127 
       
   128 
   130 
   129         /** Create a StandardDocFile for a given file. */
   131         /** Create a StandardDocFile for a given file. */
   130         private StandardDocFile(File file) {
   132         private StandardDocFile(Path file) {
   131             super(configuration);
   133             super(configuration);
   132             this.file = file;
   134             this.file = file;
   133         }
   135         }
   134 
   136 
   135         /** Create a StandardDocFile for a given location and relative path. */
   137         /** Create a StandardDocFile for a given location and relative path. */
   176             }
   178             }
   177         }
   179         }
   178 
   180 
   179         /** Return true if the file can be read. */
   181         /** Return true if the file can be read. */
   180         public boolean canRead() {
   182         public boolean canRead() {
   181             return file.canRead();
   183             return Files.isReadable(file);
   182         }
   184         }
   183 
   185 
   184         /** Return true if the file can be written. */
   186         /** Return true if the file can be written. */
   185         public boolean canWrite() {
   187         public boolean canWrite() {
   186             return file.canWrite();
   188             return Files.isWritable(file);
   187         }
   189         }
   188 
   190 
   189         /** Return true if the file exists. */
   191         /** Return true if the file exists. */
   190         public boolean exists() {
   192         public boolean exists() {
   191             return file.exists();
   193             return Files.exists(file);
   192         }
   194         }
   193 
   195 
   194         /** Return the base name (last component) of the file name. */
   196         /** Return the base name (last component) of the file name. */
   195         public String getName() {
   197         public String getName() {
   196             return file.getName();
   198             return file.getFileName().toString();
   197         }
   199         }
   198 
   200 
   199         /** Return the file system path for this file. */
   201         /** Return the file system path for this file. */
   200         public String getPath() {
   202         public String getPath() {
   201             return file.getPath();
   203             return file.toString();
   202         }
   204         }
   203 
   205 
   204         /** Return true is file has an absolute path name. */
   206         /** Return true is file has an absolute path name. */
   205         public boolean isAbsolute() {
   207         public boolean isAbsolute() {
   206             return file.isAbsolute();
   208             return file.isAbsolute();
   207         }
   209         }
   208 
   210 
   209         /** Return true is file identifies a directory. */
   211         /** Return true is file identifies a directory. */
   210         public boolean isDirectory() {
   212         public boolean isDirectory() {
   211             return file.isDirectory();
   213             return Files.isDirectory(file);
   212         }
   214         }
   213 
   215 
   214         /** Return true is file identifies a file. */
   216         /** Return true is file identifies a file. */
   215         public boolean isFile() {
   217         public boolean isFile() {
   216             return file.isFile();
   218             return Files.isRegularFile(file);
   217         }
   219         }
   218 
   220 
   219         /** Return true if this file is the same as another. */
   221         /** Return true if this file is the same as another. */
   220         public boolean isSameFile(DocFile other) {
   222         public boolean isSameFile(DocFile other) {
   221             if (!(other instanceof StandardDocFile))
   223             if (!(other instanceof StandardDocFile))
   222                 return false;
   224                 return false;
   223 
   225 
   224             try {
   226             try {
   225                 return file.exists()
   227                 return Files.isSameFile(file, ((StandardDocFile) other).file);
   226                         && file.getCanonicalFile().equals(((StandardDocFile) other).file.getCanonicalFile());
       
   227             } catch (IOException e) {
   228             } catch (IOException e) {
   228                 return false;
   229                 return false;
   229             }
   230             }
   230         }
   231         }
   231 
   232 
   232         /** If the file is a directory, list its contents. */
   233         /** If the file is a directory, list its contents. */
   233         public Iterable<DocFile> list() {
   234         public Iterable<DocFile> list() throws IOException {
   234             List<DocFile> files = new ArrayList<>();
   235             List<DocFile> files = new ArrayList<DocFile>();
   235             for (File f: file.listFiles()) {
   236             try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) {
   236                 files.add(new StandardDocFile(f));
   237                 for (Path f: ds) {
       
   238                     files.add(new StandardDocFile(f));
       
   239                 }
   237             }
   240             }
   238             return files;
   241             return files;
   239         }
   242         }
   240 
   243 
   241         /** Create the file as a directory, including any parent directories. */
   244         /** Create the file as a directory, including any parent directories. */
   242         public boolean mkdirs() {
   245         public boolean mkdirs() {
   243             return file.mkdirs();
   246             try {
       
   247                 Files.createDirectories(file);
       
   248                 return true;
       
   249             } catch (IOException e) {
       
   250                 return false;
       
   251             }
   244         }
   252         }
   245 
   253 
   246         /**
   254         /**
   247          * Derive a new file by resolving a relative path against this file.
   255          * Derive a new file by resolving a relative path against this file.
   248          * The new file will inherit the configuration and location of this file
   256          * The new file will inherit the configuration and location of this file
   259          * If this file has a path set, the new file will have a corresponding
   267          * If this file has a path set, the new file will have a corresponding
   260          * new path.
   268          * new path.
   261          */
   269          */
   262         public DocFile resolve(String p) {
   270         public DocFile resolve(String p) {
   263             if (location == null && path == null) {
   271             if (location == null && path == null) {
   264                 return new StandardDocFile(new File(file, p));
   272                 return new StandardDocFile(file.resolve(p));
   265             } else {
   273             } else {
   266                 return new StandardDocFile(location, path.resolve(p));
   274                 return new StandardDocFile(location, path.resolve(p));
   267             }
   275             }
   268         }
   276         }
   269 
   277 
   270         /**
   278         /**
   271          * Resolve a relative file against the given output location.
   279          * Resolve a relative file against the given output location.
   272          * @param locn Currently, only
   280          * @param locn Currently, only
   273          * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} is supported.
   281          * {@link DocumentationTool.Location.DOCUMENTATION_OUTPUT} is supported.
   274          */
   282          */
   275         public DocFile resolveAgainst(Location locn) {
   283         public DocFile resolveAgainst(Location locn) {
   276             if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
   284             if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
   277                 throw new IllegalArgumentException();
   285                 throw new IllegalArgumentException();
   278             return new StandardDocFile(newFile(getDestDir(), file.getPath()));
   286             return new StandardDocFile(getDestDir().resolve(file));
   279         }
   287         }
   280 
   288 
   281         /** Return a string to identify the contents of this object,
   289         /** Return a string to identify the contents of this object,
   282          * for debugging purposes.
   290          * for debugging purposes.
   283          */
   291          */
   284         @Override
   292         @Override
   285         public String toString() {
   293         public String toString() {
   286             StringBuilder sb = new StringBuilder();
   294             StringBuilder sb = new StringBuilder();
   287             sb.append("StandardDocFile[");
   295             sb.append("PathDocFile[");
   288             if (location != null)
   296             if (location != null)
   289                 sb.append("locn:").append(location).append(",");
   297                 sb.append("locn:").append(location).append(",");
   290             if (path != null)
   298             if (path != null)
   291                 sb.append("path:").append(path.getPath()).append(",");
   299                 sb.append("path:").append(path.getPath()).append(",");
   292             sb.append("file:").append(file);
   300             sb.append("file:").append(file);
   293             sb.append("]");
   301             sb.append("]");
   294             return sb.toString();
   302             return sb.toString();
   295         }
   303         }
   296 
   304 
   297         private JavaFileObject getJavaFileObjectForInput(File file) {
   305         private JavaFileObject getJavaFileObjectForInput(Path file) {
   298             return fileManager.getJavaFileObjects(file).iterator().next();
   306             return fileManager.getJavaFileObjects(file).iterator().next();
   299         }
   307         }
   300 
   308 
   301         private FileObject getFileObjectForOutput(DocPath path) throws IOException {
   309         private FileObject getFileObjectForOutput(DocPath path) throws IOException {
   302             // break the path into a package-part and the rest, by finding
   310             // break the path into a package-part and the rest, by finding