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

Thu, 15 Nov 2012 19:54:20 -0800

author
jjg
date
Thu, 15 Nov 2012 19:54:20 -0800
changeset 1412
400a4e8accd3
child 1413
bdcef2ef52d2
permissions
-rw-r--r--

8002079: update DocFile to use a JavaFileManager
Reviewed-by: darcy

     1 /*
     2  * Copyright (c) 1998, 2012, 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.Files;
    38 import java.nio.file.Path;
    39 import java.util.ArrayList;
    40 import java.util.Arrays;
    41 import java.util.LinkedHashSet;
    42 import java.util.List;
    43 import java.util.Set;
    45 import javax.tools.FileObject;
    46 import javax.tools.JavaFileManager.Location;
    47 import javax.tools.JavaFileObject;
    48 import javax.tools.StandardLocation;
    50 import com.sun.tools.doclets.internal.toolkit.Configuration;
    51 import com.sun.tools.javac.nio.PathFileManager;
    54 /**
    55  * Implementation of DocFileFactory using a {@link PathFileManager}.
    56  *
    57  *  <p><b>This is NOT part of any supported API.
    58  *  If you write code that depends on this, you do so at your own risk.
    59  *  This code and its internal interfaces are subject to change or
    60  *  deletion without notice.</b>
    61  *
    62  * @since 1.8
    63  */
    64 class PathDocFileFactory extends DocFileFactory {
    65     private final PathFileManager fileManager;
    66     private final Path destDir;
    68     public PathDocFileFactory(Configuration configuration) {
    69         super(configuration);
    70         fileManager = (PathFileManager) configuration.getFileManager();
    72         if (!configuration.destDirName.isEmpty()
    73                 || !fileManager.hasLocation(StandardLocation.CLASS_OUTPUT)) {
    74             try {
    75                 String dirName = configuration.destDirName.isEmpty() ? "." : configuration.destDirName;
    76                 Path dir = fileManager.getDefaultFileSystem().getPath(dirName);
    77                 fileManager.setLocation(StandardLocation.CLASS_OUTPUT, Arrays.asList(dir));
    78             } catch (IOException e) {
    79                 throw new DocletAbortException();
    80             }
    81         }
    83         destDir = fileManager.getLocation(StandardLocation.CLASS_OUTPUT).iterator().next();
    84     }
    86     public DocFile createFileForDirectory(String file) {
    87         return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file));
    88     }
    90     public DocFile createFileForInput(String file) {
    91         return new StandardDocFile(fileManager.getDefaultFileSystem().getPath(file));
    92     }
    94     public DocFile createFileForOutput(DocPath path) {
    95         return new StandardDocFile(StandardLocation.CLASS_OUTPUT, path);
    96     }
    98     @Override
    99     Iterable<DocFile> list(Location location, DocPath path) {
   100         if (location != StandardLocation.SOURCE_PATH)
   101             throw new IllegalArgumentException();
   103         Set<DocFile> files = new LinkedHashSet<DocFile>();
   104         if (fileManager.hasLocation(location)) {
   105             for (Path f: fileManager.getLocation(location)) {
   106                 if (Files.isDirectory(f)) {
   107                     f = f.resolve(path.getPath());
   108                     if (Files.exists(f))
   109                         files.add(new StandardDocFile(f));
   110                 }
   111             }
   112         }
   113         return files;
   114     }
   116     class StandardDocFile extends DocFile {
   117         private Path file;
   119         /** Create a StandardDocFile for a given file. */
   120         private StandardDocFile(Path file) {
   121             super(configuration);
   122             this.file = file;
   123         }
   125         /** Create a StandardDocFile for a given location and relative path. */
   126         private StandardDocFile(Location location, DocPath path) {
   127             super(configuration, location, path);
   128             this.file = destDir.resolve(path.getPath());
   129         }
   131         /** Open an input stream for the file. */
   132         public InputStream openInputStream() throws IOException {
   133             JavaFileObject fo = getJavaFileObjectForInput(file);
   134             return new BufferedInputStream(fo.openInputStream());
   135         }
   137         /**
   138          * Open an output stream for the file.
   139          * The file must have been created with a location of
   140          * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   141          */
   142         public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
   143             if (location != StandardLocation.CLASS_OUTPUT)
   144                 throw new IllegalStateException();
   146             OutputStream out = getFileObjectForOutput(path).openOutputStream();
   147             return new BufferedOutputStream(out);
   148         }
   150         /**
   151          * Open an writer for the file, using the encoding (if any) given in the
   152          * doclet configuration.
   153          * The file must have been created with a location of
   154          * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   155          */
   156         public Writer openWriter() throws IOException, UnsupportedEncodingException {
   157             if (location != StandardLocation.CLASS_OUTPUT)
   158                 throw new IllegalStateException();
   160             OutputStream out = getFileObjectForOutput(path).openOutputStream();
   161             if (configuration.docencoding == null) {
   162                 return new BufferedWriter(new OutputStreamWriter(out));
   163             } else {
   164                 return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
   165             }
   166         }
   168         /** Return true if the file can be read. */
   169         public boolean canRead() {
   170             return Files.isReadable(file);
   171         }
   173         /** Return true if the file can be written. */
   174         public boolean canWrite() {
   175             return Files.isWritable(file);
   176         }
   178         /** Return true if the file exists. */
   179         public boolean exists() {
   180             return Files.exists(file);
   181         }
   183         /** Return the base name (last component) of the file name. */
   184         public String getName() {
   185             return file.getFileName().toString();
   186         }
   188         /** Return the file system path for this file. */
   189         public String getPath() {
   190             return file.toString();
   191         }
   193         /** Return true is file has an absolute path name. */
   194         public boolean isAbsolute() {
   195             return file.isAbsolute();
   196         }
   198         /** Return true is file identifies a directory. */
   199         public boolean isDirectory() {
   200             return Files.isDirectory(file);
   201         }
   203         /** Return true is file identifies a file. */
   204         public boolean isFile() {
   205             return Files.isRegularFile(file);
   206         }
   208         /** Return true if this file is the same as another. */
   209         public boolean isSameFile(DocFile other) {
   210             if (!(other instanceof StandardDocFile))
   211                 return false;
   213             try {
   214                 return Files.isSameFile(file, ((StandardDocFile) other).file);
   215             } catch (IOException e) {
   216                 return false;
   217             }
   218         }
   220         /** If the file is a directory, list its contents. */
   221         public Iterable<DocFile> list() throws IOException {
   222             List<DocFile> files = new ArrayList<DocFile>();
   223             for (Path f: Files.newDirectoryStream(file)) {
   224                 files.add(new StandardDocFile(f));
   225             }
   226             return files;
   227         }
   229         /** Create the file as a directory, including any parent directories. */
   230         public boolean mkdirs() {
   231             try {
   232                 Files.createDirectories(file);
   233                 return true;
   234             } catch (IOException e) {
   235                 return false;
   236             }
   237         }
   239         /**
   240          * Derive a new file by resolving a relative path against this file.
   241          * The new file will inherit the configuration and location of this file
   242          * If this file has a path set, the new file will have a corresponding
   243          * new path.
   244          */
   245         public DocFile resolve(DocPath p) {
   246             return resolve(p.getPath());
   247         }
   249         /**
   250          * Derive a new file by resolving a relative path against this file.
   251          * The new file will inherit the configuration and location of this file
   252          * If this file has a path set, the new file will have a corresponding
   253          * new path.
   254          */
   255         public DocFile resolve(String p) {
   256             if (location == null && path == null) {
   257                 return new StandardDocFile(file.resolve(p));
   258             } else {
   259                 return new StandardDocFile(location, path.resolve(p));
   260             }
   261         }
   263         /**
   264          * Resolve a relative file against the given output location.
   265          * @param locn Currently, only SOURCE_OUTPUT is supported.
   266          */
   267         public DocFile resolveAgainst(StandardLocation locn) {
   268             if (locn != StandardLocation.CLASS_OUTPUT)
   269                 throw new IllegalArgumentException();
   270             return new StandardDocFile(destDir.resolve(file));
   271         }
   273         /** Return a string to identify the contents of this object,
   274          * for debugging purposes.
   275          */
   276         @Override
   277         public String toString() {
   278             StringBuilder sb = new StringBuilder();
   279             sb.append("PathDocFile[");
   280             if (location != null)
   281                 sb.append("locn:").append(location).append(",");
   282             if (path != null)
   283                 sb.append("path:").append(path.getPath()).append(",");
   284             sb.append("file:").append(file);
   285             sb.append("]");
   286             return sb.toString();
   287         }
   289         private JavaFileObject getJavaFileObjectForInput(Path file) {
   290             return fileManager.getJavaFileObjects(file).iterator().next();
   291         }
   293         private FileObject getFileObjectForOutput(DocPath path) throws IOException {
   294             // break the path into a package-part and the rest, by finding
   295             // the position of the last '/' before an invalid character for a
   296             // package name, such as the "." before an extension or the "-"
   297             // in filenames like package-summary.html, doc-files or src-html.
   298             String p = path.getPath();
   299             int lastSep = -1;
   300             for (int i = 0; i < p.length(); i++) {
   301                 char ch = p.charAt(i);
   302                 if (ch == '/') {
   303                     lastSep = i;
   304                 } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
   305                         || !Character.isJavaIdentifierPart(ch)) {
   306                     break;
   307                 }
   308             }
   309             String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
   310             String rest = p.substring(lastSep + 1);
   311             return fileManager.getFileForOutput(location, pkg, rest, null);
   312         }
   313     }
   315 }

mercurial