/*
	ParserFileVisitor
 */

package ejbm.parser;

import java.io.*;
import java.nio.*;
import java.nio.file.*;
import java.nio.file.attribute.*;

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.contentstream.*;
import org.apache.pdfbox.text.*;

import static java.nio.file.FileVisitResult.*;

/**
 * Extends FileVistor to for a recursive search of journal articles for semantic parsing.
 */
public class ParserFileVisitor extends SimpleFileVisitor<Path> {

    private static String JOURNAL_NAME_SHORT = "EJBM Einstein J. Biol. Med.";
    private static String[] ARTICLE_TYPES = { "CASE REPORT" };

    private Path path;
    private String[] content;

    private String firstPage;
    private String lastPage;
    private String publicationYear;
    private String volumeNumber;
    private String articleType;

    @Override
    public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) {

        if (attrs.isRegularFile() || attrs.isSymbolicLink()) {
            if (path.toString().toLowerCase().endsWith(".pdf")) {

                this.path = path;

                try {
                    PDDocument pddDocument = PDDocument.load(path.toFile());
                    PDFTextStripper textStripper = new PDFTextStripper();
                    textStripper.setStartPage( 1 );
                    textStripper.setEndPage( 1 );
                    content = textStripper.getText(pddDocument).split("\\n");
                } catch (IOException e) {
                    e.printStackTrace();
                    System.err.println("Caught IOException in ParserFileVisitor visitFile().");
                }
                
                parseHeader();
                parseArticleType();

                printOutput();
            }
        }

        return CONTINUE;
    }

    private void printOutput() {
        System.out.println("-------- " + path.getFileName().toString() + " --------");
        System.out.println("First Page: " + firstPage);
        System.out.println("Last Page: " + lastPage);
        System.out.println("Publication Year: " + publicationYear);
        System.out.println("Volume No: " + volumeNumber);
        System.out.println();
        System.out.println("Article Type: " + articleType);
        System.out.println("--------");
    }

    private void parseHeader() {
        String tail = content[0].split(JOURNAL_NAME_SHORT + " \\(")[1];
        publicationYear = tail.split("\\)")[0];

        tail = tail.split("\\) ")[1];
        volumeNumber = tail.split("\\:")[0];

        tail = tail.split("\\:")[1];
        firstPage = tail.split("-")[0];

        lastPage = tail.split("-")[1];
    }

    private void parseArticleType() {
        articleType = "UKNOWN";
        for (String type : ARTICLE_TYPES) {
            System.out.println(content[1]);
            System.out.println(type);

            if (content[1].equals(type)) {
                articleType = type;
                break;
            }
        }
    }

    @Override
    public FileVisitResult postVisitDirectory(Path path, IOException ioException) {
        return CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path path, IOException ioException) {
        return CONTINUE;
    }
}
@@ -588,23 +588,201 @@
 S = 
-%7B %22CASE REPORT%22
+%0A    %7B %0A        %22BLOG POST%22,%0A        %22CASE REPORT%22,%0A        %22COMMENTARY%22,%0A        %22BRIEF COMMUNICATION%22,%0A        %22HISTORICAL PERSPECTIVE%22,%0A        %22MEDICAL ETHICS%22,%0A        %22MEDICAL REVIEW%22%0A   
  %7D;%0A
@@ -834,16 +834,48 @@
 content;
+%0A    private String fullContent;
 %0A%0A    pr
@@ -1455,24 +1455,95 @@
 Stripper();%0A
+%0A                    fullContent = textStripper.getText(pddDocument);%0A%0A
             
@@ -1706,24 +1706,65 @@
 lit(%22%5C%5Cn%22);%0A
+                    pddDocument.close();%0A
             
@@ -2029,24 +2029,26 @@
 cleType();%0A%0A
+%0A%0A
             
@@ -2565,24 +2565,105 @@
 ticleType);%0A
+        System.out.println(%22++++++++%22);%0A        System.out.println(fullContent);%0A
         Syst
@@ -3129,16 +3129,16 @@
 KNOWN%22;%0A
+
         
@@ -3177,91 +3177,8 @@
 ) %7B%0A
-            System.out.println(content%5B1%5D);%0A            System.out.println(type);%0A%0A
     

Add article types