Mercurial > repos > IBBoard.WarFoundry.API
view api/Exporters/WarFoundryHtmlExporter.cs @ 261:b9b8b0e60c31
Re #281: Export multiple stat lines in HTML
* Add initial version of multiple stat lines per unit - still needs some work
Also:
* Line ending cleanup
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 29 May 2010 20:03:17 +0000 |
parents | 22509bd03ca2 |
children | 900adf96a915 |
line wrap: on
line source
// This file (WarFoundryHtmlExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard // // 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. using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Xml; using System.Xml.Schema; using IBBoard.Lang; using IBBoard.Xml; using IBBoard.WarFoundry.API.Objects; using IBBoard.WarFoundry.API.Util; namespace IBBoard.WarFoundry.API.Exporters { /// <summary> /// Custom exporter that exports an army as a basic HTML file /// </summary> public class WarFoundryHtmlExporter : IWarFoundryExporter { private static WarFoundryHtmlExporter exporter; private delegate string GetStatCellTextDelegate(Stat stat); public static WarFoundryHtmlExporter GetDefault() { if (exporter == null) { exporter = new WarFoundryHtmlExporter(); } return exporter; } private WarFoundryHtmlExporter() { //Hide constructor } public void ExportArmy(Army army, string path) { XmlDocument doc = new XmlDocument(); CustomXmlResolver resolver = new CustomXmlResolver(); resolver.AddMapping("-//W3C//DTD XHTML 1.0 Strict//EN", new Uri("file://" + IBBoard.Constants.ExecutablePath + "/schemas/xhtml1-strict.dtd")); doc.XmlResolver = resolver; doc.AppendChild(doc.CreateDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", null)); XmlElement html = doc.CreateElement("html"); doc.AppendChild(html); XmlElement head = doc.CreateElement("head"); html.AppendChild(head); XmlElement title = doc.CreateElement("title"); title.InnerXml = army.Name; head.AppendChild(title); XmlElement metaCharset = doc.CreateElement("meta"); metaCharset.SetAttribute("http-equiv", "Content-Type"); metaCharset.SetAttribute("content", "text/html;charset=UTF-8"); head.AppendChild(metaCharset); XmlElement style = doc.CreateElement("style"); style.InnerText = "table, th, td { border: 1px solid #000; border-spacing: 0 }" + "table table { width: 100% }"; head.AppendChild(style); XmlElement body = doc.CreateElement("body"); html.AppendChild(body); XmlElement header = doc.CreateElement("h1"); header.InnerText = Translation.GetTranslation("armyHtmlOutputBodyHeader", "{0} - {1}pts", army.Name, army.Points); body.AppendChild(header); foreach (XmlElement table in CreateTables(army, doc)) { if (!IsTableOnlyHeader(table)) { body.AppendChild(table); } } StreamWriter writer = new StreamWriter(path, false); try { writer.Write(doc.OuterXml); } finally { writer.Close(); } } private bool IsTableOnlyHeader(XmlElement table) { return table.ChildNodes.Count == 1; } private XmlElement[] CreateTables(Army army, XmlDocument doc) { Dictionary<string, XmlElement> tables = new Dictionary<string, XmlElement>(); foreach (SystemStats statSets in army.GameSystem.SystemStats) { tables[statSets.ID] = CreateTable(statSets, doc); } foreach (Unit unit in army.GetUnits()) { tables[GetFirstStatType(unit)].AppendChild(CreateUnitRow(unit, doc)); } return DictionaryUtils.ToArray(tables); } private static string GetFirstStatType(Unit unit) { string[] unitStatIDs = unit.UnitStatsArrayIDs; return GetFirstStatType(unitStatIDs); } public static string GetFirstStatType(string[] unitStatIDs) { return unitStatIDs[0]; } private XmlElement CreateTable(SystemStats stats, XmlDocument doc) { XmlElement table = doc.CreateElement("table"); XmlElement headerRow = doc.CreateElement("tr"); table.AppendChild(headerRow); XmlElement name = doc.CreateElement("th"); name.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitName", "name"); headerRow.AppendChild(name); XmlElement unitTypeName = doc.CreateElement("th"); unitTypeName.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitTypeName", "type name"); headerRow.AppendChild(unitTypeName); foreach (StatSlot stat in stats.StatSlots) { XmlElement statHeader = doc.CreateElement("th"); statHeader.InnerText = stat.Name; headerRow.AppendChild(statHeader); } XmlElement notes = doc.CreateElement("th"); notes.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitNotes", "name");; headerRow.AppendChild(notes); XmlElement points = doc.CreateElement("th"); points.InnerText = Translation.GetTranslation("armyHtmlOutputTableHeaderUnitPoints", "name");; headerRow.AppendChild(points); return table; } private XmlElement CreateUnitRow(Unit unit, XmlDocument doc) { XmlElement row = doc.CreateElement("tr"); XmlElement name = doc.CreateElement("td"); name.InnerText = unit.Name; row.AppendChild(name); CreateStatsBlock(unit, row); StringBuilder sb = new StringBuilder(); UnitEquipmentItem[] unitEquipment = unit.GetEquipment (); if (unitEquipment.Length > 0) { bool addSeparator = false; foreach (UnitEquipmentItem equip in unitEquipment) { if (!addSeparator) { addSeparator = true; } else { sb.Append(", "); } string amountString; double amount = UnitEquipmentUtil.GetEquipmentAmount(unit, equip); if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(unit, equip)) { if (amount == 100) { amountString = GetEquipmentAmountAllTranslation(unit); } else { int number = UnitEquipmentUtil.GetEquipmentAmountTaken(unit, equip); amountString = GetEquipmentAmountRatioTranslation (amount, number); } } else { if (amount == -1) { amountString = GetEquipmentAmountAllTranslation(unit); } else { amountString = GetEquipmentAmountNumberTranslation((int)amount); } } sb.Append(Translation.GetTranslation("armyHtmlExportEquipAmountRatio", "{0} for {1}", equip.Name, amountString)); } sb.Append(". "); } ICollection<Ability> abilities = unit.Abilities; if (abilities.Count > 0) { bool addSeparator = false; foreach (Ability ability in abilities) { if (!addSeparator) { addSeparator = true; } else { sb.Append(", "); } sb.Append(ability.Name); } sb.Append(". "); } XmlElement notes = doc.CreateElement("td"); notes.InnerText = sb.ToString(); row.AppendChild(notes); XmlElement points = doc.CreateElement("td"); points.InnerText = unit.Points.ToString(); row.AppendChild(points); return row; } private void CreateStatsBlock(Unit unit, XmlElement unitRow) { XmlDocument doc = unitRow.OwnerDocument; XmlElement statsWrapperCell = doc.CreateElement("td"); unitRow.AppendChild(statsWrapperCell); Stat[][] memberStats = unit.UnitStatsArraysWithName; statsWrapperCell.SetAttribute("colspan", memberStats[0].Length.ToString()); string[] statTypeIDs = unit.UnitStatsArrayIDs; string defaultStatType = GetFirstStatType(statTypeIDs); Dictionary<string, XmlElement> statTables = CreateEmptyStatsTables(statTypeIDs, statsWrapperCell); int statCount = statTypeIDs.Length; for (int i = 0; i < statCount; i++) { Stat[] statLine = memberStats[i]; string statTypeID = statTypeIDs[i]; XmlElement tableElement = DictionaryUtils.GetValue(statTables, statTypeID); int statLineCount = statLine.Length; XmlElement statRow = doc.CreateElement("tr"); tableElement.AppendChild(statRow); GetStatCellTextDelegate statCellTextDelegate = (statTypeID.Equals(defaultStatType) ? new GetStatCellTextDelegate(GetDefaultStatCellText) : new GetStatCellTextDelegate(GetOtherStatCellText)); AddStatCell(statLine[0].SlotValueString, statRow); for (int j = 1; j < statLineCount; j++) { string statText = statCellTextDelegate(statLine[j]); AddStatCell(statText, statRow); } } } private Dictionary<string, XmlElement> CreateEmptyStatsTables(string[] statTypeIDs, XmlElement containerCell) { Dictionary<string, XmlElement> statTables = new Dictionary<string, XmlElement>(); XmlDocument doc = containerCell.OwnerDocument; foreach (string statTypeID in statTypeIDs) { if (!statTables.ContainsKey(statTypeID)) { XmlElement tableElement = doc.CreateElement("table"); containerCell.AppendChild(tableElement); statTables[statTypeID] = tableElement; } } return statTables; } private string GetDefaultStatCellText(Stat stat) { return Translation.GetTranslation("armyHtmlExportDefaultStatCellText", "{0}", stat.SlotValueString, stat.ParentSlotName); } private string GetOtherStatCellText(Stat stat) { return Translation.GetTranslation("armyHtmlExportOtherStatCellText", "{1}: {0}", stat.SlotValueString, stat.ParentSlotName); } private static void AddStatCell(string statValue, XmlElement row) { XmlElement statCell = row.OwnerDocument.CreateElement("td"); statCell.InnerText = statValue; row.AppendChild(statCell); } private string GetEquipmentAmountRatioTranslation (double amount, int number) { return Translation.GetTranslation ("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number); } private string GetEquipmentAmountNumberTranslation(int amount) { return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount); } private string GetEquipmentAmountAllTranslation(Unit unit) { return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size); } } }