view API/Exporters/WarFoundryXMLWithXSLExporter.cs @ 401:0cfa43fd9020

Basic exporter completed
author Dan.Kulinski@dank-laptop.Global.Local
date Sun, 14 Aug 2011 14:40:16 -0600
parents
children 3a71f8af5bde
line source
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.
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;
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;
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 }
35 return exporter;
36 }
38 private WarFoundryXmlWithXslExporter()
39 {
40 // Hide constructor
41 }
43 public void ExportArmy(Army army, string path)
44 {
45 XmlDocument armyList = new XmlDocument();
47 // Everything will be a child of the army element
48 XmlElement root = armyList.CreateElement("army");
50 // Basic army information
51 XmlElement armyRace = armyList.CreateElement("race");
52 armyRace.InnerText = army.Race.Name;
53 root.AppendChild(armyRace);
55 XmlElement armyName = armyList.CreateElement("name");
56 armyName.InnerText = army.Name;
57 root.AppendChild(armyName);
59 XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable");
60 armyAvailablePoints.InnerText = army.MaxPoints.ToString();
61 root.AppendChild(armyAvailablePoints);
63 XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed");
64 armyUsedPoints.InnerText = army.Points.ToString();
65 root.AppendChild(armyUsedPoints);
67 // Get Categories and interate through each
68 foreach(ArmyCategory cat in army.Categories)
69 {
70 if (cat.GetUnits().Length == 0)
71 continue;
72 XmlElement armyCategory = armyList.CreateElement("category");
73 armyCategory.SetAttribute("type", cat.Name);
76 // Get units and iterate through each
77 foreach(Unit uni in cat.GetUnits())
78 {
79 XmlElement armyUnit = armyList.CreateElement("unit");
80 armyUnit.SetAttribute("name", uni.UnitType.Name);
82 foreach (Stat[] stat in uni.UnitStatsArraysWithName)
83 {
84 XmlElement armyStatLine = armyList.CreateElement("statLine");
85 foreach (Stat singleStat in stat)
86 {
87 XmlElement armyStat = armyList.CreateElement("stat");
88 armyStat.SetAttribute("name", singleStat.ParentSlotName);
89 armyStat.SetAttribute("value", singleStat.SlotValueString);
90 armyStatLine.AppendChild(armyStat);
91 }
92 armyUnit.AppendChild(armyStatLine);
93 }
94 armyUnit.SetAttribute("points", uni.Points.ToString());
95 armyUnit.SetAttribute("models", uni.Size.ToString());
97 foreach (UnitEquipmentItem equip in uni.GetEquipment())
98 {
99 XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
100 armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString());
102 // Item Name
103 XmlElement armyEquipmentName = armyList.CreateElement("name");
104 armyEquipmentName.InnerText = equip.Name;
105 armyEquipmentItem.AppendChild(armyEquipmentName);
107 // Item description
108 XmlElement armyEquipmentDesc = armyList.CreateElement("desc");
109 double amount = UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
110 string amountString = "";
113 // TODO Change to report number, no string needed, add as attribute to armyEquipmentItem
114 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
115 {
116 if (amount == 100)
117 {
118 amountString = GetEquipmentAmountAllTranslation(uni);
119 }
120 else
121 {
122 int number = UnitEquipmentUtil.GetEquipmentAmountTaken(uni, equip);
123 amountString = GetEquipmentAmountRatioTranslation(amount, number);
124 }
125 }
126 else
127 {
128 if (amount == -1)
129 {
130 amountString = GetEquipmentAmountAllTranslation(uni);
131 }
132 else
133 {
134 amountString = GetEquipmentAmountNumberTranslation((int)amount);
135 }
136 }
137 armyEquipmentDesc.InnerText = Translation.GetTranslation("armyHtmlExportEquipAmountRatio","{0} for {1}", equip.Name, amountString);
138 armyEquipmentItem.SetAttribute("count", amountString);
140 armyEquipmentItem.AppendChild(armyEquipmentDesc);
141 armyUnit.AppendChild(armyEquipmentItem);
142 }
144 foreach (Ability abil in uni.Abilities)
145 {
146 XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
148 XmlElement armyAbilityName = armyList.CreateElement("name");
149 armyAbilityName.InnerText = abil.Name;
150 armyAbilityItem.AppendChild(armyAbilityName);
152 XmlElement armyAbilityDesc = armyList.CreateElement("description");
153 armyAbilityDesc.InnerText = abil.Description;
154 armyAbilityItem.AppendChild(armyAbilityDesc);
156 armyUnit.AppendChild(armyAbilityItem);
157 }
159 armyCategory.AppendChild(armyUnit);
160 }
161 root.AppendChild(armyCategory);
162 }
167 // Append all Categories to the XML doc
169 // Append tree to document
170 armyList.AppendChild(root);
172 // Simple XML output settings
173 XmlWriterSettings xmlSettings = new XmlWriterSettings();
174 xmlSettings.Indent = true;
175 xmlSettings.IndentChars = " ";
177 // Write XML to file
178 using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
179 {
180 armyList.Save(writer);
181 writer.Flush();
182 }
183 }
184 private string GetEquipmentAmountRatioTranslation(double amount, int number)
185 {
186 return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
187 }
189 private string GetEquipmentAmountNumberTranslation(int amount)
190 {
191 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
192 }
194 private string GetEquipmentAmountAllTranslation(Unit unit)
195 {
196 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
197 }
198 }
199 }