Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate API/Exporters/WarFoundryXMLWithXSLExporter.cs @ 443:86725e88052e
Re #380: WarFoundry chokes on zips written by Mac OS X
* Add "is dot-file" check to behave like Linux and Mac and ignore files with name ".something"
Also:
* Fix load failure message argument order
* Save some regex checks by making sure zip entry is a file first
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 04 Dec 2011 20:40:31 +0000 |
parents | 71fceea2725b |
children | cd367acd7c48 |
rev | line source |
---|---|
398 | 1 // This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski |
2 // | |
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. | |
4 | |
5 using System; | |
6 using System.Collections.Generic; | |
7 using System.IO; | |
8 using System.Text; | |
9 using System.Xml; | |
10 using System.Xml.Xsl; | |
11 using System.Xml.XPath; | |
12 using System.Xml.Schema; | |
13 using IBBoard.Lang; | |
14 using IBBoard.Xml; | |
15 using IBBoard.WarFoundry.API.Objects; | |
16 using IBBoard.WarFoundry.API.Util; | |
17 | |
18 namespace IBBoard.WarFoundry.API.Exporters | |
19 { | |
20 /// <summary> | |
21 /// Custom exporter that exports an army as an XML file with an XSLT applied | |
22 /// </summary> | |
23 public class WarFoundryXmlWithXslExporter : IWarFoundryExporter | |
24 { | |
25 private static WarFoundryXmlWithXslExporter exporter; | |
26 | |
27 // Return the default class associated with this exporter | |
28 public static WarFoundryXmlWithXslExporter GetDefault() | |
29 { | |
30 if (exporter == null) | |
31 { | |
32 exporter = new WarFoundryXmlWithXslExporter(); | |
33 } | |
34 | |
35 return exporter; | |
36 } | |
37 | |
38 private WarFoundryXmlWithXslExporter() | |
39 { | |
40 // Hide constructor | |
41 } | |
42 | |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
43 // Write to file |
398 | 44 public void ExportArmy(Army army, string path) |
45 { | |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
46 XmlDocument xmlDoc = BuildXml(army); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
47 // Simple XML output settings |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
48 XmlWriterSettings xmlSettings = new XmlWriterSettings(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
49 xmlSettings.Indent = true; |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
50 xmlSettings.IndentChars = " "; |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
51 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
52 // Write XML to file |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
53 using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
54 { |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
55 xmlDoc.Save(writer); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
56 writer.Flush(); |
407
2c52f0235774
Closed exported file after completion of export/transformation.
Dan.Kulinski@dank-laptop.Global.Local
parents:
403
diff
changeset
|
57 writer.Close(); |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
58 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
59 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
60 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
61 // Write to file with transform |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
62 public void ExportArmyWithTransform(Army army, string savePath, string xslPath) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
63 { |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
64 XmlDocument xmlDoc = BuildXml(army); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
65 XslCompiledTransform xslTransform = new XslCompiledTransform(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
66 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
67 xslTransform.Load(xslPath); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
68 XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
69 xslTransform.Transform(xmlDoc, writer); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
70 writer.Flush(); |
407
2c52f0235774
Closed exported file after completion of export/transformation.
Dan.Kulinski@dank-laptop.Global.Local
parents:
403
diff
changeset
|
71 writer.Close(); |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
72 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
73 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
74 // Build the XML document to save or transform |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
75 private XmlDocument BuildXml(Army army) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
76 { |
398 | 77 XmlDocument armyList = new XmlDocument(); |
78 | |
79 // Everything will be a child of the army element | |
80 XmlElement root = armyList.CreateElement("army"); | |
81 | |
82 // Basic army information | |
83 XmlElement armyRace = armyList.CreateElement("race"); | |
84 armyRace.InnerText = army.Race.Name; | |
85 root.AppendChild(armyRace); | |
86 | |
87 XmlElement armyName = armyList.CreateElement("name"); | |
88 armyName.InnerText = army.Name; | |
89 root.AppendChild(armyName); | |
90 | |
91 XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable"); | |
92 armyAvailablePoints.InnerText = army.MaxPoints.ToString(); | |
93 root.AppendChild(armyAvailablePoints); | |
94 | |
95 XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed"); | |
96 armyUsedPoints.InnerText = army.Points.ToString(); | |
97 root.AppendChild(armyUsedPoints); | |
98 | |
99 // Get Categories and interate through each | |
100 foreach(ArmyCategory cat in army.Categories) | |
101 { | |
102 if (cat.GetUnits().Length == 0) | |
103 continue; | |
104 XmlElement armyCategory = armyList.CreateElement("category"); | |
105 armyCategory.SetAttribute("type", cat.Name); | |
106 | |
107 | |
108 // Get units and iterate through each | |
109 foreach(Unit uni in cat.GetUnits()) | |
110 { | |
111 XmlElement armyUnit = armyList.CreateElement("unit"); | |
112 armyUnit.SetAttribute("name", uni.UnitType.Name); | |
113 | |
114 foreach (Stat[] stat in uni.UnitStatsArraysWithName) | |
115 { | |
116 XmlElement armyStatLine = armyList.CreateElement("statLine"); | |
117 foreach (Stat singleStat in stat) | |
118 { | |
119 XmlElement armyStat = armyList.CreateElement("stat"); | |
120 armyStat.SetAttribute("name", singleStat.ParentSlotName); | |
121 armyStat.SetAttribute("value", singleStat.SlotValueString); | |
122 armyStatLine.AppendChild(armyStat); | |
123 } | |
124 armyUnit.AppendChild(armyStatLine); | |
125 } | |
126 armyUnit.SetAttribute("points", uni.Points.ToString()); | |
127 armyUnit.SetAttribute("models", uni.Size.ToString()); | |
128 | |
129 foreach (UnitEquipmentItem equip in uni.GetEquipment()) | |
130 { | |
131 XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); | |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
132 armyEquipmentItem.SetAttribute("name", equip.Name); |
398 | 133 |
401
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
134 int armyEquipAmount = 0; |
419 | 135 armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip); |
401
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
136 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
137 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip)) |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
138 { |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
139 float fraction = (float)(armyEquipAmount / 100.0); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
140 armyEquipAmount = (int)(fraction * uni.Size); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
141 } |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
142 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
398
diff
changeset
|
143 armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString()); |
398 | 144 |
145 armyUnit.AppendChild(armyEquipmentItem); | |
146 } | |
147 | |
148 foreach (Ability abil in uni.Abilities) | |
149 { | |
150 XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); | |
151 | |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
152 armyAbilityItem.SetAttribute("name", abil.Name); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
153 armyAbilityItem.SetAttribute("description", abil.Description); |
398 | 154 |
155 armyUnit.AppendChild(armyAbilityItem); | |
156 } | |
157 | |
158 armyCategory.AppendChild(armyUnit); | |
159 } | |
160 root.AppendChild(armyCategory); | |
161 } | |
162 | |
163 | |
164 | |
165 | |
166 // Append all Categories to the XML doc | |
167 | |
168 // Append tree to document | |
169 armyList.AppendChild(root); | |
170 | |
403
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
401
diff
changeset
|
171 return armyList; |
398 | 172 } |
173 private string GetEquipmentAmountRatioTranslation(double amount, int number) | |
174 { | |
175 return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number); | |
176 } | |
177 | |
178 private string GetEquipmentAmountNumberTranslation(int amount) | |
179 { | |
180 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount); | |
181 } | |
182 | |
183 private string GetEquipmentAmountAllTranslation(Unit unit) | |
184 { | |
185 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size); | |
186 } | |
187 } | |
188 } |