package OWL2generator;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 *  <b><code>GetSectionsFromConfigFile</code></b> class 
 *  parses a configuration file to extract sections delineated by specific 
 *  start and end tokens, returning a data structure containing these 
 *  sections. It serves as a foundational component within the 
 *  simplified Java framework. By parsing configuration files, it 
 *  provides structured data for subsequent OWL/XML ontology 
 *  generation, likely independent of the specific ontology details.
 *  <p>   
 *  <b>Functionality:</b>
 *  <p>   
 *  <code>Constructor</code>: Takes the configuration file path, section start/end 
 *  tokens, and comment character as input.
 *  <p>   
 *  <code>getSectionContent</code> (private method): Reads the file line 
 *  by line, accumulating lines within a section until the end token is 
 *  encountered.
 *  <p>   
 *  <code>getSections</code> (public method): Returns the extracted 
 *  sections as a List of Lists (each inner list represents a section).
 *  <p>   
 *  <b>Key Points:</b>
 *  <p>   
 *  Leverages <code>BufferedReader</code> for efficient line-by-line 
 *  file reading. Handles empty lines and comments within the
 *  configuration file. Employs <code>ArrayList</code> for dynamic
 *  storage of sections and their content (lines). 
 *  <p>
 *  <b>Environment</b>:
 *  <ul>
 *  <li>    IDE:              Eclipse IDE for Java Developers
 *  <li>    Version:          2021-12 (4.22.0)
 *  <li>    Build id:         20211202-1639
 *  <li>    HW Model Name:    iMac, MacOS Monterey, 12.5.1
 *  <li>    Processor Name:   Quad-Core Intel Core i5
 *  <li>    Processor Speed:  3.2 GHz
 *  <li>    Memory:           32 GB 1867 MHz DDR3
 *  <li>    Disk:             APPLE SSD SM0256G    
 *  <li>    Serial:           DGKRC080GG7V
 *  </ul>
 *  @version 1-001
 *  @since   2024/06/19
 *  @author  Edit Hlaszny (https://www.edithlaszny.eu/ edithlaszny@gmail.com)
 */

public class GetSectionsFromConfigFile 
{
	/**
	 *  Any text following the <code>comment</code> string on the first position 
	 *  is ignored by this simple parser in the class
	 */
	private String             comment  = "" ;

	/**
	 *  The text-file (the app uses several parameters files: each of them is 
	 *  ASCII flat text files) is read with <code>BufferedReader</code> method
	 */
	private BufferedReader     reader   = null ;

	/**
	 *  The result of the parsing is a <code>List&lt;List&lt;String&gt;&gt;</code>
	 *  class member, which can be accessed with the <code>getSection()</code> method
	 */
	private List<List<String>> sections = new ArrayList<>() ;
	
	/**
	 *  It sets the class members and reads the parameter file up to the 
	 *  beginning of the section and then calls the method processing the 
	 *  section. The sections will be added to the <code>List&lt;List&lt;String&gt;&gt; 
	 *  sections</code> one after the other. 
	 *  
     *  @param  configFilePath      Pathname of the configuration file.
     *  @param  sectionStartToken   Denotes the start of a section. 
     *  @param  sectionEndToken     Denotes the end of a section. 
     *  @param  comment             Marks the line (until CR/LF) as comment.
     *  @param  sections            The class member <code>List&lt;List&lt;String&gt;&gt; 
	 *  sections</code> contains all scanned sections as produced result of the class.
	 */
    GetSectionsFromConfigFile(String configFilePath,     
                              String sectionStartToken,   
                              String sectionEndToken,     
                              String comment             
                             )
    {	
    	this.comment = comment ;
 	
        try
        {
          	this.reader = new BufferedReader(new FileReader(configFilePath)) ;
    	        
      	    String line = "" ;
      	  
            while ((line = reader.readLine()) != null)
            {
        		if  (line.isEmpty() || line.startsWith(this.comment))
            	    continue;
            	  
            	if (line.startsWith(sectionStartToken))
            		this.sections.add(getSectionContent(sectionEndToken)) ;
            }  
            
            //  task is completed
        }
        catch (IOException e) 
        {
            e.printStackTrace() ;
        } 
        finally 
        {
            // Close the file (optional, as FileWriter closes automatically on close())
            try 
            {
        	    this.reader.close(); // Close the reader explicitly here (optional)closeFile(file) ;	            
            } 
            catch (IOException e) 
            {
                System.err.println("Error closing file: " + e.getMessage()) ;
            }
        }
        
    }   //  end of constructor()
    
    /**
     *  It returns with an excerpt from a text file (limited by the constructor's 
     *  start, and end token), which the calling method then appends to the result list.
     *  
     *  @param   sectionEndToken (the end-of-cut token)  
     *  
     *  @return  one section (=list of one lines, extracted from a parameter/text file
     *  
     *  @throws IOexception (can be caused by a BufferedReader)
     */
    private List<String> getSectionContent(String  sectionEndToken) 
    {
    	List<String> currentSection = new ArrayList<String>();
    
        try  
        {
            String line = "" ;
            while ((line = reader.readLine()) != null)
            {
                if  (line.isEmpty() || line.startsWith(this.comment))
                    continue;
    
                if (line.startsWith(sectionEndToken))
                    break ;
                else
                    currentSection.add(line) ;
            }
        } 
        catch (IOException e) 
        {
            e.printStackTrace() ;
        }        
          
        return currentSection ;
    	
    }   //  end of method getsectionContent()
    
    /**
     *  Returns an excerpt from a text file (limited by the constructor's 
     *  start, and end token). Used by the classes: WriteDisjointClassDefs, 
     *  WriteTriplets
     *  
     *  @param   none
     *  
     *  @return  list of sections, extracted from a parameter/text file
     */
    public List<List<String>> getSections()  
    {
    	return this.sections ;
    
    }  //  end of method getSections()
    
}   //  end of class GetSectionsFromConfigFile
