langtools/src/jdk.javadoc/share/classes/jdk/javadoc/internal/doclets/toolkit/util/Group.java
author ksrini
Mon, 05 Jun 2017 13:45:34 -0700
changeset 45417 f7479ee8de69
parent 45157 f5f796453339
permissions -rw-r--r--
8177848: Rename Configuration(Impl) classes Reviewed-by: jjg
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
     2
 * Copyright (c) 1998, 2017, 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: 1789
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: 1789
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: 1789
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 1789
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.util.*;
8d2148961366 8000663: clean up langtools imports
jjg
parents: 5520
diff changeset
    29
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
    30
import javax.lang.model.element.ModuleElement;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    31
import javax.lang.model.element.PackageElement;
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    32
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 45157
diff changeset
    33
import jdk.javadoc.internal.doclets.toolkit.BaseConfiguration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
    34
import jdk.javadoc.internal.doclets.toolkit.Messages;
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
    35
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
/**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
    38
 * Process and manage grouping of elements, as specified by "-group" option on
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
 * the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
 * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
 * For example, if user has used -group option as
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
 * -group "Core Packages" "java.*" -group "CORBA Packages" "org.omg.*", then
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
 * the packages specified on the command line will be grouped according to their
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
 * names starting with either "java." or "org.omg.". All the other packages
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
 * which do not fall in the user given groups, are grouped in default group,
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
 * named as either "Other Packages" or "Packages" depending upon if "-group"
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
 * option used or not at all used respectively.
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
 * </p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
 * <p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
 * Also the packages are grouped according to the longest possible match of
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 * their names with the grouping information provided. For example, if there
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 * are two groups, like -group "Lang" "java.lang" and -group "Core" "java.*",
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 * will put the package java.lang in the group "Lang" and not in group "Core".
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
 * </p>
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
 *
14260
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    56
 *  <p><b>This is NOT part of any supported API.
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    57
 *  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: 14258
diff changeset
    58
 *  This code and its internal interfaces are subject to change or
727a84636f12 8000665: fix "internal API" comments on javadoc files
jjg
parents: 14258
diff changeset
    59
 *  deletion without notice.</b>
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
 * @author Atul M Dambalkar
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
public class Group {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
     * Map of regular expressions with the corresponding group name.
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
     */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
    68
    private Map<String,String> regExpGroupMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
     * List of regular expressions sorted according to the length. Regular
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
     * expression with longest length will be first in the sorted order.
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
     */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
    74
    private List<String> sortedRegExpList = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
     * List of group names in the same order as given on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
     */
