import java.io.*;
import java.util.regex.*;

public class CrossRef
{
	// ************************************************************
  	// *														  *
	// *                       main Method      				  *
	// *														  *
	// ************************************************************
	public static void main(String[] args) throws Exception
	{
		System.out.println("\n * * * Welcome to the Program  * * * \n");
/*CALL*/readNumFile();
		System.out.println("\n * * * End of the Cross Refrencing * * * \n");
	}
 	// ************************************************************
	// *														  *
	// *                    readNumFile Method     				  *
	// *														  *
    // ************************************************************
	public static void readNumFile()
	{
		String lineStNum="";
				
		try // Read the Num.txt File
		{
			FileReader numFileReader = new FileReader("Num.txt"); 
			BufferedReader numFileBuffer = new BufferedReader(numFileReader);
			lineStNum=numFileBuffer.readLine(); 
//			System.out.println(lineStNum);
			readHTMLFiles(lineStNum);
			while(true) // Until the end of the Num.txt file
			{
				lineStNum=numFileBuffer.readLine();
				if(lineStNum==null) // End of the Num.txt file
				{
					break;
				}
//				System.out.println(lineStNum);
				readHTMLFiles(lineStNum);
			} // End of the WHILE loop (End of the file)
			numFileReader.close();
		} // End of the TRY
		catch(FileNotFoundException e)
		{
			System.out.println("Unable to Open Num.txt File");
		}
		catch(IOException e)
		{
			System.out.println("Unable to Close Num.txt File");
		}
	}
    // ************************************************************
	// *														  *
	// *                  readHTMLFiles Method     			 	  *
	// *														  *
    // ************************************************************
	public static void readHTMLFiles(String tempSt)
	{
		int counter;
		char backslash=92;
		String umlword="UML";
		String firstPartPath=umlword+backslash; // "UML/"
		String inFilePath="";
		String outFilePath="";
		String lineStHTMLFiles = "";
		
		inFilePath=firstPartPath.concat(tempSt).concat(".html"); // "UML/7.3.8.html"
		outFilePath=tempSt.concat(".html"); // "7.3.8.html";
//		System.out.println(inFilePath+" & "+outFilePath);
		try // Read a HTML File
		{
			FileReader htmlFilesReader = new FileReader(inFilePath);
			BufferedReader htmlFilesBuffer = new BufferedReader(htmlFilesReader);
			for(counter=0;counter<=15;counter++) // To jump the head of each HTML files
			{
				lineStHTMLFiles=htmlFilesBuffer.readLine();
				writeHTMLFiles(outFilePath,lineStHTMLFiles);
//				System.out.println(lineStHTMLFiles);
			}
			while(true) // Until the end of a HTML file
			{
				lineStHTMLFiles=htmlFilesBuffer.readLine();
				if(lineStHTMLFiles==null) // End of the HTML file
				{
					break;
				}
//				System.out.println(lineStHTMLFiles);
				lineStHTMLFiles=readKeywordsFile(lineStHTMLFiles);
				writeHTMLFiles(outFilePath,lineStHTMLFiles);
//				System.out.println(lineStHTMLFiles);
			}
			htmlFilesReader.close();
		} // End of the TRY
		catch(FileNotFoundException e)
		{
			System.out.println("Unable to Open HTML Files");
		}
		catch(IOException e)
		{
			System.out.println("Unable to Close HTML Files");
		}
	}
 	// ************************************************************
	// *														  *
	// *               readKeywordsFile Method     				  *
	// *														  *
    // ************************************************************
	public static String readKeywordsFile(String tempSt)
	{
//		int i,j;
//		String[] chars={" ",".",",",":",";","(",")"}; //Space, Period, Comma, Colon, Semicolon, Left & Right Paranthesis
		String lineStKeywords="";
		String[] lineStKeywordsSplited;
				
		try // Read the Keywords File
		{
			FileReader keywordsFileReader = new FileReader("Unique Keywords.txt"); 
			BufferedReader keywordsFileBuffer = new BufferedReader(keywordsFileReader);
			while(true) // Until the end of the Keywords.txt file
			{
				lineStKeywords=keywordsFileBuffer.readLine();
				if(lineStKeywords==null) // End of the Keywords.txt file
				{
					break;
				}
//				System.out.println(lineStKeywords);
				lineStKeywordsSplited=lineStKeywords.split("@");
//				System.out.println(lineStKeywordsSplited[0]);
//				System.out.println(lineStKeywordsSplited[1]);
				Pattern p = Pattern.compile((char)32+lineStKeywordsSplited[0]+(char)32);
				Matcher m = p.matcher(tempSt);
				tempSt = m.replaceAll((char)32+lineStKeywordsSplited[1]+(char)32);				
//				for(i=0;i<5;i++)
//				{
//					for(j=0;j<5;j++)
//					{
//						lineStKeywordsSplited[0]=chars[i]+lineStKeywordsSplited[0]+chars[j];
//						lineStKeywordsSplited[1]=chars[i]+lineStKeywordsSplited[1]+chars[j];
//						System.out.println(lineStKeywordsSplited[0]);
//						System.out.println(lineStKeywordsSplited[1]);
//						Pattern p = Pattern.compile(lineStKeywordsSplited[0]);
//						Matcher m = p.matcher(tempSt);
//						tempSt = m.replaceAll(lineStKeywordsSplited[1]);
//						lineStKeywordsSplited[0]=lineStKeywordsSplited[0].substring(1, (lineStKeywordsSplited[0].length()-1));
//						lineStKeywordsSplited[1]=lineStKeywordsSplited[1].substring(1, (lineStKeywordsSplited[1].length()-1));
//					}
//				}
			} // End of the WHILE loop (End of the file)
			keywordsFileReader.close();
		} // End of the TRY
		catch(FileNotFoundException e)
		{
			System.out.println("Unable to Open Keywords.txt File");
		}
		catch(IOException e)
		{
			System.out.println("Unable to Close Keywords.txt File");
		}
		return tempSt;
	}
	// ************************************************************
	// *														  *
	// *                    writeHTMLFiles Method     			  *
	// *														  *
    // ************************************************************
	public static void writeHTMLFiles(String outFilePath, 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(outFilePath,x);
			outputFileWriter.write(tempString+(char)10);
			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**********************
	// ************************************************************
    // ************************************************************
}