import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

/*
 * This program parses through an IDX an extracts the unique reference and writes it to a .LIST file.
 *		 	joe@joeharouni.info 
 * 			10/19/2009
 */

public class parseIDX
{
        public static void main(String args[])
        {
        	
        	long startTime = System.currentTimeMillis();

			try
			{
				BufferedReader brIDX = new BufferedReader(new FileReader("C:\\Autonomy\\IDX\\EXPORT.IDX"));
			   	BufferedWriter bwIDX = new BufferedWriter(new FileWriter("C:\\Autonomy\\IDX\\EXPORT.LIST"));
			   	String sLine = "";
			   	String curRef = "";
			   	String curTitle ="";
			   	int i = 0;
			   	while(brIDX.ready())
			   	{
				   sLine=brIDX.readLine();

				   if(sLine.startsWith("#DREREFERENCE"))
				  	{
						sLine=sLine.substring(sLine.indexOf(" ")+1);

						if(!curRef.equals(sLine))
						{
							bwIDX.write(sLine);
							curTitle=brIDX.readLine();
							curTitle=brIDX.readLine(); //IN MOST IDEXES THE DRETITLE IS ITS OWNL LINE FOLLOWED BY NEWLINE CONTAINING THE TITLE
							bwIDX.write("^"+curTitle);
							bwIDX.newLine();	
							//bwIDX.newLine();
							i++;
						}
						curRef=sLine;
						
					}
  			   	}
			   	long endTime = System.currentTimeMillis();
			   	
			   	bwIDX.newLine();
			   	bwIDX.write("--------------------");
			   	bwIDX.newLine();
				bwIDX.write("Took "+((endTime-startTime)/1000)+" seconds to run through "+ i + " pages");
				
			   	brIDX.close();
			   	bwIDX.close();

			}catch(IOException e)
			{
			   //
			   System.out.print(e.toString());
			   //
			}


        }

}
