changeset 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
files api/Factories/Xml/WarFoundryXmlFactory.cs api/Objects/EquipmentItem.cs api/Objects/GameSystem.cs dtds/race.xsd dtds/warfoundry-core.xsd
diffstat 5 files changed, 56 insertions(+), 38 deletions(-) [+]
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)
--- a/api/Objects/EquipmentItem.cs	Sun Mar 22 16:37:43 2009 +0000
+++ b/api/Objects/EquipmentItem.cs	Sun Mar 22 17:05:01 2009 +0000
@@ -12,11 +12,11 @@
 	/// </summary>
 	public class EquipmentItem : WarFoundryObject
 	{
-		private float cost, min, max;
+		private double cost, min, max;
 		private ArmourType armourType;
 		private Race equipForRace;
 
-		public EquipmentItem(string id, string name, float itemCost, float minimum, float maximum, ArmourType itemArmourType, Race race) : base(id, name)
+		public EquipmentItem(string id, string name, double itemCost, double minimum, double maximum, ArmourType itemArmourType, Race race) : base(id, name)
 		{
 			cost = itemCost;
 			min = minimum;
@@ -30,7 +30,7 @@
 			get { return ((MaxNumber < 1 && MaxNumber > 0) || (MaxNumber == 1 && MinNumber > 0)); }
 		}
 
-		public float MinNumber
+		public double MinNumber
 		{
 			get { return min; }
 			set 
@@ -44,7 +44,7 @@
 			}
 		}
 
-		public float MaxNumber
+		public double MaxNumber
 		{
 			get { return max; }
 			set 
@@ -64,7 +64,7 @@
 			set { armourType = value; }
 		}
 
-		public float Cost
+		public double Cost
 		{
 			get { return cost; }
 			set { cost = value; }
--- a/api/Objects/GameSystem.cs	Sun Mar 22 16:37:43 2009 +0000
+++ b/api/Objects/GameSystem.cs	Sun Mar 22 17:05:01 2009 +0000
@@ -80,6 +80,7 @@
 		{
 			get
 			{
+				EnsureFullyLoaded();
 				return stats[defaultStats];
 			}
 		}
--- a/dtds/race.xsd	Sun Mar 22 16:37:43 2009 +0000
+++ b/dtds/race.xsd	Sun Mar 22 17:05:01 2009 +0000
@@ -62,8 +62,8 @@
   <xs:attribute name="id" type="xs:IDREF" />
   <xs:attribute name="required" type="xs:boolean" default="false"/>
   <xs:attribute name="exclusivityGroup" type="xs:string" default=""/>
-  <xs:attribute name="minNum" type="core:positiveOrInfiniteInteger" default="-1"/>
-  <xs:attribute name="maxNum" type="core:positiveOrInfiniteInteger" default="-1"/>
+  <xs:attribute name="minNum" type="core:nonNegativeOrInfiniteIntegerOrRatio" default="-1"/>
+  <xs:attribute name="maxNum" type="core:nonNegativeOrInfiniteIntegerOrRatio" default="-1"/>
   <xs:attribute name="minPercentage" type="core:percentage" default="100"/>
   <xs:attribute name="maxPercentage" type="core:percentage" default="100"/>
   <xs:attribute name="roundDirection" type="updowntype" default="up"/>
@@ -127,7 +127,7 @@
   <xs:attribute name="id" type="xs:ID" use="required"/>
   <xs:attribute name="name" type="xs:string" use="required"/>
   <xs:attribute name="cost" type="core:nonNegativeNonInfiniteDouble" use="required"/>
-  <xs:attribute name="armoutType" type="armourtype" default="none"/>
+  <xs:attribute name="armoutType" type="armourtype" default="None"/>
 </xs:complexType>
 <xs:simpleType name="armourtype">
   <xs:restriction base="xs:string">
--- a/dtds/warfoundry-core.xsd	Sun Mar 22 16:37:43 2009 +0000
+++ b/dtds/warfoundry-core.xsd	Sun Mar 22 17:05:01 2009 +0000
@@ -30,4 +30,13 @@
     <xs:maxInclusive value="100"/>
   </xs:restriction>
 </xs:simpleType>
+<xs:simpleType name="nonNegativeOrInfiniteIntegerOrRatio">
+  <xs:union memberTypes="xs:nonNegativeInteger infinity ratio"/>
+</xs:simpleType>
+<xs:simpleType name="ratio">
+  <xs:restriction base="xs:double">
+    <xs:minInclusive value="0"/>
+    <xs:maxInclusive value="1"/>
+  </xs:restriction>
+</xs:simpleType>
 </xs:schema>
\ No newline at end of file