langtools/src/share/classes/com/sun/tools/doclets/internal/toolkit/util/Extern.java
author jjg
Tue, 15 Jul 2008 19:22:51 -0700
changeset 868 d0f233085cbb
parent 10 06bc494ca11e
child 1264 076a3cde30d5
permissions -rw-r--r--
6657907: javadoc has unchecked warnings Reviewed-by: bpatel
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
06bc494ca11e Initial load
duke
parents:
diff changeset
     2
 * Copyright 1998-2003 Sun Microsystems, Inc.  All Rights Reserved.
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
06bc494ca11e Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
06bc494ca11e Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
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
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
06bc494ca11e Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
06bc494ca11e Initial load
duke
parents:
diff changeset
    23
 * have any questions.
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.doclets.internal.toolkit.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
import com.sun.tools.doclets.internal.toolkit.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
import com.sun.javadoc.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    31
import java.util.Map;
06bc494ca11e Initial load
duke
parents:
diff changeset
    32
import java.util.HashMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
import java.io.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
import java.net.*;
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
/**
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
 * Process and manage "-link" and "-linkoffline" to external packages. The
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
 * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * generates "package-list"(lists all the packages which are getting
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * documented) file in the current or the destination directory, while
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * generating the documentation.
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * This code is not part of an API.
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * It is implementation that is subject to change.
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * Do not use it as an API
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * @author Robert Field
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
public class Extern {
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
     * Map package names onto Extern Item objects.
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
     * Lazily initialized.
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    56
    private Map<String,Item> packageToItemMap;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
    private final Configuration configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
     * True if we are using -linkoffline and false if -link is used instead.
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
    private boolean linkoffline = false;
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
     * Stores the info for one external doc set
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
    private class Item {
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
         * Package name, found in the "package-list" file in the {@link path}.
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
        final String packageName;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
         * The URL or the directory path at which the package documentation will be
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
         * avaliable.
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        final String path;
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
         * If given path is directory path then true else if it is a URL then false.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
        final boolean relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
         * Constructor to build a Extern Item object and map it with the package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
         * If the same package name is found in the map, then the first mapped
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
         * Item object or offline location will be retained.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
         *
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
         * @param packagename Package name found in the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
         * @param path        URL or Directory path from where the "package-list"
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
         * file is picked.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
         * @param relative    True if path is URL, false if directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        Item(String packageName, String path, boolean relative) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
            this.packageName = packageName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
            this.path = path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            this.relative = relative;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            if (packageToItemMap == null) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   104
                packageToItemMap = new HashMap<String,Item>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
            if (!packageToItemMap.containsKey(packageName)) { // save the previous
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
                packageToItemMap.put(packageName, this);        // mapped location
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
        /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
         * String representation of "this" with packagename and the path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
         */
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
        public String toString() {
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
            return packageName + (relative? " -> " : " => ") + path;
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
    public Extern(Configuration configuration) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
        this.configuration = configuration;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
     * Determine if a doc item is externally documented.
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
     * @param doc A ProgramElementDoc.
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
    public boolean isExternal(ProgramElementDoc doc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
        if (packageToItemMap == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
        return packageToItemMap.get(doc.containingPackage().name()) != null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     * Convert a link to be an external link if appropriate.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     * @param pkgName The package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     * @param relativepath    The relative path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     * @param link    The link to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     * @return if external return converted link else return null
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
    public String getExternalLink(String pkgName,
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
                                  String relativepath, String link) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        Item fnd = findPackageItem(pkgName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
        if (fnd != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
            String externlink = fnd.path + link;
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
            if (fnd.relative) {  // it's a relative path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
                return relativepath + externlink;
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
                return externlink;
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * Build the extern package list from given URL or the directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * Flag error if the "-link" or "-linkoffline" option is already used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * @param url        URL or Directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * @param pkglisturl This can be another URL for "package-list" or ordinary
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     *                   file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * @param reporter   The <code>DocErrorReporter</code> used to report errors.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     * @param linkoffline True if -linkoffline isused and false if -link is used.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    public boolean url(String url, String pkglisturl,
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                              DocErrorReporter reporter, boolean linkoffline) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
        this.linkoffline = linkoffline;
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        String errMsg = composeExternPackageList(url, pkglisturl);
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        if (errMsg != null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
            reporter.printWarning(errMsg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     * Get the Extern Item object associated with this package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     * @param pkgname Package name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
    private Item findPackageItem(String pkgName) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        if (packageToItemMap == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
            return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
        }
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   188
        return packageToItemMap.get(pkgName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     * Adjusts the end file separator if it is missing from the URL or the
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
     * directory path and depending upon the URL or file path, fetch or
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
     * read the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
     * @param urlOrDirPath        URL or the directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
     * @param pkgListUrlOrDirPath URL or directory path for the "package-list" file or the "package-list"
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
     * file itself.
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
    private String composeExternPackageList(String urlOrDirPath, String pkgListUrlOrDirPath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
        urlOrDirPath = adjustEndFileSeparator(urlOrDirPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        pkgListUrlOrDirPath = adjustEndFileSeparator(pkgListUrlOrDirPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return isUrl(pkgListUrlOrDirPath) ?
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
            fetchURLComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath) :
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
            readFileComposeExternPackageList(urlOrDirPath, pkgListUrlOrDirPath);
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     * If the URL or Directory path is missing end file separator, add that.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    private String adjustEndFileSeparator(String url) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        String filesep = "/";
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        if (!url.endsWith(filesep)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
            url += filesep;
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        return url;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     * Fetch the URL and read the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     * @param urlpath        Path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
     * @param pkglisturlpath URL or the path to the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    private String fetchURLComposeExternPackageList(String urlpath,
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
                                                   String pkglisturlpath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
        String link = pkglisturlpath + "package-list";
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
            readPackageList((new URL(link)).openStream(), urlpath, false);
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        } catch (MalformedURLException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
            return configuration.getText("doclet.MalformedURL", link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        } catch (IOException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
            return configuration.getText("doclet.URL_error", link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
     * Read the "package-list" file which is available locally.
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
     * @param path URL or directory path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
     * @param pkgListPath Path to the local "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
    private String readFileComposeExternPackageList(String path,
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
                                                   String pkgListPath) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
        String link = pkgListPath + "package-list";
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
        if (! ((new File(pkgListPath)).isAbsolute() || linkoffline)){
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
            link = configuration.destDirName + link;
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
            File file = new File(link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
            if (file.exists() && file.canRead()) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
                readPackageList(new FileInputStream(file), path,
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
                    ! ((new File(path)).isAbsolute() || isUrl(path)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
                return configuration.getText("doclet.File_error", link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        } catch (FileNotFoundException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
            return configuration.getText("doclet.File_error", link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
        } catch (IOException exc) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            return configuration.getText("doclet.File_error", link);
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     * Read the file "package-list" and for each package name found, create
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * Extern object and associate it with the package name in the map.
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * @param input    InputStream from the "package-list" file.
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * @param path     URL or the directory path to the packages.
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * @param relative Is path relative?
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    private void readPackageList(InputStream input, String path,
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                                boolean relative)
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                         throws IOException {
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        BufferedReader in = new BufferedReader(new InputStreamReader(input));
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
        StringBuffer strbuf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            int c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
            while ((c = in.read()) >= 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                char ch = (char)c;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                if (ch == '\n' || ch == '\r') {
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                    if (strbuf.length() > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                        String packname = strbuf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                        String packpath = path +
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                                      packname.replace('.', '/') + '/';
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                        new Item(packname, packpath, relative);
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                        strbuf.setLength(0);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    strbuf.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        } finally {
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            input.close();
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
    public boolean isUrl (String urlCandidate) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
        try {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
            new URL(urlCandidate);
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            //No exception was thrown, so this must really be a URL.
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        } catch (MalformedURLException e) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
            //Since exception is thrown, this must be a directory path.
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
}