package pl.edu.icm.cermine;

import java.io.InputStream;
import org.jdom.Element;
import pl.edu.icm.cermine.exception.AnalysisException;
import pl.edu.icm.cermine.structure.model.BxDocument;

/**
 * NLM-based content extractor from PDF files.
 *
 * @author Dominika Tkaczyk
 */
public class PdfNLMContentExtractor implements DocumentContentExtractor<Element> {
  
    /** geometric structure extractor */
    private DocumentStructureExtractor structureExtractor;
    
    /** document metadata extractor from geometric structure */
    private DocumentMetadataExtractor<Element> metadataExtractor;
    
    /** parsed references extractor from geometric structure */
    private DocumentReferencesExtractor<Element> referencesExtractor;

    public PdfNLMContentExtractor() throws AnalysisException {
        structureExtractor = new PdfBxStructureExtractor();
        metadataExtractor = new PdfNLMMetadataExtractor();
        referencesExtractor = new PdfNLMReferencesExtractor();
    }

    public PdfNLMContentExtractor(DocumentStructureExtractor structureExtractor, DocumentMetadataExtractor<Element> metadataExtractor, DocumentReferencesExtractor<Element> referencesExtractor) {
        this.structureExtractor = structureExtractor;
        this.metadataExtractor = metadataExtractor;
        this.referencesExtractor = referencesExtractor;
    }
    
    public PdfNLMContentExtractor(InputStream initialModel, InputStream initialRange, 
            InputStream metadataModel, InputStream metadataRange, InputStream refModel) throws AnalysisException {
        structureExtractor = new PdfBxStructureExtractor(initialModel, initialRange);
        metadataExtractor = new PdfNLMMetadataExtractor(metadataModel, metadataRange);
        referencesExtractor = new PdfNLMReferencesExtractor(refModel);
    }
    
    /**
     * Extracts content from PDF file and stored it in NLM format.
     * 
     * @param stream
     * @return extracted content in NLM format
     * @throws AnalysisException 
     */
    @Override
    public Element extractContent(InputStream stream) throws AnalysisException {
        BxDocument document = structureExtractor.extractStructure(stream);
        return extractContent(document);
    }

    /**
     * Extracts content from PDF file and stored it in NLM format.
     * 
     * @param document
     * @return extracted content in NLM format
     * @throws AnalysisException 
     */
    @Override
    public Element extractContent(BxDocument document) throws AnalysisException {
        Element content = metadataExtractor.extractMetadata(document);
        Element[] references = referencesExtractor.extractReferences(document);
        
        Element back = content.getChild("back");
        Element refList = back.getChild("ref-list");
        for (Element ref : references) {
            Element r = new Element("ref");
            r.addContent(ref);
            refList.addContent(r);
        }
        
        return content;
    }
    
}

@@ -37,16 +37,20 @@
 java.io.
+File
 InputStr
@@ -65,25 +65,242 @@
 ort 
-org.jdom.Element;
+java.io.FileNotFoundException;%0Aimport java.io.InputStream;%0A%0Aimport javax.xml.xpath.XPathExpressionException;%0A%0Aimport org.jdom.Element;%0Aimport org.jdom.JDOMException;%0Aimport org.jdom.output.Format;%0Aimport org.jdom.output.XMLOutputter;%0A
 %0Aimp
@@ -3199,10 +3199,568 @@
  %7D%0A    %0A
+    public static void main(String%5B%5D args) throws AnalysisException, FileNotFoundException, XPathExpressionException, JDOMException %7B%0A    %09if(args.length != 1)%7B%0A    %09%09System.err.println(%22USAGE: program FILE_PATH%22);%0A    %09%09System.exit(1);%0A    %09%7D%0A%0A    %09PdfNLMContentExtractor extractor = new PdfNLMContentExtractor();%0A    %09InputStream in = new FileInputStream(args%5B0%5D);%0A    %09Element result = extractor.extractContent(in);%0A%0A    %09XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());%0A    %09System.out.println(outputter.outputString(result));%0A    %7D%0A%0A
 %7D%0A

add a test method to content extractor