langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java
author jjg
Mon, 15 Feb 2016 22:21:39 -0800
changeset 36040 a664cd8faf9c
parent 35426 374342e56a56
child 40587 1c355ea550ed
permissions -rw-r--r--
8149903: Fix other Extern. Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
     2
 * Copyright (c) 1998, 2016, 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;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    33
import javax.lang.model.element.Element;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    34
import javax.lang.model.element.PackageElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    35
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
    36
import javax.tools.DocumentationTool;
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
    37
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    38
import jdk.javadoc.doclet.Reporter;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    39
import jdk.javadoc.internal.doclets.toolkit.Configuration;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * Process and manage "-link" and "-linkoffline" to external packages. The
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * generates "package-list"(lists all the packages which are getting
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * documented) file in the current or the destination directory, while
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * generating the documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    48
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14259
diff changeset
    49
 *  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
    50
 *  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
    51
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
public class Extern {
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * Map package names onto Extern Item objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     * Lazily initialized.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    62
    private Map<String, Item> packageToItemMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
    private final Configuration configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     * True if we are using -linkoffline and false if -link is used instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
    private boolean linkoffline = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
     * Stores the info for one external doc set
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
    private class Item {
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
         * Package name, found in the "package-list" file in the {@link path}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        final String packageName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
         * The URL or the directory path at which the package documentation will be
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
         * avaliable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
        final String path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
         * If given path is directory path then true else if it is a URL then false.
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
        final boolean relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
         * Constructor to build a Extern Item object and map it with the package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
         * If the same package name is found in the map, then the first mapped
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
         * Item object or offline location will be retained.
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
         *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   100
         * @param packageName Package name found in the "package-list" file.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
         * @param path        URL or Directory path from where the "package-list"
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
         * file is picked.
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
         * @param relative    True if path is URL, false if directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        Item(String packageName, String path, boolean relative) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            this.packageName = packageName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            this.relative = relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
            if (packageToItemMap == null) {
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
   110
                packageToItemMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
            if (!packageToItemMap.containsKey(packageName)) { // save the previous
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
                packageToItemMap.put(packageName, this);        // mapped location
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
         * String representation of "this" with packagename and the path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            return packageName + (relative? " -> " : " => ") + path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
    public Extern(Configuration configuration) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /**
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   130
     * Determine if a element item is externally documented.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   132
     * @param element an Element.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   134
    public boolean isExternal(Element element) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
        if (packageToItemMap == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   138
        PackageElement pe = configuration.utils.containingPackage(element);
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   139
        if (pe.isUnnamed()) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   140
            return false;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   141
        }
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   142
        return packageToItemMap.get(configuration.utils.getPackageName(pe)) != null;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
     * Convert a link to be an external link if appropriate.
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     * @param pkgName The package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     * @param relativepath    The relative path.
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   150
     * @param filename    The link to convert.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     * @return if external return converted link else return null
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   153
    public DocLink getExternalLink(String pkgName, DocPath relativepath, String filename) {
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   154
        return getExternalLink(pkgName, relativepath, filename, null);
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   155
    }
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   156
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   157
    public DocLink getExternalLink(String pkgName, DocPath relativepath, String filename,
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   158
            String memberName) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
        Item fnd = findPackageItem(pkgName);
14358
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   160
        if (fnd == null)
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   161
            return null;
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   162
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   163
        DocPath p = fnd.relative ?
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   164
                relativepath.resolve(fnd.path).resolve(filename) :
9eda9239cba0 8000416: refactor javadoc to provide and use an abstraction for relative URIs
jjg
parents: 14357
diff changeset
   165
                DocPath.create(fnd.path).resolve(filename);
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
        return new DocLink(p, "is-external=true", memberName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
     * Build the extern package list from given URL or the directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
     * Flag error if the "-link" or "-linkoffline" option is already used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
     * @param url        URL or Directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     * @param pkglisturl This can be another URL for "package-list" or ordinary
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     *                   file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     * @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
   178
     * @param linkoffline True if -linkoffline is used and false if -link is used.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   180
    public boolean link(String url, String pkglisturl, Reporter reporter, boolean linkoffline) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
        this.linkoffline = linkoffline;
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   182
        try {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   183
            url = adjustEndFileSeparator(url);
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   184
            if (isUrl(pkglisturl)) {
23974
d53628eda3d1 8038976: javadoc requires a trailing / for links where java 7's javadoc didn't
ksrini
parents: 22163
diff changeset
   185
                readPackageListFromURL(url, toURL(adjustEndFileSeparator(pkglisturl)));
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   186
            } else {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
   187
                readPackageListFromFile(url, DocFile.createFileForInput(configuration, pkglisturl));
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   188
            }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   189
            return true;
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   190
        } catch (Fault f) {
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   191
            reporter.print(Diagnostic.Kind.WARNING, f.getMessage());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            return false;
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   193
        }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   194
    }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   195
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   196
    private URL toURL(String url) throws Fault {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   197
        try {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   198
            return new URL(url);
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   199
        } catch (MalformedURLException e) {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   200
            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
   201
        }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   202
    }
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   203
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   204
    private class Fault extends Exception {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   205
        private static final long serialVersionUID = 0;
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   206
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   207
        Fault(String msg, Exception cause) {
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   208
            super(msg, cause);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
     * Get the Extern Item object associated with this package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     *
14259
fb94a1df0d53 8000208: fix langtools javadoc comment issues
jjg
parents: 14258
diff changeset
   215
     * @param pkgName Package name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    private Item findPackageItem(String pkgName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
        if (packageToItemMap == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        }
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   221
        return packageToItemMap.get(pkgName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
     * If the URL or Directory path is missing end file separator, add that.
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    private String adjustEndFileSeparator(String url) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   228
        return url.endsWith("/") ? url : url + '/';
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
     * Fetch the URL and read the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     * @param urlpath        Path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     * @param pkglisturlpath URL or the path to the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   237
    private void readPackageListFromURL(String urlpath, URL pkglisturlpath) throws Fault {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        try {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   239
            URL link = pkglisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL();
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   240
            readPackageList(link.openStream(), urlpath, false);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14545
diff changeset
   241
        } catch (URISyntaxException | MalformedURLException exc) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   242
            throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc);
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14545
diff changeset
   243
        }
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14545
diff changeset
   244
        catch (IOException exc) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   245
            throw new Fault(configuration.getText("doclet.URL_error", pkglisturlpath.toString()), exc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     * Read the "package-list" file which is available locally.
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
     * @param path URL or directory path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
     * @param pkgListPath Path to the local "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
     */
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
   255
    private void readPackageListFromFile(String path, DocFile pkgListPath)
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   256
            throws Fault {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
   257
        DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST);
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   258
        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
   259
            file = file.resolveAgainst(DocumentationTool.Location.DOCUMENTATION_OUTPUT);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            if (file.exists() && file.canRead()) {
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
   263
                boolean pathIsRelative =
36040
a664cd8faf9c 8149903: Fix other Extern.
jjg
parents: 35426
diff changeset
   264
                        !isUrl(path)
a664cd8faf9c 8149903: Fix other Extern.
jjg
parents: 35426
diff changeset
   265
                        && !DocFile.createFileForInput(configuration, path).isAbsolute();
14368
6f4c62de6985 8001664: refactor javadoc to use abstraction to handle files
jjg
parents: 14361
diff changeset
   266
                readPackageList(file.openInputStream(), path, pathIsRelative);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
            } else {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   268
                throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        } catch (IOException exc) {
14361
2814fa6977ac 8001219: Clean up use of URLs in javadoc Extern class
jjg
parents: 14358
diff changeset
   271
           throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     * Read the file "package-list" and for each package name found, create
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     * Extern object and associate it with the package name in the map.
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * @param input    InputStream from the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
     * @param path     URL or the directory path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     * @param relative Is path relative?
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   283
    private void readPackageList(InputStream input, String path, boolean relative)
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                         throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 14260
diff changeset
   286
        StringBuilder strbuf = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
            int c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
            while ((c = in.read()) >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                char ch = (char)c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                if (ch == '\n' || ch == '\r') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                    if (strbuf.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                        String packname = strbuf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                        String packpath = path +
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                                      packname.replace('.', '/') + '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                        new Item(packname, packpath, relative);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
                        strbuf.setLength(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
                    strbuf.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            input.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    public boolean isUrl (String urlCandidate) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
            new URL(urlCandidate);
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
            //No exception was thrown, so this must really be a URL.
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
            //Since exception is thrown, this must be a directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
}