src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Extern.java

Print this page


   1 /*
   2  * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.tools.doclets.internal.toolkit.util;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 



  33 import javax.tools.DocumentationTool;
  34 
  35 import com.sun.javadoc.*;
  36 import com.sun.tools.doclets.internal.toolkit.*;
  37 
  38 /**
  39  * Process and manage "-link" and "-linkoffline" to external packages. The
  40  * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
  41  * generates "package-list"(lists all the packages which are getting
  42  * documented) file in the current or the destination directory, while
  43  * generating the documentation.
  44  *
  45  *  <p><b>This is NOT part of any supported API.
  46  *  If you write code that depends on this, you do so at your own risk.
  47  *  This code and its internal interfaces are subject to change or
  48  *  deletion without notice.</b>
  49  *
  50  * @author Atul M Dambalkar
  51  * @author Robert Field
  52  */
  53 public class Extern {
  54 
  55     /**
  56      * Map package names onto Extern Item objects.
  57      * Lazily initialized.
  58      */
  59     private Map<String,Item> packageToItemMap;
  60 
  61     /**
  62      * The global configuration information for this run.
  63      */
  64     private final Configuration configuration;
  65 
  66     /**
  67      * True if we are using -linkoffline and false if -link is used instead.
  68      */
  69     private boolean linkoffline = false;
  70 
  71     /**
  72      * Stores the info for one external doc set
  73      */
  74     private class Item {
  75 
  76         /**
  77          * Package name, found in the "package-list" file in the {@link path}.
  78          */
  79         final String packageName;


 107                 packageToItemMap = new HashMap<>();
 108             }
 109             if (!packageToItemMap.containsKey(packageName)) { // save the previous
 110                 packageToItemMap.put(packageName, this);        // mapped location
 111             }
 112         }
 113 
 114         /**
 115          * String representation of "this" with packagename and the path.
 116          */
 117         public String toString() {
 118             return packageName + (relative? " -> " : " => ") + path;
 119         }
 120     }
 121 
 122     public Extern(Configuration configuration) {
 123         this.configuration = configuration;
 124     }
 125 
 126     /**
 127      * Determine if a doc item is externally documented.
 128      *
 129      * @param doc A ProgramElementDoc.
 130      */
 131     public boolean isExternal(ProgramElementDoc doc) {
 132         if (packageToItemMap == null) {
 133             return false;
 134         }
 135         return packageToItemMap.get(doc.containingPackage().name()) != null;


 136     }


 137 
 138     /**
 139      * Convert a link to be an external link if appropriate.
 140      *
 141      * @param pkgName The package name.
 142      * @param relativepath    The relative path.
 143      * @param filename    The link to convert.
 144      * @return if external return converted link else return null
 145      */
 146     public DocLink getExternalLink(String pkgName,
 147                                   DocPath relativepath, String filename) {
 148         return getExternalLink(pkgName, relativepath, filename, null);
 149     }
 150 
 151     public DocLink getExternalLink(String pkgName,
 152                                   DocPath relativepath, String filename, String memberName) {
 153         Item fnd = findPackageItem(pkgName);
 154         if (fnd == null)
 155             return null;
 156 
 157         DocPath p = fnd.relative ?
 158                 relativepath.resolve(fnd.path).resolve(filename) :
 159                 DocPath.create(fnd.path).resolve(filename);
 160 
 161         return new DocLink(p, "is-external=true", memberName);
 162     }
 163 
 164     /**
 165      * Build the extern package list from given URL or the directory path.
 166      * Flag error if the "-link" or "-linkoffline" option is already used.
 167      *
 168      * @param url        URL or Directory path.
 169      * @param pkglisturl This can be another URL for "package-list" or ordinary
 170      *                   file.
 171      * @param reporter   The <code>DocErrorReporter</code> used to report errors.
 172      * @param linkoffline True if -linkoffline is used and false if -link is used.
 173      */
 174     public boolean link(String url, String pkglisturl,
 175                               DocErrorReporter reporter, boolean linkoffline) {
 176         this.linkoffline = linkoffline;
 177         try {
 178             url = adjustEndFileSeparator(url);
 179             if (isUrl(pkglisturl)) {
 180                 readPackageListFromURL(url, toURL(adjustEndFileSeparator(pkglisturl)));
 181             } else {
 182                 readPackageListFromFile(url, DocFile.createFileForInput(configuration, pkglisturl));
 183             }
 184             return true;
 185         } catch (Fault f) {
 186             reporter.printWarning(f.getMessage());
 187             return false;
 188         }
 189     }
 190 
 191     private URL toURL(String url) throws Fault {
 192         try {
 193             return new URL(url);
 194         } catch (MalformedURLException e) {
 195             throw new Fault(configuration.getText("doclet.MalformedURL", url), e);
 196         }
 197     }
 198 
 199     private class Fault extends Exception {
 200         private static final long serialVersionUID = 0;
 201 
 202         Fault(String msg, Exception cause) {
 203             super(msg, cause);
 204         }
 205     }
 206 


 212     private Item findPackageItem(String pkgName) {
 213         if (packageToItemMap == null) {
 214             return null;
 215         }
 216         return packageToItemMap.get(pkgName);
 217     }
 218 
 219     /**
 220      * If the URL or Directory path is missing end file separator, add that.
 221      */
 222     private String adjustEndFileSeparator(String url) {
 223         return url.endsWith("/") ? url : url + '/';
 224     }
 225 
 226     /**
 227      * Fetch the URL and read the "package-list" file.
 228      *
 229      * @param urlpath        Path to the packages.
 230      * @param pkglisturlpath URL or the path to the "package-list" file.
 231      */
 232     private void readPackageListFromURL(String urlpath, URL pkglisturlpath)
 233             throws Fault {
 234         try {
 235             URL link = pkglisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL();
 236             readPackageList(link.openStream(), urlpath, false);
 237         } catch (URISyntaxException | MalformedURLException exc) {
 238             throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc);
 239         }
 240         catch (IOException exc) {
 241             throw new Fault(configuration.getText("doclet.URL_error", pkglisturlpath.toString()), exc);
 242         }
 243     }
 244 
 245     /**
 246      * Read the "package-list" file which is available locally.
 247      *
 248      * @param path URL or directory path to the packages.
 249      * @param pkgListPath Path to the local "package-list" file.
 250      */
 251     private void readPackageListFromFile(String path, DocFile pkgListPath)
 252             throws Fault {
 253         DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST);


 259                 boolean pathIsRelative =
 260                         !DocFile.createFileForInput(configuration, path).isAbsolute()
 261                         && !isUrl(path);
 262                 readPackageList(file.openInputStream(), path, pathIsRelative);
 263             } else {
 264                 throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
 265             }
 266         } catch (IOException exc) {
 267            throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc);
 268         }
 269     }
 270 
 271     /**
 272      * Read the file "package-list" and for each package name found, create
 273      * Extern object and associate it with the package name in the map.
 274      *
 275      * @param input    InputStream from the "package-list" file.
 276      * @param path     URL or the directory path to the packages.
 277      * @param relative Is path relative?
 278      */
 279     private void readPackageList(InputStream input, String path,
 280                                 boolean relative)
 281                          throws IOException {
 282         BufferedReader in = new BufferedReader(new InputStreamReader(input));
 283         StringBuilder strbuf = new StringBuilder();
 284         try {
 285             int c;
 286             while ((c = in.read()) >= 0) {
 287                 char ch = (char)c;
 288                 if (ch == '\n' || ch == '\r') {
 289                     if (strbuf.length() > 0) {
 290                         String packname = strbuf.toString();
 291                         String packpath = path +
 292                                       packname.replace('.', '/') + '/';
 293                         new Item(packname, packpath, relative);
 294                         strbuf.setLength(0);
 295                     }
 296                 } else {
 297                     strbuf.append(ch);
 298                 }
 299             }
 300         } finally {
   1 /*
   2  * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package jdk.javadoc.internal.doclets.toolkit.util;
  27 
  28 import java.io.*;
  29 import java.net.*;
  30 import java.util.HashMap;
  31 import java.util.Map;
  32 
  33 import javax.lang.model.element.Element;
  34 import javax.lang.model.element.PackageElement;
  35 import javax.tools.Diagnostic;
  36 import javax.tools.DocumentationTool;
  37 
  38 import jdk.javadoc.doclet.Reporter;
  39 import jdk.javadoc.internal.doclets.toolkit.Configuration;
  40 
  41 /**
  42  * Process and manage "-link" and "-linkoffline" to external packages. The
  43  * options "-link" and "-linkoffline" both depend on the fact that Javadoc now
  44  * generates "package-list"(lists all the packages which are getting
  45  * documented) file in the current or the destination directory, while
  46  * generating the documentation.
  47  *
  48  *  <p><b>This is NOT part of any supported API.
  49  *  If you write code that depends on this, you do so at your own risk.
  50  *  This code and its internal interfaces are subject to change or
  51  *  deletion without notice.</b>
  52  *
  53  * @author Atul M Dambalkar
  54  * @author Robert Field
  55  */
  56 public class Extern {
  57 
  58     /**
  59      * Map package names onto Extern Item objects.
  60      * Lazily initialized.
  61      */
  62     private Map<String, Item> packageToItemMap;
  63 
  64     /**
  65      * The global configuration information for this run.
  66      */
  67     private final Configuration configuration;
  68 
  69     /**
  70      * True if we are using -linkoffline and false if -link is used instead.
  71      */
  72     private boolean linkoffline = false;
  73 
  74     /**
  75      * Stores the info for one external doc set
  76      */
  77     private class Item {
  78 
  79         /**
  80          * Package name, found in the "package-list" file in the {@link path}.
  81          */
  82         final String packageName;


 110                 packageToItemMap = new HashMap<>();
 111             }
 112             if (!packageToItemMap.containsKey(packageName)) { // save the previous
 113                 packageToItemMap.put(packageName, this);        // mapped location
 114             }
 115         }
 116 
 117         /**
 118          * String representation of "this" with packagename and the path.
 119          */
 120         public String toString() {
 121             return packageName + (relative? " -> " : " => ") + path;
 122         }
 123     }
 124 
 125     public Extern(Configuration configuration) {
 126         this.configuration = configuration;
 127     }
 128 
 129     /**
 130      * Determine if a element item is externally documented.
 131      *
 132      * @param element an Element.
 133      */
 134     public boolean isExternal(Element element) {
 135         if (packageToItemMap == null) {
 136             return false;
 137         }
 138         PackageElement pe = configuration.utils.containingPackage(element);
 139         if (pe.isUnnamed()) {
 140             return false;
 141         }
 142         return packageToItemMap.get(configuration.utils.getPackageName(pe)) != null;
 143     }
 144 
 145     /**
 146      * Convert a link to be an external link if appropriate.
 147      *
 148      * @param pkgName The package name.
 149      * @param relativepath    The relative path.
 150      * @param filename    The link to convert.
 151      * @return if external return converted link else return null
 152      */
 153     public DocLink getExternalLink(String pkgName, DocPath relativepath, String filename) {

 154         return getExternalLink(pkgName, relativepath, filename, null);
 155     }
 156 
 157     public DocLink getExternalLink(String pkgName, DocPath relativepath, String filename,
 158             String memberName) {
 159         Item fnd = findPackageItem(pkgName);
 160         if (fnd == null)
 161             return null;
 162 
 163         DocPath p = fnd.relative ?
 164                 relativepath.resolve(fnd.path).resolve(filename) :
 165                 DocPath.create(fnd.path).resolve(filename);
 166 
 167         return new DocLink(p, "is-external=true", memberName);
 168     }
 169 
 170     /**
 171      * Build the extern package list from given URL or the directory path.
 172      * Flag error if the "-link" or "-linkoffline" option is already used.
 173      *
 174      * @param url        URL or Directory path.
 175      * @param pkglisturl This can be another URL for "package-list" or ordinary
 176      *                   file.
 177      * @param reporter   The <code>DocErrorReporter</code> used to report errors.
 178      * @param linkoffline True if -linkoffline is used and false if -link is used.
 179      */
 180     public boolean link(String url, String pkglisturl, Reporter reporter, boolean linkoffline) {

 181         this.linkoffline = linkoffline;
 182         try {
 183             url = adjustEndFileSeparator(url);
 184             if (isUrl(pkglisturl)) {
 185                 readPackageListFromURL(url, toURL(adjustEndFileSeparator(pkglisturl)));
 186             } else {
 187                 readPackageListFromFile(url, DocFile.createFileForInput(configuration, pkglisturl));
 188             }
 189             return true;
 190         } catch (Fault f) {
 191             reporter.print(Diagnostic.Kind.WARNING, f.getMessage());
 192             return false;
 193         }
 194     }
 195 
 196     private URL toURL(String url) throws Fault {
 197         try {
 198             return new URL(url);
 199         } catch (MalformedURLException e) {
 200             throw new Fault(configuration.getText("doclet.MalformedURL", url), e);
 201         }
 202     }
 203 
 204     private class Fault extends Exception {
 205         private static final long serialVersionUID = 0;
 206 
 207         Fault(String msg, Exception cause) {
 208             super(msg, cause);
 209         }
 210     }
 211 


 217     private Item findPackageItem(String pkgName) {
 218         if (packageToItemMap == null) {
 219             return null;
 220         }
 221         return packageToItemMap.get(pkgName);
 222     }
 223 
 224     /**
 225      * If the URL or Directory path is missing end file separator, add that.
 226      */
 227     private String adjustEndFileSeparator(String url) {
 228         return url.endsWith("/") ? url : url + '/';
 229     }
 230 
 231     /**
 232      * Fetch the URL and read the "package-list" file.
 233      *
 234      * @param urlpath        Path to the packages.
 235      * @param pkglisturlpath URL or the path to the "package-list" file.
 236      */
 237     private void readPackageListFromURL(String urlpath, URL pkglisturlpath) throws Fault {

 238         try {
 239             URL link = pkglisturlpath.toURI().resolve(DocPaths.PACKAGE_LIST.getPath()).toURL();
 240             readPackageList(link.openStream(), urlpath, false);
 241         } catch (URISyntaxException | MalformedURLException exc) {
 242             throw new Fault(configuration.getText("doclet.MalformedURL", pkglisturlpath.toString()), exc);
 243         }
 244         catch (IOException exc) {
 245             throw new Fault(configuration.getText("doclet.URL_error", pkglisturlpath.toString()), exc);
 246         }
 247     }
 248 
 249     /**
 250      * Read the "package-list" file which is available locally.
 251      *
 252      * @param path URL or directory path to the packages.
 253      * @param pkgListPath Path to the local "package-list" file.
 254      */
 255     private void readPackageListFromFile(String path, DocFile pkgListPath)
 256             throws Fault {
 257         DocFile file = pkgListPath.resolve(DocPaths.PACKAGE_LIST);


 263                 boolean pathIsRelative =
 264                         !DocFile.createFileForInput(configuration, path).isAbsolute()
 265                         && !isUrl(path);
 266                 readPackageList(file.openInputStream(), path, pathIsRelative);
 267             } else {
 268                 throw new Fault(configuration.getText("doclet.File_error", file.getPath()), null);
 269             }
 270         } catch (IOException exc) {
 271            throw new Fault(configuration.getText("doclet.File_error", file.getPath()), exc);
 272         }
 273     }
 274 
 275     /**
 276      * Read the file "package-list" and for each package name found, create
 277      * Extern object and associate it with the package name in the map.
 278      *
 279      * @param input    InputStream from the "package-list" file.
 280      * @param path     URL or the directory path to the packages.
 281      * @param relative Is path relative?
 282      */
 283     private void readPackageList(InputStream input, String path, boolean relative)

 284                          throws IOException {
 285         BufferedReader in = new BufferedReader(new InputStreamReader(input));
 286         StringBuilder strbuf = new StringBuilder();
 287         try {
 288             int c;
 289             while ((c = in.read()) >= 0) {
 290                 char ch = (char)c;
 291                 if (ch == '\n' || ch == '\r') {
 292                     if (strbuf.length() > 0) {
 293                         String packname = strbuf.toString();
 294                         String packpath = path +
 295                                       packname.replace('.', '/') + '/';
 296                         new Item(packname, packpath, relative);
 297                         strbuf.setLength(0);
 298                     }
 299                 } else {
 300                     strbuf.append(ch);
 301                 }
 302             }
 303         } finally {