jdk/make/src/classes/build/tools/docs/GenDocsBundlePage.java
author mchung
Wed, 07 Jun 2017 21:15:06 -0700
changeset 45528 1e8c2f062ad3
parent 45259 e09145bcfcc5
permissions -rw-r--r--
8181639: Add tool and services information to module summary Reviewed-by: alanb, psandoz, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     1
/*
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     4
 *
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    10
 *
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    15
 * accompanied this code).
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    16
 *
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    20
 *
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    23
 * questions.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    24
 */
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    25
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    26
package build.tools.docs;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    27
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    28
import java.io.BufferedReader;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    29
import java.io.BufferedWriter;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    30
import java.io.IOException;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    31
import java.io.InputStream;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    32
import java.io.InputStreamReader;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    33
import java.io.PrintWriter;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    34
import java.lang.module.ModuleDescriptor;
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    35
import java.lang.module.ModuleFinder;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    36
import java.lang.module.ModuleReference;
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    37
import java.nio.charset.StandardCharsets;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    38
import java.nio.file.Files;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    39
import java.nio.file.Path;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    40
import java.nio.file.Paths;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    41
import java.util.Arrays;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    42
import java.util.Comparator;
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    43
import java.util.HashMap;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    44
import java.util.Locale;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    45
import java.util.Map;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    46
import java.util.Properties;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    47
import java.util.Set;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    48
import java.util.function.Predicate;
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    49
import java.util.stream.Stream;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
    50
import static java.util.stream.Collectors.*;
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    51
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    52
/**
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    53
 * Build tool to generate the docs bundle index page.
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    54
 */
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    55
public class GenDocsBundlePage {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    56
    private static String DOCS_BUNDLE_PAGE = "docs-bundle-page.html";
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    57
    private static String MODULE_GROUPS_PROPS = "docs-module-groups.properties";
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    58
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    59
    private static String USAGE =
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    60
        "GenDocsBundlePage --output <file path> --title <title>" +
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    61
        "                  [--template <template>]";
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    62
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    63
    public static void main(String... args) throws IOException {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    64
        String title = null;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    65
        Path outputfile = null;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    66
        Path template = null;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    67
        for (int i=0; i < args.length; i++) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    68
            String option = args[i];
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    69
            if (option.equals("--output")) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    70
                outputfile = Paths.get(getArgument(args, option, ++i));
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    71
            } else if (option.equals("--title")) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    72
                title = getArgument(args, option, ++i);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    73
            } else if (option.equals("--template")) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    74
                template = Paths.get(getArgument(args, option, ++i));
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    75
            } else if (option.startsWith("-")) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    76
                throw new IllegalArgumentException("Invalid option: " + option);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    77
            }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    78
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    79
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    80
        if (outputfile == null) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    81
            System.err.println("ERROR: must specify --output option");
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    82
            System.exit(1);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    83
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    84
        if (title == null) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    85
            System.err.println("ERROR: must specify --title option");
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    86
            System.exit(1);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    87
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    88
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    89
        try (InputStream is = readTemplate(template);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    90
             BufferedReader reader = new BufferedReader(new InputStreamReader(is)))
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    91
        {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    92
            new GenDocsBundlePage(title, outputfile).run(reader);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    93
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    94
    }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    95
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    96
    private static String getArgument(String[] args, String option, int index) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    97
        if (index < args.length) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    98
            return args[index];
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
    99
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   100
        throw new IllegalArgumentException("Argument must be specified for " + option);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   101
    }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   102
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   103
    private static InputStream readTemplate(Path template) throws IOException {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   104
        if (template != null) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   105
            return Files.newInputStream(template);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   106
        } else {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   107
            return GenDocsBundlePage.class.getResourceAsStream(DOCS_BUNDLE_PAGE);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   108
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   109
    }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   110
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   111
    private static final String HEADER_TITLE = "@HEADER_TITLE@";
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   112
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   113
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   114
    final Path outputfile;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   115
    final String title;
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   116
    final Map<String, Set<ModuleDescriptor>> moduleGroups = new HashMap<>();
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   117
    GenDocsBundlePage(String title, Path outputfile) throws IOException
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   118
    {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   119
        this.outputfile = outputfile;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   120
        this.title = title;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   121
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   122
        // read module groups
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   123
        ModuleFinder finder = ModuleFinder.ofSystem();
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   124
        try (InputStream in = GenDocsBundlePage.class.getResourceAsStream(MODULE_GROUPS_PROPS)) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   125
            Properties props = new Properties();
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   126
            props.load(in);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   127
            for (String key: props.stringPropertyNames()) {
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   128
                Set<ModuleDescriptor> mods =
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   129
                    Stream.of(props.getProperty(key).split("\\s+"))
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   130
                          .map(String::trim)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   131
                          .flatMap(mn -> finder.find(mn).stream())
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   132
                          .map(ModuleReference::descriptor)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   133
                          .collect(toSet());
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   134
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   135
                String name = "@" + key.toUpperCase(Locale.ENGLISH) + "@";
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   136
                moduleGroups.put(name, mods);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   137
            };
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   138
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   139
    }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   140
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   141
    void run(BufferedReader reader) throws IOException {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   142
        if (Files.notExists(outputfile.getParent())) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   143
            Files.createDirectories(outputfile.getParent());
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   144
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   145
        try (BufferedWriter bw = Files.newBufferedWriter(outputfile, StandardCharsets.UTF_8);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   146
             PrintWriter writer = new PrintWriter(bw)) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   147
            reader.lines().map(this::genOutputLine)
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   148
                  .forEach(writer::println);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   149
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   150
    }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   151
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   152
    String genOutputLine(String line) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   153
        if (line.contains(HEADER_TITLE)) {
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   154
            line = line.replace(HEADER_TITLE, title);
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   155
        }
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   156
        int i = line.indexOf('@');
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   157
        int j = line.indexOf('@', i+1);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   158
        if (i >= 0 && i < j) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   159
            String name = line.substring(i, j+1);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   160
            if (moduleGroups.containsKey(name)) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   161
                line = line.replace(name, formatModuleGroup(name));
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   162
            }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   163
        }
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   164
        return line;
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   165
    }
