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, 55 insertions(+), 37 deletions(-) [+]
line diff
     1.1 --- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Mar 22 16:37:43 2009 +0000
     1.2 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Mar 22 17:05:01 2009 +0000
     1.3 @@ -65,7 +65,7 @@
     1.4  			XmlDocument doc = CreateXmlDocumentFromStream(stream);			
     1.5  			XmlElement elem = (XmlElement)doc.LastChild;
     1.6  			
     1.7 -			if (!elem.Name.Equals(elementName.Value))
     1.8 +			if (!elem.LocalName.Equals(elementName.Value))
     1.9  			{
    1.10  				throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name));
    1.11  			}
    1.12 @@ -198,9 +198,9 @@
    1.13  			
    1.14  			XmlNode elem = GetExtraData(system);
    1.15  			LoadCategoriesForSystem(system, elem);
    1.16 -			XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:categories");
    1.17 +			XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:sysStatsList");
    1.18  			XmlNode statsElem = nodeList.Item(0);
    1.19 -			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats", "http://ibboard.co.uk/warfoundry/system");
    1.20 +			string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats");
    1.21  			LoadSystemStatsForSystem(system, elem);
    1.22  			system.StandardSystemStatsID = defaultStatsID;
    1.23  			LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name);
    1.24 @@ -212,7 +212,7 @@
    1.25  		{
    1.26  			WarFoundryObject tempObj;
    1.27  						
    1.28 -			foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:category"))
    1.29 +			foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat"))
    1.30  			{
    1.31  				tempObj = CreateObjectFromElement(cat);
    1.32  				
    1.33 @@ -377,7 +377,7 @@
    1.34  			WarFoundryObject obj = null;
    1.35  			LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name);
    1.36  			
    1.37 -			if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
    1.38 +			if (elem.LocalName.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value))
    1.39  			{
    1.40  				LogNotifier.Debug(GetType(), "Create Category");
    1.41  				obj = CreateCategoryFromElement(elem);
    1.42 @@ -413,6 +413,30 @@
    1.43  				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
    1.44  			}
    1.45  		}
    1.46 +				
    1.47 +		private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName)
    1.48 +		{
    1.49 +			double doubleVal = double.NaN;
    1.50 +			string attribValue = elem.GetAttribute(attributeName);
    1.51 +			
    1.52 +			if (attribValue == "INF")
    1.53 +			{
    1.54 +				doubleVal = double.PositiveInfinity;
    1.55 +			}
    1.56 +			else
    1.57 +			{
    1.58 +				try
    1.59 +				{
    1.60 +					return int.Parse(attribValue);
    1.61 +				}
    1.62 +				catch(FormatException)
    1.63 +				{
    1.64 +					throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
    1.65 +				}
    1.66 +			}
    1.67 +			
    1.68 +			return doubleVal;
    1.69 +		}
    1.70  						
    1.71  		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
    1.72  		{
    1.73 @@ -500,43 +524,27 @@
    1.74  		{
    1.75  			string id = elem.GetAttribute("id");
    1.76  			string name = elem.GetAttribute("name");
    1.77 -			float cost = 0, min = 0, max = 0;
    1.78 +			double cost = 0, min = 0, max = 0;
    1.79  			ArmourType armourType;
    1.80  			
    1.81  			try
    1.82  			{
    1.83 -				cost = float.Parse(elem.GetAttribute("cost"));
    1.84 +				cost = GetDoubleValueFromAttribute(elem, "cost");
    1.85  			}
    1.86 -			catch(FormatException)
    1.87 +			catch(FormatException ex)
    1.88  			{
    1.89 -				throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number");
    1.90 +				throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex);
    1.91  			}			
    1.92  			
    1.93 -			try
    1.94 -			{
    1.95 -				min = float.Parse(elem.GetAttribute("min"));
    1.96 -			}
    1.97 -			catch(FormatException)
    1.98 -			{
    1.99 -				throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number");
   1.100 -			}			
   1.101 -			
   1.102 -			try
   1.103 -			{
   1.104 -				max = float.Parse(elem.GetAttribute("max"));
   1.105 -			}
   1.106 -			catch(FormatException)
   1.107 -			{
   1.108 -				throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number");
   1.109 -			}
   1.110 -			
   1.111  			try
   1.112  			{
   1.113  				armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType"));
   1.114  			}
   1.115 -			catch(FormatException)
   1.116 +			catch(ArgumentException ex)
   1.117  			{
   1.118 -				throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
   1.119 +				//throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration");
   1.120 +				LogNotifier.WarnFormat(GetType(), "Invalid 'armourType' for equipment {0} - {1}", id, ex.Message);
   1.121 +				armourType = ArmourType.None;
   1.122  			}
   1.123  			
   1.124  			if (elem.ChildNodes.Count>0)
     2.1 --- a/api/Objects/EquipmentItem.cs	Sun Mar 22 16:37:43 2009 +0000
     2.2 +++ b/api/Objects/EquipmentItem.cs	Sun Mar 22 17:05:01 2009 +0000
     2.3 @@ -12,11 +12,11 @@
     2.4  	/// </summary>
     2.5  	public class EquipmentItem : WarFoundryObject
     2.6  	{
     2.7 -		private float cost, min, max;
     2.8 +		private double cost, min, max;
     2.9  		private ArmourType armourType;
    2.10  		private Race equipForRace;
    2.11  
    2.12 -		public EquipmentItem(string id, string name, float itemCost, float minimum, float maximum, ArmourType itemArmourType, Race race) : base(id, name)
    2.13 +		public EquipmentItem(string id, string name, double itemCost, double minimum, double maximum, ArmourType itemArmourType, Race race) : base(id, name)
    2.14  		{
    2.15  			cost = itemCost;
    2.16  			min = minimum;
    2.17 @@ -30,7 +30,7 @@
    2.18  			get { return ((MaxNumber < 1 && MaxNumber > 0) || (MaxNumber == 1 && MinNumber > 0)); }
    2.19  		}
    2.20  
    2.21 -		public float MinNumber
    2.22 +		public double MinNumber
    2.23  		{
    2.24  			get { return min; }
    2.25  			set 
    2.26 @@ -44,7 +44,7 @@
    2.27  			}
    2.28  		}
    2.29  
    2.30 -		public float MaxNumber
    2.31 +		public double MaxNumber
    2.32  		{
    2.33  			get { return max; }
    2.34  			set 
    2.35 @@ -64,7 +64,7 @@
    2.36  			set { armourType = value; }
    2.37  		}
    2.38  
    2.39 -		public float Cost
    2.40 +		public double Cost
    2.41  		{
    2.42  			get { return cost; }
    2.43  			set { cost = value; }
     3.1 --- a/api/Objects/GameSystem.cs	Sun Mar 22 16:37:43 2009 +0000
     3.2 +++ b/api/Objects/GameSystem.cs	Sun Mar 22 17:05:01 2009 +0000
     3.3 @@ -80,6 +80,7 @@
     3.4  		{
     3.5  			get
     3.6  			{
     3.7 +				EnsureFullyLoaded();
     3.8  				return stats[defaultStats];
     3.9  			}
    3.10  		}
     4.1 --- a/dtds/race.xsd	Sun Mar 22 16:37:43 2009 +0000
     4.2 +++ b/dtds/race.xsd	Sun Mar 22 17:05:01 2009 +0000
     4.3 @@ -62,8 +62,8 @@
     4.4    <xs:attribute name="id" type="xs:IDREF" />
     4.5    <xs:attribute name="required" type="xs:boolean" default="false"/>
     4.6    <xs:attribute name="exclusivityGroup" type="xs:string" default=""/>
     4.7 -  <xs:attribute name="minNum" type="core:positiveOrInfiniteInteger" default="-1"/>
     4.8 -  <xs:attribute name="maxNum" type="core:positiveOrInfiniteInteger" default="-1"/>
     4.9 +  <xs:attribute name="minNum" type="core:nonNegativeOrInfiniteIntegerOrRatio" default="-1"/>
    4.10 +  <xs:attribute name="maxNum" type="core:nonNegativeOrInfiniteIntegerOrRatio" default="-1"/>
    4.11    <xs:attribute name="minPercentage" type="core:percentage" default="100"/>
    4.12    <xs:attribute name="maxPercentage" type="core:percentage" default="100"/>
    4.13    <xs:attribute name="roundDirection" type="updowntype" default="up"/>
    4.14 @@ -127,7 +127,7 @@
    4.15    <xs:attribute name="id" type="xs:ID" use="required"/>
    4.16    <xs:attribute name="name" type="xs:string" use="required"/>
    4.17    <xs:attribute name="cost" type="core:nonNegativeNonInfiniteDouble" use="required"/>
    4.18 -  <xs:attribute name="armoutType" type="armourtype" default="none"/>
    4.19 +  <xs:attribute name="armoutType" type="armourtype" default="None"/>
    4.20  </xs:complexType>
    4.21  <xs:simpleType name="armourtype">
    4.22    <xs:restriction base="xs:string">
     5.1 --- a/dtds/warfoundry-core.xsd	Sun Mar 22 16:37:43 2009 +0000
     5.2 +++ b/dtds/warfoundry-core.xsd	Sun Mar 22 17:05:01 2009 +0000
     5.3 @@ -30,4 +30,13 @@
     5.4      <xs:maxInclusive value="100"/>
     5.5    </xs:restriction>
     5.6  </xs:simpleType>
     5.7 +<xs:simpleType name="nonNegativeOrInfiniteIntegerOrRatio">
     5.8 +  <xs:union memberTypes="xs:nonNegativeInteger infinity ratio"/>
     5.9 +</xs:simpleType>
    5.10 +<xs:simpleType name="ratio">
    5.11 +  <xs:restriction base="xs:double">
    5.12 +    <xs:minInclusive value="0"/>
    5.13 +    <xs:maxInclusive value="1"/>
    5.14 +  </xs:restriction>
    5.15 +</xs:simpleType>
    5.16  </xs:schema>
    5.17 \ No newline at end of file