View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * A simple tool to convert CLI options into an Ant report base class.
49   */
50  public final class AntDocumentation {
51      /** The directory to write to. */
52      private final File outputDir;
53  
54      /**
55       * Creates apt documentation files for Ant.
56       * Requires 1 argument:
57       * <ol>
58       *     <li>the directory in which to write the documentation files.</li>
59       * </ol>
60       * @param args the arguments.
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      * A class to write APT formatted text.
170      */
171     private static final class AptFormat  {
172 
173         /**
174          * Copy the "license.apt" from the resources to the writer.
175          * @param writer the writer to write to.
176          * @throws IOException on error.
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          * Write a title.
189          * @param writer the writer to write to.
190          * @param title the title to write.
191          * @throws IOException on error.
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          * Write a paragraph.
199          * @param writer the writer to write to.
200          * @param paragraph the paragraph to write.
201          * @throws IOException on error.
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          * Write a header.
209          * @param writer the writer to write to.
210          * @param level the level of the header
211          * @param text the text for the header
212          * @throws IOException on error.
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          * Write a list.
224          * @param writer the writer to write to.
225          * @param list the list to write.
226          * @throws IOException on error.
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          * Write a table.
237          * @param writer the Writer to write to.
238          * @param table the Table to write. A collection of collections of Strings.
239          * @param pattern the pattern before and after the table.
240          * @param caption the caption for the table.
241          * @throws IOException on error.
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          * Write a table entry.
260          * @param writer the Writer to write to.
261          * @param table the Table to write
262          * @param pattern the pattern before and after the table.
263          * @throws IOException on error
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 }