view API/Exporters/WarFoundryHtmlExporter.cs @ 481:81f32062c9fa

Re #419: Remove assumptions of a file-based install * Convert schemas to resources and load them from there * Remove use of "data" folder relative to exe location from default "Hacks" (GTK/WinForms GUIs can re-add it locally)
author IBBoard <dev@ibboard.co.uk>
date Mon, 25 Jun 2012 21:04:02 +0100
parents 6da9db4a9c23
children 55b39514cbf8
line source
1 // This file (WarFoundryHtmlExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
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.Schema;
11 using IBBoard.Lang;
12 using IBBoard.Xml;
13 using IBBoard.WarFoundry.API.Objects;
14 using IBBoard.WarFoundry.API.Util;
15 using System.Reflection;
17 namespace IBBoard.WarFoundry.API.Exporters
18 {
19 /// <summary>
20 /// Custom exporter that exports an army as a basic HTML file
21 /// </summary>
22 public class WarFoundryHtmlExporter : IWarFoundryExporter
23 {
24 private static WarFoundryHtmlExporter exporter;
25 private delegate string GetStatCellTextDelegate(Stat stat);
27 public static WarFoundryHtmlExporter GetDefault()
28 {
29 if (exporter == null)
30 {
31 exporter = new WarFoundryHtmlExporter();
32 }
34 return exporter;
35 }
37 private WarFoundryHtmlExporter()
38 {
39 //Hide constructor
40 }
42 public void ExportArmy(Army army, string path)
43 {
44 XmlDocument doc = new XmlDocument();
45 XmlResourceResolver resolver = new XmlResourceResolver(Assembly.GetExecutingAssembly());
46 resolver.AddMapping("-//W3C//ENTITIES Latin 1 for XHTML//EN", "IBBoard.API.schemas.xhtml1-lat1.ent");
47 resolver.AddMapping("http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent", "IBBoard.API.schemas.xhtml1-lat1.ent");
48 resolver.AddMapping("-//W3C//ENTITIES Symbols for XHTML//EN", "IBBoard.API.schemas.xhtml1-symbol.ent");
49 resolver.AddMapping("http://www.w3.org/TR/xhtml1/DTD/xhtml-symbol.ent", "IBBoard.API.schemas.xhtml1-symbol.ent");
50 resolver.AddMapping("-//W3C//ENTITIES Special for XHTML//EN", "IBBoard.API.schemas.xhtml1-special.ent");
51 resolver.AddMapping("http://www.w3.org/TR/xhtml1/DTD/xhtml-special.ent", "IBBoard.API.schemas.xhtml1-special.ent");
52 resolver.AddMapping("-//W3C//DTD XHTML 1.0 Strict//EN", "IBBoard.WarFoundry.schemas.xhtml1-strict.dtd");
53 resolver.AddMapping("http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", "IBBoard.WarFoundry.schemas.xhtml1-strict.dtd");
54 doc.XmlResolver = resolver;
55 doc.AppendChild(doc.CreateDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null));
56 XmlElement html = doc.CreateElement("html");
57 doc.AppendChild(html);
58 XmlElement head = doc.CreateElement("head");
59 html.AppendChild(head);
60 XmlElement title = doc.CreateElement("title");
61 title.InnerXml = army.Name;
62 head.AppendChild(title);
63 XmlElement metaCharset = doc.CreateElement("meta");
64 metaCharset.SetAttribute("http-equiv", "Content-Type");
65 metaCharset.SetAttribute("content", "text/html;charset=UTF-8");
66 head.AppendChild(metaCharset);
67 XmlElement style = doc.CreateElement("style");
68 style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0; border-collapse: collapse; margin: 0 }\n"
69 + "table table { width: 100%; border-width: 0; margin: -2px }\n"
70 + "table table td { border-width:0 1px }";
71 head.AppendChild(style);
72 XmlElement body = doc.CreateElement("body");
73 html.AppendChild(body);
74 XmlElement header = doc.CreateElement("h1");
75 header.InnerText = Translation.GetTranslation("armyHtmlOutputBodyHeader", "{0} - {1}pts", army.Name, army.Points);
76 body.AppendChild(header);
78 foreach (XmlElement table in CreateTables(army, doc))
79 {
80 if (!IsTableOnlyHeader(table))
81 {
82 body.AppendChild(table);
83 }
84 }
86 File.WriteAllText(path, doc.OuterXml);
87 }
89 private bool IsTableOnlyHeader(XmlElement table)
90 {
91 return table.ChildNodes.Count == 1;
92 }
94 private XmlElement[] CreateTables(Army army, XmlDocument doc)
95 {
96 Dictionary<string, XmlElement> tables = new Dictionary<string, XmlElement>();
98 foreach (SystemStats statSets in army.GameSystem.SystemStats)
99 {
100 tables[statSets.ID] = CreateTable(statSets, doc);
101 }
103 foreach (Unit unit in army.GetUnits())
104 {
105 CreateUnitRow(unit, tables[GetFirstStatType(unit)]);
106 }
108 return DictionaryUtils.ToArray(tables);
109 }
111 private static string GetFirstStatType(Unit unit)
112 {
113 string[] unitStatIDs = unit.UnitStatsArrayIDs;
114 return GetFirstStatType(unitStatIDs);
115 }
117 public static string GetFirstStatType(string[] unitStatIDs)
118 {
119 return unitStatIDs[0];
120 }
122 private XmlElement CreateTable(SystemStats stats, XmlDocument doc)
123 {
124 XmlElement table = doc.CreateElement("table");
125 XmlElement headerRow = doc.CreateElement("tr");
126 table.AppendChild(headerRow);
127 XmlElement name = doc.CreateElement("th");
128 name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name");
129 headerRow.AppendChild(name);
131 XmlElement unitTypeName = doc.CreateElement("th");
132 unitTypeName.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitTypeName", "type name");
133 headerRow.AppendChild(unitTypeName);
135 foreach (StatSlot stat in stats.StatSlots)
136 {
137 XmlElement statHeader = doc.CreateElement("th");
138 statHeader.InnerText = stat.Name;
139 headerRow.AppendChild(statHeader);
140 }
142 XmlElement notes = doc.CreateElement("th");
143 notes.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitNotes", "name");
144 ;
145 headerRow.AppendChild(notes);
147 XmlElement points = doc.CreateElement("th");
148 points.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitPoints", "name");
149 ;
150 headerRow.AppendChild(points);
152 return table;
153 }
155 private XmlElement CreateUnitRow(Unit unit, XmlElement tableElem)
156 {
157 XmlDocument doc = tableElem.OwnerDocument;
158 XmlElement row = doc.CreateElement("tr");
159 tableElem.AppendChild(row);
160 Stat[][] memberStats = unit.UnitStatsArraysWithName;
161 string[] statTypeIDs = unit.UnitStatsArrayIDs;
162 string defaultStatType = GetFirstStatType(statTypeIDs);
163 int statRowCount = 0;
164 bool hasOther = false;
166 foreach (string statTypeID in statTypeIDs)
167 {
168 if (statTypeID.Equals(defaultStatType))
169 {
170 statRowCount++;
171 } else if (!hasOther)
172 {
173 statRowCount++;
174 hasOther = true;
175 }
176 }
178 XmlElement name = doc.CreateElement("td");
179 name.InnerText = unit.Name;
180 SetRowSpan(name, statRowCount);
181 row.AppendChild(name);
182 CreateStatsBlock(row, memberStats, statTypeIDs);
184 StringBuilder sb = new StringBuilder();
185 UnitEquipmentItem[] unitEquipment = unit.GetEquipment();
187 if (unitEquipment.Length > 0)
188 {
189 bool addSeparator = false;
191 foreach (UnitEquipmentItem equip in unitEquipment)
192 {
193 if (!addSeparator)
194 {
195 addSeparator = true;
196 } else
197 {
198 sb.Append(", ");
199 }
201 string amountString;
202 double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip);
204 if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip))
205 {
207 if (amount == 100)
208 {
209 amountString = GetEquipmentAmountAllTranslation(unit);
210 } else
211 {
212 int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, equip);
213 amountString = GetEquipmentAmountRatioTranslation(amount, number);
214 }
215 } else
216 {
217 if (amount == -1)
218 {
219 amountString = GetEquipmentAmountAllTranslation(unit);
220 } else
221 {
222 amountString = GetEquipmentAmountNumberTranslation((int)amount);
223 }
224 }
226 sb.Append(Translation.GetTranslation("armyHtmlExportEquipAmountRatio", "{0} for {1}", equip.Name, amountString));
227 }
229 sb.Append(". ");
230 }
232 ICollection<Ability> abilities = unit.Abilities;
234 if (abilities.Count > 0)
235 {
236 bool addSeparator = false;
238 foreach (Ability ability in abilities)
239 {
240 if (!addSeparator)
241 {
242 addSeparator = true;
243 } else
244 {
245 sb.Append(", ");
246 }
248 sb.Append(ability.Name);
249 }
251 sb.Append(". ");
252 }
254 XmlElement notes = doc.CreateElement("td");
255 notes.InnerText = sb.ToString();
256 SetRowSpan(notes, statRowCount);
257 row.AppendChild(notes);
259 XmlElement points = doc.CreateElement("td");
260 points.InnerText = unit.Points.ToString();
261 SetRowSpan(points, statRowCount);
262 row.AppendChild(points);
264 return row;
265 }
267 private static void SetRowSpan(XmlElement xmlElement, int statRowCount)
268 {
269 if (statRowCount > 1)
270 {
271 xmlElement.SetAttribute("rowspan", statRowCount.ToString());
272 }
273 }
275 private void CreateStatsBlock(XmlElement unitRow, Stat[][] memberStats, string[] statTypeIDs)
276 {
277 XmlDocument doc = unitRow.OwnerDocument;
278 string defaultStatType = GetFirstStatType(statTypeIDs);
280 Stat[] defaultStatLine = memberStats[0];
281 int defaultStatLineCount = defaultStatLine.Length;
282 AddStatCell(defaultStatLine[0].SlotValueString, unitRow);
284 for (int i = 1; i < defaultStatLineCount; i++)
285 {
286 string statText = GetDefaultStatCellText(defaultStatLine[i]);
287 AddStatCell(statText, unitRow);
288 }
290 int statCount = statTypeIDs.Length;
292 if (statCount > 1)
293 {
294 XmlElement unitTable = (XmlElement)unitRow.ParentNode;
295 Dictionary<string, XmlElement> statParents = CreateStatsParentElements(statTypeIDs, unitTable);
297 for (int i = 1; i < statCount; i++)
298 {
299 Stat[] statLine = memberStats[i];
300 string statTypeID = statTypeIDs[i];
301 XmlElement tableElement = DictionaryUtils.GetValue(statParents, statTypeID);
302 int statLineCount = statLine.Length;
303 XmlElement statRow = doc.CreateElement("tr");
304 tableElement.AppendChild(statRow);
305 GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText));
306 AddStatCell(statLine[0].SlotValueString, statRow);
308 for (int j = 1; j < statLineCount; j++)
309 {
310 string statText = statCellTextDelegate(statLine[j]);
311 AddStatCell(statText, statRow);
312 }
313 }
315 if (statParents.Count > 1)
316 {
317 AddOtherUnitStatTables(statParents, unitTable, defaultStatLineCount);
318 }
319 }
320 }
322 private static void AddOtherUnitStatTables(Dictionary<string, XmlElement> statParents, XmlElement unitTable, int defaultStatLineCount)
323 {
324 XmlDocument doc = unitTable.OwnerDocument;
325 XmlElement otherStatsRow = doc.CreateElement("tr");
326 unitTable.AppendChild(otherStatsRow);
327 XmlElement otherStatsCell = doc.CreateElement("td");
328 otherStatsCell.SetAttribute("colspan", defaultStatLineCount.ToString());
329 otherStatsRow.AppendChild(otherStatsCell);
331 foreach (XmlElement tableElem in statParents.Values)
332 {
333 if (tableElem != unitTable)
334 {
335 otherStatsCell.AppendChild(tableElem);
336 }
337 }
338 }
340 private Dictionary<string, XmlElement> CreateStatsParentElements(string[] statTypeIDs, XmlElement parentTable)
341 {
342 Dictionary<string, XmlElement> statParents = new Dictionary<string, XmlElement>();
343 XmlDocument doc = parentTable.OwnerDocument;
344 string defaultStatTypeID = GetFirstStatType(statTypeIDs);
345 statParents[defaultStatTypeID] = parentTable;
347 foreach (string statTypeID in statTypeIDs)
348 {
349 if (!statParents.ContainsKey(statTypeID))
350 {
351 XmlElement tableElement = doc.CreateElement("table");
352 statParents[statTypeID] = tableElement;
353 }
354 }
356 return statParents;
357 }
359 private string GetDefaultStatCellText(Stat stat)
360 {
361 return Translation.GetTranslation("armyHtmlExportDefaultStatCellText", "{0}", stat.SlotValueString, stat.ParentSlotName);
362 }
364 private string GetOtherStatCellText(Stat stat)
365 {
366 return Translation.GetTranslation("armyHtmlExportOtherStatCellText", "{1}: {0}", stat.SlotValueString, stat.ParentSlotName);
367 }
369 private static void AddStatCell(string statValue, XmlElement row)
370 {
371 XmlElement statCell = row.OwnerDocument.CreateElement("td");
372 statCell.InnerText = statValue;
373 row.AppendChild(statCell);
374 }
376 private string GetEquipmentAmountRatioTranslation(double amount, int number)
377 {
378 return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
379 }
381 private string GetEquipmentAmountNumberTranslation(int amount)
382 {
383 return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
384 }
386 private string GetEquipmentAmountAllTranslation(Unit unit)
387 {
388 return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
389 }
390 }
391 }