src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java
author pmuthuswamy
Tue, 25 Sep 2018 12:36:45 +0530
changeset 51861 f7d40158eb2f
parent 48654 36f58bd6269f
child 52644 43efb4ca6d6c
permissions -rw-r--r--
8205593: Javadoc -link makes broken links if module name matches package name Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48295
diff changeset
     2
 * Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1264
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
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;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
14258
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    28
import java.io.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
import java.net.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    30
import java.util.HashMap;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    31
import java.util.Map;
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
    32
import java.util.TreeMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    34
import javax.lang.model.element.Element;
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
    35
import javax.lang.model.element.ModuleElement;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    36
import javax.lang.model.element.PackageElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    37
import javax.tools.Diagnostic;
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14368
diff changeset
    38
import javax.tools.DocumentationTool;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
    39
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    40
import jdk.javadoc.doclet.Reporter;
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    41
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * Process and manage "-link" and "-linkoffline" to external packages. The
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * generates "package-list"(lists all the packages which are getting
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * documented) file in the current or the destination directory, while
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * generating the documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    50
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    51
 *  If you write code that depends on this, you do so at your own risk.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    52
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    53
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
public class Extern {
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
    61
     * Map element names onto Extern Item objects.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
     * Lazily initialized.
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
     */
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
    64
    private Map<String, Item> moduleItems = new HashMap<>();
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
    65
    private Map<String, Map<String, Item>> packageItems = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
    70
    private final BaseConfiguration configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     * True if we are using -linkoffline and false if -link is used instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
    private boolean linkoffline = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     * Stores the info for one external doc set
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    private class Item {
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
    83
         * Element name, found in the "element-list" file in the {@link path}.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
         */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
    85
        final String elementName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
    88
         * The URL or the directory path at which the element documentation will be
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
         * avaliable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
         */
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
    91
        final DocPath path;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
         * If given path is directory path then true else if it is a URL then false.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
        final boolean relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
    99
         * Constructor to build a Extern Item object and map it with the element name.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   100
         * If the same element name is found in the map, then the first mapped
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
         * Item object or offline location will be retained.
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
         *
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   103
         * @param elementName Element name found in the "element-list" file.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   104
         * @param path        URL or Directory path from where the "element-list"
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
         * file is picked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
         * @param relative    True if path is URL, false if directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
         */
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   108
        Item(String elementName, DocPath path, boolean relative) {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   109
            this.elementName = elementName;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            this.relative = relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   115
         * String representation of "this" with elementname and the path.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
         */
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   117
        @Override
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        public String toString() {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   119
            return elementName + (relative? " -> " : " => ") + path.getPath();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 40587
diff changeset
   123
    public Extern(BaseConfiguration configuration) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    /**
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   128
     * Determine if a element item is externally documented.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   130
     * @param element an Element.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   131
     * @return true if the element is externally documented
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   133
    public boolean isExternal(Element element) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   134
        if (packageItems.isEmpty()) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   137
        PackageElement pe = configuration.utils.containingPackage(element);
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   138
        if (pe.isUnnamed()) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   139
            return false;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   140
        }
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   141
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   142
        return findElementItem(pe) != null;
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   143
    }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   144
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   145
    /**
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   146
     * Determine if a element item is a module or not.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   147
     *
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   148
     * @param elementName name of the element.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   149
     * @return true if the element is a module
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   150
     */
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   151
    public boolean isModule(String elementName) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   152
        Item elem = moduleItems.get(elementName);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   153
        return (elem == null) ? false : true;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * Convert a link to be an external link if appropriate.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     *
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   159
     * @param element The element .
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     * @param relativepath    The relative path.
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   161
     * @param filename    The link to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * @return if external return converted link else return null
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     */
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   164
    public DocLink getExternalLink(Element element, DocPath relativepath, String filename) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   165
        return getExternalLink(element, relativepath, filename, null);
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   166
    }
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   167
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   168
    public DocLink getExternalLink(Element element, DocPath relativepath, String filename,
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   169
            String memberName) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   170
        Item fnd = findElementItem(element);
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   171
        if (fnd == null)
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   172
            return null;
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   173
48654
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48295
diff changeset
   174
        // The following is somewhat questionable since we are using DocPath
36f58bd6269f 8195796: Reduce the size of relative URLs in generated docs
jjg
parents: 48295
diff changeset
   175
        // to contain external URLs!
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   176
        DocPath p = fnd.relative ?
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   177
                relativepath.resolve(fnd.path).resolve(filename) :
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   178
                fnd.path.resolve(filename);
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   179
        return new DocLink(p, "is-external=true", memberName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   183
     * Build the extern element list from given URL or the directory path,
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   184
     * as specified with the "-link" flag.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   185
     * Flag error if the "-link" or "-linkoffline" option is already used.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   186
     *
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   187
     * @param url        URL or Directory path.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   188
     * @param reporter   The <code>DocErrorReporter</code> used to report errors.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   189
     * @return true if successful, false otherwise
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   190
     * @throws DocFileIOException if there is a problem reading a element list file
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   191
     */
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   192
    public boolean link(String url, Reporter reporter) throws DocFileIOException {
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   193
        return link(url, url, reporter, false);
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   194
    }
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   195
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   196
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   197
     * Build the extern element list from given URL or the directory path,
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   198
     * as specified with the "-linkoffline" flag.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   199
     * Flag error if the "-link" or "-linkoffline" option is already used.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   200
     *
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   201
     * @param url        URL or Directory path.
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   202
     * @param elemlisturl This can be another URL for "element-list" or ordinary
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   203
     *                   file.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   204
     * @param reporter   The <code>DocErrorReporter</code> used to report errors.
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   205
     * @return true if successful, false otherwise
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   206
     * @throws DocFileIOException if there is a problem reading the element list file
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   207
     */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   208
    public boolean link(String url, String elemlisturl, Reporter reporter) throws DocFileIOException {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   209
        return link(url, elemlisturl, reporter, true);
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   210
    }
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   211
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   212
    /*
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   213
     * Build the extern element list from given URL or the directory path.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     * Flag error if the "-link" or "-linkoffline" option is already used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     * @param url        URL or Directory path.
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   217
     * @param elemlisturl This can be another URL for "element-list" or ordinary
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     *                   file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
     * @param reporter   The <code>DocErrorReporter</code> used to report errors.
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   220
     * @param linkoffline True if -linkoffline is used and false if -link is used.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   221
     * @return true if successful, false otherwise
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   222
     * @throws DocFileIOException if there is a problem reading the element list file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   224
    private boolean link(String url, String elemlisturl, Reporter reporter, boolean linkoffline)
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   225
                throws DocFileIOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
        this.linkoffline = linkoffline;
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   227
        try {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   228
            url = adjustEndFileSeparator(url);
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   229
            if (isUrl(elemlisturl)) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   230
                readElementListFromURL(url, toURL(adjustEndFileSeparator(elemlisturl)));
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   231
            } else {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   232
                readElementListFromFile(url, DocFile.createFileForInput(configuration, elemlisturl));
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   233
            }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   234
            return true;
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   235
        } catch (Fault f) {
47877
7e57d42b5eeb 8180019: javadoc should treat failure to access URL as an error, not a warning.
ksrini
parents: 47216
diff changeset
   236
            reporter.print(Diagnostic.Kind.ERROR, f.getMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
            return false;
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   238
        }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   239
    }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   240
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   241
    private URL toURL(String url) throws Fault {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   242
        try {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   243
            return new URL(url);
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   244
        } catch (MalformedURLException e) {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   245
            throw new Fault(configuration.getText("doclet.MalformedURL", url), e);
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   246
        }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   247
    }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   248
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   249
    private class Fault extends Exception {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   250
        private static final long serialVersionUID = 0;
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   251
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   252
        Fault(String msg, Exception cause) {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   253
            super(msg, cause);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   258
     * Get the Extern Item object associated with this element name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
     *
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   260
     * @param element Element
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
     */
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   262
    private Item findElementItem(Element element) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   263
        Item item = null;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   264
        if (element instanceof ModuleElement) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   265
            item = moduleItems.get(configuration.utils.getModuleName((ModuleElement)element));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
        }
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   267
        else if (element instanceof PackageElement) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   268
            PackageElement packageElement = (PackageElement)element;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   269
            ModuleElement moduleElement = configuration.utils.containingModule(packageElement);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   270
            Map<String, Item> pkgMap = packageItems.get(configuration.utils.getModuleName(moduleElement));
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   271
            item = (pkgMap != null) ? pkgMap.get(configuration.utils.getPackageName(packageElement)) : null;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   272
        }
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   273
        return item;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     * If the URL or Directory path is missing end file separator, add that.
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
    private String adjustEndFileSeparator(String url) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   280
        return url.endsWith("/") ? url : url + '/';
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   284
     * Fetch the URL and read the "element-list" file.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   285
     *
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   286
     * @param urlpath        Path to the elements.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   287
     * @param elemlisturlpath URL or the path to the "element-list" file.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   288
     */
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   289
    private void readElementListFromURL(String urlpath, URL elemlisturlpath) throws Fault {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   290
        try {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   291
            URL link = elemlisturlpath.toURI().resolve(DocPaths.ELEMENT_LIST.getPath()).toURL();
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   292
            readElementList(link.openStream(), urlpath, false);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   293
        } catch (URISyntaxException | MalformedURLException exc) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   294
            throw new Fault(configuration.getText("doclet.MalformedURL", elemlisturlpath.toString()), exc);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   295
        } catch (IOException exc) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   296
            readAlternateURL(urlpath, elemlisturlpath);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   297
        }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   298
    }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   299
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   300
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     * Fetch the URL and read the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
     * @param urlpath        Path to the packages.
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   304
     * @param elemlisturlpath URL or the path to the "package-list" file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
     */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   306
    private void readAlternateURL(String urlpath, URL elemlisturlpath) throws Fault {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        try {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   308
            URL link = elemlisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL();
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   309
            readElementList(link.openStream(), urlpath, false);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14545
diff changeset
   310
        } catch (URISyntaxException | MalformedURLException exc) {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   311
            throw new Fault(configuration.getText("doclet.MalformedURL", elemlisturlpath.toString()), exc);
46079
059faa5e1267 8184969: Cannot specify multiple -link to jdk9 javadoc
ksrini
parents: 45417
diff changeset
   312
        } catch (IOException exc) {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   313
            throw new Fault(configuration.getText("doclet.URL_error", elemlisturlpath.toString()), exc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   318
     * Read the "element-list" file which is available locally.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     *
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   320
     * @param path URL or directory path to the elements.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   321
     * @param elemListPath Path to the local "element-list" file.
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   322
     * @throws Fault if an error occurs that can be treated as a warning
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   323
     * @throws DocFileIOException if there is a problem opening the element list file
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
     */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   325
    private void readElementListFromFile(String path, DocFile elemListPath)
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   326
            throws Fault, DocFileIOException {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   327
        DocFile file = elemListPath.resolve(DocPaths.ELEMENT_LIST);
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   328
        if (! (file.isAbsolute() || linkoffline)){
14545
2e7bab0639b8 6493690: javadoc should have a javax.tools.Tool service provider installed in tools.jar
jjg
parents: 14368
diff changeset
   329
            file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        }
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   331
        if (file.exists()) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   332
            readElementList(file, path);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   333
        } else {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   334
            DocFile file1 = elemListPath.resolve(DocPaths.PACKAGE_LIST);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   335
            if (!(file1.isAbsolute() || linkoffline)) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   336
                file1 = file1.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   337
            }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   338
            if (file1.exists()) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   339
                readElementList(file1, path);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   340
            } else {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   341
                throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   342
            }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   343
        }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   344
    }
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   345
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   346
    private void readElementList(DocFile file, String path) throws Fault, DocFileIOException {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   347
        try {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   348
            if (file.canRead()) {
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   349
                boolean pathIsRelative
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   350
                        = !isUrl(path)
36040
a664cd8faf9c 8149903: Fix other Extern.
jjg
parents: 35426
diff changeset
   351
                        && !DocFile.createFileForInput(configuration, path).isAbsolute();
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   352
                readElementList(file.openInputStream(), path, pathIsRelative);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
            } else {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   354
                throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
        } catch (IOException exc) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   357
           throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   358
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   359
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   360
06bc494ca11e Initial load
duke
parents:
diff changeset
   361
    /**
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   362
     * Read the file "element-list" and for each element name found, create
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   363
     * Extern object and associate it with the element name in the map.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   364
     *
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   365
     * @param input     InputStream from the "element-list" file.
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   366
     * @param path     URL or the directory path to the elements.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   367
     * @param relative Is path relative?
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   368
     * @throws IOException if there is a problem reading or closing the stream
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   369
     */
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   370
    private void readElementList(InputStream input, String path, boolean relative)
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   371
                         throws Fault, IOException {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   372
        try (BufferedReader in = new BufferedReader(new InputStreamReader(input))) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   373
            String elemname = null;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   374
            String moduleName = null;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   375
            DocPath elempath = null;
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   376
            DocPath basePath  = DocPath.create(path);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   377
            while ((elemname = in.readLine()) != null) {
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   378
                if (elemname.length() > 0) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   379
                    elempath = basePath;
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   380
                    if (elemname.startsWith(DocletConstants.MODULE_PREFIX)) {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   381
                        moduleName = elemname.replace(DocletConstants.MODULE_PREFIX, "");
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   382
                        Item item = new Item(moduleName, elempath, relative);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   383
                        moduleItems.put(moduleName, item);
48295
c79d31ba84b9 8164407: Add module support for -link and -linkoffline javadoc option
bpatel
parents: 47877
diff changeset
   384
                    } else {
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   385
                        DocPath pkgPath = DocPath.create(elemname.replace('.', '/'));
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   386
                        if (configuration.useModuleDirectories && moduleName != null) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   387
                            elempath = elempath.resolve(DocPath.create(moduleName).resolve(pkgPath));
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   388
                        } else {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   389
                            elempath = elempath.resolve(pkgPath);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   390
                        }
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   391
                        checkLinkCompatibility(elemname, moduleName, path);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   392
                        Item item = new Item(elemname, elempath, relative);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   393
                        packageItems.computeIfAbsent(moduleName == null ?
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   394
                            DocletConstants.DEFAULT_ELEMENT_NAME : moduleName, k -> new TreeMap<>())
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   395
                            .put(elemname, item);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   396
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   397
                }
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   398
            }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   399
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   400
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   401
06bc494ca11e Initial load
duke
parents:
diff changeset
   402
    public boolean isUrl (String urlCandidate) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   403
        try {
40587
1c355ea550ed 8164130: Simplify doclet IOException handling
jjg
parents: 36040
diff changeset
   404
            URL ignore = new URL(urlCandidate);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   405
            //No exception was thrown, so this must really be a URL.
06bc494ca11e Initial load
duke
parents:
diff changeset
   406
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   407
        } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   408
            //Since exception is thrown, this must be a directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   409
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   410
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   411
    }
51861
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   412
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   413
    private void checkLinkCompatibility(String packageName, String moduleName, String path) throws Fault {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   414
        PackageElement pe = configuration.utils.elementUtils.getPackageElement(packageName);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   415
        if (pe != null) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   416
            ModuleElement me = (ModuleElement)pe.getEnclosingElement();
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   417
            if (me == null || me.isUnnamed()) {
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   418
                if (moduleName != null)
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   419
                    throw new Fault(configuration.getText("doclet.linkMismatch_PackagedLinkedtoModule",
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   420
                            path), null);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   421
            } else if (moduleName == null)
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   422
                throw new Fault(configuration.getText("doclet.linkMismatch_ModuleLinkedtoPackage",
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   423
                        path), null);
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   424
        }
f7d40158eb2f 8205593: Javadoc -link makes broken links if module name matches package name
pmuthuswamy
parents: 48654
diff changeset
   425
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   426
}