annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 107:c4ee96a91018

Re #53: Add XML saver * Actually add unit elements, since creating them doesn't add them as well!
author IBBoard <dev@ibboard.co.uk>
date Fri, 21 Aug 2009 19:51:24 +0000
parents 9bf5eff0992a
children 2060f23abee9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
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
306558904c2a Re #1 - LGPL license all code
IBBoard <dev@ibboard.co.uk>
parents: 0
diff changeset
4
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.Collections.Generic;
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using IBBoard.Lang;
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
11 using IBBoard.WarFoundry.API.Factories.Xml.Zip;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12 using IBBoard.WarFoundry.API.Objects;
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
13 using IBBoard.WarFoundry.API.Savers;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
14 using ICSharpCode.SharpZipLib.Zip;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 namespace IBBoard.WarFoundry.API.Factories.Xml
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
18 public class WarFoundryXmlSaver : IWarFoundryFileSaver
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
20 public bool Save(Army toSave, string savePath)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
22 bool success = false;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
23 ZipFile file = null;
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
24
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
25 try
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
27 file = ZipFile.Create(savePath);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
28 file.BeginUpdate();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
29 file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.army");
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
30 file.CommitUpdate();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
31 success = true;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
33 finally
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
35 if (file != null)
82
3ea0ab04352b * Fix line terminators
IBBoard <dev@ibboard.co.uk>
parents: 15
diff changeset
36 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
37 file.Close();
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
40
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
41 return success;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
43
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 private string CreateXmlString(WarFoundryObject toSave)
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
46 /*
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
47 <army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
48 <units>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
49 <unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
50 <equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
51 <equipItem id="equip1" amount="1"/>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
52 </equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
53 </unit>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
54 <unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20">
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
55 <equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
56 <equipItem id="equip1" amount="1"/>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
57 <equipItem id="equip2" amount="1"/>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
58 </equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
59 </unit>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
60 <unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15">
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
61 <equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
62 <equipItem id="equip3" amount="1"/>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
63 </equipment>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
64 </unit>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
65 </units>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
66 </army>
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
67 */
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
68 string xmlString = "";
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
69
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
70 if (toSave is Army)
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
71 {
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
72 xmlString = CreateArmyXmlString((Army)toSave);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
73 }
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
74
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
75 return xmlString;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 }
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
77
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
78 private string CreateArmyXmlString(Army toSave)
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79 {
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
80 XmlDocument doc = new XmlDocument();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
81 XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
82 doc.AppendChild(declaration);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
83 XmlSchema schema = new XmlSchema();
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
84 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
85 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
86 doc.Schemas.Add(schema);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
87 XmlElement root = doc.CreateElement("army");
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
88 doc.AppendChild(root);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
89 root.SetAttribute("id", toSave.ID);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
90 root.SetAttribute("name", toSave.Name);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
91 root.SetAttribute("system", toSave.GameSystem.ID);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
92 root.SetAttribute("race", toSave.Race.ID);
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
93 root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
106
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
94 XmlElement units = doc.CreateElement("units");
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
95 root.AppendChild(units);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
96
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
97 foreach (Unit unit in toSave.GetUnits())
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
98 {
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
99 XmlElement unitElem = doc.CreateElement("unit");
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
100 unitElem.SetAttribute("id", unit.ID);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
101 unitElem.SetAttribute("unitName", unit.Name);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
102 unitElem.SetAttribute("unitType", unit.UnitType.ID);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
103 unitElem.SetAttribute("size", unit.Size.ToString());
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
104
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
105 if (!unit.Race.Equals(toSave.Race))
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
106 {
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
107 unitElem.SetAttribute("race", unit.Race.ID);
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
108 }
107
c4ee96a91018 Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 106
diff changeset
109
c4ee96a91018 Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 106
diff changeset
110 units.AppendChild(unitElem);
106
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
111 }
9bf5eff0992a Re #53: Add XML saver
IBBoard <dev@ibboard.co.uk>
parents: 105
diff changeset
112
105
f5aec697b8ea Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents: 104
diff changeset
113 return doc.OuterXml;
0
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 }
520818033bb6 Initial commit of WarFoundry code
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 }