# HG changeset patch # User IBBoard # Date 1238100582 0 # Node ID 85f2b9c3609c32f1f656fae298db5d48cb26430d # Parent a5855fcd75abc7bb7e1aab17fbbba5f61a985a9b Re #13 - Use XPath for file loading * Replace loading of race unit types, categories and equipment with XPath method * Add loading of abilities using XPath Also: * Add implementation for Ability * Add AddAbility method to Race * Alter some logging so that "for ID" is followed by ID not name diff -r a5855fcd75ab -r 85f2b9c3609c api/Factories/Xml/WarFoundryXmlFactory.cs --- a/api/Factories/Xml/WarFoundryXmlFactory.cs Mon Mar 23 20:57:07 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlFactory.cs Thu Mar 26 20:49:42 2009 +0000 @@ -203,8 +203,8 @@ string defaultStatsID = ((XmlElement)statsElem).GetAttribute("defaultStats"); LoadSystemStatsForSystem(system, elem); system.StandardSystemStatsID = defaultStatsID; - LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.Name); - LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.Name, system.StandardSystemStatsID); + LogNotifier.DebugFormat(GetType(), "Completed loading of GameSystem with ID {0}", system.ID); + LogNotifier.DebugFormat(GetType(), "GameSystem with ID {0} default stats: {1}", system.ID, system.StandardSystemStatsID); system.SetAsFullyLoaded(); } @@ -244,40 +244,32 @@ race.SetAsLoading(); XmlNode elem = GetExtraData(race); - XmlNode colNode = elem.FirstChild; - - foreach (XmlElement node in colNode.ChildNodes) + + foreach (XmlElement node in SelectNodes(elem, "/race:race/race:units/race:unit")) { UnitType type = CreateUnitTypeFromElement(node, race, race.GameSystem); race.AddUnitType(type); } - colNode = colNode.NextSibling; - - if (colNode!=null && colNode.Name == WarFoundryXmlElementName.CATEGORIES_ELEMENT.Value) + foreach (XmlElement node in SelectNodes(elem, "/race:race/race:categories/cat:cat")) { - foreach (XmlElement node in colNode.ChildNodes) - { - race.AddCategory(CreateCategoryFromElement(node)); - } - - colNode = colNode.NextSibling; + race.AddCategory(CreateCategoryFromElement(node)); } - if (colNode!=null && colNode.Name == WarFoundryXmlElementName.RACE_EQUIPMENT_ITEMS_ELEMENT.Value) + foreach (XmlElement node in SelectNodes(elem, "/race:race/race:equipment/cat:equipmentItem")) { - foreach (XmlElement node in colNode.ChildNodes) - { - EquipmentItem item = CreateEquipmentItemFromElement(node, race); - race.AddEquipmentItem(item); - } + EquipmentItem item = CreateEquipmentItemFromElement(node, race); + race.AddEquipmentItem(item); + } + + foreach (XmlElement node in SelectNodes(elem, "/race:race/race:abilities/cat:ability")) + { + Ability ability = CreateAbilityFromElement(node, race); + race.AddAbility(ability); } - Dictionary raceAbilities = new Dictionary(); - //TODO: Load abilities - race.SetAbilities(raceAbilities); race.SetAsFullyLoaded(); - LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.Name); + LogNotifier.DebugFormat(GetType(), "Completed loading of Race with ID {0}", race.ID); } protected XmlDocument CreateXmlDocumentFromStream(Stream stream) @@ -287,7 +279,7 @@ try { - doc.Load(reader); + doc.Load(reader); } //Don't catch XMLSchemaExceptions - let them get thrown out finally @@ -549,5 +541,15 @@ return new EquipmentItem(id, name, cost, min, max, armourType, race); } + + private Ability CreateAbilityFromElement(XmlElement elem, Race race) + { + string id = elem.GetAttribute("id"); + string name = elem.GetAttribute("name"); + Ability ability = new Ability(id, name); + XmlNode node = elem.SelectSingleNode("description", GetNamespaceManager()); + ability.Description = (node == null) ? "" : node.InnerText; + return ability; + } } } \ No newline at end of file diff -r a5855fcd75ab -r 85f2b9c3609c api/Objects/Ability.cs --- a/api/Objects/Ability.cs Mon Mar 23 20:57:07 2009 +0000 +++ b/api/Objects/Ability.cs Thu Mar 26 20:49:42 2009 +0000 @@ -6,10 +6,27 @@ namespace IBBoard.WarFoundry.API.Objects { + /// + /// An Ability is a special rule that a UnitType has, made up of an ability name and a description. + /// public class Ability : WarFoundryObject { - public Ability() + private string description; + + public Ability(String id, String name) : base(id, name) + { + } + + public string Description { + get { return description; } + set + { + if (value!=null) + { + description = value.Trim(); + } + } } } } diff -r a5855fcd75ab -r 85f2b9c3609c api/Objects/Race.cs --- a/api/Objects/Race.cs Mon Mar 23 20:57:07 2009 +0000 +++ b/api/Objects/Race.cs Thu Mar 26 20:49:42 2009 +0000 @@ -238,9 +238,19 @@ return items; } + public void AddAbility(Ability newAbility) + { + //TODO: Throw DuplicateItemException + abilities.Add(newAbility.ID, newAbility); + } + + [Obsolete("Use AddAbility method instead")] public void SetAbilities(Dictionary newAbilities) { - abilities = newAbilities; + foreach (Ability ability in newAbilities.Values) + { + AddAbility(ability); + } } public Ability GetAbility(string id)