Author: lehmi
Date: Thu Oct 26 06:01:14 2023
New Revision: 1913337

URL: http://svn.apache.org/viewvc?rev=1913337&view=rev
Log:
PDFBOX-5670: allow repeatable subcommands based on a proposal by Marcelo Modesto

Added:
    pdfbox/trunk/tools/src/test/resources/org/apache/pdfbox/hello3.pdf   (with 
props)
Modified:
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractText.java
    pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java
    
pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/TestExtractText.java

Modified: 
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractText.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractText.java?rev=1913337&r1=1913336&r2=1913337&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractText.java 
(original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ExtractText.java 
Thu Oct 26 06:01:14 2023
@@ -21,6 +21,7 @@ import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.OutputStreamWriter;
 import java.io.PrintStream;
+import java.io.PrintWriter;
 import java.io.Writer;
 import java.util.Map;
 import java.util.Set;
@@ -112,6 +113,12 @@ public final class ExtractText  implemen
     @Option(names = {"-o", "--output"}, description = "the exported text file")
     private File outfile;
 
+    @Option(names = "-addFileName", description = "Print PDF file name to the 
output text")
+    private boolean addFileName = false;
+
+    @Option(names = "-append", description = "Use append mode for output file")
+    private boolean append = false;
+
     /**
      * Constructor.
      */
@@ -150,8 +157,19 @@ public final class ExtractText  implemen
             outfile = new File(outPath);
         }
 
+        if (toHTML && !STD_ENCODING.equals(encoding))
+        {
+            encoding = STD_ENCODING;
+            SYSOUT.println("The encoding parameter is ignored when writing 
html output.");
+        }
+
+        if (toConsole && encoding != null)
+        {
+            SYSOUT.println("The encoding parameter is ignored when writing to 
the console.");
+        }
+
         try (PDDocument document = Loader.loadPDF(infile, password);
-             Writer output = toConsole ? new OutputStreamWriter( SYSOUT, 
encoding ) : new OutputStreamWriter( new FileOutputStream( outfile ), encoding 
))
+                Writer output = createOutputWriter())
         {
             long startTime = startProcessing("Loading PDF " + infile);
 
@@ -164,14 +182,14 @@ public final class ExtractText  implemen
             
             stopProcessing("Time for loading: ", startTime);
 
-            if (toHTML && !STD_ENCODING.equals(encoding))
+            startTime = startProcessing("Starting text extraction");
+
+            if (addFileName)
             {
-                encoding = STD_ENCODING;
-                SYSOUT.println("The encoding parameter is ignored when writing 
html output.");
+                output.write("PDF file: " + infile);
+                output.write(System.getProperty("line.separator"));
             }
 
-            startTime = startProcessing("Starting text extraction");
-
             if (debug)
             {
                 SYSERR.println("Writing to " + outfile.getAbsolutePath());
@@ -252,6 +270,7 @@ public final class ExtractText  implemen
                     }
                 }
             }
+            output.flush();
             stopProcessing("Time for extraction: ", startTime);
         }
         catch (IOException ioe)
@@ -263,6 +282,25 @@ public final class ExtractText  implemen
         return 0;
     }
 
