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