45259
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   166
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   167
    String toHRef(ModuleDescriptor md) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   168
        String mn = md.name();
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   169
        String formattedName;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   170
        if (hasExportedAPIs(md)) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   171
            // has exported APIs
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   172
            formattedName = mn;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   173
        } else if (!md.provides().isEmpty()) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   174
            // a provider
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   175
            formattedName = "<i>" + mn + "</i>";
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   176
        } else {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   177
            // a tool
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   178
            formattedName = "<i>" + mn + "</i>";
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   179
        }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   180
        return String.format("<a href=\"api/%s-summary.html\">%s</a>",
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   181
                             mn, formattedName);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   182
    }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   183
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   184
    String formatModuleGroup(String groupName) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   185
        StringBuilder sb = new StringBuilder();
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   186
        // organize in Java SE, JDK, JavaFX, JCP groups
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   187
        Set<ModuleDescriptor> modules = moduleGroups.get(groupName);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   188
        Arrays.stream(ModuleGroup.values())
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   189
            .forEach(g -> {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   190
                Set<ModuleDescriptor> mods = modules.stream()
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   191
                    .filter(md -> g.predicate.test(md.name()))
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   192
                    .collect(toSet());
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   193
                if (!mods.isEmpty()) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   194
                    sb.append("<div class=" + g.cssClass + ">\n");
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   195
                    // modules with exported API
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   196
                    mods.stream()
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   197
                        .filter(this::hasExportedAPIs)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   198
                        .sorted(Comparator.comparing(ModuleDescriptor::name))
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   199
                        .map(this::toHRef)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   200
                        .forEach(m -> sb.append(m).append("\n"));
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   201
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   202
                    // tools and providers
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   203
                    mods.stream()
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   204
                        .filter(md -> !hasExportedAPIs(md))
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   205
                        .sorted(Comparator.comparing(ModuleDescriptor::name))
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   206
                        .map(this::toHRef)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   207
                        .forEach(m -> sb.append(m).append("\n"));
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   208
                    sb.append("</div>");
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   209
                }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   210
            });
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   211
        return sb.toString();
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   212
    }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   213
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   214
    private boolean hasExportedAPIs(ModuleDescriptor md) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   215
        if (md.exports().stream().anyMatch(e -> !e.isQualified())) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   216
            return true;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   217
        }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   218
        // this should check if any indirect exports
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   219
        // checking requires transitive would be sufficient for JDK modules
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   220
        if (md.requires().stream()
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   221
              .map(ModuleDescriptor.Requires::modifiers)
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   222
              .anyMatch(mods -> mods.contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE))) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   223
            return true;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   224
        }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   225
        return false;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   226
    }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   227
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   228
    private static final Set<String> NON_JAVA_SE_MODULES =
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   229
        Set.of("java.jnlp", "java.smartcardio");
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   230
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   231
    /**
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   232
     * CSS class names are defined in docs-bundle-page.html
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   233
     */
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   234
    enum ModuleGroup {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   235
        JAVA_SE("javase", mn -> mn.startsWith("java.") && !NON_JAVA_SE_MODULES.contains(mn)),
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   236
        JDK("jdk", mn -> mn.startsWith("jdk.")),
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   237
        JAVAFX("javafx", mn -> mn.startsWith("javafx.")),
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   238
        NON_JAVA_SE("jcp", NON_JAVA_SE_MODULES::contains);
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   239
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   240
        final String cssClass;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   241
        final Predicate<String> predicate;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   242
        ModuleGroup(String cssClass, Predicate<String> predicate) {
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   243
            this.cssClass = cssClass;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   244
            this.predicate = predicate;
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   245
        }
e09145bcfcc5 8180717: Upgrade the docs bundle index page
mchung
parents: 45139
diff changeset
   246
    }
45139
7be55dfa1742 8180208: Provide a new docs bundle page
mchung
parents:
diff changeset
   247
}