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

mercurial