diff api/Factories/Xml/WarFoundryXmlFactory.cs @ 43:d0812d7de39d

Re #49 - Resolve namespace issues * Fix up some XPath queries * Remove one unnecessary namespace * Check local name of elements, not name (which is qualified) * Add method to get double value from an attribute including handling INF * Remove min/max for equipment item as it is now moved to the UnitType's reference to the equipment item *
author IBBoard <dev@ibboard.co.uk>
date Sun, 22 Mar 2009 17:05:01 +0000
parents d0d44434b557
children db951aad24b9
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Mar 22 16:37:43 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Mar 22 17:05:01 2009 +0000
@@ -65,7 +65,7 @@
 			XmlDocument doc = CreateXmlDocumentFromStream(stream);			
 			XmlElement elem = (XmlElement)doc.LastChild;
 			
-			if (!elem.Name.Equals(elementName.Value))
+			if (!elem.LocalName.Equals(elementName.Value))
 			{
 				throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name));
 			}
@@ -198,9 +198,9 @@
 			
 			XmlNode elem = GetExtraData(system);
 			LoadCategoriesForSystem(system, elem);
-			XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:categories");
+			XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:sysStatsList");
 			XmlNode statsElem = nodeList.Item(0);
-			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats", "http://ibboard.co.uk/warfoundry/system");
+			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats");
 			LoadSystemStatsForSystem(system, elem);
 			system.StandardSystemStatsID = defaultStatsID;
 			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
@@ -212,7 +212,7 @@
 		{
 			WarFoundryObject tempObj;
 						
-			foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:category"))
+			foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat"))
 			{
 				tempObj = CreateObjectFromElement(cat);
 				
@@ -377,7 +377,7 @@
 			WarFoundryObject obj = null;
 			LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name);
 			
-			if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
+			if (elem.LocalName.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
 			{
 				LogNotifier.Debug(GetType(), "Create Category");
 				obj = CreateCategoryFromElement(elem);
@@ -413,6 +413,30 @@
 				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
 			}
 		}
+				
+		private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName)
+		{
+			double doubleVal = double.NaN;
+			string attribValue = elem.GetAttribute(attributeName);
+			
+			if (attribValue == "INF")
+			{
+				doubleVal = double.PositiveInfinity;
+			}
+			else
+			{
+				try
+				{
+					return int.Parse(attribValue);
+				}
+				catch(FormatException)
+				{
+					throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
+				}
+			}
+			
+			return doubleVal;
+		}
 						
 		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
 		{
@@ -500,43 +524,27 @@
 		{
 			string id = elem.GetAttribute("id");
 			string name = elem.GetAttribute("name");
-			float cost = 0, min = 0, max = 0;
+			double cost = 0, min = 0, max = 0;
 			ArmourType armourType;
 			
 			try
 			{
-				cost = float.Parse(elem.GetAttribute("cost"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number");
-			}			
-			
-			try
-			{
-				min = float.Parse(elem.GetAttribute("min"));
+				cost = GetDoubleValueFromAttribute(elem, "cost");
 			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number");
-			}			
-			
-			try
+			catch(FormatException ex)
 			{
-				max = float.Parse(elem.GetAttribute("max"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number");
-			}
+				throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex);
+			}			
 			
 			try
 			{
 				armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
 			}
-			catch(FormatException)
+			catch(ArgumentException ex)
 			{
-				throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
+				//throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
+				LogNotifier.WarnFormat(GetType(), "Invalid 'armourType' for equipment {0} - {1}", id, ex.Message);
+				armourType = ArmourType.None;
 			}
 			
 			if (elem.ChildNodes.Count>0)