Mercurial > repos > IBBoard.WarFoundry.API
comparison api/Factories/Xml/WarFoundryXmlRaceFactory.cs @ 217:89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
* Add null check and throw InvalidFileException when getting unit XML by ID
* Change InvalidDataException (which is a core exception about data streams) to InvalidFileException (which is our exception for incorrect data in a file)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Nov 2009 16:06:06 +0000 |
parents | 897c53d5a8aa |
children | f609bcf7035b |
comparison
equal
deleted
inserted
replaced
216:65553d2c8612 | 217:89e26d51afc2 |
---|---|
90 return cat; | 90 return cat; |
91 } | 91 } |
92 | 92 |
93 private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace) | 93 private UnitType GetUnitTypeFromDocument(XmlDocument doc, string id, Race parentRace) |
94 { | 94 { |
95 return GetUnitTypeForElement(WarFoundryXmlFactoryUtils.SelectSingleElement(doc, "/race:race/race:units/race:unit[@id='"+id+"']"), parentRace); | 95 XmlElement unitWithId = WarFoundryXmlFactoryUtils.SelectSingleElement (doc, "/race:race/race:units/race:unit[@id='" + id + "']"); |
96 | |
97 if (unitWithId == null) | |
98 { | |
99 throw new InvalidFileException("Could not find unit with ID "+id); | |
100 } | |
101 | |
102 return GetUnitTypeForElement(unitWithId, parentRace); | |
96 } | 103 } |
97 | 104 |
98 private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace) | 105 private UnitType GetUnitTypeForElement(XmlElement elem, Race parentRace) |
99 { | 106 { |
100 string id = elem.GetAttribute("id"); | 107 string id = elem.GetAttribute("id"); |
136 type.CostPerTrooper = XmlTools.GetDoubleValueFromAttribute(elem, "points"); | 143 type.CostPerTrooper = XmlTools.GetDoubleValueFromAttribute(elem, "points"); |
137 type.BaseUnitCost = XmlTools.GetDoubleValueFromAttribute(elem, "unitPoints"); | 144 type.BaseUnitCost = XmlTools.GetDoubleValueFromAttribute(elem, "unitPoints"); |
138 } | 145 } |
139 catch (FormatException ex) | 146 catch (FormatException ex) |
140 { | 147 { |
141 throw new InvalidDataException(ex.Message, ex); | 148 throw new InvalidFileException(ex.Message, ex); |
142 } | 149 } |
143 | 150 |
144 string mainCatID = elem.GetAttribute("cat"); | 151 string mainCatID = elem.GetAttribute("cat"); |
145 Category cat = type.Race.GetCategory(mainCatID); | 152 Category cat = type.Race.GetCategory(mainCatID); |
146 | 153 |
147 if (cat == null) | 154 if (cat == null) |
148 { | 155 { |
149 throw new InvalidDataException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID)); | 156 throw new InvalidFileException(String.Format("Category with ID '{1}' did not exist for UnitType '{0}'", type.Name, mainCatID)); |
150 } | 157 } |
151 | 158 |
152 type.MainCategory = cat; | 159 type.MainCategory = cat; |
153 XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats"); | 160 XmlElement statsElement = WarFoundryXmlFactoryUtils.SelectSingleElement(elem, "race:stats"); |
154 Stats unitStats = ParseUnitStats(statsElement, type.GameSystem); | 161 Stats unitStats = ParseUnitStats(statsElement, type.GameSystem); |