+    private Writer createOutputWriter() throws IOException
+    {
+        if (toConsole)
+        {
+            return new PrintWriter(SYSOUT)
+            {
+                @Override
+                public void close()
+                {
+                    // don't close the console
+                };
+            };
+        }
+        else
+        {
+            return new OutputStreamWriter(new FileOutputStream(outfile, 
append), encoding);
+        }
+    }
+
     private void extractPages(int startPage, int endPage,
             PDFTextStripper stripper, PDDocument document, Writer output,
             boolean rotationMagic, boolean alwaysNext) throws IOException

Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java?rev=1913337&r1=1913336&r2=1913337&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java 
(original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java Thu 
Oct 26 06:01:14 2023
@@ -30,11 +30,10 @@ import picocli.CommandLine.Spec;
  * Used as the main class in the runnable standalone PDFBox jar.
  */
 @Command(name="pdfbox",
-    customSynopsis = "pdfbox [COMMAND] [OPTIONS]",
-    footer = {
-        "See 'pdfbox help <command>' to read about a specific subcommand"
-    },
-    versionProvider = Version.class)
+        subcommandsRepeatable = true,
+        customSynopsis = "pdfbox [COMMAND] [OPTIONS]",
+        footer = { "See 'pdfbox help <command>' to read about a specific 
subcommand" },
+        versionProvider = Version.class)
 public final class PDFBox implements Runnable
 {
     @Spec CommandLine.Model.CommandSpec spec;

Modified: 
pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/TestExtractText.java
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/TestExtractText.java?rev=1913337&r1=1913336&r2=1913337&view=diff
==============================================================================
--- 
pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/TestExtractText.java 
(original)
+++ 
pdfbox/trunk/tools/src/test/java/org/apache/pdfbox/tools/TestExtractText.java 
Thu Oct 26 06:01:14 2023
@@ -18,11 +18,20 @@ package org.apache.pdfbox.tools;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertFalse;
 
 import java.io.ByteArrayOutputStream;
 import java.io.PrintStream;
+import java.io.UnsupportedEncodingException;
+import java.nio.file.Path;
+import java.nio.file.Files;
+import java.nio.file.Paths;
+import java.nio.file.InvalidPathException;
 
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.io.TempDir;
 
 import picocli.CommandLine;
 
@@ -32,6 +41,36 @@ import picocli.CommandLine;
  */
 class TestExtractText
 {
+
+    final PrintStream originalOut = System.out;
+    final ByteArrayOutputStream out = new ByteArrayOutputStream();
+    PrintStream printStream = null;
+
+    @BeforeEach
+    public void setUpStreams()
+    {
+        out.reset();
+        try
+        {
+            printStream = new PrintStream(out, true, "utf-8");
+            System.setOut(printStream);
+        }
+        catch (UnsupportedEncodingException e)
+        {
+            // shouldn't happen at all
+            e.printStackTrace();
+        }
+    }
+
+    @AfterEach
+    public void restoreStreams()
+    {
+        System.setOut(originalOut);
+        if (printStream != null)
+        {
+            printStream.close();
+        }
+    }
     
     /**
      * Run the text extraction test using a pdf with embedded pdfs.
@@ -41,25 +80,177 @@ class TestExtractText
     @Test
     void testEmbeddedPDFs() throws Exception 
     {
-        ByteArrayOutputStream outBytes = new ByteArrayOutputStream();
-        PrintStream stdout = System.out;
-        System.setOut(new PrintStream(outBytes));
+        ExtractText app = new ExtractText();
+        CommandLine cmd = new CommandLine(app);
+        int exitCode = cmd.execute("-i", 
"src/test/resources/org/apache/pdfbox/testPDFPackage.pdf",
+                "-console");
+        assertEquals(0, exitCode);
+
+        String result = out.toString("UTF-8");
+        assertTrue(result.contains("PDF1"));
+        assertTrue(result.contains("PDF2"));
+        assertFalse(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertFalse(result.contains("Hello"));
+        assertFalse(result.contains("World."));
+        assertFalse(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
+    }
+
+    /**
+     * Run the text extraction with -addFileName
+     * 
+     * @throws Exception if something went wrong
+     */
+    @Test
+    void testAddFileName() throws Exception
+    {
+        ExtractText app = new ExtractText();
+        CommandLine cmd = new CommandLine(app);
+        int exitCode = cmd.execute("-i", 
"src/test/resources/org/apache/pdfbox/testPDFPackage.pdf",
+                "-console", "-addFileName");
+        assertEquals(0, exitCode);
+
+        String result = out.toString("UTF-8");
+        assertTrue(result.contains("PDF1"));
+        assertTrue(result.contains("PDF2"));
+        assertTrue(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertFalse(result.contains("Hello"));
+        assertFalse(result.contains("World."));
+        assertFalse(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
+    }
+
+    /**
+     * Run the text extraction as a PDFBox repeatable subcommand
+     * 
+     * @throws Exception if something went wrong
+     */
+    @Test
+    void testPDFBoxRepeatableSubcommand() throws Exception
+    {
+        // Please, copy from pdfbox/src/test/resources/input/hello3.pdf
+        
assertTrue(Files.exists(Paths.get("src/test/resources/org/apache/pdfbox/hello3.pdf")));
+
+        PDFBox.main(new String[] { "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/testPDFPackage.pdf", 
"-console",
+                "export:text", "-i", 
"src/test/resources/org/apache/pdfbox/hello3.pdf",
+                "-console" });
+
+        String result = out.toString("UTF-8");
+        assertTrue(result.contains("PDF1"));
+        assertTrue(result.contains("PDF2"));
+        assertFalse(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertTrue(result.contains("Hello"));
+        assertTrue(result.contains("World."));
+        assertFalse(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
+    }
+
+    /**
+     * Run the text extraction as a PDFBox repeatable subcommand with 
-addFileName
+     * 
+     * @throws Exception if something went wrong
+     */
+    @Test
+    void testPDFBoxRepeatableSubcommandAddFileName() throws Exception
+    {
+        
assertTrue(Files.exists(Paths.get("src/test/resources/org/apache/pdfbox/hello3.pdf")));
+
+        PDFBox.main(new String[] { "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/testPDFPackage.pdf", 
"-console",
+                "-addFileName", "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/hello3.pdf", "-console", 
"-addFileName" });
+
+        String result = out.toString("UTF-8");
+        assertTrue(result.contains("PDF1"));
+        assertTrue(result.contains("PDF2"));
+        assertTrue(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertTrue(result.contains("Hello"));
+        assertTrue(result.contains("World."));
+        assertTrue(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
+    }
+
+    /**
+     * Run the text extraction as a PDFBox repeatable subcommand with 
-addFileName, with -o <outfile> and without
+     * -append
+     * 
+     * @throws Exception if something went wrong
+     */
+    @Test
+    void testPDFBoxRepeatableSubcommandAddFileNameOutfile(@TempDir Path 
tempDir) throws Exception
+    {
+        
assertTrue(Files.exists(Paths.get("src/test/resources/org/apache/pdfbox/hello3.pdf")));
+
+        Path path = null;
+
+        try
+        {
+            path = tempDir.resolve("outfile.txt");
+            Files.deleteIfExists(path);
+        }
+        catch (InvalidPathException ipe)
+        {
+            System.err.println(
+                    "Error creating temporary test file in " + 
this.getClass().getSimpleName());
+        }
+        assertFalse(path == null);
+
+        PDFBox.main(new String[] { "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/testPDFPackage.pdf", 
"-encoding", "UTF-8",
+                "-addFileName", "-o", path.toString(), "export:text", "-o", 
path.toString(), "-i",
+                "src/test/resources/org/apache/pdfbox/hello3.pdf", 
"-encoding", "UTF-8",
+                "-addFileName" });
+
+        String result = new String(Files.readAllBytes(path), "UTF-8");
+        assertFalse(result.contains("PDF1"));
+        assertFalse(result.contains("PDF2"));
+        assertFalse(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertTrue(result.contains("Hello"));
+        assertTrue(result.contains("World."));
+        assertTrue(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
+    }
+
+    /**
+     * Run the text extraction as a PDFBox repeatable subcommand with 
-addFileName, -o <outfile> and -append
+     * 
+     * @throws Exception if something went wrong
+     */
+    @Test
+    void testPDFBoxRepeatableSubcommandAddFileNameOutfileAppend(@TempDir Path 
tempDir)
+            throws Exception
+    {
+        
assertTrue(Files.exists(Paths.get("src/test/resources/org/apache/pdfbox/hello3.pdf")));
+
+        Path path = null;
+
         try 
         {
-            ExtractText app = new ExtractText();
-            CommandLine cmd = new CommandLine(app);
-            int exitCode = cmd.execute("-i", 
"src/test/resources/org/apache/pdfbox/testPDFPackage.pdf",
-            "-console", "-encoding", "UTF-8");
-            assertEquals(0, exitCode);
-        } 
-        finally 
+            path = tempDir.resolve("outfile.txt");
+            Files.deleteIfExists(path);
+        }
+        catch (InvalidPathException ipe)
         {
-            // Restore stdout
-            System.setOut(stdout);
+            System.err.println(
+                    "Error creating temporary test file in " + 
this.getClass().getSimpleName());
         }
+        assertFalse(path == null);
 
-        String result = outBytes.toString("UTF-8");
+        PDFBox.main(new String[] { "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/testPDFPackage.pdf", 
"-encoding", "UTF-8",
+                "-addFileName", "-o", path.toString(), "export:text", "-i",
+                "src/test/resources/org/apache/pdfbox/hello3.pdf", 
"-encoding", "UTF-8",
+                "-addFileName", "-o", path.toString(), "-append" });
+
+        String result = new String(Files.readAllBytes(path), "UTF-8");
         assertTrue(result.contains("PDF1"));
         assertTrue(result.contains("PDF2"));
+        assertTrue(result
+                .contains("PDF file: 
src/test/resources/org/apache/pdfbox/testPDFPackage.pdf"));
+        assertTrue(result.contains("Hello"));
+        assertTrue(result.contains("World."));
+        assertTrue(result.contains("PDF file: 
src/test/resources/org/apache/pdfbox/hello3.pdf"));
     }
+
 }

Added: pdfbox/trunk/tools/src/test/resources/org/apache/pdfbox/hello3.pdf
URL: 
http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/test/resources/org/apache/pdfbox/hello3.pdf?rev=1913337&view=auto
==============================================================================
Binary file - no diff available.

Propchange: pdfbox/trunk/tools/src/test/resources/org/apache/pdfbox/hello3.pdf
------------------------------------------------------------------------------
    svn:mime-type = application/pdf


Reply via email to