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

Thu, 31 Aug 2017 15:17:03 +0800

author
aoqi
date
Thu, 31 Aug 2017 15:17:03 +0800
changeset 2525
2eb010b6cb22
parent 2010
64328fe5e4a6
parent 0
959103a6100f
permissions
-rw-r--r--

merge

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

mercurial