langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/PackageListWriter.java
changeset 40587 1c355ea550ed
parent 40508 74ef30d16fb9
child 45417 f7479ee8de69
equal deleted inserted replaced
40519:e17429a7e843 40587:1c355ea550ed
    24  */
    24  */
    25 
    25 
    26 package jdk.javadoc.internal.doclets.toolkit.util;
    26 package jdk.javadoc.internal.doclets.toolkit.util;
    27 
    27 
    28 import java.io.*;
    28 import java.io.*;
    29 import java.util.*;
       
    30 
    29 
    31 import javax.lang.model.element.PackageElement;
    30 import javax.lang.model.element.PackageElement;
    32 
    31 
    33 import jdk.javadoc.doclet.DocletEnvironment;
    32 import jdk.javadoc.doclet.DocletEnvironment;
    34 import jdk.javadoc.internal.doclets.toolkit.Configuration;
    33 import jdk.javadoc.internal.doclets.toolkit.Configuration;
    35 import jdk.javadoc.internal.doclets.toolkit.Messages;
       
    36 
    34 
    37 
    35 
    38 /**
    36 /**
    39  * Write out the package index.
    37  * Write out the package index.
    40  *
    38  *
    43  *  This code and its internal interfaces are subject to change or
    41  *  This code and its internal interfaces are subject to change or
    44  *  deletion without notice.</b>
    42  *  deletion without notice.</b>
    45  *
    43  *
    46  * @author Atul M Dambalkar
    44  * @author Atul M Dambalkar
    47  */
    45  */
    48 public class PackageListWriter extends PrintWriter {
    46 public class PackageListWriter {
    49 
    47 
    50     private final Configuration configuration;
    48     private final Configuration configuration;
    51     private final Utils utils;
    49     private final Utils utils;
       
    50     private final DocFile file;
    52 
    51 
    53     /**
    52     /**
    54      * Constructor.
    53      * Constructor.
    55      *
    54      *
    56      * @param configuration the current configuration of the doclet.
    55      * @param configuration the current configuration of the doclet.
    57      */
    56      */
    58     public PackageListWriter(Configuration configuration) throws IOException {
    57     public PackageListWriter(Configuration configuration) {
    59         super(DocFile.createFileForOutput(configuration, DocPaths.PACKAGE_LIST).openWriter());
    58         file = DocFile.createFileForOutput(configuration, DocPaths.PACKAGE_LIST);
    60         this.configuration = configuration;
    59         this.configuration = configuration;
    61         this.utils = configuration.utils;
    60         this.utils = configuration.utils;
    62     }
    61     }
    63 
    62 
    64     /**
    63     /**
    65      * Generate the package index.
    64      * Generate the package index.
    66      *
    65      *
    67      * @param configuration the current configuration of the doclet.
    66      * @param configuration the current configuration of the doclet.
    68      * @throws DocletAbortException
    67      * @throws DocFileIOException if there is a problem writing the output
    69      */
    68      */
    70     public static void generate(Configuration configuration) {
    69     public static void generate(Configuration configuration) throws DocFileIOException {
    71         PackageListWriter packgen;
    70         PackageListWriter packgen = new PackageListWriter(configuration);
    72         try {
    71         packgen.generatePackageListFile(configuration.docEnv);
    73             packgen = new PackageListWriter(configuration);
    72     }
    74             packgen.generatePackageListFile(configuration.docEnv);
    73 
    75             packgen.close();
    74     protected void generatePackageListFile(DocletEnvironment docEnv) throws DocFileIOException {
    76         } catch (IOException exc) {
    75         try (BufferedWriter out = new BufferedWriter(file.openWriter())) {
    77             Messages messages = configuration.getMessages();
    76             for (PackageElement pkg : configuration.packages) {
    78             messages.error("doclet.exception_encountered",
    77                 // if the -nodeprecated option is set and the package is marked as
    79                 exc.toString(), DocPaths.PACKAGE_LIST);
    78                 // deprecated, do not include it in the packages list.
    80             throw new DocletAbortException(exc);
    79                 if (!(configuration.nodeprecated && utils.isDeprecated(pkg))) {
       
    80                     out.write(pkg.toString());
       
    81                     out.newLine();
       
    82                 }
       
    83             }
       
    84         } catch (IOException e) {
       
    85             throw new DocFileIOException(file, DocFileIOException.Mode.WRITE, e);
    81         }
    86         }
    82     }
    87     }
    83 
       
    84     protected void generatePackageListFile(DocletEnvironment docEnv) {
       
    85         ArrayList<PackageElement> names = new ArrayList<>();
       
    86         for (PackageElement pkg : configuration.packages) {
       
    87             // if the -nodeprecated option is set and the package is marked as
       
    88             // deprecated, do not include it in the packages list.
       
    89             if (!(configuration.nodeprecated && utils.isDeprecated(pkg)))
       
    90                 names.add(pkg);
       
    91         }
       
    92         names.stream().forEach((name) -> {
       
    93             println(name);
       
    94         });
       
    95     }
       
    96 }
    88 }