This is an automated email from the ASF dual-hosted git repository.

kwin pushed a commit to branch 
feature/optionally-remove-input-file-after-conversion
in repository https://gitbox.apache.org/repos/asf/maven-doxia-converter.git

commit 544e6dfd87f7edf7488d192a1fcdbebf28b942a3
Author: Konrad Windszus <k...@apache.org>
AuthorDate: Tue Oct 29 18:03:24 2024 +0100

    [DOXIATOOLS-88] Optionally remove input files after conversion
---
 .../org/apache/maven/doxia/DefaultConverter.java   |  8 +++++
 .../org/apache/maven/doxia/cli/CLIManager.java     |  6 ++++
 .../org/apache/maven/doxia/cli/ConverterCli.java   |  3 +-
 .../maven/doxia/wrapper/InputFileWrapper.java      | 35 +++++++++++++++++++---
 4 files changed, 47 insertions(+), 5 deletions(-)

diff --git a/src/main/java/org/apache/maven/doxia/DefaultConverter.java 
b/src/main/java/org/apache/maven/doxia/DefaultConverter.java
index 11a8783..8ad892b 100644
--- a/src/main/java/org/apache/maven/doxia/DefaultConverter.java
+++ b/src/main/java/org/apache/maven/doxia/DefaultConverter.java
@@ -229,6 +229,7 @@ public class DefaultConverter implements Converter {
         try {
             if (input.getFile().isFile()) {
                 parse(input.getFile(), input.getEncoding(), input.getFormat(), 
output);
+                cleanUp(input, input.getFile());
             } else {
                 List<File> files;
                 try {
@@ -249,6 +250,7 @@ public class DefaultConverter implements Converter {
                     File relativeOutputDirectory = new File(
                             
PathTool.getRelativeFilePath(input.getFile().getAbsolutePath(), f.getParent()));
                     parse(f, input.getEncoding(), input.getFormat(), output, 
relativeOutputDirectory);
+                    cleanUp(input, f);
                 }
             }
         } finally {
@@ -256,6 +258,12 @@ public class DefaultConverter implements Converter {
         }
     }
 
+    private void cleanUp(InputFileWrapper input, File f) {
+        if (input.cleanUp(f)) {
+            LOGGER.info("Removed input file \"{}\" after successfull 
conversion", f);
+        }
+    }
+
     /** {@inheritDoc} */
     @Override
     public void convert(InputReaderWrapper input, OutputStreamWrapper output)
diff --git a/src/main/java/org/apache/maven/doxia/cli/CLIManager.java 
b/src/main/java/org/apache/maven/doxia/cli/CLIManager.java
index dbee83e..0c7d626 100644
--- a/src/main/java/org/apache/maven/doxia/cli/CLIManager.java
+++ b/src/main/java/org/apache/maven/doxia/cli/CLIManager.java
@@ -47,6 +47,8 @@ class CLIManager {
     /** in String */
     static final String IN = "in";
 
+    static final String REMOVE_IN = "removeIn";
+
     /** out String */
     static final String OUT = "out";
 
@@ -93,6 +95,10 @@ class CLIManager {
                 .desc("Input file or directory.")
                 .hasArg()
                 .build());
+        OPTIONS.addOption(Option.builder(IN)
+                .longOpt("removeInputAfterConversion")
+                .desc("Whether to remove the input file(s) after successfull 
conversion")
+                .build());
         OPTIONS.addOption(Option.builder(OUT)
                 .longOpt("output")
                 .desc("Output file or directory.")
diff --git a/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java 
b/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java
index 2998a3e..3ad605b 100644
--- a/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java
+++ b/src/main/java/org/apache/maven/doxia/cli/ConverterCli.java
@@ -132,7 +132,8 @@ public class ConverterCli {
             input = InputFileWrapper.valueOf(
                     commandLine.getOptionValue(CLIManager.IN),
                     parserFormat,
-                    commandLine.getOptionValue(CLIManager.INENCODING));
+                    commandLine.getOptionValue(CLIManager.INENCODING),
+                    commandLine.hasOption(CLIManager.REMOVE_IN));
             output = OutputFileWrapper.valueOf(
                     commandLine.getOptionValue(CLIManager.OUT),
                     sinkFormat,
diff --git a/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java 
b/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java
index a0dab50..b47bc4c 100644
--- a/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java
+++ b/src/main/java/org/apache/maven/doxia/wrapper/InputFileWrapper.java
@@ -18,6 +18,7 @@
  */
 package org.apache.maven.doxia.wrapper;
 
+import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.UnsupportedEncodingException;
 
@@ -35,6 +36,11 @@ public class InputFileWrapper extends AbstractFileWrapper {
 
     private final DefaultConverter.DoxiaFormat format;
 
+    /**
+     * If true, the related input file(s) will be removed after the conversion.
+     */
+    private final boolean removeAfterConversion;
+
     /**
      * Private constructor.
      *
@@ -44,11 +50,12 @@ public class InputFileWrapper extends AbstractFileWrapper {
      * @throws UnsupportedEncodingException if the encoding is unsupported.
      * @throws FileNotFoundException if the file for absolutePath is not found.
      */
-    private InputFileWrapper(String absolutePath, DefaultConverter.DoxiaFormat 
format, String charsetName)
+    private InputFileWrapper(String absolutePath, DefaultConverter.DoxiaFormat 
format, String charsetName, boolean removeAfterConversion)
             throws UnsupportedEncodingException, FileNotFoundException {
         super(absolutePath, charsetName);
 
         this.format = format;
+        this.removeAfterConversion = removeAfterConversion;
         if (!getFile().exists()) {
             throw new FileNotFoundException("The file '" + 
getFile().getAbsolutePath() + "' doesn't exist.");
         }
@@ -64,20 +71,40 @@ public class InputFileWrapper extends AbstractFileWrapper {
      */
     public static InputFileWrapper valueOf(String absolutePath, 
DefaultConverter.DoxiaFormat format)
             throws UnsupportedEncodingException, FileNotFoundException {
-        return valueOf(absolutePath, format, WriterFactory.UTF_8);
+        return valueOf(absolutePath, format, WriterFactory.UTF_8, false);
     }
 
+    /**
+     * Performs potential clean up operations on the given file
+     * @param file the file to clean up, not null.
+     * @return {@code true} if the file was removed.
+     */
+    public boolean cleanUp(File file) {
+        if (!file.toString().startsWith(getFile().toString())) {
+            throw new IllegalStateException("The given file " + file + " is 
not related to the input file: " + getFile());
+        }
+        if (removeAfterConversion) {
+            return file.delete();
+        }
+        return false;
+    }
+
+    public static InputFileWrapper valueOf(String absolutePath, 
DefaultConverter.DoxiaFormat format, String charsetName)
+            throws UnsupportedEncodingException, FileNotFoundException {
+        return valueOf(absolutePath, format, charsetName, false);
+    }
     /**
      * @param absolutePath for a wanted file or a wanted directory, not null.
      * @param format not null
      * @param charsetName could be null
+     * @param removeAfterConversion if true, the file will be removed after 
the conversion
      * @return a type safe input reader
      * @throws UnsupportedEncodingException if the encoding is unsupported.
      * @throws FileNotFoundException if the file for absolutePath is not found.
      */
-    public static InputFileWrapper valueOf(String absolutePath, 
DefaultConverter.DoxiaFormat format, String charsetName)
+    public static InputFileWrapper valueOf(String absolutePath, 
DefaultConverter.DoxiaFormat format, String charsetName, boolean 
removeAfterConversion)
             throws UnsupportedEncodingException, FileNotFoundException {
-        return new InputFileWrapper(absolutePath, format, charsetName);
+        return new InputFileWrapper(absolutePath, format, charsetName, 
removeAfterConversion);
     }
 
     public DefaultConverter.DoxiaFormat getFormat() {

Reply via email to