
import java.io.*;

public class ThesisIIV1
{
    // ************************************************************
  	// *														  *
	// *                       main Method      				  *
	// *														  *
	// ************************************************************
	public static void main(String[] args) throws Exception
	{
		String[] inputStrings;

		System.out.println("\n * * * Welcome to the UML Specification Analysis * * * \n");
/*CALL*/inputStrings=input();
/*CALL*/readUMLFile(inputStrings);
		System.out.println("\n * * * End of the Document Analysis * * * \n");
	}
  	// ************************************************************
  	// *														  *
	// *                    input Method  		            	  *
	// *														  *
	// ************************************************************
	public static String[] input() throws Exception
	{
		String[] inputStrings={"",""}; // Two dimensional string

		System.out.print(" Type the Name of the Input File, Then Press Enter:\t");
/*CALL*/inputStrings[0]=readString();
		System.out.print(" Type the Name of the First Header, Then Press Enter:\t");
/*CALL*/inputStrings[1]=readString();
		return inputStrings;
	}
	// ************************************************************
  	// *														  *
	// *                    readString Method  		           	  *
	// *														  *
	// ************************************************************
	public static String readString() throws Exception
	{
		char tempCh;
		String tempSt="";

		tempCh = (char)System.in.read();
		while((int)tempCh != 13 ) // Enter = 13
		{
			  tempSt = tempSt + tempCh;
			  tempCh = (char)System.in.read();
		}
		tempCh = (char)System.in.read(); //Abort the ENTER key
		return tempSt;
	}
	// ************************************************************
	// *														  *
	// *                    readUMLFile Method     				  *
	// *														  *
    // ************************************************************
	public static void readUMLFile(String[] inputStrings)
	{
		int indexOfLinkTarget; // "LinkTarger" is a key word for processing
		int indexOfFirstHeader;
		int counter=1;
		boolean startAnalysis=false;
		String firstTags="<Book>"; // Begining of the INPUT FILE
		String lastTags="</Book>";
		String lineSt="";

/*CALL*/writeUMLFile(firstTags+(char)10);
		try // Read the INPUT FILE
		{
			FileReader inputFileReader = new FileReader(inputStrings[0]); // inputReader is an object and inputStrings[0] is the name of the input file
			BufferedReader inputFileBuffer = new BufferedReader(inputFileReader);
			while(true) // Until the end of the input file
			{
				lineSt=inputFileBuffer.readLine(); // Read a line from the input file
//				System.out.println(lineSt);
				if (lineSt==null) // End of the input file
				{
					break;
				}
				indexOfLinkTarget=lineSt.indexOf("LinkTarget");
				if(indexOfLinkTarget!=-1) // I found a "LinkTarget"
				{
					if(startAnalysis!=true)
					{
						indexOfFirstHeader=lineSt.indexOf(inputStrings[1]);
						if(indexOfFirstHeader!=-1) // I found the "First Header" and skipped the "Table of Contents"
						{
//							System.out.println("\n\n"+counter+")\t"+lineSt);
/*CALL*/					writeUMLFile(counter+"\t"+lineSt+(char)10);
							startAnalysis=true;
						}
					}
					else
					{
						counter ++;
//						System.out.println(counter+")\t"+lineSt);
/*CALL*/				writeUMLFile(counter+"\t"+lineSt+(char)10);
					}
				}
			} // End of the WHILE loop (End of the file)
			inputFileReader.close();
		} // End of the TRY
		catch(FileNotFoundException e)
		{
			System.out.println("Unable to Open INPUT File");
		}
		catch(IOException e)
		{
			System.out.println("Unable to Close INPUT File");
		}
/*CALL*/writeUMLFile(lastTags);
	}

    // ************************************************************
	// *														  *
	// *                    headersAnalysis Method     			  *
	// *														  *
    // ************************************************************
	public static int headersAnalysis(String tempStHeader)
	{
		int lengthHeader;
		int periodCounter=0;
		int firstIndex=0, secondIndex=0;
		char tempChar=' ';
		String firstPartHeader="";
		String secondPartHeader="";
		String thirdPartHeader="";

		lengthHeader=tempStHeader.length();
		firstIndex=tempStHeader.indexOf(" "); // Find the first "space" to seperate numbers
		for(int i=0;i<firstIndex;i++)
		{
			tempChar=tempStHeader.charAt(i);
			if(tempChar==46) // Period "."
			{
				periodCounter ++;
			}
			firstPartHeader=firstPartHeader+tempChar;
		}
//		System.out.println("\n"+firstPartHeader);
		firstIndex ++; // To skip the "space" for the next while loop
		while(firstIndex<lengthHeader)
		{
			tempChar=tempStHeader.charAt(firstIndex);
			if(tempChar==40) // If you found the "(", it means we have third part which is "refrence part"
			{
				break;
			}
			secondPartHeader=secondPartHeader+tempChar;
			firstIndex ++;
		}
//		System.out.println(secondPartHeader);
		secondIndex=tempStHeader.indexOf(" ("); //Find the " (" to seperate the refrence part
		if(secondIndex!=-1) // Found the " ("
		{
			secondIndex=secondIndex+7; // To skip the " (from "
			while(secondIndex<(lengthHeader-1))
			{
				tempChar=tempStHeader.charAt(secondIndex);
				thirdPartHeader=thirdPartHeader+tempChar;
				secondIndex ++;
			}
//		    System.out.println(thirdPartHeader);
		}
		if(periodCounter==0)
		{
/*CALL*/	writeUMLFile("<Chapter Number="+(char)34+firstPartHeader+
		    (char)34+">"+(char)10+"<Name>"+secondPartHeader+"</Name>"+(char)10);
		}
		else if (periodCounter==1)
		{
/*CALL*/	writeUMLFile("<Section Number="+(char)34+firstPartHeader+
		    (char)34+">"+(char)10+"<Name>"+secondPartHeader+"</Name>"+(char)10);
		}
		else // periodCounter==2 like 1.2.3
		{
/*CALL*/	writeUMLFile("<Subsection Number="+(char)34+firstPartHeader+
		    (char)34+">"+(char)10+"<Name>"+secondPartHeader+"</Name>"+
		    (char)10+"<References>"+thirdPartHeader+"</References>"+(char)10);
		}
		return periodCounter;
	}
	// ************************************************************
	// *														  *
	// *                    writeUMLFile Method     			  *
	// *														  *
    // ************************************************************
	public static void writeUMLFile(String tempString)
	{
		boolean x=true; //Each time you add a new string at the end of the FILE

		try // Creat a new file
		{
			FileWriter outputFileWriter = new FileWriter("Document Headers.txt",x);
			outputFileWriter.write(tempString);
			outputFileWriter.close();
		}
		catch(FileNotFoundException e)
		{
			System.out.println("Unable to Open OUTPUT File");
		}
		catch(IOException e)
		{
			System.out.println("Unable to close OUTPUT File");
		}
	}
    // ************************************************************
	// ************************************************************
	// ********************End Of the Program**********************
	// ************************************************************
    // ************************************************************
}