diff api/Factories/Xml/WarFoundryXmlFactory.cs @ 47:85f2b9c3609c

Re #13 - Use XPath for file loading * Replace loading of race unit types, categories and equipment with XPath method * Add loading of abilities using XPath Also: * Add implementation for Ability * Add AddAbility method to Race * Alter some logging so that "for ID" is followed by ID not name
author IBBoard <dev@ibboard.co.uk>
date Thu, 26 Mar 2009 20:49:42 +0000
parents 75a44b7753d4
children b49372dd8afa
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Mon Mar 23 20:57:07 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Thu Mar 26 20:49:42 2009 +0000
@@ -203,8 +203,8 @@
 			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats");
 			LoadSystemStatsForSystem(system, elem);
 			system.StandardSystemStatsID = defaultStatsID;
-			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
-			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID);
+			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID);
+			LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID);
 			system.SetAsFullyLoaded();
 		}
 		
@@ -244,40 +244,32 @@
 			race.SetAsLoading();
 			
 			XmlNode elem = GetExtraData(race);
-			XmlNode colNode = elem.FirstChild;
-						
-			foreach (XmlElement node in colNode.ChildNodes)
+			
+			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:units/race:unit"))
 			{
 				UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem);
 				race.AddUnitType(type);
 			}
 			
-			colNode = colNode.NextSibling;
-			
-			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value)
+			foreach (XmlElement node in SelectNodes(elem, "/race:race/race:categories/cat:cat"))
 			{
-				foreach (XmlElement node in colNode.ChildNodes)
-				{
-					race.AddCategory(CreateCategoryFromElement(node));
-				}
-				
-				colNode = colNode.NextSibling;
+				race.AddCategory(CreateCategoryFromElement(node));
 			}
 							
-			if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value)
+			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:equipment/cat:equipmentItem"))
 			{
-				foreach (XmlElement node in colNode.ChildNodes)
-				{
-					EquipmentItem item = CreateEquipmentItemFromElement(node, race);
-					race.AddEquipmentItem(item);
-				}
+				EquipmentItem item = CreateEquipmentItemFromElement(node, race);
+				race.AddEquipmentItem(item);
+			}
+							
+			foreach (XmlElement node  in SelectNodes(elem, "/race:race/race:abilities/cat:ability"))
+			{
+				Ability ability = CreateAbilityFromElement(node, race);
+				race.AddAbility(ability);
 			}
 			
-			Dictionary<string, Ability> raceAbilities = new Dictionary<string, Ability>();
-			//TODO: Load abilities
-			race.SetAbilities(raceAbilities);
 			race.SetAsFullyLoaded();
-			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name);
+			LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID);
 		}
 		
 		protected XmlDocument CreateXmlDocumentFromStream(Stream stream)
@@ -287,7 +279,7 @@
 			
 			try
 			{
-				doc.Load(reader);			
+				doc.Load(reader);
 			}
 			//Don't catch XMLSchemaExceptions - let them get thrown out
 			finally
@@ -549,5 +541,15 @@
 			
 			return new EquipmentItem(id, name, cost, min, max, armourType, race);
 		}
+		
+		private Ability CreateAbilityFromElement(XmlElement elem, Race race)
+		{
+			string id = elem.GetAttribute("id");
+			string name = elem.GetAttribute("name");
+			Ability ability = new Ability(id, name);
+			XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager());
+			ability.Description = (node == null) ? "" : node.InnerText;
+			return ability;
+		}
 	}
 }
\ No newline at end of file