Mercurial > repos > IBBoard.WarFoundry.API
changeset 497:cd367acd7c48 default tip
Re #419: Remove assumptions of a file-based install
* Swap XSLTs to resources
* Update XSL Exporter to use Streams, not files
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 28 Nov 2012 20:24:36 +0000 |
parents | 00d6cf940c3c |
children | |
files | API/Exporters/WarFoundryXMLWithXSLExporter.cs IBBoard.WarFoundry.API.csproj |
diffstat | 2 files changed, 206 insertions(+), 196 deletions(-) [+] |
line wrap: on
line diff
--- a/API/Exporters/WarFoundryXMLWithXSLExporter.cs Sat Sep 01 15:28:26 2012 +0100 +++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs Wed Nov 28 20:24:36 2012 +0000 @@ -1,188 +1,198 @@ -// This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski -// -// 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.Xsl; -using System.Xml.XPath; -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 an XML file with an XSLT applied - /// </summary> - public class WarFoundryXmlWithXslExporter : IWarFoundryExporter - { - private static WarFoundryXmlWithXslExporter exporter; - - // Return the default class associated with this exporter - public static WarFoundryXmlWithXslExporter GetDefault() - { - if (exporter == null) - { - exporter = new WarFoundryXmlWithXslExporter(); - } - - return exporter; - } - - private WarFoundryXmlWithXslExporter() - { - // Hide constructor - } - - // Write to file - public void ExportArmy(Army army, string path) - { - XmlDocument xmlDoc = BuildXml(army); - // Simple XML output settings - XmlWriterSettings xmlSettings = new XmlWriterSettings(); - xmlSettings.Indent = true; - xmlSettings.IndentChars = " "; - - // Write XML to file - using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) - { - xmlDoc.Save(writer); - writer.Flush(); - writer.Close(); - } - } - - // Write to file with transform - public void ExportArmyWithTransform(Army army, string savePath, string xslPath) - { - XmlDocument xmlDoc = BuildXml(army); - XslCompiledTransform xslTransform = new XslCompiledTransform(); - - xslTransform.Load(xslPath); - XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings); - xslTransform.Transform(xmlDoc, writer); - writer.Flush(); - writer.Close(); - } - - // Build the XML document to save or transform - private XmlDocument BuildXml(Army army) - { - XmlDocument armyList = new XmlDocument(); - - // Everything will be a child of the army element - XmlElement root = armyList.CreateElement("army"); - - // Basic army information - XmlElement armyRace = armyList.CreateElement("race"); - armyRace.InnerText = army.Race.Name; - root.AppendChild(armyRace); - - XmlElement armyName = armyList.CreateElement("name"); - armyName.InnerText = army.Name; - root.AppendChild(armyName); - - XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable"); - armyAvailablePoints.InnerText = army.MaxPoints.ToString(); - root.AppendChild(armyAvailablePoints); - - XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed"); - armyUsedPoints.InnerText = army.Points.ToString(); - root.AppendChild(armyUsedPoints); - - // Get Categories and interate through each - foreach(ArmyCategory cat in army.Categories) - { - if (cat.GetUnits().Length == 0) - continue; - XmlElement armyCategory = armyList.CreateElement("category"); - armyCategory.SetAttribute("type", cat.Name); - - - // Get units and iterate through each - foreach(Unit uni in cat.GetUnits()) - { - XmlElement armyUnit = armyList.CreateElement("unit"); - armyUnit.SetAttribute("name", uni.UnitType.Name); - - foreach (Stat[] stat in uni.UnitStatsArraysWithName) - { - XmlElement armyStatLine = armyList.CreateElement("statLine"); - foreach (Stat singleStat in stat) - { - XmlElement armyStat = armyList.CreateElement("stat"); - armyStat.SetAttribute("name", singleStat.ParentSlotName); - armyStat.SetAttribute("value", singleStat.SlotValueString); - armyStatLine.AppendChild(armyStat); - } - armyUnit.AppendChild(armyStatLine); - } - armyUnit.SetAttribute("points", uni.Points.ToString()); - armyUnit.SetAttribute("models", uni.Size.ToString()); - - foreach (UnitEquipmentItem equip in uni.GetEquipment()) - { - XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); - armyEquipmentItem.SetAttribute("name", equip.Name); - - int armyEquipAmount = 0; - armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip); - - if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip)) - { - float fraction = (float)(armyEquipAmount / 100.0); - armyEquipAmount = (int)(fraction * uni.Size); - } - - armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString()); - - armyUnit.AppendChild(armyEquipmentItem); - } - - foreach (Ability abil in uni.Abilities) - { - XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); - - armyAbilityItem.SetAttribute("name", abil.Name); - armyAbilityItem.SetAttribute("description", abil.Description); - - armyUnit.AppendChild(armyAbilityItem); - } - - armyCategory.AppendChild(armyUnit); - } - root.AppendChild(armyCategory); - } - - - - - // Append all Categories to the XML doc - - // Append tree to document - armyList.AppendChild(root); - - return armyList; - } - 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); - } - } -} +// This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski +// +// 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.Xsl; +using System.Xml.XPath; +using System.Xml.Schema; +using IBBoard.Lang; +using IBBoard.Xml; +using IBBoard.WarFoundry.API.Objects; +using IBBoard.WarFoundry.API.Util; +using System.Reflection; +using IBBoard.IO; + +namespace IBBoard.WarFoundry.API.Exporters +{ + /// <summary> + /// Custom exporter that exports an army as an XML file with an XSLT applied + /// </summary> + public class WarFoundryXmlWithXslExporter : IWarFoundryExporter + { + private static WarFoundryXmlWithXslExporter exporter; + + // Return the default class associated with this exporter + public static WarFoundryXmlWithXslExporter GetDefault() + { + if (exporter == null) + { + exporter = new WarFoundryXmlWithXslExporter(); + } + + return exporter; + } + + private WarFoundryXmlWithXslExporter() + { + // Hide constructor + } + + public NamedStream[] GetXsltStreams() { + Assembly assm = GetType().Assembly; + NamedStream[] streams = new NamedStream[2]; + streams[0] = new NamedStream("Default Roster", assm.GetManifestResourceStream("IBBoard.WarFoundry.API.xsl.default_html.xsl")); + streams[1] = new NamedStream("Unit Cards", assm.GetManifestResourceStream("IBBoard.WarFoundry.API.xsl.unitcard.xsl")); + return streams; + } + + // Write to file + public void ExportArmy(Army army, string path) + { + XmlDocument xmlDoc = BuildXml(army); + // Simple XML output settings + XmlWriterSettings xmlSettings = new XmlWriterSettings(); + xmlSettings.Indent = true; + xmlSettings.IndentChars = " "; + + // Write XML to file + using (XmlWriter writer = XmlWriter.Create(path, xmlSettings)) + { + xmlDoc.Save(writer); + writer.Flush(); + writer.Close(); + } + } + + // Write to file with transform + public void ExportArmyWithTransform(Army army, string savePath, Stream xslStream) + { + XmlDocument xmlDoc = BuildXml(army); + XslCompiledTransform xslTransform = new XslCompiledTransform(); + + xslTransform.Load(new XmlTextReader(xslStream)); + XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings); + xslTransform.Transform(xmlDoc, writer); + writer.Flush(); + writer.Close(); + } + + // Build the XML document to save or transform + private XmlDocument BuildXml(Army army) + { + XmlDocument armyList = new XmlDocument(); + + // Everything will be a child of the army element + XmlElement root = armyList.CreateElement("army"); + + // Basic army information + XmlElement armyRace = armyList.CreateElement("race"); + armyRace.InnerText = army.Race.Name; + root.AppendChild(armyRace); + + XmlElement armyName = armyList.CreateElement("name"); + armyName.InnerText = army.Name; + root.AppendChild(armyName); + + XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable"); + armyAvailablePoints.InnerText = army.MaxPoints.ToString(); + root.AppendChild(armyAvailablePoints); + + XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed"); + armyUsedPoints.InnerText = army.Points.ToString(); + root.AppendChild(armyUsedPoints); + + // Get Categories and interate through each + foreach(ArmyCategory cat in army.Categories) + { + if (cat.GetUnits().Length == 0) + continue; + XmlElement armyCategory = armyList.CreateElement("category"); + armyCategory.SetAttribute("type", cat.Name); + + + // Get units and iterate through each + foreach(Unit uni in cat.GetUnits()) + { + XmlElement armyUnit = armyList.CreateElement("unit"); + armyUnit.SetAttribute("name", uni.UnitType.Name); + + foreach (Stat[] stat in uni.UnitStatsArraysWithName) + { + XmlElement armyStatLine = armyList.CreateElement("statLine"); + foreach (Stat singleStat in stat) + { + XmlElement armyStat = armyList.CreateElement("stat"); + armyStat.SetAttribute("name", singleStat.ParentSlotName); + armyStat.SetAttribute("value", singleStat.SlotValueString); + armyStatLine.AppendChild(armyStat); + } + armyUnit.AppendChild(armyStatLine); + } + armyUnit.SetAttribute("points", uni.Points.ToString()); + armyUnit.SetAttribute("models", uni.Size.ToString()); + + foreach (UnitEquipmentItem equip in uni.GetEquipment()) + { + XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem"); + armyEquipmentItem.SetAttribute("name", equip.Name); + + int armyEquipAmount = 0; + armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip); + + if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip)) + { + float fraction = (float)(armyEquipAmount / 100.0); + armyEquipAmount = (int)(fraction * uni.Size); + } + + armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString()); + + armyUnit.AppendChild(armyEquipmentItem); + } + + foreach (Ability abil in uni.Abilities) + { + XmlElement armyAbilityItem = armyList.CreateElement("abilityItem"); + + armyAbilityItem.SetAttribute("name", abil.Name); + armyAbilityItem.SetAttribute("description", abil.Description); + + armyUnit.AppendChild(armyAbilityItem); + } + + armyCategory.AppendChild(armyUnit); + } + root.AppendChild(armyCategory); + } + + + + + // Append all Categories to the XML doc + + // Append tree to document + armyList.AppendChild(root); + + return armyList; + } + 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); + } + } +}
--- a/IBBoard.WarFoundry.API.csproj Sat Sep 01 15:28:26 2012 +0100 +++ b/IBBoard.WarFoundry.API.csproj Wed Nov 28 20:24:36 2012 +0000 @@ -180,14 +180,6 @@ <Name>ICSharpCode.SharpZLib</Name> </ProjectReference> </ItemGroup> - <ItemGroup> - <Content Include="xsl\default_html.xsl"> - <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> - </Content> - <Content Include="xsl\unitcard.xsl"> - <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> - </Content> - </ItemGroup> <ProjectExtensions> <MonoDevelop> <Properties> @@ -232,5 +224,13 @@ </EmbeddedResource> <EmbeddedResource Include="data\Empire.race" /> <EmbeddedResource Include="data\SampleSystem.system" /> + <EmbeddedResource Include="xsl\default_html.xsl"> + <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations> + <LogicalName>IBBoard.WarFoundry.API.xsl.default_html.xsl</LogicalName> + </EmbeddedResource> + <EmbeddedResource Include="xsl\unitcard.xsl"> + <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations> + <LogicalName>IBBoard.WarFoundry.API.xsl.unitcard.xsl</LogicalName> + </EmbeddedResource> </ItemGroup> </Project> \ No newline at end of file