changeset 9:6ad505b6c36e

Re #10 - Refactor for readability * Refactor out repeated number parsing in to a separate method
author IBBoard <dev@ibboard.co.uk>
date Sun, 04 Jan 2009 20:06:28 +0000
parents 613bc5eaac59
children 607c3232d689
files api/Factories/Xml/WarFoundryXmlFactory.cs
diffstat 1 files changed, 24 insertions(+), 132 deletions(-) [+]
line wrap: on
line diff
--- a/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Jan 04 19:24:13 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlFactory.cs	Sun Jan 04 20:06:28 2009 +0000
@@ -355,91 +355,30 @@
 		{
 			string id = elem.GetAttribute("id");
 			string name = elem.GetAttribute("name");
-			int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount;
-			
-			try
-			{
-				minPc = int.Parse(elem.GetAttribute("minPercentage"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'minPercentage' of category "+id+" was not a valid number");
-			}
-
+			int minPc, maxPc, minPts, maxPts, minChoices, maxChoices, baseValue, incValue, incAmount;
+			minPc = GetIntValueFromAttribute(elem, "minPercentage");
+			maxPc = GetIntValueFromAttribute(elem, "maxPercentage");
+			minPts = GetIntValueFromAttribute(elem, "minPoints");
+			maxPts = GetIntValueFromAttribute(elem, "maxPoints");
+			minChoices = GetIntValueFromAttribute(elem, "minChoices");
+			maxChoices = GetIntValueFromAttribute(elem, "maxChoices");
+			baseValue = GetIntValueFromAttribute(elem, "baseValue");
+			incValue = GetIntValueFromAttribute(elem, "incValue");
+			incAmount = GetIntValueFromAttribute(elem, "incAmount");
+						
+			return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount);
+		}
+				
+		private GetIntValueFromAttribute(XmlElement elem, string attributeName)
+		{
 			try
 			{
-				maxPc = int.Parse(elem.GetAttribute("maxPercentage"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'maxPercentage' of category "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				minPts = int.Parse(elem.GetAttribute("minPoints"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'minPoints' of category "+id+" was not a valid number");
-			}
-
-			try
-			{
-				maxPts = int.Parse(elem.GetAttribute("maxPoints"));
+				return int.Parse(elem.GetAttribute(attributeName));
 			}
 			catch(FormatException)
 			{
-				throw new FormatException("Attribute 'maxPoints' of category "+id+" was not a valid number");
+				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
 			}
-			
-			try
-			{
-				minChoices = int.Parse(elem.GetAttribute("minChoices"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'minChoices' of category "+id+" was not a valid number");
-			}
-
-			try
-			{
-				maxChoices = int.Parse(elem.GetAttribute("maxChoices"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'maxChoices' of category "+id+" was not a valid number");
-			}
-
-			try
-			{
-				baseValue = int.Parse(elem.GetAttribute("baseValue"));
-			
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'baseValue' of category "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				incValue = int.Parse(elem.GetAttribute("incValue"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'incValue' of category "+id+" was not a valid number");
-			}
-
-			try
-			{
-				incAmount = int.Parse(elem.GetAttribute("incAmount"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'incAmount' of category "+id+" was not a valid number");
-			}
-			
-			return new Category(id, name, minPts, maxPts, minPc, maxPc, minChoices, maxChoices, baseValue, incValue, incAmount);
 		}
 						
 		private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system)
@@ -454,65 +393,18 @@
 			bool found = false;
 			List<string> catIDs = new List<string>();
 			string catID;
-			
-			try
-			{
-				minNum = int.Parse(elem.GetAttribute("minNum"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'minNum' of unit "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				maxNum = int.Parse(elem.GetAttribute("maxNum"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'maxNum' of unit "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				minSize = int.Parse(elem.GetAttribute("minSize"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'minSize' of unit "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				maxSize = int.Parse(elem.GetAttribute("maxSize"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'maxSize' of unit "+id+" was not a valid number");
-			}
+			minNum = GetIntValueFromAttribute(elem, "minNum");
+			maxNum = GetIntValueFromAttribute(elem, "maxNum");
+			minSize = GetIntValueFromAttribute(elem, "minSize");
+			maxSize = GetIntValueFromAttribute(elem, "maxSize");
 			
 			if (minSize > maxSize && maxSize!=-1)
 			{
 				minSize = maxSize;
 			}
 			
-			try
-			{
-				points = int.Parse(elem.GetAttribute("points"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'points' of unit "+id+" was not a valid number");
-			}
-			
-			try
-			{
-				unitPoints = int.Parse(elem.GetAttribute("unitPoints"));
-			}
-			catch(FormatException)
-			{
-				throw new FormatException("Attribute 'trooperPoints' of unit "+id+" was not a valid number");
-			}
+			points = GetIntValueFromAttribute(elem, "points");
+			unitPoints = GetIntValueFromAttribute(elem, "unitPoints");
 			
 			XmlNode node = elem.FirstChild;