Mercurial > repos > IBBoard.WarFoundry.API
comparison 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 |
comparison
equal
deleted
inserted
replaced
42:d0d44434b557 | 43:d0812d7de39d |
---|---|
63 protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) | 63 protected XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) |
64 { | 64 { |
65 XmlDocument doc = CreateXmlDocumentFromStream(stream); | 65 XmlDocument doc = CreateXmlDocumentFromStream(stream); |
66 XmlElement elem = (XmlElement)doc.LastChild; | 66 XmlElement elem = (XmlElement)doc.LastChild; |
67 | 67 |
68 if (!elem.Name.Equals(elementName.Value)) | 68 if (!elem.LocalName.Equals(elementName.Value)) |
69 { | 69 { |
70 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | 70 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); |
71 } | 71 } |
72 | 72 |
73 return elem; | 73 return elem; |
196 | 196 |
197 system.SetAsLoading(); | 197 system.SetAsLoading(); |
198 | 198 |
199 XmlNode elem = GetExtraData(system); | 199 XmlNode elem = GetExtraData(system); |
200 LoadCategoriesForSystem(system, elem); | 200 LoadCategoriesForSystem(system, elem); |
201 XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:categories"); | 201 XmlNodeList nodeList = SelectNodes(elem, "/system:system/system:sysStatsList"); |
202 XmlNode statsElem = nodeList.Item(0); | 202 XmlNode statsElem = nodeList.Item(0); |
203 string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats", "http://ibboard.co.uk/warfoundry/system"); | 203 string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats"); |
204 LoadSystemStatsForSystem(system, elem); | 204 LoadSystemStatsForSystem(system, elem); |
205 system.StandardSystemStatsID = defaultStatsID; | 205 system.StandardSystemStatsID = defaultStatsID; |
206 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); | 206 LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); |
207 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID); | 207 LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID); |
208 system.SetAsFullyLoaded(); | 208 system.SetAsFullyLoaded(); |
210 | 210 |
211 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) | 211 private void LoadCategoriesForSystem(GameSystem system, XmlNode elem) |
212 { | 212 { |
213 WarFoundryObject tempObj; | 213 WarFoundryObject tempObj; |
214 | 214 |
215 foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:category")) | 215 foreach (XmlElement cat in SelectNodes(elem, "/system:system/system:categories/cat:cat")) |
216 { | 216 { |
217 tempObj = CreateObjectFromElement(cat); | 217 tempObj = CreateObjectFromElement(cat); |
218 | 218 |
219 if (tempObj is Category) | 219 if (tempObj is Category) |
220 { | 220 { |
375 private WarFoundryObject CreateObjectFromElement(XmlElement elem) | 375 private WarFoundryObject CreateObjectFromElement(XmlElement elem) |
376 { | 376 { |
377 WarFoundryObject obj = null; | 377 WarFoundryObject obj = null; |
378 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name); | 378 LogNotifier.DebugFormat(GetType(), "Create object for <{0}>", elem.Name); |
379 | 379 |
380 if (elem.Name.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) | 380 if (elem.LocalName.Equals(WarFoundryXmlElementName.CATEGORY_ELEMENT.Value)) |
381 { | 381 { |
382 LogNotifier.Debug(GetType(), "Create Category"); | 382 LogNotifier.Debug(GetType(), "Create Category"); |
383 obj = CreateCategoryFromElement(elem); | 383 obj = CreateCategoryFromElement(elem); |
384 } | 384 } |
385 else | 385 else |
410 } | 410 } |
411 catch(FormatException) | 411 catch(FormatException) |
412 { | 412 { |
413 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); | 413 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
414 } | 414 } |
415 } | |
416 | |
417 private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) | |
418 { | |
419 double doubleVal = double.NaN; | |
420 string attribValue = elem.GetAttribute(attributeName); | |
421 | |
422 if (attribValue == "INF") | |
423 { | |
424 doubleVal = double.PositiveInfinity; | |
425 } | |
426 else | |
427 { | |
428 try | |
429 { | |
430 return int.Parse(attribValue); | |
431 } | |
432 catch(FormatException) | |
433 { | |
434 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); | |
435 } | |
436 } | |
437 | |
438 return doubleVal; | |
415 } | 439 } |
416 | 440 |
417 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) | 441 private UnitType CreateUnitTypeFromElement(XmlElement elem, Race parentRace, GameSystem system) |
418 { | 442 { |
419 string id = elem.GetAttribute("id"); | 443 string id = elem.GetAttribute("id"); |
498 | 522 |
499 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) | 523 private EquipmentItem CreateEquipmentItemFromElement(XmlElement elem, Race race) |
500 { | 524 { |
501 string id = elem.GetAttribute("id"); | 525 string id = elem.GetAttribute("id"); |
502 string name = elem.GetAttribute("name"); | 526 string name = elem.GetAttribute("name"); |
503 float cost = 0, min = 0, max = 0; | 527 double cost = 0, min = 0, max = 0; |
504 ArmourType armourType; | 528 ArmourType armourType; |
505 | 529 |
506 try | 530 try |
507 { | 531 { |
508 cost = float.Parse(elem.GetAttribute("cost")); | 532 cost = GetDoubleValueFromAttribute(elem, "cost"); |
509 } | 533 } |
510 catch(FormatException) | 534 catch(FormatException ex) |
511 { | 535 { |
512 throw new FormatException("Attribute 'cost' of equipment item "+id+" was not a valid number"); | 536 throw new InvalidFileException("Attribute 'cost' of equipment item "+id+" was not a valid number", ex); |
513 } | 537 } |
514 | 538 |
515 try | 539 try |
516 { | 540 { |
517 min = float.Parse(elem.GetAttribute("min")); | |
518 } | |
519 catch(FormatException) | |
520 { | |
521 throw new FormatException("Attribute 'min' of equipment item "+id+" was not a valid number"); | |
522 } | |
523 | |
524 try | |
525 { | |
526 max = float.Parse(elem.GetAttribute("max")); | |
527 } | |
528 catch(FormatException) | |
529 { | |
530 throw new FormatException("Attribute 'max' of equipment item "+id+" was not a valid number"); | |
531 } | |
532 | |
533 try | |
534 { | |
535 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); | 541 armourType = (ArmourType)Enum.Parse(typeof(ArmourType), elem.GetAttribute("armourType")); |
536 } | 542 } |
537 catch(FormatException) | 543 catch(ArgumentException ex) |
538 { | 544 { |
539 throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration"); | 545 //throw new InvalidFileException("Attribute 'armourType' of equipment "+id+" was not a valid value from the enumeration"); |
546 LogNotifier.WarnFormat(GetType(), "Invalid 'armourType' for equipment {0} - {1}", id, ex.Message); | |
547 armourType = ArmourType.None; | |
540 } | 548 } |
541 | 549 |
542 if (elem.ChildNodes.Count>0) | 550 if (elem.ChildNodes.Count>0) |
543 { | 551 { |
544 //It has stats! | 552 //It has stats! |