/*
 * 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.pdfbox.examples.ant;

import java.io.File;

import java.util.ArrayList;
import java.util.List;

import org.apache.pdfbox.tools.ExtractText;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Task;

import org.apache.tools.ant.types.FileSet;

/**
 * This is an Ant task that will allow pdf documents to be converted using an
 * Ant task.
 *
 * @author Ben Litchfield
 */
public class PDFToTextTask extends Task
{
    private final List<FileSet> fileSets = new ArrayList<>();

    /**
     * Adds a set of files (nested fileset attribute).
     *
     * @param set Another fileset to add.
     */
    public void addFileset( FileSet set )
    {
        fileSets.add( set );
    }

    /**
     * This will perform the execution.
     */
    @Override
    public void execute()
    {
        log( "PDFToTextTask executing" );

        for (FileSet fileSet : fileSets)
        {
            DirectoryScanner dirScanner = fileSet.getDirectoryScanner(getProject());
            dirScanner.scan();
            String[] files = dirScanner.getIncludedFiles();
            for (String file : files)
            {
                File f = new File(dirScanner.getBasedir(), file);
                log( "processing: " + f.getAbsolutePath() );
                String pdfFile = f.getAbsolutePath();
                if( pdfFile.toUpperCase().endsWith( ".PDF" ) )
                {
                    String textFile = pdfFile.substring( 0, pdfFile.length() -3 );
                    textFile = textFile + "txt";
                    try
                    {
                        ExtractText.main(new String[]{pdfFile, textFile});
                    }
                    catch( Exception e )
                    {
                        log( "Error processing " + pdfFile + e.getMessage() );
                    }
                }
            }

        }
    }
}

@@ -857,16 +857,44 @@
 io.File;
+%0Aimport java.io.IOException;
 %0A%0Aimport
@@ -987,54 +987,8 @@
 xt;%0A
-import org.apache.tools.ant.DirectoryScanner;%0A
 impo
@@ -1143,19 +1143,16 @@
 using an
-%0A *
  Ant tas
@@ -1654,91 +1654,52 @@
    f
-or (F
 ileSet
- fileSet : fileSets)%0A        %7B%0A            DirectoryScanner dirScanner =
+s.stream().%0A            map(fileSet -%3E
  fil
@@ -1736,18 +1736,78 @@
 oject())
-;%0A
+).%0A            forEachOrdered(dirScanner -%3E%0A            %7B%0A    
         
@@ -1845,24 +1845,30 @@
     
+    for (
 String
-%5B%5D
  file
-s =
+ :
  dir
@@ -1893,17 +1893,17 @@
 dFiles()
-;
+)
 %0A       
@@ -1911,33 +1911,13 @@
     
-for (String file : files)
+    %7B
 %0A   
@@ -1914,37 +1914,32 @@
      %7B%0A         
-   %7B%0A
                 
@@ -1933,21 +1933,16 @@
         
-     
 File f =
@@ -1999,21 +1999,24 @@
         
+    
 log(
- 
 %22process
@@ -2043,20 +2043,23 @@
 tePath()
- 
 );%0A
+    
         
@@ -2124,12 +2124,16 @@
     
-if( 
+    if (
 pdfF
@@ -2163,18 +2163,15 @@
 ith(
- 
 %22.PDF%22
- ) 
+)
 )%0A  
@@ -2176,34 +2176,40 @@
                 
-%7B%0A
+    %7B%0A  
                 
@@ -2208,24 +2208,26 @@
             
+  
 String textF
@@ -2250,17 +2250,16 @@
 bstring(
- 
 0, pdfFi
@@ -2275,52 +2275,11 @@
 () -
-3 );%0A                    textFile = textFile
+ 3)
  + %22
@@ -2304,16 +2304,20 @@
         
+    
 try%0A    
@@ -2324,34 +2324,42 @@
                 
-%7B%0A
+    %7B%0A    
                 
@@ -2395,16 +2395,17 @@
 String%5B%5D
+ 
 %7BpdfFile
@@ -2430,34 +2430,42 @@
                 
-%7D%0A
+    %7D%0A    
                 
@@ -2473,18 +2473,20 @@
    catch
-(
  
+(IO
 Exceptio
@@ -2488,19 +2488,22 @@
 eption e
- 
 )%0A
+    
         
@@ -2540,21 +2540,24 @@
         
+    
 log(
- 
 %22Error p
@@ -2594,20 +2594,45 @@
 essage()
- 
 );%0A
+                        %7D%0A
         
@@ -2680,19 +2680,10 @@
    %7D
-%0A%0A        %7D
+);
 %0A   

use functional syntax