All pastes #525017 Raw Edit

Someone

public text v1 · immutable
#525017 ·published 2007-05-31 07:20 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;

namespace Cooking
{
    public class Recipe
    {
        /// <summary>
        /// Path to the xml file we will be reading/writing to.
        /// </summary>
        public static string RecipePath = @"Docs\Recipes.xml";

        /// <summary>
        /// The XML document we will be using.
        /// </summary>
        public static XmlDocument RecipeDoc = new XmlDocument();
        
        /// <summary>
        /// The local collection of recipes we will be using/building.
        /// </summary>
        public static List<Recipe> m_Recipes;

        /// <summary>
        /// The name of the recipe.
        /// </summary>
        private string m_Name;
        public string Name { get { return m_Name; } set { m_Name = value; } }

        /// <summary>
        /// User added notes about the recipe.
        /// </summary>
        private string m_Notes;
        public string Notes { get { return m_Notes; } set { m_Notes = value; } }

        /// <summary>
        /// The recipes collection of bases.
        /// </summary>
        private List<Ingredient> m_Bases;
        public List<Ingredient> Bases { get { return m_Bases; } set { m_Bases = value; } }

        /// <summary>
        /// The recipes collection of additives.
        /// </summary>
        private List<Ingredient> m_Additives;
        public List<Ingredient> Additives { get { return m_Additives; } set { m_Additives = value; } }

        /// <summary>
        /// The entire collection of ingredients, includes all bases and additives.
        /// </summary>
        private List<Ingredient> m_AllIngredients;

        public Recipe(string name)
        {
            Initialize();
            m_Name = name;
        }

        /// <summary>
        /// Initializes various things( like the bases, additives, and ingredits collections ).
        /// </summary>
        private void Initialize()
        {
            m_Bases = new List<Ingredient>();
            m_Additives = new List<Ingredient>();
            m_AllIngredients = new List<Ingredient>();
        }

        /// <summary>
        /// Adds a cooking base to the Bases collection and Ingredients collection.
        /// </summary>
        /// <param name="b">The cooking base to add.</param>
        public void AddBase(Ingredient b)
        {
            m_Bases.Add(b);
            m_AllIngredients.Add(b);
        }

        /// <summary>
        /// Adds a cooking additive to the Additives collection and Ingredients collection.
        /// </summary>
        /// <param name="a">The cooking additive to add.</param>
        public void AddAdditive(Ingredient a)
        {
            m_Additives.Add(a);
            m_AllIngredients.Add(a);
        }

