Mercurial > repos > IBBoard.WarFoundry.API
annotate API/Exporters/WarFoundryXMLWithXSLExporter.cs @ 404:5d7584a73d7f xsl-output
XSL with transform operations
author | Dan.Kulinski@dank-laptop.Global.Local |
---|---|
date | Thu, 18 Aug 2011 11:14:58 -0600 |
parents | 3a71f8af5bde |
children | 2c52f0235774 |
rev | line source |
---|---|
399 | 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 | |
404
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
43 // Write to file |
399 | 44 public void ExportArmy(Army army, string path) |
45 { | |
404
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
46 XmlDocument xmlDoc = BuildXml(army); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
47 // Simple XML output settings |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
48 XmlWriterSettings xmlSettings = new XmlWriterSettings(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
49 xmlSettings.Indent = true; |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
50 xmlSettings.IndentChars = " "; |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
51 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
52 // Write XML to file |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
53 using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
54 { |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
55 xmlDoc.Save(writer); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
56 writer.Flush(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
57 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
58 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
59 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
60 // Write to file with transform |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
61 public void ExportArmyWithTransform(Army army, string savePath, string xslPath) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
62 { |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
63 XmlDocument xmlDoc = BuildXml(army); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
64 XslCompiledTransform xslTransform = new XslCompiledTransform(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
65 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
66 xslTransform.Load(xslPath); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
67 XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
68 xslTransform.Transform(xmlDoc, writer); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
69 writer.Flush(); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
70 } |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
71 |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
72 // Build the XML document to save or transform |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
73 private XmlDocument BuildXml(Army army) |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
74 { |
399 | 75 XmlDocument armyList = new XmlDocument(); |
76 | |
77 // Everything will be a child of the army element | |
78 XmlElement root = armyList.CreateElement("army"); | |
79 | |
80 // Basic army information | |
81 XmlElement armyRace = armyList.CreateElement("race"); | |
82 armyRace.InnerText = army.Race.Name; | |
83 root.AppendChild(armyRace); | |
84 | |
85 XmlElement armyName = armyList.CreateElement("name"); | |
86 armyName.InnerText = army.Name; | |
87 root.AppendChild(armyName); | |
88 | |
89 XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable"); | |
90 armyAvailablePoints.InnerText = army.MaxPoints.ToString(); | |
91 root.AppendChild(armyAvailablePoints); | |
92 | |
93 XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed"); | |
94 armyUsedPoints.InnerText = army.Points.ToString(); | |
95 root.AppendChild(armyUsedPoints); | |
96 | |
97 // Get Categories and interate through each | |
98 foreach(ArmyCategory cat in army.Categories) | |
99 { | |
100 if (cat.GetUnits().Length == 0) | |
101 continue; | |
102 XmlElement armyCategory = armyList.CreateElement("category"); | |
103 armyCategory.SetAttribute("type", cat.Name); | |
104 | |
105 | |
106 // Get units and iterate through each | |
107 foreach(Unit uni in cat.GetUnits()) | |
108 { | |
109 XmlElement armyUnit = armyList.CreateElement("unit"); | |
110 armyUnit.SetAttribute("name", uni.UnitType.Name); | |
111 | |
112 foreach (Stat[] stat in uni.UnitStatsArraysWithName) | |
113 { | |
114 XmlElement armyStatLine = armyList.CreateElement("statLine"); | |
115 foreach (Stat singleStat in stat) | |
116 { | |
117 XmlElement armyStat = armyList.CreateElement("stat"); | |
118 armyStat.SetAttribute("name", singleStat.ParentSlotName); | |
119 armyStat.SetAttribute("value", singleStat.SlotValueString); | |
120 armyStatLine.AppendChild(armyStat); | |
121 } | |
122 armyUnit.AppendChild(armyStatLine); | |
123 } | |
124 armyUnit.SetAttribute("points", uni.Points.ToString()); | |
125 armyUnit.SetAttribute("models", uni.Size.ToString()); | |
126 | |
127 foreach (UnitEquipmentItem equip in uni.GetEquipment()) | |
128 { | |
129 XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); | |
404
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
130 armyEquipmentItem.SetAttribute("name", equip.Name); |
399 | 131 |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
132 int armyEquipAmount = 0; |
399 | 133 |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
134 if (UnitEquipmentUtil.GetEquipmentAmount(uni, equip) == null) |
399 | 135 { |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
136 armyEquipAmount = 0; |
399 | 137 } |
138 else | |
139 { | |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
140 armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip); |
399 | 141 } |
402
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
142 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
143 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip)) |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
144 { |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
145 float fraction = (float)(armyEquipAmount / 100.0); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
146 armyEquipAmount = (int)(fraction * uni.Size); |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
147 } |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
148 |
3a71f8af5bde
Correct count of unit items
Dan.Kulinski@dank-laptop.Global.Local
parents:
399
diff
changeset
|
149 armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString()); |
399 | 150 |
151 armyUnit.AppendChild(armyEquipmentItem); | |
152 } | |
153 | |
154 foreach (Ability abil in uni.Abilities) | |
155 { | |
156 XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); | |
157 | |
404
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
158 armyAbilityItem.SetAttribute("name", abil.Name); |
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
159 armyAbilityItem.SetAttribute("description", abil.Description); |
399 | 160 |
161 armyUnit.AppendChild(armyAbilityItem); | |
162 } | |
163 | |
164 armyCategory.AppendChild(armyUnit); | |
165 } | |
166 root.AppendChild(armyCategory); | |
167 } | |
168 | |
169 | |
170 | |
171 | |
172 // Append all Categories to the XML doc | |
173 | |
174 // Append tree to document | |
175 armyList.AppendChild(root); | |
176 | |
404
5d7584a73d7f
XSL with transform operations
Dan.Kulinski@dank-laptop.Global.Local
parents:
402
diff
changeset
|
177 return armyList; |
399 | 178 } |
179 private string GetEquipmentAmountRatioTranslation(double amount, int number) | |
180 { | |
181 return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number); | |
182 } | |
183 | |
184 private string GetEquipmentAmountNumberTranslation(int amount) | |
185 { | |
186 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount); | |
187 } | |
188 | |
189 private string GetEquipmentAmountAllTranslation(Unit unit) | |
190 { | |
191 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size); | |
192 } | |
193 } | |
194 } |