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

kwin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven-doxia-converter.git


The following commit(s) were added to refs/heads/master by this push:
     new c9053c3  [DOXIATOOLS-88] Optionally remove input files after 
conversion (#85)
c9053c3 is described below

commit c9053c39dc9fd89b2fc9f9cc5e6e98c32b98a86f
Author: Konrad Windszus <k...@apache.org>
AuthorDate: Mon Nov 11 17:56:58 2024 +0100

    [DOXIATOOLS-88] Optionally remove input files after conversion (#85)
---
 .../org/apache/maven/doxia/DefaultConverter.java   | 22 ++++++++++---
 .../org/apache/maven/doxia/cli/CLIManager.java     |  6 ++++
 .../org/apache/maven/doxia/cli/ConverterCli.java   |  3 +-
 .../maven/doxia/wrapper/InputFileWrapper.java      | 38 +++++++++++++++++++---
 4 files changed, 59 insertions(+), 10 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..250d968 100644
--- a/src/main/java/org/apache/maven/doxia/DefaultConverter.java
+++ b/src/main/java/org/apache/maven/doxia/DefaultConverter.java
@@ -228,7 +228,8 @@ public class DefaultConverter implements Converter {
 
         try {
             if (input.getFile().isFile()) {
-                parse(input.getFile(), input.getEncoding(), input.getFormat(), 
output);
+                convert(input.getFile(), input.getEncoding(), 
input.getFormat(), output);
+                cleanUp(input, input.getFile());
             } else {
                 List<File> files;
                 try {
@@ -248,7 +249,8 @@ public class DefaultConverter implements Converter {
                 for (File f : files) {
                     File relativeOutputDirectory = new File(
                             
PathTool.getRelativeFilePath(input.getFile().getAbsolutePath(), f.getParent()));
-                    parse(f, input.getEncoding(), input.getFormat(), output, 
relativeOutputDirectory);
+                    convert(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 successful 
conversion", f);
+        }
+    }
+
     /** {@inheritDoc} */
     @Override
     public void convert(InputReaderWrapper input, OutputStreamWrapper output)
@@ -317,9 +325,9 @@ public class DefaultConverter implements Converter {
      * @throws ConverterException if any
      * @throws UnsupportedFormatException if any
      */
-    private void parse(File inputFile, String inputEncoding, DoxiaFormat 
parserFormat, OutputFileWrapper output)
+    private void convert(File inputFile, String inputEncoding, DoxiaFormat 
parserFormat, OutputFileWrapper output)
             throws ConverterException, UnsupportedFormatException {
-        parse(inputFile, inputEncoding, parserFormat, output, null);
+        convert(inputFile, inputEncoding, parserFormat, output, null);
     }
 
     /**
@@ -331,7 +339,7 @@ public class DefaultConverter implements Converter {
      * @throws ConverterException if any
      * @throws UnsupportedFormatException if any
      */
-    private void parse(
+    private void convert(
             File inputFile,
             String inputEncoding,
             DoxiaFormat parserFormat,
@@ -426,6 +434,10 @@ public class DefaultConverter implements Converter {
                 throw new ConverterException("IOException: " + e.getMessage(), 
e);
             }
         }
+        LOGGER.info(
+                "Successfully converted file \"{}\" to \"{}\"",
+                inputFile.getAbsolutePath(),
+                outputFile.getAbsolutePath());
     }
 
     /**
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..e8a0759 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(REMOVE_IN)
+                .longOpt("removeInputAfterConversion")
+                .desc("Whether to remove the input file(s) after successful 
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..2b20c1c 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,13 @@ 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 +72,42 @@ 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