src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/StandardDocFileFactory.java
author ksrini
Thu, 07 Dec 2017 14:15:14 -0800
changeset 48213 59696a3595bd
parent 48029 e9dab2492924
permissions -rw-r--r--
8191030: @value Tags are not resolved by javadoc 9. Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
     1
/*
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
     2
 * Copyright (c) 1998, 2017, 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
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    26
package jdk.javadoc.internal.doclets.toolkit.util;
14544
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.IOException;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    32
import java.io.InputStream;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    33
import java.io.OutputStream;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    34
import java.io.OutputStreamWriter;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    35
import java.io.UnsupportedEncodingException;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    36
import java.io.Writer;
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    37
import java.nio.file.DirectoryStream;
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    38
import java.nio.file.Files;
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    39
import java.nio.file.Path;
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    40
import java.nio.file.Paths;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    41
import java.util.ArrayList;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    42
import java.util.Arrays;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    43
import java.util.LinkedHashSet;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    44
import java.util.List;
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    45
import java.util.Objects;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    46
import java.util.Set;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    47
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
    48
import javax.tools.DocumentationTool;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    49
import javax.tools.FileObject;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    50
import javax.tools.JavaFileManager.Location;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    51
import javax.tools.JavaFileObject;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    52
import javax.tools.StandardJavaFileManager;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    53
import javax.tools.StandardLocation;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    54
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    55
import com.sun.tools.javac.util.Assert;
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    56
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    57
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    58
/**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    59
 * Implementation of DocFileFactory using a {@link StandardJavaFileManager}.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    60
 *
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    61
 *  <p><b>This is NOT part of any supported API.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    62
 *  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
    63
 *  This code and its internal interfaces are subject to change or
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    64
 *  deletion without notice.</b>
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    65
 *
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    66
 */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    67
