/*
 * 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.tika.example;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashSet;

import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.sax.BodyContentHandler;
import org.apache.tika.sax.PhoneExtractingContentHandler;

/**
 * Class to demonstrate how to use the {@link org.apache.tika.sax.PhoneExtractingContentHandler}
 * to get a list of all of the phone numbers from every file in a directory.
 * <p>
 * You can run this main method by running
 * <code>
 *     mvn exec:java -Dexec.mainClass="org.apache.tika.example.GrabPhoneNumbersExample" -Dexec.args="/path/to/directory"
 * </code>
 * from the tika-example directory.
 */
public class GrabPhoneNumbersExample {
    private static HashSet<String> phoneNumbers = new HashSet<String>();
    private static int failedFiles, successfulFiles = 0;

    public static void main(String[] args) {
        if (args.length != 1) {
            System.err.println("Usage `java GrabPhoneNumbers [corpus]");
            return;
        }
        final File folder = new File(args[0]);
        System.out.println("Searching " + folder.getAbsolutePath() + "...");
        processFolder(folder);
        System.out.println(phoneNumbers.toString());
        System.out.println("Parsed " + successfulFiles + "/" + (successfulFiles + failedFiles));
    }

    public static void processFolder(final File folder) {
        for (final File fileEntry : folder.listFiles()) {
            if (fileEntry.isDirectory()) {
                processFolder(fileEntry);
            } else {
                try {
                    process(fileEntry);
                    successfulFiles++;
                } catch (Exception e) {
                    failedFiles++;
                    // Ignore this file...
                }
            }
        }
    }

    public static void process(File file) throws Exception {
        Parser parser = new AutoDetectParser();
        Metadata metadata = new Metadata();
        // The PhoneExtractingContentHandler will examine any characters for phone numbers before passing them
        // to the underlying Handler.
        PhoneExtractingContentHandler handler = new PhoneExtractingContentHandler(new BodyContentHandler(), metadata);
        try (InputStream stream = new FileInputStream(file)) {
            parser.parse(stream, handler, metadata, new ParseContext());
        }
        String[] numbers = metadata.getValues("phonenumbers");
        for (String number : numbers) {
            phoneNumbers.add(number);
        }
    }
}

@@ -849,33 +849,72 @@
 .io.
-File;%0Aimport java.io.File
+BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.
 Inpu
@@ -930,38 +930,253 @@
 import java.
+n
 io.
-InputStream
+file.FileVisitResult;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.Paths;%0Aimport java.nio.file.SimpleFileVisitor;%0Aimport java.nio.file.attribute.BasicFileAttributes;%0Aimport java.util.Collections
 ;%0Aimport jav
@@ -1981,22 +1981,16 @@
 HashSet%3C
-String
 %3E();%0A   
@@ -2232,26 +2232,20 @@
         
-final File
+Path
  folder 
@@ -2246,24 +2246,25 @@
 older = 
-new File
+Paths.get
 (args%5B0%5D
@@ -2315,19 +2315,18 @@
  folder.
-ge
 t
+o
 Absolute
@@ -2567,26 +2567,20 @@
 sFolder(
-final File
+Path
  folder)
@@ -2594,164 +2594,223 @@
     
-for (final File fileEntry : folder.listFiles()) %7B%0A            if (fileEntry.isDirectory()) %7B%0A                processFolder(fileEntry);%0A            %7D else %7B%0A
+try %7B%0A            Files.walkFileTree(folder, new SimpleFileVisitor%3CPath%3E() %7B%0A                @Override%0A                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException %7B%0A    
     
@@ -2843,24 +2843,28 @@
             
+    
 process(file
@@ -2867,16 +2867,15 @@
 file
-Entry);%0A
+);%0A    
     
@@ -2925,16 +2925,20 @@
         
+   
+ 
 %7D catch 
@@ -2973,16 +2973,20 @@
         
+    
 failedFi
@@ -2984,32 +2984,36 @@
  failedFiles++;%0A
+    
                 
@@ -3019,17 +3019,17 @@
      // 
-I
+i
 gnore th
@@ -3039,43 +3039,418 @@
 file
-...%0A                %7D%0A            %7D
+%0A                    %7D%0A                    return FileVisitResult.CONTINUE;%0A                %7D%0A%0A                @Override%0A                public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException %7B%0A                    failedFiles++;%0A                    return FileVisitResult.CONTINUE;%0A                %7D%0A            %7D);%0A        %7D catch (IOException e) %7B%0A            // ignore failure
 %0A   
@@ -3498,17 +3498,17 @@
 ess(
-File file
+Path path
 ) th
@@ -3922,20 +3922,24 @@
 m = new 
-File
+Buffered
 InputStr
@@ -3942,20 +3942,42 @@
 tStream(
-f
+F
 ile
+s.newInputStream(path)
 )) %7B%0A   
@@ -4131,52 +4131,27 @@
     
-for (String number : numbers) %7B%0A            
+Collections.addAll(
 phon
@@ -4162,32 +4162,20 @@
 bers
-.add(
+, 
 number
+s
 );%0A
-        %7D%0A
     

Migrate phone numbers example to file walk API