22163
3651128c74eb 8030244: Update langtools to use Diamond
briangoetz
parents: 22159
diff changeset
    79
    private List<String> groupList = new ArrayList<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
    82
     * Map of non-regular expressions(possible package or module names) with the
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
     * corresponding group name.
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
     */
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
    85
    private Map<String,String> elementNameGroupMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     * The global configuration information for this run.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     */
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 45157
diff changeset
    90
    private final BaseConfiguration configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
    91
    private Messages messages;
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     * Since we need to sort the keys in the reverse order(longest key first),
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
     * the compare method in the implementing class is doing the reverse
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
     * comparison.
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
     */
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    98
    private static class MapKeyComparator implements Comparator<String> {
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
    99
        public int compare(String key1, String key2) {
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   100
            return key2.length() - key1.length();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 45157
diff changeset
   104
    public Group(BaseConfiguration configuration) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
        this.configuration = configuration;
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   106
        messages = configuration.getMessages();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   110
     * Depending upon the format of the module name provided in the "-group"
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   111
     * option, generate two separate maps. There will be a map for mapping
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   112
     * regular expression(only meta character allowed is '*' and that is at the
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   113
     * end of the regular expression) on to the group name. And another map
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   114
     * for mapping (possible) module names(if the name format doesn't contain
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   115
     * meta character '*', then it is assumed to be a module name) on to the
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   116
     * group name. This will also sort all the regular expressions found in the
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   117
     * reverse order of their lengths, i.e. longest regular expression will be
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   118
     * first in the sorted list.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   119
     *
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   120
     * @param groupname       The name of the group from -group option.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   121
     * @param moduleNameFormList List of the module name formats.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   122
     */
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   123
    public boolean checkModuleGroups(String groupname, String moduleNameFormList) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   124
        String[] mdlPatterns = moduleNameFormList.split(":");
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   125
        if (groupList.contains(groupname)) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   126
            initMessages();
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   127
            messages.warning("doclet.Groupname_already_used", groupname);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   128
            return false;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   129
        }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   130
        groupList.add(groupname);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   131
        for (String mdlPattern : mdlPatterns) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   132
            if (mdlPattern.length() == 0) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   133
                initMessages();
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   134
                messages.warning("doclet.Error_in_grouplist", groupname, moduleNameFormList);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   135
                return false;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   136
            }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   137
            if (mdlPattern.endsWith("*")) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   138
                mdlPattern = mdlPattern.substring(0, mdlPattern.length() - 1);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   139
                if (foundGroupFormat(regExpGroupMap, mdlPattern)) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   140
                    return false;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   141
                }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   142
                regExpGroupMap.put(mdlPattern, groupname);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   143
                sortedRegExpList.add(mdlPattern);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   144
            } else {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   145
                if (foundGroupFormat(elementNameGroupMap, mdlPattern)) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   146
                    return false;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   147
                }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   148
                elementNameGroupMap.put(mdlPattern, groupname);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   149
            }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   150
        }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   151
        Collections.sort(sortedRegExpList, new MapKeyComparator());
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   152
        return true;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   153
    }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   154
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   155
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     * Depending upon the format of the package name provided in the "-group"
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     * option, generate two separate maps. There will be a map for mapping
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     * regular expression(only meta character allowed is '*' and that is at the
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     * end of the regular expression) on to the group name. And another map
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   160
     * for mapping (possible) package names(if the name format doesn't contain
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     * meta character '*', then it is assumed to be a package name) on to the
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     * group name. This will also sort all the regular expressions found in the
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     * reverse order of their lengths, i.e. longest regular expression will be
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     * first in the sorted list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     * @param groupname       The name of the group from -group option.
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     * @param pkgNameFormList List of the package name formats.
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   169
    public boolean checkPackageGroups(String groupname, String pkgNameFormList) {
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   170
        String[] pkgPatterns = pkgNameFormList.split(":");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        if (groupList.contains(groupname)) {
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   172
            initMessages();
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   173
            messages.warning("doclet.Groupname_already_used", groupname);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
        groupList.add(groupname);
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   177
        for (String pkgPattern : pkgPatterns) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   178
            if (pkgPattern.length() == 0) {
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   179
                initMessages();
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   180
                messages.warning("doclet.Error_in_grouplist", groupname, pkgNameFormList);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
            }
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   183
            if (pkgPattern.endsWith("*")) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   184
                pkgPattern = pkgPattern.substring(0, pkgPattern.length() - 1);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   185
                if (foundGroupFormat(regExpGroupMap, pkgPattern)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                }
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   188
                regExpGroupMap.put(pkgPattern, groupname);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   189
                sortedRegExpList.add(pkgPattern);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
            } else {
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   191
                if (foundGroupFormat(elementNameGroupMap, pkgPattern)) {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
                    return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                }
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   194
                elementNameGroupMap.put(pkgPattern, groupname);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        Collections.sort(sortedRegExpList, new MapKeyComparator());
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   201
    // Lazy init of the messages for now, because Group is created
45417
f7479ee8de69 8177848: Rename Configuration(Impl) classes
ksrini
parents: 45157
diff changeset
   202
    // in BaseConfiguration before configuration is fully initialized.
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   203
    private void initMessages() {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   204
        if (messages == null) {
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   205
            messages = configuration.getMessages();
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   206
        }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   207
    }
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   208
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   210
     * Search if the given map has the given element format.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
     * @param map Map to be searched.
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   213
     * @param elementFormat The format to search.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
     *
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   215
     * @return true if element name format found in the map, else false.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     */
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   217
    boolean foundGroupFormat(Map<String,?> map, String elementFormat) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   218
        if (map.containsKey(elementFormat)) {
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   219
            initMessages();
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   220
            messages.error("doclet.Same_element_name_used", elementFormat);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
            return true;
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        return false;
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   227
     * Group the modules according the grouping information provided on the
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   228
     * command line. Given a list of modules, search each module name in
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   229
     * regular expression map as well as module name map to get the
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   230
     * corresponding group name. Create another map with mapping of group name
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   231
     * to the module list, which will fall under the specified group. If any
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   232
     * module doesn't belong to any specified group on the command line, then
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   233
     * a new group named "Other Modules" will be created for it. If there are
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   234
     * no groups found, in other words if "-group" option is not at all used,
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   235
     * then all the modules will be grouped under group "Modules".
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   236
     *
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   237
     * @param modules Specified modules.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   238
     * @return map of group names and set of module elements.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   239
     */
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   240
    public Map<String, SortedSet<ModuleElement>> groupModules(Set<ModuleElement> modules) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   241
        Map<String, SortedSet<ModuleElement>> groupModuleMap = new HashMap<>();
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   242
        String defaultGroupName =
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   243
            (elementNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   244
                configuration.getResources().getText("doclet.Modules") :
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   245
                configuration.getResources().getText("doclet.Other_Modules");
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   246
        // if the user has not used the default group name, add it
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   247
        if (!groupList.contains(defaultGroupName)) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   248
            groupList.add(defaultGroupName);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   249
        }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   250
        for (ModuleElement mdl : modules) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   251
            String moduleName = mdl.isUnnamed() ? null : mdl.getQualifiedName().toString();
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   252
            String groupName = mdl.isUnnamed() ? null : elementNameGroupMap.get(moduleName);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   253
            // if this module is not explicitly assigned to a group,
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   254
            // try matching it to group specified by regular expression
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   255
            if (groupName == null) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   256
                groupName = regExpGroupName(moduleName);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   257
            }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   258
            // if it is in neither group map, put it in the default
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   259
            // group
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   260
            if (groupName == null) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   261
                groupName = defaultGroupName;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   262
            }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   263
            getModuleList(groupModuleMap, groupName).add(mdl);
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   264
        }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   265
        return groupModuleMap;
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   266
    }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   267
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   268
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
     * Group the packages according the grouping information provided on the
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
     * command line. Given a list of packages, search each package name in
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
     * regular expression map as well as package name map to get the
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
     * corresponding group name. Create another map with mapping of group name
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
     * to the package list, which will fall under the specified group. If any
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   274
     * package doesn't belong to any specified group on the command line, then
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
     * a new group named "Other Packages" will be created for it. If there are
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
     * no groups found, in other words if "-group" option is not at all used,
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
     * then all the packages will be grouped under group "Packages".
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
     *
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
     * @param packages Packages specified on the command line.
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   280
     * @return map of group names and set of package elements
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   282
    public Map<String, SortedSet<PackageElement>> groupPackages(Set<PackageElement> packages) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   283
        Map<String, SortedSet<PackageElement>> groupPackageMap = new HashMap<>();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
        String defaultGroupName =
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   285
            (elementNameGroupMap.isEmpty() && regExpGroupMap.isEmpty())?
