src/share/classes/com/sun/tools/doclets/internal/toolkit/util/SimpleDocFileFactory.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.FileInputStream;
    33 import java.io.FileNotFoundException;
    34 import java.io.FileOutputStream;
    35 import java.io.IOException;
    36 import java.io.InputStream;
    37 import java.io.OutputStream;
    38 import java.io.OutputStreamWriter;
    39 import java.io.UnsupportedEncodingException;
    40 import java.io.Writer;
    41 import java.util.ArrayList;
    42 import java.util.LinkedHashSet;
    43 import java.util.List;
    44 import java.util.Set;
    46 import javax.tools.JavaFileManager.Location;
    47 import javax.tools.StandardLocation;
    49 import com.sun.tools.doclets.internal.toolkit.Configuration;
    51 /**
    52  * Implementation of DocFileFactory that just uses java.io.File API,
    53  * and does not use a JavaFileManager..
    54  *
    55  *  <p><b>This is NOT part of any supported API.
    56  *  If you write code that depends on this, you do so at your own risk.
    57  *  This code and its internal interfaces are subject to change or
    58  *  deletion without notice.</b>
    59  *
    60  * @since 1.8
    61  */
    62 class SimpleDocFileFactory extends DocFileFactory {
    64     public SimpleDocFileFactory(Configuration configuration) {
    65         super(configuration);
    66     }
    68     public DocFile createFileForDirectory(String file) {
    69         return new SimpleDocFile(new File(file));
    70     }
    72     public DocFile createFileForInput(String file) {
    73         return new SimpleDocFile(new File(file));
    74     }
    76     public DocFile createFileForOutput(DocPath path) {
    77         return new SimpleDocFile(StandardLocation.CLASS_OUTPUT, path);
    78     }
    80     @Override
    81     Iterable<DocFile> list(Location location, DocPath path) {
    82         if (location != StandardLocation.SOURCE_PATH)
    83             throw new IllegalArgumentException();
    85         Set<DocFile> files = new LinkedHashSet<DocFile>();
    86         for (String s : configuration.sourcepath.split(File.pathSeparator)) {
    87             if (s.isEmpty())
    88                 continue;
    89             File f = new File(s);
    90             if (f.isDirectory()) {
    91                 f = new File(f, path.getPath());
    92                 if (f.exists())
    93                     files.add(new SimpleDocFile(f));
    94             }
    95         }
    96         return files;
    97     }
    99     class SimpleDocFile extends DocFile {
   100         private File file;
   102         /** Create a DocFile for a given file. */
   103         private SimpleDocFile(File file) {
   104             super(configuration);
   105             this.file = file;
   106         }
   108         /** Create a DocFile for a given location and relative path. */
   109         private SimpleDocFile(Location location, DocPath path) {
   110             super(configuration, location, path);
   111             String destDirName = configuration.destDirName;
   112             this.file = destDirName.isEmpty() ? new File(path.getPath())
   113                     : new File(destDirName, path.getPath());
   114         }
   116         /** Open an input stream for the file. */
   117         public InputStream openInputStream() throws FileNotFoundException {
   118             return new BufferedInputStream(new FileInputStream(file));
   119         }
   121         /**
   122          * Open an output stream for the file.
   123          * The file must have been created with a location of
   124          * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   125          */
   126         public OutputStream openOutputStream() throws IOException, UnsupportedEncodingException {
   127             if (location != StandardLocation.CLASS_OUTPUT)
   128                 throw new IllegalStateException();
   130             createDirectoryForFile(file);
   131             return new BufferedOutputStream(new FileOutputStream(file));
   132         }
   134         /**
   135          * Open an writer for the file, using the encoding (if any) given in the
   136          * doclet configuration.
   137          * The file must have been created with a location of
   138          * {@link StandardLocation#CLASS_OUTPUT} and a corresponding relative path.
   139          */
   140         public Writer openWriter() throws IOException, UnsupportedEncodingException {
   141             if (location != StandardLocation.CLASS_OUTPUT)
   142                 throw new IllegalStateException();
   144             createDirectoryForFile(file);
   145             FileOutputStream fos = new FileOutputStream(file);
   146             if (configuration.docencoding == null) {
   147                 return new BufferedWriter(new OutputStreamWriter(fos));
   148             } else {
   149                 return new BufferedWriter(new OutputStreamWriter(fos, configuration.docencoding));
   150             }
   151         }
   153         /** Return true if the file can be read. */
   154         public boolean canRead() {
   155             return file.canRead();
   156         }
   158         /** Return true if the file can be written. */
   159         public boolean canWrite() {
   160             return file.canRead();
   161         }
   163         /** Return true if the file exists. */
   164         public boolean exists() {
   165             return file.exists();
   166         }
   168         /** Return the base name (last component) of the file name. */
   169         public String getName() {
   170             return file.getName();
   171         }
   173         /** Return the file system path for this file. */
   174         public String getPath() {
   175             return file.getPath();
   176         }
   178         /** Return true is file has an absolute path name. */
   179         public boolean isAbsolute() {
   180             return file.isAbsolute();
   181         }
   183         /** Return true is file identifies a directory. */
   184         public boolean isDirectory() {
   185             return file.isDirectory();
   186         }
   188         /** Return true is file identifies a file. */
   189         public boolean isFile() {
   190             return file.isFile();
   191         }
   193         /** Return true if this file is the same as another. */
   194         public boolean isSameFile(DocFile other) {
   195             if (!(other instanceof SimpleDocFile))
   196                 return false;
   198             try {
   199                 return file.exists()
   200                         && file.getCanonicalFile().equals(((SimpleDocFile)other).file.getCanonicalFile());
   201             } catch (IOException e) {
   202                 return false;
   203             }
   204         }
   206         /** If the file is a directory, list its contents. */
   207         public Iterable<DocFile> list() {
   208             List<DocFile> files = new ArrayList<DocFile>();
   209             for (File f: file.listFiles()) {
   210                 files.add(new SimpleDocFile(f));
   211             }
   212             return files;
   213         }
   215         /** Create the file as a directory, including any parent directories. */
   216         public boolean mkdirs() {
   217             return file.mkdirs();
   218         }
   220         /**
   221          * Derive a new file by resolving a relative path against this file.
   222          * The new file will inherit the configuration and location of this file
   223          * If this file has a path set, the new file will have a corresponding
   224          * new path.
   225          */
   226         public DocFile resolve(DocPath p) {
   227             return resolve(p.getPath());
   228         }
   230         /**
   231          * Derive a new file by resolving a relative path against this file.
   232          * The new file will inherit the configuration and location of this file
   233          * If this file has a path set, the new file will have a corresponding
   234          * new path.
   235          */
   236         public DocFile resolve(String p) {
   237             if (location == null && path == null) {
   238                 return new SimpleDocFile(new File(file, p));
   239             } else {
   240                 return new SimpleDocFile(location, path.resolve(p));
   241             }
   242         }
   244         /**
   245          * Resolve a relative file against the given output location.
   246          * @param locn Currently, only SOURCE_OUTPUT is supported.
   247          */
   248         public DocFile resolveAgainst(StandardLocation locn) {
   249             if (locn != StandardLocation.CLASS_OUTPUT)
   250                 throw new IllegalArgumentException();
   251             return new SimpleDocFile(
   252                     new File(configuration.destDirName, file.getPath()));
   253         }
   255         /**
   256          * Given a path string create all the directories in the path. For example,
   257          * if the path string is "java/applet", the method will create directory
   258          * "java" and then "java/applet" if they don't exist. The file separator
   259          * string "/" is platform dependent system property.
   260          *
   261          * @param path Directory path string.
   262          */
   263         private void createDirectoryForFile(File file) {
   264             File dir = file.getParentFile();
   265             if (dir == null || dir.exists() || dir.mkdirs())
   266                 return;
   268             configuration.message.error(
   269                    "doclet.Unable_to_create_directory_0", dir.getPath());
   270             throw new DocletAbortException();
   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("DocFile[");
   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     }
   290 }

mercurial