
import java.io.*;

public class HeadersTaggingCompleted
{
    // ************************************************************
  	// *														  *
	// *                       main Method      				  *
	// *														  *
	// ************************************************************
	public static void main(String[] args) throws Exception
	{
		System.out.println("\n\n******* Welcome to the UML Specification Analysis *******\n\n\n");
		String f_Name = "UMLChap7-pdf Acrobat-htm Stylus-xml.xml"; // Name of the INPUT FILE
		int inputArray[] = new int [10];

//		inputArray = inputValues();
//		System.out.println("InputArray = "+inputArray[0]+"\n\n\n");
		readUMLFile(f_Name,inputArray[0]);
	}
  	// ************************************************************
  	// *														  *
	// *                    inputValues Method  				  *
	// *														  *
	// ************************************************************
	public static int[] inputValues() throws Exception
	{
		int[] inputArray = new int [10];
		char chTemp;
		String stTemp="";

		System.out.print(" Input the Number of Characters, Then Press ENTER:\t");
		chTemp = (char)System.in.read();
		while((int)chTemp != 13 ) // Enter = 13
		{
			  stTemp = stTemp + chTemp;
			  chTemp = (char)System.in.read();
		}
		chTemp = (char)System.in.read(); //Abort the ENTER key
		System.out.print("\n");
	    inputArray[0] = Integer.parseInt(stTemp); //Convert string to the integer
		return inputArray;
	}
	// ************************************************************
	// *														  *
	// *                    readUMLFile Method     				  *
	// *														  *
    // ************************************************************
	public static void readUMLFile(String f_Name,int numChar)
	{
		int fileChar; // File characters
		int tempInt=0;
		int stackPointer=0;
		boolean flagSpaceChar=false;
		String firstTags="<html>"; // Begining of the INPUT FILE
		String lastTags="</html>";
		String tempStDiv="";
		String tempStHeader="";
		int [] tempStack = new int[100];

/*CALL*/writeUMLFile(firstTags+(char)10);
		try // Read the INPUT FILE
		{
			FileReader inputReader = new FileReader(f_Name); // inputReader is an object
			fileChar=inputReader.read();
			while(fileChar!=-1) // It is not end of the FILE
			{
				if(fileChar==60) // 60 = "<"
				{
//					System.out.println("<"); I found "<"
					for(int i=0;i<4;i++)
					{
						fileChar=inputReader.read();
						tempStDiv=tempStDiv+(char)fileChar;
					}
//					System.out.println(tempSt); // 4 Characters
					if(tempStDiv.equals("div "))
					{
						while(true)
						{
							fileChar=inputReader.read();
							if (fileChar==62) // 62 = ">"
							{
								fileChar=inputReader.read();
								if(fileChar >= 49 && fileChar <= 57) // 1 .. 9
								{
									do
									{
//										System.out.println("------->"+fileChar);
										if(fileChar!=13 && fileChar!=10 && fileChar!=555) // Next line "\n" and "\r"
										{
											tempStHeader=tempStHeader+(char)fileChar;
											if(fileChar==32) // 32 = Space
											{
												flagSpaceChar=true; // I saw the first SPACE
											}
										}
										fileChar=inputReader.read();
										if(flagSpaceChar==true && fileChar==32) //I saw the second SPACE
										{
											fileChar=555; // To skip first IF in the DO loop
//											Syst em.out.println("Second Space: "+fileChar);
										}else // (flagSpaceChar == False || fileChar!=32)
										if(flagSpaceChar==true) // Just one SPACE or last SPACE
										{
											flagSpaceChar=false;
//											System.out.println("Last Space: "+fileChar);
										}
									}while(fileChar!=60); // 60 = "<"
//									System.out.println(tempStHeader);
/*CALL*/							tempInt=headersAnalysis(tempStHeader);
									tempStack[stackPointer]=tempInt;
									stackPointer++;
									tempStHeader="";
									break; // Exit from WHILE(true)
								} // End of the IF ("1 .. 9")
							} // End of the IF ("62")
						} // End of the WHILE(true)
					}else // End of the IF ("div ")
					if(tempStDiv.equals("/div"))
					{
						stackPointer --;
						if(tempStack[stackPointer]==0)
						{
/*CALL*/					writeUMLFile("</Chapter>"+(char)10);
						}
						else if (tempStack[stackPointer]==1)
						{
/*CALL*/					writeUMLFile("</Section>"+(char)10);
						}
						else // periodCounter==2 like 1.2.3
						{
/*CALL*/					writeUMLFile("</Subsection>"+(char)10);
						}
					} // // End of the IF ("/div")
					tempStDiv="";
				} // End of the IF ("<")
				fileChar = inputReader.read();
			} // End of the WHILE loop (End of the file)
			inputReader.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 outputWriter = new FileWriter("Improved UML.xml",x);
			outputWriter.write(tempString);
			outputWriter.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**********************
	// ************************************************************
    // ************************************************************
}