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