40303
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   286
                configuration.getResources().getText("doclet.Packages") :
96a1226aca18 8160697: HTMLWriter needs perf cleanup
jjg
parents: 35426
diff changeset
   287
                configuration.getResources().getText("doclet.Other_Packages");
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
        // if the user has not used the default group name, add it
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
        if (!groupList.contains(defaultGroupName)) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
            groupList.add(defaultGroupName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        }
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   292
        for (PackageElement pkg : packages) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   293
            String pkgName = pkg.isUnnamed() ? null : configuration.utils.getPackageName(pkg);
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   294
            String groupName = pkg.isUnnamed() ? null : elementNameGroupMap.get(pkgName);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
            // if this package is not explicitly assigned to a group,
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
            // try matching it to group specified by regular expression
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            if (groupName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                groupName = regExpGroupName(pkgName);
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
            // if it is in neither group map, put it in the default
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
            // group
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
            if (groupName == null) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
                groupName = defaultGroupName;
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
            getPkgList(groupPackageMap, groupName).add(pkg);
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        return groupPackageMap;
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   311
     * Search for element name in the sorted regular expression
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
     * list, if found return the group name.  If not, return null.
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
     *
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   314
     * @param elementName Name of element to be found in the regular
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
     * expression list.
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
     */
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   317
    String regExpGroupName(String elementName) {
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 14260
diff changeset
   318
        for (String regexp : sortedRegExpList) {
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   319
            if (elementName.startsWith(regexp)) {
868
d0f233085cbb 6657907: javadoc has unchecked warnings
jjg
parents: 10
diff changeset
   320
                return regExpGroupMap.get(regexp);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
        return null;
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
     * For the given group name, return the package list, on which it is mapped.
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
     * Create a new list, if not found.
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
     *
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   330
     * @param map Map to be searched for group name.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
     * @param groupname Group name to search.
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
     */
35426
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   333
    SortedSet<PackageElement> getPkgList(Map<String, SortedSet<PackageElement>> map,
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   334
            String groupname) {
374342e56a56 8035473: [javadoc] Revamp the existing Doclet APIs
ksrini
parents: 25874
diff changeset
   335
        return map.computeIfAbsent(groupname, g -> new TreeSet<>(configuration.utils.makePackageComparator()));
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
06bc494ca11e Initial load
duke
parents:
diff changeset
   338
    /**
45157
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   339
     * For the given group name, return the module list, on which it is mapped.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   340
     * Create a new list, if not found.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   341
     *
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   342
     * @param map Map to be searched for group name.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   343
     * @param groupname Group name to search.
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   344
     */
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   345
    SortedSet<ModuleElement> getModuleList(Map<String, SortedSet<ModuleElement>> map,
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   346
            String groupname) {
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   347
        return map.computeIfAbsent(groupname, g -> new TreeSet<>(configuration.utils.makeModuleComparator()));
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   348
    }
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   349
f5f796453339 8178043: Support grouping modules in unified javadoc
bpatel
parents: 40303
diff changeset
   350
    /**
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   351
     * Return the list of groups, in the same order as specified
06bc494ca11e Initial load
duke
parents:
diff changeset
   352
     * on the command line.
06bc494ca11e Initial load
duke
parents:
diff changeset
   353
     */
1789
7ac8c0815000 6765045: Remove rawtypes warnings from langtools
mcimadamore
parents: 1475
diff changeset
   354
    public List<String> getGroupList() {
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   355
        return groupList;
06bc494ca11e Initial load
duke
parents:
diff changeset
   356
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   357
}