# HG changeset patch # User IBBoard # Date 1250938310 0 # Node ID 45f1db6356e9afcb5467f0075078faa9f2a58b68 # Parent 2060f23abee909abdf14f8f3f7b6bab647c9036b Fixes #53: Make XML file saver * force file extension * Make sure IDs are in valid XML format * Remove commented example of file * Output contained units diff -r 2060f23abee9 -r 45f1db6356e9 api/Factories/Xml/WarFoundryXmlSaver.cs --- a/api/Factories/Xml/WarFoundryXmlSaver.cs Fri Aug 21 20:12:27 2009 +0000 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs Sat Aug 22 10:51:50 2009 +0000 @@ -8,6 +8,7 @@ using System.Xml; using System.Xml.Schema; using IBBoard.Lang; +using IBBoard.Xml; using IBBoard.WarFoundry.API.Factories.Xml.Zip; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Savers; @@ -17,10 +18,17 @@ { public class WarFoundryXmlSaver : IWarFoundryFileSaver { + public const string ARMY_FILE_EXTENSION = ".army"; + public bool Save(Army toSave, string savePath) { bool success = false; ZipFile file = null; + + if (!savePath.EndsWith(ARMY_FILE_EXTENSION)) + { + savePath = savePath + ARMY_FILE_EXTENSION; + } try { @@ -55,28 +63,6 @@ private string CreateArmyXmlString(Army toSave) { - /* - - - - - - - - - - - - - - - - - - - - -*/ XmlDocument doc = new XmlDocument(); XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); doc.AppendChild(declaration); @@ -86,8 +72,10 @@ doc.Schemas.Add(schema); XmlElement root = doc.CreateElement("army"); doc.AppendChild(root); - root.SetAttribute("id", toSave.ID); + root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); root.SetAttribute("name", toSave.Name); + //Don't convert system and race to ID format as they could be stored in non-XML file formats + //If they are in XML files then they'll already be valid root.SetAttribute("system", toSave.GameSystem.ID); root.SetAttribute("race", toSave.Race.ID); root.SetAttribute("maxPoints", toSave.MaxPoints.ToString()); @@ -97,7 +85,6 @@ foreach (Unit unit in toSave.GetUnits()) { units.AppendChild(CreateUnitElement(unit, doc)); - } return doc.OuterXml; @@ -106,7 +93,7 @@ private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) { XmlElement unitElem = doc.CreateElement("unit"); - unitElem.SetAttribute("id", unit.ID); + unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); unitElem.SetAttribute("unitName", unit.Name); unitElem.SetAttribute("unitType", unit.UnitType.ID); unitElem.SetAttribute("size", unit.Size.ToString()); @@ -123,6 +110,13 @@ unitElem.AppendChild(equipmentElem); } + XmlElement containedElem = CreateContainedUnitsElement(unit, doc); + + if (containedElem != null) + { + unitElem.AppendChild(containedElem); + } + return unitElem; } @@ -153,5 +147,31 @@ equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed"); return equipmentItemElem; } + + private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc) + { + Unit[] containedUnits = unit.ContainedUnits; + int containedCount = containedUnits.Length; + XmlElement containedElem = null; + + if (containedCount > 0) + { + containedElem = doc.CreateElement("contained"); + + for (int i = 0; i < containedCount; i++) + { + containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc)); + } + } + + return containedElem; + } + + private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc) + { + XmlElement containedUnitElem = doc.CreateElement("containedUnit"); + containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID)); + return containedUnitElem; + } } }