        /// <summary>
        /// Adds the created recipe to the Recipes collection and then regenerates the xml document from the contents of Recipes.
        /// </summary>
        /// <param name="m_Recipe"></param>
        public static void AddWriteRecipe(Recipe m_Recipe)
        {
            m_Recipes.Add(m_Recipe);

            RecipeDoc = new XmlDocument();

            if (File.Exists(RecipePath))
                File.Delete(RecipePath);

            XmlDeclaration Declaration = RecipeDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            RecipeDoc.AppendChild(Declaration);

            XmlNode Recipes = RecipeDoc.CreateNode(XmlNodeType.Element, "Recipes", "");
            XmlComment Comment = RecipeDoc.CreateComment("This file was generated by ATITD Companion, written by Sithid( *********** ).");
            Recipes.AppendChild(Comment);

            foreach (Recipe r in m_Recipes)
            {
                // Create the recipe node.
                XmlNode RecipeNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Recipe", "");

                // Add the notes attribute.
                XmlAttribute RecipeNotesAttribute = RecipeDoc.CreateAttribute("notes"); // Create the attribute.
                RecipeNotesAttribute.InnerText = r.Notes; // Change the InnerText.
                RecipeNode.Attributes.Append(RecipeNotesAttribute); // Add the attribute to the main nodes attributes collection.

                // Add the name node.
                XmlNode RecipeNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", ""); // Create the node.
                RecipeNameNode.InnerText = r.Name; // Change the InnerText.
                RecipeNode.AppendChild(RecipeNameNode); // Add the node as a child to the main recipe node.

                // Add the ingredients node.
                XmlNode RecipeIngredientsNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Ingredients", ""); // Create the ingredients node.

                // Add the bases node.
                XmlNode AllBasesNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Bases", "");

                // Loop through the recipes Bases collection.
                foreach (Ingredient i in r.Bases)
                {
                    XmlNode BaseNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Base", "");

                    XmlNode BaseNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", "");
                    BaseNameNode.InnerText = i.Name;
                    BaseNode.AppendChild(BaseNameNode);

                    XmlNode BaseAmountNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Amount", "");
                    BaseAmountNode.InnerText = i.Amount.ToString();
                    BaseNode.AppendChild(BaseAmountNode);

                    AllBasesNode.AppendChild(BaseNode);
                }

                RecipeIngredientsNode.AppendChild(AllBasesNode);

                // Add the additives node.
                XmlNode AllAdditivesNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Additives", "");

                foreach (Ingredient i in r.Additives)
                {
                    XmlNode AdditiveNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Additive", "");

                    XmlNode AdditiveNameNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Name", "");
                    AdditiveNameNode.InnerText = i.Name;
                    AdditiveNode.AppendChild(AdditiveNameNode);

                    XmlNode AdditiveAmountNode = RecipeDoc.CreateNode(XmlNodeType.Element, "Amount", "");
                    AdditiveAmountNode.InnerText = i.Amount.ToString();
                    AdditiveNode.AppendChild(AdditiveAmountNode);

                    AllAdditivesNode.AppendChild(AdditiveNode);
                }

                RecipeIngredientsNode.AppendChild(AllAdditivesNode);
                RecipeNode.AppendChild(RecipeIngredientsNode);
                Recipes.AppendChild(RecipeNode);
            }

            RecipeDoc.AppendChild(Recipes);
            RecipeDoc.Save(RecipePath);

            LoadRecipes();
        }

        public static List<Recipe> LoadRecipes()
        {
            List<Recipe> list = new List<Recipe>();

            if (File.Exists(RecipePath))
                RecipeDoc.Load(RecipePath);
            else
            {
                m_Recipes = new List<Recipe>();
                return new List<Recipe>();
            }

            XmlNodeList Nodes = RecipeDoc.SelectNodes("Recipes/Recipe");

            foreach (XmlNode RecipeNode in Nodes)
            {
                Recipe r = new Recipe("generic_name");

                r.Notes = RecipeNode.Attributes["notes"].Value;
                r.Name = RecipeNode.SelectSingleNode("Name").InnerText;

                XmlNodeList Ingredients = RecipeNode.SelectNodes("Ingredients");

                foreach (XmlNode n in Ingredients)
                {
                    XmlNodeList Bases = n.SelectNodes("Bases/Base");
                    XmlNodeList Additives = n.SelectNodes("Additives/Additive");
                    string TempName = string.Empty;
                    int TempAmount = 0;

                    foreach (XmlNode bn in Bases)
                    {
                        try
                        {
                            TempName = bn.SelectSingleNode("Name").InnerText;

                            if (TempName == "")
                                TempName = "empty";
                        }
                        catch { }

                        try
                        {
                            if (!int.TryParse(bn.SelectSingleNode("Amount").InnerText, out TempAmount))
                                TempAmount = 0;
                        }
                        catch { }

                        r.AddBase(new Ingredient(TempName, TempAmount));
                    }

                    foreach (XmlNode an in Additives)
                    {
                        try
                        {
                            TempName = an.SelectSingleNode("Name").InnerText;

                            if (TempName == "")
                                TempName = "string.Empty";
                        }
                        catch { }

                        try
                        {
                            if (!int.TryParse(an.SelectSingleNode("Amount").InnerText, out TempAmount))
                                TempAmount = 0;
                        }
                        catch { }

                        r.AddAdditive(new Ingredient(TempName, TempAmount));
                    }
                }

                list.Add(r);
            }

            m_Recipes = list;

            return list;
        }

        public override string ToString()
        {
            return m_Name;
        }
    }
}