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