/*
 *  Team 1
 *  CS575: Software Design
 *  Project Phase I
 *  Object-Oriented style
 *  OOMain.java 
 */

import java.io.*;
import java.util.Vector;

/**
 *  <p>Main class for the Object Oriented style implementation of the KWIC system.</p>
 *
 *  <p><b>PRE:</b> expects to receive the name(s) of the input files(s)
 *  <br><b>POST:</b> after execution, all modules will have been used.
 *
 *  <br>For consistency, code resembles PNFMain by Diana Tetelman
 *  where possible.
 *
 *  @author Luiza da Silva
 */

public class OOMain {
    //class instance variables
    private static Vector inFiles = null;
    
    /**
     * Default Constructor.
     */
    OOMain ()
    {
        //Initializes vector that will contain input file names
        inFiles = new Vector();
    }
    
    /**
     * Main method of the Object-Oriented implementation of the KWIC project.
     *
     * @param args[]  command line parameters
     * @exception KWICException exception class of the KWIC system.
     */
    
    public static void main (String[] args) throws KWICException
    {
        //Instantiates OOMain class.
        OOMain main = new OOMain();
    
     //************ Input *************//
     
      //command line parsing and usage clause
        int optind; 
        if (args.length < 1)
        {
            System.out.println("Usage: java OOMain [infile1] [infile2] ... [infilen]");
            System.exit(1);
        }
        else
        {
            for (optind=0; optind < args.length; optind++)
            {
                inFiles.add(args[optind]);
            }
        }
        
        try
        {
            SentenceCollection data = new SentenceCollection();
            SentenceCollection shifteddata = new SentenceCollection();
            
            String temp;
            OOInput in = new OOInput(inFiles);
	    data = in.readSentenceCollection();
            
            
       //*********** Shifting **************//
            //Instantiates OOShifter class
            OOShifter shift = new OOShifter();
            
            //Starts iterator in data collection to start shifting
            data.startKWICRowIterator();
            while(data.hasNextKWICRow() == true) {
                Vector results = new Vector();
                Object[] temps = data.getNextKWICRow();
                int count = temps.length;
                for (int i=0; i<count; i++) {
                    //shifts line contents and stores in vector results
                    results = shift.shiftString(temps[i].toString());
                    for (int j=0; j<results.size(); j++) {
                        //add shifted lines to data collection
                        shifteddata.addKWICRow(new Object[]{results.elementAt(j)});
                    }
                }
            }
       
            
     //********* Sorting ************//
            
           //Instantiates OOSorter class
            OOSorter sort = new OOSorter();
            
            //Invokes sorting method from OOSorter class
            sort.sortData(shifteddata);
            
            
     //********* Output *************//
            //Instantiates OOOutput class
            OOOutput out = new OOOutput();
            
            //Invokes output method
            out.printData(shifteddata);
        }
        catch (KWICException ke) {
	    System.err.println (ke.getMessage());
	}
    }
} // end of class OOMain
