45139
|
1 |
/*
|
|
2 |
* Copyright (c) 2017, 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 build.tools.docs;
|
|
27 |
|
|
28 |
import java.io.BufferedReader;
|
|
29 |
import java.io.BufferedWriter;
|
|
30 |
import java.io.IOException;
|
|
31 |
import java.io.InputStream;
|
|
32 |
import java.io.InputStreamReader;
|
|
33 |
import java.io.PrintWriter;
|
45259
|
34 |
import java.lang.module.ModuleDescriptor;
|
45139
|
35 |
import java.lang.module.ModuleFinder;
|
45259
|
36 |
import java.lang.module.ModuleReference;
|
45139
|
37 |
import java.nio.charset.StandardCharsets;
|
|
38 |
import java.nio.file.Files;
|
|
39 |
import java.nio.file.Path;
|
|
40 |
import java.nio.file.Paths;
|
45259
|
41 |
import java.util.Arrays;
|
|
42 |
import java.util.Comparator;
|
45139
|
43 |
import java.util.HashMap;
|
|
44 |
import java.util.Locale;
|
|
45 |
import java.util.Map;
|
|
46 |
import java.util.Properties;
|
|
47 |
import java.util.Set;
|
45259
|
48 |
import java.util.function.Predicate;
|
45139
|
49 |
import java.util.stream.Stream;
|
45259
|
50 |
import static java.util.stream.Collectors.*;
|
45139
|
51 |
|
|
52 |
/**
|
|
53 |
* Build tool to generate the docs bundle index page.
|
|
54 |
*/
|
|
55 |
public class GenDocsBundlePage {
|
|
56 |
private static String DOCS_BUNDLE_PAGE = "docs-bundle-page.html";
|
|
57 |
private static String MODULE_GROUPS_PROPS = "docs-module-groups.properties";
|
|
58 |
|
|
59 |
private static String USAGE =
|
|
60 |
"GenDocsBundlePage --output <file path> --title <title>" +
|
|
61 |
" [--template <template>]";
|
|
62 |
|
|
63 |
public static void main(String... args) throws IOException {
|
|
64 |
String title = null;
|
|
65 |
Path outputfile = null;
|
|
66 |
Path template = null;
|
|
67 |
for (int i=0; i < args.length; i++) {
|
|
68 |
String option = args[i];
|
|
69 |
if (option.equals("--output")) {
|
|
70 |
outputfile = Paths.get(getArgument(args, option, ++i));
|
|
71 |
} else if (option.equals("--title")) {
|
|
72 |
title = getArgument(args, option, ++i);
|
|
73 |
} else if (option.equals("--template")) {
|
|
74 |
template = Paths.get(getArgument(args, option, ++i));
|
|
75 |
} else if (option.startsWith("-")) {
|
|
76 |
throw new IllegalArgumentException("Invalid option: " + option);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
if (outputfile == null) {
|
|
81 |
System.err.println("ERROR: must specify --output option");
|
|
82 |
System.exit(1);
|
|
83 |
}
|
|
84 |
if (title == null) {
|
|
85 |
System.err.println("ERROR: must specify --title option");
|
|
86 |
System.exit(1);
|
|
87 |
}
|
|
88 |
|
|
89 |
try (InputStream is = readTemplate(template);
|
|
90 |
BufferedReader reader = new BufferedReader(new InputStreamReader(is)))
|
|
91 |
{
|
|
92 |
new GenDocsBundlePage(title, outputfile).run(reader);
|
|
93 |
}
|
|
94 |
}
|
|
95 |
|
|
96 |
private static String getArgument(String[] args, String option, int index) {
|
|
97 |
if (index < args.length) {
|
|
98 |
return args[index];
|
|
99 |
}
|
|
100 |
throw new IllegalArgumentException("Argument must be specified for " + option);
|
|
101 |
}
|
|
102 |
|
|
103 |
private static InputStream readTemplate(Path template) throws IOException {
|
|
104 |
if (template != null) {
|
|
105 |
return Files.newInputStream(template);
|
|
106 |
} else {
|
|
107 |
return GenDocsBundlePage.class.getResourceAsStream(DOCS_BUNDLE_PAGE);
|
|
108 |
}
|
|
109 |
}
|
|
110 |
|
|
111 |
private static final String HEADER_TITLE = "@HEADER_TITLE@";
|
45259
|
112 |
|
|
113 |
|
45139
|
114 |
final Path outputfile;
|
|
115 |
final String title;
|
45259
|
116 |
final Map<String, Set<ModuleDescriptor>> moduleGroups = new HashMap<>();
|
45139
|
117 |
GenDocsBundlePage(String title, Path outputfile) throws IOException
|
|
118 |
{
|
|
119 |
this.outputfile = outputfile;
|
|
120 |
this.title = title;
|
|
121 |
|
45259
|
122 |
// read module groups
|
45139
|
123 |
ModuleFinder finder = ModuleFinder.ofSystem();
|
|
124 |
try (InputStream in = GenDocsBundlePage.class.getResourceAsStream(MODULE_GROUPS_PROPS)) {
|
|
125 |
Properties props = new Properties();
|
|
126 |
props.load(in);
|
|
127 |
for (String key: props.stringPropertyNames()) {
|
45259
|
128 |
Set<ModuleDescriptor> mods =
|
|
129 |
Stream.of(props.getProperty(key).split("\\s+"))
|
|
130 |
.map(String::trim)
|
|
131 |
.flatMap(mn -> finder.find(mn).stream())
|
|
132 |
.map(ModuleReference::descriptor)
|
|
133 |
.collect(toSet());
|
45139
|
134 |
|
|
135 |
String name = "@" + key.toUpperCase(Locale.ENGLISH) + "@";
|
45259
|
136 |
moduleGroups.put(name, mods);
|
|
137 |
};
|
45139
|
138 |
}
|
|
139 |
}
|
|
140 |
|
|
141 |
void run(BufferedReader reader) throws IOException {
|
|
142 |
if (Files.notExists(outputfile.getParent())) {
|
|
143 |
Files.createDirectories(outputfile.getParent());
|
|
144 |
}
|
|
145 |
try (BufferedWriter bw = Files.newBufferedWriter(outputfile, StandardCharsets.UTF_8);
|
|
146 |
PrintWriter writer = new PrintWriter(bw)) {
|
|
147 |
reader.lines().map(this::genOutputLine)
|
|
148 |
.forEach(writer::println);
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
String genOutputLine(String line) {
|
|
153 |
if (line.contains(HEADER_TITLE)) {
|
|
154 |
line = line.replace(HEADER_TITLE, title);
|
|
155 |
}
|
45259
|
156 |
int i = line.indexOf('@');
|
|
157 |
int j = line.indexOf('@', i+1);
|
|
158 |
if (i >= 0 && i < j) {
|
|
159 |
String name = line.substring(i, j+1);
|
|
160 |
if (moduleGroups.containsKey(name)) {
|
|
161 |
line = line.replace(name, formatModuleGroup(name));
|
45139
|
162 |
}
|
|
163 |
}
|
|
164 |
return line;
|
|
165 |
}
|
45259
|
166 |
|
|
167 |
String toHRef(ModuleDescriptor md) {
|
|
168 |
String mn = md.name();
|
|
169 |
String formattedName;
|
|
170 |
if (hasExportedAPIs(md)) {
|
|
171 |
// has exported APIs
|
|
172 |
formattedName = mn;
|
|
173 |
} else if (!md.provides().isEmpty()) {
|
|
174 |
// a provider
|
|
175 |
formattedName = "<i>" + mn + "</i>";
|
|
176 |
} else {
|
|
177 |
// a tool
|
|
178 |
formattedName = "<i>" + mn + "</i>";
|
|
179 |
}
|
|
180 |
return String.format("<a href=\"api/%s-summary.html\">%s</a>",
|
|
181 |
mn, formattedName);
|
|
182 |
}
|
|
183 |
|
|
184 |
String formatModuleGroup(String groupName) {
|
|
185 |
StringBuilder sb = new StringBuilder();
|
|
186 |
// organize in Java SE, JDK, JavaFX, JCP groups
|
|
187 |
Set<ModuleDescriptor> modules = moduleGroups.get(groupName);
|
|
188 |
Arrays.stream(ModuleGroup.values())
|
|
189 |
.forEach(g -> {
|
|
190 |
Set<ModuleDescriptor> mods = modules.stream()
|
|
191 |
.filter(md -> g.predicate.test(md.name()))
|
|
192 |
.collect(toSet());
|
|
193 |
if (!mods.isEmpty()) {
|
|
194 |
sb.append("<div class=" + g.cssClass + ">\n");
|
|
195 |
// modules with exported API
|
|
196 |
mods.stream()
|
|
197 |
.filter(this::hasExportedAPIs)
|
|
198 |
.sorted(Comparator.comparing(ModuleDescriptor::name))
|
|
199 |
.map(this::toHRef)
|
|
200 |
.forEach(m -> sb.append(m).append("\n"));
|
|
201 |
|
|
202 |
// tools and providers
|
|
203 |
mods.stream()
|
|
204 |
.filter(md -> !hasExportedAPIs(md))
|
|
205 |
.sorted(Comparator.comparing(ModuleDescriptor::name))
|
|
206 |
.map(this::toHRef)
|
|
207 |
.forEach(m -> sb.append(m).append("\n"));
|
|
208 |
sb.append("</div>");
|
|
209 |
}
|
|
210 |
});
|
|
211 |
return sb.toString();
|
|
212 |
}
|
|
213 |
|
|
214 |
private boolean hasExportedAPIs(ModuleDescriptor md) {
|
|
215 |
if (md.exports().stream().anyMatch(e -> !e.isQualified())) {
|
|
216 |
return true;
|
|
217 |
}
|
|
218 |
// this should check if any indirect exports
|
|
219 |
// checking requires transitive would be sufficient for JDK modules
|
|
220 |
if (md.requires().stream()
|
|
221 |
.map(ModuleDescriptor.Requires::modifiers)
|
|
222 |
.anyMatch(mods -> mods.contains(ModuleDescriptor.Requires.Modifier.TRANSITIVE))) {
|
|
223 |
return true;
|
|
224 |
}
|
|
225 |
return false;
|
|
226 |
}
|
|
227 |
|
|
228 |
private static final Set<String> NON_JAVA_SE_MODULES =
|
|
229 |
Set.of("java.jnlp", "java.smartcardio");
|
|
230 |
|
|
231 |
/**
|
|
232 |
* CSS class names are defined in docs-bundle-page.html
|
|
233 |
*/
|
|
234 |
enum ModuleGroup {
|
|
235 |
JAVA_SE("javase", mn -> mn.startsWith("java.") && !NON_JAVA_SE_MODULES.contains(mn)),
|
|
236 |
JDK("jdk", mn -> mn.startsWith("jdk.")),
|
|
237 |
JAVAFX("javafx", mn -> mn.startsWith("javafx.")),
|
|
238 |
NON_JAVA_SE("jcp", NON_JAVA_SE_MODULES::contains);
|
|
239 |
|
|
240 |
final String cssClass;
|
|
241 |
final Predicate<String> predicate;
|
|
242 |
ModuleGroup(String cssClass, Predicate<String> predicate) {
|
|
243 |
this.cssClass = cssClass;
|
|
244 |
this.predicate = predicate;
|
|
245 |
}
|
|
246 |
}
|
45139
|
247 |
}
|