/**
 * @(#)RecipeParser.java
 *
 *
 * @author 
 * @version 1.00 2007/11/22
 */


import java.io.*;
import java.util.*; 


public class RecipeParser { 

    final private String xmlStartTag = "<xml>";
    final private String xmlEndTag = "</xml>";
	private ArrayList<Recipe> recipeArray;
   
    public RecipeParser(String fileName) throws FileOpeningException {
		try {
			ArrayList<Recipe> recipes = new ArrayList<Recipe>(); //parsed recipes
			Recipe currentRecipe = null; //recipe being parsed
			StringBuffer text = new StringBuffer(); //for collecting current info
			String activeTag = ""; //current xml tag being parsed
			String input; //current input line from data file
			int numberOfStartRecipes = 0; //number of <recipe> tags read
			int numberOfEndRecipes = 0; //number of </recipe> tags  read
			//input data file where recipes will be read from
			BufferedReader xmlInputFile = new BufferedReader(new FileReader(fileName));
	    
			//results file where recipes will be saved to
			//PrintWriter    xmlResultFile = new PrintWriter(new FileWriter("xmlResults.txt"));
			
			//parse recipes
			while((input = xmlInputFile.readLine()) != null) { 
				String s = input.trim();
				//System.out.println(s);
				
				if(s.startsWith(xmlStartTag)){
					System.out.println("START PARSING");
				}
				else if(s.startsWith(xmlEndTag)){
					System.out.println("END PARSING");
				}
				else if (s.startsWith(Recipe.getStartTag())) {
					//start a new recipe
					currentRecipe = new Recipe();
					text = new StringBuffer();
					numberOfStartRecipes++;
				}
				else if (s.startsWith(Recipe.getEndTag())) {
					//end the current recipe
					recipes.add(currentRecipe);
					currentRecipe = null; //reset current recipe
					numberOfEndRecipes++;
				}
				else if (Recipe.isXMLStartTag(s)) {
					//change the field whose description is being collected
					activeTag = s;
					text = new StringBuffer();
				}
				else if (Recipe.isXMLEndTag(s)) {
					//set the field of whose description is being collected
					if(currentRecipe != null) currentRecipe.setXMLField(activeTag, text);
					text = null;
				}
				else
					//append text to current recipe field being collected
					if(text != null) text.append(s).append("\n");

			}
			xmlInputFile.close(); //close the input file
			
			//Report the number of recipe objects created
			//System.out.println("number of recipes = " + recipes.size() );
			
			//print all the recipes to the xml results file
			//This would normally be done with the application quits and 
			//the current recipes are to be saved
			
			/*xmlResultFile.println(xmlStartTag);
			
			for(Recipe aRecipe : recipes) 
				aRecipe.printXMLOn("   ", xmlResultFile); 
			
			xmlResultFile.println(xmlEndTag);
			
			xmlResultFile.close();*/ 
			
			recipeArray = recipes;
			
			//Run user menu and Display the recipes for the user 
			/*String prompt = "Enter recipe number 1..." + recipes.size() + ", student number or CR to quit";
			System.out.println(prompt);
			
			String userInputString;
			while((userInputString = userInput.nextLine()).length() > 0 ) {
				try {
					int recipeNumber = Integer.parseInt(userInputString);
					if((recipeNumber > 0) && (recipeNumber <= recipes.size() )) {
						Recipe aRecipe = recipes.get(recipeNumber - 1);
						aRecipe.printOn(System.out);
					}
					else{
						System.out.println("RECIPE NUMBER IS OUT OF RANGE");
					}
				}
				catch (NumberFormatException e) {
					System.out.println("INVALID RECIPE NUMBER FORMAT");
				}
				
				System.out.println(prompt);  
			}*/
			
			//catch file IO errors   
		} catch (FileNotFoundException e) {
			throw new FileOpeningException("Error: Cannot open file for reading.");
		} catch (EOFException e) {
			throw new FileOpeningException("Error: EOF encountered, file may be corrupted.");
		} catch (IOException e) {
			throw new FileOpeningException("Error: Cannot read from file.");
		}

	}
	
	public ArrayList<Recipe> getRecipes(){ return recipeArray; }
} //end class RecipeParser