|
| 1 | +package org.openqa.selenium.tools.javadoc; |
| 2 | + |
| 3 | +import javax.tools.DocumentationTool; |
| 4 | +import javax.tools.JavaFileObject; |
| 5 | +import javax.tools.StandardJavaFileManager; |
| 6 | +import javax.tools.ToolProvider; |
| 7 | +import java.io.File; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStream; |
| 10 | +import java.io.OutputStream; |
| 11 | +import java.io.StringWriter; |
| 12 | +import java.io.UncheckedIOException; |
| 13 | +import java.io.Writer; |
| 14 | +import java.nio.charset.StandardCharsets; |
| 15 | +import java.nio.file.Files; |
| 16 | +import java.nio.file.Path; |
| 17 | +import java.nio.file.Paths; |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Comparator; |
| 20 | +import java.util.HashSet; |
| 21 | +import java.util.List; |
| 22 | +import java.util.Locale; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.stream.Collectors; |
| 25 | +import java.util.zip.ZipEntry; |
| 26 | +import java.util.zip.ZipInputStream; |
| 27 | +import java.util.zip.ZipOutputStream; |
| 28 | + |
| 29 | +public class JavadocJarMaker { |
| 30 | + |
| 31 | + public static void main(String[] args) throws IOException { |
| 32 | + Set<Path> sourceJars = new HashSet<>(); |
| 33 | + Path out = null; |
| 34 | + Set<Path> classpath = new HashSet<>(); |
| 35 | + |
| 36 | + for (int i = 0; i < args.length; i++) { |
| 37 | + String flag = args[i]; |
| 38 | + String next = args[++i]; |
| 39 | + |
| 40 | + switch (flag) { |
| 41 | + case "--cp": |
| 42 | + classpath.add(Paths.get(next)); |
| 43 | + break; |
| 44 | + |
| 45 | + case "--in": |
| 46 | + sourceJars.add(Paths.get(next)); |
| 47 | + break; |
| 48 | + |
| 49 | + case "--out": |
| 50 | + out = Paths.get(next); |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + if (sourceJars.isEmpty()) { |
| 56 | + throw new IllegalArgumentException("At least one input just must be specified via the --in flag"); |
| 57 | + } |
| 58 | + |
| 59 | + if (out == null) { |
| 60 | + throw new IllegalArgumentException("The output jar location must be specified via the --out flag"); |
| 61 | + } |
| 62 | + |
| 63 | + Set<Path> tempDirs = new HashSet<>(); |
| 64 | + Path dir = Files.createTempDirectory("javadocs"); |
| 65 | + tempDirs.add(dir); |
| 66 | + |
| 67 | + try { |
| 68 | + DocumentationTool tool = ToolProvider.getSystemDocumentationTool(); |
| 69 | + try (StandardJavaFileManager fileManager = tool.getStandardFileManager(null, Locale.getDefault(), StandardCharsets.UTF_8)) { |
| 70 | + fileManager.setLocation(DocumentationTool.Location.DOCUMENTATION_OUTPUT, List.of(dir.toFile())); |
| 71 | + |
| 72 | + Set<JavaFileObject> sources = new HashSet<>(); |
| 73 | + Set<String> topLevelPackages = new HashSet<>(); |
| 74 | + |
| 75 | + Path unpackTo = Files.createTempDirectory("unpacked-sources"); |
| 76 | + tempDirs.add(unpackTo); |
| 77 | + Set<String> fileNames = new HashSet<>(); |
| 78 | + readSourceFiles(unpackTo, fileManager, sourceJars, sources, topLevelPackages, fileNames); |
| 79 | + List<String> options = new ArrayList<>(); |
| 80 | + options.addAll(List.of("-html5", "-notimestamp", "-use", "-quiet", "-Xdoclint:-missing", "-encoding", "UTF8")); |
| 81 | + if (!classpath.isEmpty()) { |
| 82 | + options.add("-cp"); |
| 83 | + options.add(classpath.stream().map(Path::toAbsolutePath).map(String::valueOf).collect(Collectors.joining(File.pathSeparator))); |
| 84 | + } |
| 85 | + |
| 86 | + Path outputTo = Files.createTempDirectory("output-dir"); |
| 87 | + tempDirs.add(outputTo); |
| 88 | + |
| 89 | + options.addAll(List.of("-d", outputTo.toAbsolutePath().toString())); |
| 90 | + |
| 91 | + sources.forEach(obj -> options.add(obj.getName())); |
| 92 | + |
| 93 | + Writer writer = new StringWriter(); |
| 94 | + DocumentationTool.DocumentationTask task = tool.getTask(writer, fileManager, null, null, options, sources); |
| 95 | + Boolean result = task.call(); |
| 96 | + if (result == null || !result) { |
| 97 | + System.err.println(writer); |
| 98 | + return; |
| 99 | + } |
| 100 | + |
| 101 | + try (OutputStream os = Files.newOutputStream(out); |
| 102 | + ZipOutputStream zos = new ZipOutputStream(os)) { |
| 103 | + Files.walk(outputTo) |
| 104 | + .sorted(Comparator.naturalOrder()) |
| 105 | + .forEachOrdered(path -> { |
| 106 | + if (path.equals(outputTo)) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + try { |
| 111 | + if (Files.isDirectory(path)) { |
| 112 | + String name = outputTo.relativize(path) + "/"; |
| 113 | + ZipEntry entry = new ZipEntry(name); |
| 114 | + zos.putNextEntry(entry); |
| 115 | + zos.closeEntry(); |
| 116 | + } else { |
| 117 | + String name = outputTo.relativize(path).toString(); |
| 118 | + ZipEntry entry = new ZipEntry(name); |
| 119 | + zos.putNextEntry(entry); |
| 120 | + try (InputStream is = Files.newInputStream(path)) { |
| 121 | + is.transferTo(zos); |
| 122 | + } |
| 123 | + zos.closeEntry(); |
| 124 | + } |
| 125 | + } catch (IOException e) { |
| 126 | + throw new UncheckedIOException(e); |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | + } finally { |
| 132 | + tempDirs.forEach(d -> { |
| 133 | + try { |
| 134 | + Files.walk(d) |
| 135 | + .sorted(Comparator.reverseOrder()) |
| 136 | + .map(Path::toFile) |
| 137 | + .forEach(File::delete); |
| 138 | + } catch (IOException e) { |
| 139 | + throw new UncheckedIOException(e); |
| 140 | + } |
| 141 | + }); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private static void readSourceFiles( |
| 146 | + Path unpackTo, |
| 147 | + StandardJavaFileManager fileManager, |
| 148 | + Set<Path> sourceJars, |
| 149 | + Set<JavaFileObject> sources, |
| 150 | + Set<String> topLevelPackages, |
| 151 | + Set<String> fileNames) throws IOException { |
| 152 | + |
| 153 | + for (Path jar : sourceJars) { |
| 154 | + if (!Files.exists(jar)) { |
| 155 | + continue; |
| 156 | + } |
| 157 | + |
| 158 | + try (ZipInputStream zis = new ZipInputStream(Files.newInputStream(jar))) { |
| 159 | + for (ZipEntry entry = zis.getNextEntry(); entry != null; entry = zis.getNextEntry()) { |
| 160 | + String name = entry.getName(); |
| 161 | + if (!name.endsWith(".java")) { |
| 162 | + continue; |
| 163 | + } |
| 164 | + |
| 165 | + Path target = unpackTo.resolve(name).normalize(); |
| 166 | + if (!target.startsWith(unpackTo)) { |
| 167 | + throw new IOException("Attempt to write out of working directory"); |
| 168 | + } |
| 169 | + |
| 170 | + Files.createDirectories(target.getParent()); |
| 171 | + try (OutputStream out = Files.newOutputStream(target)) { |
| 172 | + zis.transferTo(out); |
| 173 | + } |
| 174 | + |
| 175 | + fileManager.getJavaFileObjects(target).forEach(sources::add); |
| 176 | + |
| 177 | + String[] segments = name.split("/"); |
| 178 | + if (segments.length > 0 && !"META-INF".equals(segments[0])) { |
| 179 | + topLevelPackages.add(segments[0]); |
| 180 | + } |
| 181 | + |
| 182 | + fileNames.add(name); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments