Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlSaver.cs @ 105:f5aec697b8ea
Re #53: Make WarFoundry XML saver
* Make XMLSaver implement Saver interface
* Strip out old code
* Start to code converting of objects to XML files
* Add class to store strings in to Zip files
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 17 Aug 2009 19:08:44 +0000 |
parents | 2f3cafb69799 |
children | 9bf5eff0992a |
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; |
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 | 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 | 15 |
16 namespace IBBoard.WarFoundry.API.Factories.Xml | |
17 { | |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
18 public class WarFoundryXmlSaver : IWarFoundryFileSaver |
0 | 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 | 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 | 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 | 32 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
33 finally |
0 | 34 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
35 if (file != null) |
82 | 36 { |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
37 file.Close(); |
0 | 38 } |
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 | 42 } |
105
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
43 |
0 | 44 private string CreateXmlString(WarFoundryObject toSave) |
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 | 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 | 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()); |
f5aec697b8ea
Re #53: Make WarFoundry XML saver
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
94 return doc.OuterXml; |
0 | 95 } |
96 } | |
97 } |