Mercurial > repos > IBBoard.WarFoundry.API
changeset 112:863518044d38
Re #54: Add Army support to WarFoundryFactory
* Stop "custom equipment" node being mandatory
* Remove unused "ratio" definition
* Change "integer or ratio" definition to "integer or percentage"
* Use "integer or percentage" definition in army XSD
* Add exception to say that required objects were missing (Game System and Race)
* Throw exceptions on creating army if game system or race is missing
Re #53: Add saving of Army to XML file
* Add namespace attributes to XML root node
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 22 Aug 2009 18:18:20 +0000 |
parents | b5ae022ce7b8 |
children | 5ebbffd4a05f |
files | IBBoard.WarFoundry.API.csproj api/Factories/RequiredDataMissingException.cs api/Factories/Xml/WarFoundryXmlArmyFactory.cs api/Factories/Xml/WarFoundryXmlSaver.cs api/WarFoundryLoader.cs dtds/army.xsd dtds/warfoundry-core.xsd |
diffstat | 7 files changed, 48 insertions(+), 17 deletions(-) [+] |
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj Sat Aug 22 15:34:25 2009 +0000 +++ b/IBBoard.WarFoundry.API.csproj Sat Aug 22 18:18:20 2009 +0000 @@ -133,6 +133,7 @@ <Compile Include="api\Commands\ReplaceUnitEquipmentWithNumericAmountItemCommand.cs" /> <Compile Include="api\Commands\ReplaceUnitEquipmentWithRatioAmountItemCommand.cs" /> <Compile Include="api\Factories\Xml\Zip\StringZipEntrySource.cs" /> + <Compile Include="api\Factories\RequiredDataMissingException.cs" /> </ItemGroup> <ItemGroup> <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/api/Factories/RequiredDataMissingException.cs Sat Aug 22 18:18:20 2009 +0000 @@ -0,0 +1,19 @@ +// This file (RequiredDataMissingException.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard +// +// The file and the library/program it is in are licensed and distributed, without warranty, under the GNU Affero GPL license, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. + +using System; + +namespace IBBoard.WarFoundry.API +{ + /// <summary> + /// An exception that is thrown when a file cannot be loaded because one of the data files that it depends on has + /// not been loaded. Normally occurs when loading an army file without having the correct game system or race. + /// </summary> + public class RequiredDataMissingException : Exception + { + public RequiredDataMissingException(String file, String missingAttribute, String requiredValue) : base(String.Format("Could not find object with ID {2} for {1} of {0}", file, missingAttribute, requiredValue)) + { + } + } +}
--- a/api/Factories/Xml/WarFoundryXmlArmyFactory.cs Sat Aug 22 15:34:25 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlArmyFactory.cs Sat Aug 22 18:18:20 2009 +0000 @@ -20,8 +20,20 @@ string name = elem.GetAttribute("name"); string systemID = elem.GetAttribute("gameSystem"); GameSystem system = WarFoundryLoader.GetDefault().GetGameSystem(systemID); + + if (system == null) + { + throw new RequiredDataMissingException(file.Name, "gameSystem", systemID); + } + string raceID = elem.GetAttribute("race"); Race race = WarFoundryLoader.GetDefault().GetRace(system, raceID); + + if (race == null) + { + throw new RequiredDataMissingException(file.Name, "race", raceID); + } + int points = XmlTools.GetIntValueFromAttribute(elem, "maxPoints"); Army army = new Army(race, name, points, file); //TODO: Complete loading of army
--- a/api/Factories/Xml/WarFoundryXmlSaver.cs Sat Aug 22 15:34:25 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs Sat Aug 22 18:18:20 2009 +0000 @@ -71,6 +71,8 @@ schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); doc.Schemas.Add(schema); XmlElement root = doc.CreateElement("army"); + root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army"); + root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); doc.AppendChild(root); root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); root.SetAttribute("name", toSave.Name);
--- a/api/WarFoundryLoader.cs Sat Aug 22 15:34:25 2009 +0000 +++ b/api/WarFoundryLoader.cs Sat Aug 22 18:18:20 2009 +0000 @@ -814,18 +814,21 @@ public Army LoadArmy(FileInfo file) { - IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); - ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(file); - + IWarFoundryFactory factory = GetArmyLoadingFactoryForFile(file); Army loadedArmy = null; - if (objs.Count == 1) + if (factory != null) { - foreach (IWarFoundryObject obj in objs) + ICollection<IWarFoundryObject> objs = factory.CreateObjectsFromFile(file); + + if (objs.Count == 1) { - if (obj is Army) + foreach (IWarFoundryObject obj in objs) { - loadedArmy = (Army) obj; + if (obj is Army) + { + loadedArmy = (Army) obj; + } } } }
--- a/dtds/army.xsd Sat Aug 22 15:34:25 2009 +0000 +++ b/dtds/army.xsd Sat Aug 22 18:18:20 2009 +0000 @@ -15,7 +15,7 @@ <xs:element name="equipItem" minOccurs="1" maxOccurs="unbounded"> <xs:complexType> <xs:attribute name="id" type="xs:string" /> <!-- ID reference to either a custom equipment item or a Race equipment item --> - <xs:attribute name="amount" type="core:nonNegativeDouble" use="required"/><!-- Double used to allow for percentages to be stored --> + <xs:attribute name="amount" type="core:nonNegativeOrInfiniteIntegerOrPercentage" use="required"/><!-- Double used to allow for percentages to be stored --> <xs:attribute name="amountType" type="equipmentAmountType" default="ratio"/> <xs:attribute name="isCustomEquipment" type="xs:boolean" default="false"/> </xs:complexType> @@ -45,7 +45,7 @@ </xs:sequence> </xs:complexType> </xs:element> - <xs:element name="customEquipment" minOccurs="1"> + <xs:element name="customEquipment" minOccurs="0"> <xs:complexType> <xs:sequence> <xs:element name="customEquip" minOccurs="0" maxOccurs="unbounded">
--- a/dtds/warfoundry-core.xsd Sat Aug 22 15:34:25 2009 +0000 +++ b/dtds/warfoundry-core.xsd Sat Aug 22 18:18:20 2009 +0000 @@ -30,13 +30,7 @@ <xs:maxInclusive value="100"/> </xs:restriction> </xs:simpleType> -<xs:simpleType name="nonNegativeOrInfiniteIntegerOrRatio"> - <xs:union memberTypes="xs:nonNegativeInteger infinity ratio"/> -</xs:simpleType> -<xs:simpleType name="ratio"> - <xs:restriction base="xs:double"> - <xs:minInclusive value="0"/> - <xs:maxInclusive value="1"/> - </xs:restriction> +<xs:simpleType name="nonNegativeOrInfiniteIntegerOrPercentage"> + <xs:union memberTypes="xs:nonNegativeInteger infinity percentage"/> </xs:simpleType> </xs:schema> \ No newline at end of file