Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 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 | f0fb96d0cfe9 |
children | d0c60b3204c1 |
rev | line source |
---|---|
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
1 // This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2008, 2009 IBBoard. |
15 | 2 // |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
82
diff
changeset
|
3 // 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. |
15 | 4 |
0 | 5 using System; |
6 using System.Collections.Generic; | |
7 using System.IO; | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
8 using System.Xml; |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
9 using System.Xml.Schema; |
0 | 10 using IBBoard.Lang; |
109 | 11 using IBBoard.Xml; |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
12 using IBBoard.WarFoundry.API.Factories.Xml.Zip; |
0 | 13 using IBBoard.WarFoundry.API.Objects; |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
14 using IBBoard.WarFoundry.API.Savers; |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
15 using ICSharpCode.SharpZipLib.Zip; |
0 | 16 |
17 namespace IBBoard.WarFoundry.API.Factories.Xml | |
18 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
19 public class WarFoundryXmlSaver : IWarFoundryFileSaver |
0 | 20 { |
109 | 21 public const string ARMY_FILE_EXTENSION = ".army"; |
22 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
23 public bool Save(Army toSave, string savePath) |
0 | 24 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
25 bool success = false; |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
26 ZipFile file = null; |
109 | 27 |
28 if (!savePath.EndsWith(ARMY_FILE_EXTENSION)) | |
29 { | |
30 savePath = savePath + ARMY_FILE_EXTENSION; | |
31 } | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
32 |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
33 try |
0 | 34 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
35 file = ZipFile.Create(savePath); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
36 file.BeginUpdate(); |
110 | 37 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.armyx"); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
38 file.CommitUpdate(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
39 success = true; |
0 | 40 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
41 finally |
0 | 42 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
43 if (file != null) |
82 | 44 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
45 file.Close(); |
0 | 46 } |
47 } | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
48 |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
49 return success; |
0 | 50 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
51 |
0 | 52 private string CreateXmlString(WarFoundryObject toSave) |
53 { | |
108 | 54 string xmlString = ""; |
55 | |
56 if (toSave is Army) | |
57 { | |
58 xmlString = CreateArmyXmlString((Army)toSave); | |
59 } | |
60 | |
61 return xmlString; | |
62 } | |
63 | |
64 private string CreateArmyXmlString(Army toSave) | |
65 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
66 XmlDocument doc = new XmlDocument(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
67 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
68 doc.AppendChild(declaration); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
69 XmlSchema schema = new XmlSchema(); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
70 schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army"); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
71 schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
72 doc.Schemas.Add(schema); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
73 XmlElement root = doc.CreateElement("army"); |
112
863518044d38
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
110
diff
changeset
|
74 root.SetAttribute("xmlns", "http://ibboard.co.uk/warfoundry/army"); |
863518044d38
Re #54: Add Army support to WarFoundryFactory
IBBoard <dev@ibboard.co.uk>
parents:
110
diff
changeset
|
75 root.SetAttribute("xmlns:core", "http://ibboard.co.uk/warfoundry/core"); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
76 doc.AppendChild(root); |
109 | 77 root.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(toSave.ID)); |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
78 root.SetAttribute("name", toSave.Name); |
109 | 79 //Don't convert system and race to ID format as they could be stored in non-XML file formats |
80 //If they are in XML files then they'll already be valid | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
81 root.SetAttribute("system", toSave.GameSystem.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
82 root.SetAttribute("race", toSave.Race.ID); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
83 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString()); |
106 | 84 XmlElement units = doc.CreateElement("units"); |
85 root.AppendChild(units); | |
86 | |
87 foreach (Unit unit in toSave.GetUnits()) | |
88 { | |
108 | 89 units.AppendChild(CreateUnitElement(unit, doc)); |
106 | 90 } |
91 | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
92 return doc.OuterXml; |
0 | 93 } |
108 | 94 |
95 private XmlElement CreateUnitElement(Unit unit, XmlDocument doc) | |
96 { | |
97 XmlElement unitElem = doc.CreateElement("unit"); | |
109 | 98 unitElem.SetAttribute("id", XmlTools.GetAsciiXmlIdForString(unit.ID)); |
108 | 99 unitElem.SetAttribute("unitName", unit.Name); |
100 unitElem.SetAttribute("unitType", unit.UnitType.ID); | |
101 unitElem.SetAttribute("size", unit.Size.ToString()); | |
102 | |
103 if (!unit.Race.Equals(unit.Army.Race)) | |
104 { | |
105 unitElem.SetAttribute("race", unit.Race.ID); | |
106 } | |
107 | |
108 XmlElement equipmentElem = CreateEquipmentItemsElement(unit, doc); | |
109 | |
110 if (equipmentElem != null) | |
111 { | |
112 unitElem.AppendChild(equipmentElem); | |
113 } | |
114 | |
109 | 115 XmlElement containedElem = CreateContainedUnitsElement(unit, doc); |
116 | |
117 if (containedElem != null) | |
118 { | |
119 unitElem.AppendChild(containedElem); | |
120 } | |
121 | |
108 | 122 return unitElem; |
123 } | |
124 | |
125 private XmlElement CreateEquipmentItemsElement(Unit unit, XmlDocument doc) | |
126 { | |
127 UnitEquipmentItem[] equipItems = unit.GetEquipment(); | |
128 int equipItemCount = equipItems.Length; | |
129 XmlElement equipmentElem = null; | |
130 | |
131 if (equipItemCount > 0) | |
132 { | |
133 equipmentElem = doc.CreateElement("equipment"); | |
134 | |
135 for (int i = 0; i < equipItemCount; i++) | |
136 { | |
137 equipmentElem.AppendChild(CreateEquipmentElement(equipItems[i], unit, doc)); | |
138 } | |
139 } | |
140 | |
141 return equipmentElem; | |
142 } | |
143 | |
144 private XmlElement CreateEquipmentElement(UnitEquipmentItem item, Unit unit, XmlDocument doc) | |
145 { | |
146 XmlElement equipmentItemElem = doc.CreateElement("equipItem"); | |
147 equipmentItemElem.SetAttribute("id", item.EquipmentItemID); | |
148 equipmentItemElem.SetAttribute("amount", unit.GetEquipmentAmount(item).ToString()); | |
149 equipmentItemElem.SetAttribute("amountType", unit.GetEquipmentAmountIsRatio(item) ? "ratio" : "fixed"); | |
150 return equipmentItemElem; | |
151 } | |
109 | 152 |
153 private XmlElement CreateContainedUnitsElement(Unit unit, XmlDocument doc) | |
154 { | |
155 Unit[] containedUnits = unit.ContainedUnits; | |
156 int containedCount = containedUnits.Length; | |
157 XmlElement containedElem = null; | |
158 | |
159 if (containedCount > 0) | |
160 { | |
161 containedElem = doc.CreateElement("contained"); | |
162 | |
163 for (int i = 0; i < containedCount; i++) | |
164 { | |
165 containedElem.AppendChild(CreateContainedUnitElement(containedUnits[i], doc)); | |
166 } | |
167 } | |
168 | |
169 return containedElem; | |
170 } | |
171 | |
172 private XmlElement CreateContainedUnitElement(Unit unit, XmlDocument doc) | |
173 { | |
174 XmlElement containedUnitElem = doc.CreateElement("containedUnit"); | |
175 containedUnitElem.SetAttribute("containedID", XmlTools.GetAsciiXmlIdForString(unit.ID)); | |
176 return containedUnitElem; | |
177 } | |
0 | 178 } |
179 } |