package processor;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Created by chiranz on 7/14/17.
 */
public class Extactor {
    private static final Logger log = Logger.getLogger(Extactor.class.getName());

    private String PATTERN = ".";

    public List<Element> getElements(String bookPath, String pattern) throws Exception {
        PATTERN = pattern;
        final int[] total = {0};
        List<Element> elementsList = new ArrayList<>();
        List<File> files = getFiles(bookPath, "XHTML");
        files.stream().forEach(p -> {
                    try {
                        Document doc = Jsoup.parse(p, "UTF-8");
                        Elements elements = doc.select(PATTERN);
//                        List<String> ids = divs.stream().map(n ->
//                                n.attr("id")
//                        ).collect(Collectors.toList());
                        elementsList.addAll(elements);
                        elements.forEach(element -> {
                            log.log(Level.INFO, p.getName() + " : \n" + element.toString());
//                            System.out.println(p.getName()+" : "+element.toString());

                        });
                        total[0] += elements.size();
//                        System.out.println(p.getName()+" has  "+elements.size()+" elements");
                        log.log(Level.INFO, p.getName() + " has  " + elements.size() + " elements");

                    } catch (IOException e) {
                        log.log(Level.SEVERE, e.getMessage());

                        e.printStackTrace();
                    }
                }
        );
//        System.out.println(bookPath + " has  " + total[0] + " elements");
        log.log(Level.INFO, bookPath + " has  " + total[0] + " elements");

        return elementsList;
    }

    public List<File> getFiles(String path, String extention) throws IOException {
        return Files.walk(Paths.get(path)).filter(p -> p.toString().toUpperCase().endsWith("." + extention.toUpperCase())).distinct().
                map(n -> n.toFile()).collect(Collectors.toList());
    }
}

@@ -1033,16 +1033,172 @@
 TTERN);%0A
+                        if(elements.size()%3E0)%0A                        System.out.println(%22*************************************************************%22);%0A%0A
 //      
@@ -1503,70 +1503,296 @@
     
-log.log(Level.INFO, p.getName() + %22 : %5Cn%22 + element.toString()
+System.out.println(%22+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%22);%0A                            log.log(Level.INFO, p.getName() + %22 : %5Cn%22 + element.toString());%0A                            System.out.println(%22+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++%22
 );%0A/
@@ -1960,16 +1960,176 @@
 size();%0A
+                       if(elements.size()%3E0) %7B%0A                            System.out.println(%22%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%22);%0A
 //      
@@ -2232,32 +2232,36 @@
                 
+    
 log.log(Level.IN
@@ -2313,32 +2313,325 @@
 + %22 elements%22);%0A
+                            System.out.println(%22%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%25%22);%0A                        %7D%0A                        if(elements.size()%3E0)%0A                        System.out.println(%22*************************************************************%22);
 %0A               
@@ -2784,32 +2784,33 @@
               %7D%0A
+%0A
                 

add console seperators