class StandardDocFileFactory extends DocFileFactory {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    68
    private final StandardJavaFileManager fileManager;
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    69
    private Path destDir;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    70
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    71
    public StandardDocFileFactory(BaseConfiguration configuration) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    72
        super(configuration);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    73
        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
    74
    }
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
    75
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    76
    @Override
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    77
    public void setDestDir(String destDirName) throws SimpleDocletException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    78
        if (destDir != null)
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    79
            throw new AssertionError("destDir already initialized: " + destDir);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    80
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    81
        if (!destDirName.isEmpty()
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    82
                || !fileManager.hasLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT)) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    83
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    84
                String dirName = destDirName.isEmpty() ? "." : destDirName;
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    85
                Path dir = Paths.get(dirName);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    86
                fileManager.setLocationFromPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT, Arrays.asList(dir));
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    87
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    88
                // generic IOException from file manager, setting location, e.g. file not a directory
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    89
                String message = configuration.getResources().getText("doclet.error.initializing.dest.dir", e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    90
                throw new SimpleDocletException(message, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    91
            }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    92
        }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    93
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    94
        destDir = fileManager.getLocationAsPaths(DocumentationTool.Location.DOCUMENTATION_OUTPUT).iterator().next();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    95
    }
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    96
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
    97
    private Path getDestDir() {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
    98
        Objects.requireNonNull(destDir, "destDir not initialized");
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
    99
        return destDir;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   100
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   101
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   102
    @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   103
    public DocFile createFileForDirectory(String file) {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   104
        return new StandardDocFile(Paths.get(file));
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   105
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   106
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   107
    @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   108
    public DocFile createFileForInput(String file) {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   109
        return new StandardDocFile(Paths.get(file));
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   110
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   111
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   112
    @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   113
    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
   114
        return new StandardDocFile(DocumentationTool.Location.DOCUMENTATION_OUTPUT, path);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   115
    }
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
    @Override
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   118
    Iterable<DocFile> list(Location location, DocPath path) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36037
diff changeset
   119
        Location l = ((location == StandardLocation.SOURCE_PATH)
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36037
diff changeset
   120
                && !fileManager.hasLocation(StandardLocation.SOURCE_PATH))
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36037
diff changeset
   121
                ? StandardLocation.CLASS_PATH
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36037
diff changeset
   122
                : location;
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   123
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 19667
diff changeset
   124
        Set<DocFile> files = new LinkedHashSet<>();
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   125
        for (Path f: fileManager.getLocationAsPaths(l)) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   126
            if (Files.isDirectory(f)) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   127
                f = f.resolve(path.getPath());
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   128
                if (Files.exists(f))
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   129
                    files.add(new StandardDocFile(f));
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   130
            }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   131
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   132
        return files;
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
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   135
    private static Path newFile(Path dir, String path) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   136
        return (dir == null) ? Paths.get(path) : dir.resolve(path);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   137
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   138
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   139
    class StandardDocFile extends DocFile {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   140
        private final Path file;
14544
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
        /** Create a StandardDocFile for a given file. */
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   143
        private StandardDocFile(Path file) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   144
            this.file = file;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   145
        }
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
        /** Create a StandardDocFile for a given location and relative path. */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   148
        private StandardDocFile(Location location, DocPath path) {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   149
            super(location, path);
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   150
            Assert.check(location == DocumentationTool.Location.DOCUMENTATION_OUTPUT);
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   151
            this.file = newFile(getDestDir(), path.getPath());
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
48029
e9dab2492924 8185985: Html files in doc-files directories should be wrapped with standard header and footer
ksrini
parents: 47216
diff changeset
   154
        @Override
e9dab2492924 8185985: Html files in doc-files directories should be wrapped with standard header and footer
ksrini
parents: 47216
diff changeset
   155
        public FileObject getFileObject()  {
e9dab2492924 8185985: Html files in doc-files directories should be wrapped with standard header and footer
ksrini
parents: 47216
diff changeset
   156
            return getJavaFileObjectForInput(file);
e9dab2492924 8185985: Html files in doc-files directories should be wrapped with standard header and footer
ksrini
parents: 47216
diff changeset
   157
        }
e9dab2492924 8185985: Html files in doc-files directories should be wrapped with standard header and footer
ksrini
parents: 47216
diff changeset
   158
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   159
        /**
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   160
         * Open an input stream for the file.
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   161
         *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   162
         * @throws DocFileIOException if there is a problem while opening stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   163
         */
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   164
        @Override
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   165
        public InputStream openInputStream() throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   166
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   167
                JavaFileObject fo = getJavaFileObjectForInput(file);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   168
                return new BufferedInputStream(fo.openInputStream());
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   169
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   170
                throw new DocFileIOException(this, DocFileIOException.Mode.READ, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   171
            }
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   172
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   173
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   174
        /**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   175
         * Open an output stream for the file.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   176
         * 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
   177
         * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   178
         *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   179
         * @throws DocFileIOException if there is a problem while opening stream
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   180
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   181
        @Override
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   182
        public OutputStream openOutputStream() throws DocFileIOException {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   183
            if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   184
                throw new IllegalStateException();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   185
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   186
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   187
                OutputStream out = getFileObjectForOutput(path).openOutputStream();
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   188
                return new BufferedOutputStream(out);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   189
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   190
                throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   191
            }
14544
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
        /**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   195
         * 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
   196
         * doclet configuration.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   197
         * 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
   198
         * {@link DocumentationTool.Location#DOCUMENTATION_OUTPUT} and a corresponding relative path.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   199
         *
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   200
         * @throws DocFileIOException if there is a problem while opening stream
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   201
         * @throws UnsupportedEncodingException if the configured encoding is not supported
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   202
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   203
        @Override
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   204
        public Writer openWriter() throws DocFileIOException, UnsupportedEncodingException {
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   205
            if (location != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   206
                throw new IllegalStateException();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   207
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   208
            try {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   209
                OutputStream out = getFileObjectForOutput(path).openOutputStream();
46080
65ccd412049b 8183582: Rationalize doclet -docencoding and -charset options
pmuthuswamy
parents: 45417
diff changeset
   210
                return new BufferedWriter(new OutputStreamWriter(out, configuration.docencoding));
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   211
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   212
                throw new DocFileIOException(this, DocFileIOException.Mode.WRITE, e);
14544
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
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   215
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   216
        /** Return true if the file can be read. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   217
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   218
        public boolean canRead() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   219
            return Files.isReadable(file);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   220
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   221
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   222
        /** Return true if the file can be written. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   223
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   224
        public boolean canWrite() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   225
            return Files.isWritable(file);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   226
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   227
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   228
        /** Return true if the file exists. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   229
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   230
        public boolean exists() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   231
            return Files.exists(file);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   232
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   233
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   234
        /** Return the base name (last component) of the file name. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   235
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   236
        public String getName() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   237
            return file.getFileName().toString();
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   238
        }
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
        /** Return the file system path for this file. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   241
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   242
        public String getPath() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   243
            return file.toString();
14544
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
        /** Return true is file has an absolute path name. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   247
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   248
        public boolean isAbsolute() {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   249
            return file.isAbsolute();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   250
        }
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
        /** Return true is file identifies a directory. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   253
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   254
        public boolean isDirectory() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   255
            return Files.isDirectory(file);
14544
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
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   258
        /** Return true is file identifies a file. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   259
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   260
        public boolean isFile() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   261
            return Files.isRegularFile(file);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   262
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   263
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   264
        /** Return true if this file is the same as another. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   265
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   266
        public boolean isSameFile(DocFile other) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   267
            if (!(other instanceof StandardDocFile))
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   268
                return false;
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
            try {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   271
                return Files.isSameFile(file, ((StandardDocFile) other).file);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   272
            } catch (IOException e) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   273
                return false;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   274
            }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   275
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   276
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   277
        /** If the file is a directory, list its contents. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   278
        @Override
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   279
        public Iterable<DocFile> list() throws DocFileIOException {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   280
            List<DocFile> files = new ArrayList<>();
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   281
            try (DirectoryStream<Path> ds = Files.newDirectoryStream(file)) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   282
                for (Path f: ds) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   283
                    files.add(new StandardDocFile(f));
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   284
                }
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   285
            } catch (IOException e) {
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   286
                throw new DocFileIOException(this, DocFileIOException.Mode.READ, e);
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   287
            }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   288
            return files;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   289
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   290
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   291
        /** Create the file as a directory, including any parent directories. */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   292
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   293
        public boolean mkdirs() {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   294
            try {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   295
                Files.createDirectories(file);
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   296
                return true;
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   297
            } catch (IOException e) {
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   298
                return false;
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   299
            }
14544
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
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   302
        /**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   303
         * Derive a new file by resolving a relative path against this file.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   304
         * The new file will inherit the configuration and location of this file
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   305
         * 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
   306
         * new path.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   307
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   308
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   309
        public DocFile resolve(DocPath p) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   310
            return resolve(p.getPath());
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   311
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   312
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   313
        /**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   314
         * Derive a new file by resolving a relative path against this file.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   315
         * The new file will inherit the configuration and location of this file
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   316
         * 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
   317
         * new path.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   318
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   319
        @Override
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   320
        public DocFile resolve(String p) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   321
            if (location == null && path == null) {
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   322
                return new StandardDocFile(file.resolve(p));
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   323
            } else {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   324
                return new StandardDocFile(location, path.resolve(p));
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   325
            }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   326
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   327
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   328
        /**
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   329
         * 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
   330
         * @param locn Currently, only
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   331
         * {@link DocumentationTool.Location.DOCUMENTATION_OUTPUT} is supported.
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   332
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36526
diff changeset
   333
        @Override
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14544
diff changeset
   334
        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
   335
            if (locn != DocumentationTool.Location.DOCUMENTATION_OUTPUT)
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   336
                throw new IllegalArgumentException();
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   337
            return new StandardDocFile(getDestDir().resolve(file));
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   338
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   339
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   340
        /** Return a string to identify the contents of this object,
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   341
         * for debugging purposes.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   342
         */
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   343
        @Override
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   344
        public String toString() {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   345
            StringBuilder sb = new StringBuilder();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   346
            sb.append("StandardDocFile[");
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   347
            if (location != null)
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   348
                sb.append("locn:").append(location).append(",");
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   349
            if (path != null)
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   350
                sb.append("path:").append(path.getPath()).append(",");
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   351
            sb.append("file:").append(file);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   352
            sb.append("]");
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   353
            return sb.toString();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   354
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   355
36037
688096b6bcc4 8149773: StandardDocFileFactory should be converted to use java.nio.file.Path
jjg
parents: 35426
diff changeset
   356
        private JavaFileObject getJavaFileObjectForInput(Path file) {
14544
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   357
            return fileManager.getJavaFileObjects(file).iterator().next();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   358
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   359
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   360
        private FileObject getFileObjectForOutput(DocPath path) throws IOException {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   361
            // break the path into a package-part and the rest, by finding
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   362
            // the position of the last '/' before an invalid character for a
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   363
            // package name, such as the "." before an extension or the "-"
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   364
            // in filenames like package-summary.html, doc-files or src-html.
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   365
            String p = path.getPath();
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   366
            int lastSep = -1;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   367
            for (int i = 0; i < p.length(); i++) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   368
                char ch = p.charAt(i);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   369
                if (ch == '/') {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   370
                    lastSep = i;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   371
                } else if (i == lastSep + 1 && !Character.isJavaIdentifierStart(ch)
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   372
                        || !Character.isJavaIdentifierPart(ch)) {
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   373
                    break;
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   374
                }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   375
            }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   376
            String pkg = (lastSep == -1) ? "" : p.substring(0, lastSep);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   377
            String rest = p.substring(lastSep + 1);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   378
            return fileManager.getFileForOutput(location, pkg, rest, null);
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   379
        }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   380
    }
d71d992cb905 8002079: update DocFile to use a JavaFileManager
jjg
parents:
diff changeset
   381
}