ottlinger commented on code in PR #257:
URL: https://github.com/apache/creadur-rat/pull/257#discussion_r1614892037


##########
apache-rat-tools/src/main/java/org/apache/rat/tools/AntGenerator.java:
##########
@@ -0,0 +1,220 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.rat.tools;
+
+import org.apache.commons.cli.Option;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.io.LineIterator;
+import org.apache.commons.text.WordUtils;
+import org.apache.rat.OptionCollection;
+import org.apache.rat.utils.CasedString;
+import org.apache.rat.utils.CasedString.StringCase;
+
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileWriter;
+import java.io.Writer;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+
+import static java.lang.String.format;
+
+/**
+ * A simple tool to convert CLI options Ant report base class .
+ */
+public class AntGenerator {
+
+    /**
+     * The list of Options that are not supported by Ant.
+     */
+    private static final List<Option> ANT_FILTER_LIST = 
Arrays.asList(OptionCollection.HELP, OptionCollection.LOG_LEVEL,
+            OptionCollection.DIR);
+
+    /**
+     * the filter to filter out CLI options that Ant does not support.
+     */
+    public static Predicate<Option> ANT_FILTER = option -> 
!(ANT_FILTER_LIST.contains(option) || option.getLongOpt() == null);
+
+
+    private AntGenerator() {}
+
+    /**
+     * Creates a base class for an Ant task.
+     * Requires 3 arguments:
+     * <ol>
+     *     <li>the package name for the class</li>
+     *     <li>the simple class name</li>
+     *     <li>the directory in which to write the class file.</li>
+     * </ol>
+     * @param args the arguments.
+     * @throws IOException on error
+     */
+    public static void main(String[] args) throws IOException {
+        String packageName = args[0];
+        String className = args[1];
+        String destDir = args[2];
+
+        List<AntOption> options = 
OptionCollection.buildOptions().getOptions().stream().filter(ANT_FILTER).map(AntOption::new)
+                .collect(Collectors.toList());
+
+        String pkgName = String.join(File.separator,new 
CasedString(StringCase.DOT, packageName).getSegments());
+        File file = new File(new File(new File(destDir), 
pkgName),className+".java");
+        file.getParentFile().mkdirs();
+        try (InputStream template = 
AntGenerator.class.getResourceAsStream("/Ant.tpl");
+             FileWriter writer = new FileWriter(file);
+             ByteArrayOutputStream bos = new ByteArrayOutputStream();
+             OutputStreamWriter customClasses = new OutputStreamWriter(bos);
+        ) {
+            if (template == null) {
+                throw new RuntimeException("Template /Ant.tpl not found");
+            }
+            LineIterator iter = IOUtils.lineIterator(new 
InputStreamReader(template));

Review Comment:
   Should we add a Charset UTF_8 here? (Did spotbugs report this issue :)?)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to