
import java.io.*;
import java.util.*; // For vector

public class ExtractingSeperatingHeadersParts
{
    // ************************************************************
  	// *														  *
	// *                       main Method      				  *
	// *														  *
	// ************************************************************
	public static void main(String[] args) throws Exception
	{
		System.out.println("\n\n******* 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 stackPointer=-1;
		boolean flagSpaceChar=false;
		String firstTags="<html>"; // Begining of the INPUT FILE
		String lastTags="</html>";
		String tempStDiv="";
		String tempStHeader="";
		String tempStHeaderEnd="";
		Vector vecStackHeaders = new Vector();

/*--->*/writeUMLFile(firstTags);
		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 = "<"
/*--->*/							headersAnalysis(tempStHeader);
//									System.out.println("\n<"+tempStHeader+">");
/*--->*/                     		writeUMLFile("<"+tempStHeader+">");
									vecStackHeaders.addElement(tempStHeader);
									stackPointer++;
									tempStHeader="";
									break; // Exit from WHILE(true)
								} // End of the IF ("1 .. 9")
							} // End of the IF ("62")
						} // End of the WHILE(true)
//						System.out.println("I found DIV");
					}else // End of the IF ("div ")
					if(tempStDiv.equals("/div"))
					{
						tempStHeaderEnd=(String) vecStackHeaders.elementAt(stackPointer);
						vecStackHeaders.removeElementAt(stackPointer);
						stackPointer --;
/*--->*/				writeUMLFile("</"+tempStHeaderEnd+">");
//						System.out.println("</"+tempStHeaderEnd+">");
					} // // 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");
		}
/*--->*/writeUMLFile(lastTags);
	}
    // ************************************************************
	// *														  *
	// *                    headersAnalysis Method     			  *
	// *														  *
    // ************************************************************
	public static void headersAnalysis(String tempStHeader)
	{
		int lengthHeader;
		int periodCounter=0, firstIndex, secondIndex;
		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("First Index:"+firstIndex);
		System.out.println("\n"+firstPartHeader);
		System.out.println("Number of Periods: "+periodCounter);
		//***//
		firstPartHeader="";
		periodCounter=0;
		secondIndex=tempStHeader.indexOf(" ("); //Find the "(" to seperate the refrence part
//		System.out.println("Second Index:"+secondIndex);

		firstIndex ++; // To skip the "space"
		while(firstIndex<lengthHeader)
		{
			tempChar=tempStHeader.charAt(firstIndex);
			if(tempChar==40) // Found the "("
			{
				break;
			}
			secondPartHeader=secondPartHeader+tempChar;
			firstIndex ++;
		}
		System.out.println(secondPartHeader);
		//***//
		secondPartHeader="";
		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);
		    //***//
			thirdPartHeader="";
	    }
	}
	// ************************************************************
	// *														  *
	// *                    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**********************
	// ************************************************************
    // ************************************************************
}