1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.rat.tools;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27 import java.nio.charset.StandardCharsets;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.List;
33 import java.util.Map;
34 import java.util.function.Predicate;
35 import java.util.function.Supplier;
36 import java.util.stream.Collectors;
37
38
39 import org.apache.commons.io.IOUtils;
40 import org.apache.commons.lang3.StringUtils;
41 import org.apache.rat.OptionCollection;
42 import org.apache.rat.commandline.Arg;
43 import org.apache.rat.utils.DefaultLog;
44
45 import static java.lang.String.format;
46
47
48
49
50 public final class AntDocumentation {
51
52 private final File outputDir;
53
54
55
56
57
58
59
60
61
62
63 public static void main(final String[] args) throws IOException {
64
65 if (args.length == 0) {
66 System.err.println("Output directory must be specified");
67 System.exit(1);
68 }
69 File outputDir = new File(args[0]);
70 if (outputDir.exists()) {
71 if (!outputDir.isDirectory()) {
72 DefaultLog.getInstance().error(format("%s is not a directory", args[0]));
73 System.exit(1);
74 }
75 } else {
76 if (!outputDir.mkdirs()) {
77 DefaultLog.getInstance().error(format("Can not create directory %s", args[0]));
78 System.exit(1);
79 }
80 }
81 new AntDocumentation(outputDir).execute();
82 }
83
84 private AntDocumentation(final File outputDir) {
85 this.outputDir = outputDir;
86 }
87
88 public void execute() {
89 List<AntOption> options = Arg.getOptions().getOptions().stream().filter(AntGenerator.getFilter()).map(AntOption::new)
90 .collect(Collectors.toList());
91
92 writeAttributes(options);
93 writeElements(options);
94 printValueTypes();
95 }
96
97 public void writeAttributes(final List<AntOption> options) {
98 File f = new File(outputDir, "report_attributes.txt");
99 try (Writer out = new OutputStreamWriter(Files.newOutputStream(f.toPath()), StandardCharsets.UTF_8)) {
100 printOptions(out, options, AntOption::isAttribute,
101 "The attribute value types are listed in a table at the bottom of this page.");
102 } catch (IOException e) {
103 throw new RuntimeException(e);
104 }
105 }
106
107 public void writeElements(final List<AntOption> options) {
108 File f = new File(outputDir, "report_elements.txt");
109 try (Writer out = new OutputStreamWriter(Files.newOutputStream(f.toPath()), StandardCharsets.UTF_8)) {
110 printOptions(out, options, AntOption::isElement,
111 "The element value types are listed in a table at the bottom of this page.");
112 } catch (IOException e) {
113 throw new RuntimeException(e);
114 }
115 }
116 private void printOptions(final Writer out, final List<AntOption> options,
117 final Predicate<AntOption> typeFilter, final String tableCaption) throws IOException {
118 boolean hasDeprecated = options.stream().anyMatch(typeFilter.and(AntOption::isDeprecated));
119
120 if (hasDeprecated) {
121 AptFormat.writeHeader(out, 2, "Current");
122 }
123
124 List<List<String>> table = new ArrayList<>();
125 table.add(Arrays.asList("Name", "Description", "Value Type", "Required"));
126 options.stream().filter(typeFilter.and(o -> !o.isDeprecated()))
127 .map(o -> Arrays.asList(o.getName(), o.getDescription(),
128 o.hasArg() ? StringUtils.defaultIfEmpty(o.getArgName(), "String") : "boolean",
129 o.isRequired() ? "true" : "false"))
130 .forEach(table::add);
131
132 AptFormat.writeTable(out, table, "*--+--+--+--+", tableCaption);
133
134 if (hasDeprecated) {
135 AptFormat.writeHeader(out, 2, "Deprecated ");
136
137 table.clear();
138 table.add(Arrays.asList("Name", "Description", "Argument Type", "Deprecated"));
139
140 options.stream().filter(typeFilter.and(AntOption::isDeprecated))
141 .map(o -> Arrays.asList(o.getName(), o.getDescription(),
142 o.hasArg() ? StringUtils.defaultIfEmpty(o.getArgName(), "String") : "boolean",
143 o.getDeprecated()))
144 .forEach(table::add);
145
146 AptFormat.writeTable(out, table, "*--+--+--+--+", tableCaption);
147 }
148 }
149
150 private void printValueTypes() {
151
152 File f = new File(outputDir, "report_arg_types.txt");
153 try (Writer writer = new OutputStreamWriter(Files.newOutputStream(f.toPath()), StandardCharsets.UTF_8)) {
154
155 List<List<String>> table = new ArrayList<>();
156 table.add(Arrays.asList("Value Type", "Description"));
157
158 for (Map.Entry<String, Supplier<String>> argInfo : OptionCollection.getArgumentTypes().entrySet()) {
159 table.add(Arrays.asList(argInfo.getKey(), argInfo.getValue().get()));
160 }
161
162 AptFormat.writeTable(writer, table, "*--+--+");
163
164 } catch (IOException e) {
165 throw new RuntimeException(e);
166 }
167 }
168
169
170
171
172 private static class AptFormat {
173
174
175
176
177
178
179 public static void writeLicense(final Writer writer) throws IOException {
180 try (InputStream in = AntDocumentation.class.getResourceAsStream("/license.apt")) {
181 if (in == null) {
182 throw new FileNotFoundException("Could not find license.apt");
183 }
184 IOUtils.copy(in, writer, StandardCharsets.UTF_8);
185 }
186 }
187
188
189
190
191
192
193
194 public static void writeTitle(final Writer writer, final String title) throws IOException {
195 writer.write(format(" -----%n %1$s%n -----%n%n%1$s%n%n", title));
196 }
197
198
199
200
201
202
203
204 public static void writePara(final Writer writer, final String paragraph) throws IOException {
205 writer.write(format(" %s%n%n", paragraph));
206 }
207
208
209
210
211
212
213
214
215 public static void writeHeader(final Writer writer, final int level, final String text) throws IOException {
216 writer.write(System.lineSeparator());
217 for (int i = 0; i < level; i++) {
218 writer.write("*");
219 }
220 writer.write(format(" %s%n%n", text));
221 }
222
223
224
225
226
227
228
229 public static void writeList(final Writer writer, final Collection<String> list) throws IOException {
230 for (String s : list) {
231 writer.write(format(" * %s%n", s));
232 }
233 writer.write(System.lineSeparator());
234 }
235
236
237
238
239
240
241
242
243
244 public static void writeTable(final Writer writer, final Collection<? extends Collection<String>> table,
245 final String pattern, final String caption) throws IOException {
246 writer.write(format("%s%n", pattern));
247 for (Collection<String> row : table) {
248 for (String cell : row) {
249 writer.write(format("| %s ", cell));
250 }
251 writer.write(format("|%n%s%n", pattern));
252 }
253 if (caption != null) {
254 writer.write(caption);
255 }
256 writer.write(System.lineSeparator());
257 }
258
259
260
261
262
263
264
265
266 public static void writeTable(final Writer writer, final Collection<? extends Collection<String>> table,
267 final String pattern) throws IOException {
268 writeTable(writer, table, pattern, null);
269 }
270 }
271 }