changeset 497:cd367acd7c48 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 diff
     1.1 --- a/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Sat Sep 01 15:28:26 2012 +0100
     1.2 +++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Wed Nov 28 20:24:36 2012 +0000
     1.3 @@ -1,188 +1,198 @@
     1.4 -// This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski
     1.5 -// 
     1.6 -// 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.
     1.7 -
     1.8 -using System;
     1.9 -using System.Collections.Generic;
    1.10 -using System.IO;
    1.11 -using System.Text;
    1.12 -using System.Xml;
    1.13 -using System.Xml.Xsl;
    1.14 -using System.Xml.XPath;
    1.15 -using System.Xml.Schema;
    1.16 -using IBBoard.Lang;
    1.17 -using IBBoard.Xml;
    1.18 -using IBBoard.WarFoundry.API.Objects;
    1.19 -using IBBoard.WarFoundry.API.Util;
    1.20 -
    1.21 -namespace IBBoard.WarFoundry.API.Exporters
    1.22 -{
    1.23 -	/// <summary>
    1.24 -	/// Custom exporter that exports an army as an XML file with an XSLT applied
    1.25 -	/// </summary>
    1.26 -    public class WarFoundryXmlWithXslExporter : IWarFoundryExporter
    1.27 -	{
    1.28 -        private static WarFoundryXmlWithXslExporter exporter;
    1.29 -
    1.30 -        // Return the default class associated with this exporter
    1.31 -        public static WarFoundryXmlWithXslExporter GetDefault()
    1.32 -        {
    1.33 -            if (exporter == null)
    1.34 -            {
    1.35 -                exporter = new WarFoundryXmlWithXslExporter();
    1.36 -            }
    1.37 -
    1.38 -            return exporter;
    1.39 -        }
    1.40 -
    1.41 -        private WarFoundryXmlWithXslExporter()
    1.42 -        {
    1.43 -            // Hide constructor
    1.44 -        }
    1.45 -
    1.46 -        // Write to file
    1.47 -        public void ExportArmy(Army army, string path)
    1.48 -        {
    1.49 -            XmlDocument xmlDoc = BuildXml(army);
    1.50 -            // Simple XML output settings
    1.51 -            XmlWriterSettings xmlSettings = new XmlWriterSettings();
    1.52 -            xmlSettings.Indent = true;
    1.53 -            xmlSettings.IndentChars = "  ";
    1.54 -
    1.55 -            // Write XML to file
    1.56 -            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
    1.57 -            {
    1.58 -                xmlDoc.Save(writer);
    1.59 -                writer.Flush();
    1.60 -                writer.Close();
    1.61 -            }
    1.62 -        }
    1.63 -
    1.64 -        // Write to file with transform
    1.65 -        public void ExportArmyWithTransform(Army army, string savePath, string xslPath)
    1.66 -        {
    1.67 -            XmlDocument xmlDoc = BuildXml(army);
    1.68 -            XslCompiledTransform xslTransform = new XslCompiledTransform();
    1.69 -
    1.70 -            xslTransform.Load(xslPath);
    1.71 -            XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings);
    1.72 -            xslTransform.Transform(xmlDoc, writer);
    1.73 -            writer.Flush();
    1.74 -            writer.Close();
    1.75 -        }
    1.76 -        
    1.77 -        // Build the XML document to save or transform
    1.78 -        private XmlDocument BuildXml(Army army)
    1.79 -        {
    1.80 -            XmlDocument armyList = new XmlDocument();
    1.81 -           
    1.82 -            // Everything will be a child of the army element
    1.83 -            XmlElement root = armyList.CreateElement("army");
    1.84 -
    1.85 -            // Basic army information
    1.86 -            XmlElement armyRace = armyList.CreateElement("race");
    1.87 -            armyRace.InnerText = army.Race.Name;
    1.88 -            root.AppendChild(armyRace);
    1.89 -
    1.90 -            XmlElement armyName = armyList.CreateElement("name");
    1.91 -            armyName.InnerText = army.Name;
    1.92 -            root.AppendChild(armyName);
    1.93 -
    1.94 -            XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable");
    1.95 -            armyAvailablePoints.InnerText = army.MaxPoints.ToString();
    1.96 -            root.AppendChild(armyAvailablePoints);
    1.97 -
    1.98 -            XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed");
    1.99 -            armyUsedPoints.InnerText = army.Points.ToString();
   1.100 -            root.AppendChild(armyUsedPoints);
   1.101 -
   1.102 -            // Get Categories and interate through each
   1.103 -            foreach(ArmyCategory cat in army.Categories)
   1.104 -            {
   1.105 -                if (cat.GetUnits().Length == 0)
   1.106 -                    continue;
   1.107 -                XmlElement armyCategory = armyList.CreateElement("category");
   1.108 -                armyCategory.SetAttribute("type", cat.Name);
   1.109 -                
   1.110 -
   1.111 -                // Get units and iterate through each
   1.112 -                foreach(Unit uni in cat.GetUnits())
   1.113 -                {
   1.114 -                    XmlElement armyUnit = armyList.CreateElement("unit");
   1.115 -                    armyUnit.SetAttribute("name", uni.UnitType.Name);
   1.116 -
   1.117 -                    foreach (Stat[] stat in uni.UnitStatsArraysWithName)
   1.118 -                    {
   1.119 -                        XmlElement armyStatLine = armyList.CreateElement("statLine");
   1.120 -                        foreach (Stat singleStat in stat)
   1.121 -                        {
   1.122 -                            XmlElement armyStat = armyList.CreateElement("stat");
   1.123 -                            armyStat.SetAttribute("name", singleStat.ParentSlotName);
   1.124 -                            armyStat.SetAttribute("value", singleStat.SlotValueString);
   1.125 -                            armyStatLine.AppendChild(armyStat);
   1.126 -                        }
   1.127 -                        armyUnit.AppendChild(armyStatLine);
   1.128 -                    }
   1.129 -                    armyUnit.SetAttribute("points", uni.Points.ToString());
   1.130 -                    armyUnit.SetAttribute("models", uni.Size.ToString());
   1.131 -
   1.132 -                    foreach (UnitEquipmentItem equip in uni.GetEquipment())
   1.133 -                    {
   1.134 -                        XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
   1.135 -                        armyEquipmentItem.SetAttribute("name", equip.Name);
   1.136 -
   1.137 -                        int armyEquipAmount = 0;
   1.138 -						armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
   1.139 -                        
   1.140 -                        if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
   1.141 -                        {
   1.142 -                            float fraction = (float)(armyEquipAmount / 100.0);
   1.143 -                            armyEquipAmount = (int)(fraction * uni.Size);
   1.144 -                        }
   1.145 -
   1.146 -                        armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString());
   1.147 -                       
   1.148 -                        armyUnit.AppendChild(armyEquipmentItem);
   1.149 -                    }
   1.150 -
   1.151 -                    foreach (Ability abil in uni.Abilities)
   1.152 -                    {
   1.153 -                        XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
   1.154 -
   1.155 -                        armyAbilityItem.SetAttribute("name", abil.Name);
   1.156 -                        armyAbilityItem.SetAttribute("description", abil.Description);
   1.157 -
   1.158 -                        armyUnit.AppendChild(armyAbilityItem);
   1.159 -                    }
   1.160 -
   1.161 -                    armyCategory.AppendChild(armyUnit);
   1.162 -                }
   1.163 -                root.AppendChild(armyCategory);
   1.164 -            }
   1.165 -
   1.166 -           
   1.167 -           
   1.168 -
   1.169 -            // Append all Categories to the XML doc
   1.170 -
   1.171 -            // Append tree to document
   1.172 -            armyList.AppendChild(root);
   1.173 -
   1.174 -            return armyList;
   1.175 -        }
   1.176 -        private string GetEquipmentAmountRatioTranslation(double amount, int number)
   1.177 -        {
   1.178 -            return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
   1.179 -        }
   1.180 -
   1.181 -        private string GetEquipmentAmountNumberTranslation(int amount)
   1.182 -        {
   1.183 -            return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
   1.184 -        }
   1.185 -
   1.186 -        private string GetEquipmentAmountAllTranslation(Unit unit)
   1.187 -        {
   1.188 -            return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
   1.189 -        }
   1.190 -    }
   1.191 -}
   1.192 +// This file (WarFoundryXmlWithXslExporter.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2011 Dan Kulinski
   1.193 +// 
   1.194 +// 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.
   1.195 +
   1.196 +using System;
   1.197 +using System.Collections.Generic;
   1.198 +using System.IO;
   1.199 +using System.Text;
   1.200 +using System.Xml;
   1.201 +using System.Xml.Xsl;
   1.202 +using System.Xml.XPath;
   1.203 +using System.Xml.Schema;
   1.204 +using IBBoard.Lang;
   1.205 +using IBBoard.Xml;
   1.206 +using IBBoard.WarFoundry.API.Objects;
   1.207 +using IBBoard.WarFoundry.API.Util;
   1.208 +using System.Reflection;
   1.209 +using IBBoard.IO;
   1.210 +
   1.211 +namespace IBBoard.WarFoundry.API.Exporters
   1.212 +{
   1.213 +	/// <summary>
   1.214 +	/// Custom exporter that exports an army as an XML file with an XSLT applied
   1.215 +	/// </summary>
   1.216 +    public class WarFoundryXmlWithXslExporter : IWarFoundryExporter
   1.217 +	{
   1.218 +        private static WarFoundryXmlWithXslExporter exporter;
   1.219 +
   1.220 +        // Return the default class associated with this exporter
   1.221 +        public static WarFoundryXmlWithXslExporter GetDefault()
   1.222 +        {
   1.223 +            if (exporter == null)
   1.224 +            {
   1.225 +                exporter = new WarFoundryXmlWithXslExporter();
   1.226 +            }
   1.227 +
   1.228 +            return exporter;
   1.229 +        }
   1.230 +
   1.231 +        private WarFoundryXmlWithXslExporter()
   1.232 +        {
   1.233 +            // Hide constructor
   1.234 +        }
   1.235 +
   1.236 +		public NamedStream[] GetXsltStreams() {
   1.237 +			Assembly assm = GetType().Assembly;
   1.238 +			NamedStream[] streams = new NamedStream[2];
   1.239 +			streams[0] = new NamedStream("Default Roster", assm.GetManifestResourceStream("IBBoard.WarFoundry.API.xsl.default_html.xsl"));
   1.240 +			streams[1] =  new NamedStream("Unit Cards", assm.GetManifestResourceStream("IBBoard.WarFoundry.API.xsl.unitcard.xsl"));
   1.241 +			return streams;
   1.242 +		}
   1.243 +
   1.244 +        // Write to file
   1.245 +        public void ExportArmy(Army army, string path)
   1.246 +        {
   1.247 +            XmlDocument xmlDoc = BuildXml(army);
   1.248 +            // Simple XML output settings
   1.249 +            XmlWriterSettings xmlSettings = new XmlWriterSettings();
   1.250 +            xmlSettings.Indent = true;
   1.251 +            xmlSettings.IndentChars = "  ";
   1.252 +
   1.253 +            // Write XML to file
   1.254 +            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
   1.255 +            {
   1.256 +                xmlDoc.Save(writer);
   1.257 +                writer.Flush();
   1.258 +                writer.Close();
   1.259 +            }
   1.260 +        }
   1.261 +
   1.262 +        // Write to file with transform
   1.263 +        public void ExportArmyWithTransform(Army army, string savePath, Stream xslStream)
   1.264 +        {
   1.265 +            XmlDocument xmlDoc = BuildXml(army);
   1.266 +            XslCompiledTransform xslTransform = new XslCompiledTransform();
   1.267 +
   1.268 +            xslTransform.Load(new XmlTextReader(xslStream));
   1.269 +            XmlWriter writer = XmlWriter.Create(savePath, xslTransform.OutputSettings);
   1.270 +            xslTransform.Transform(xmlDoc, writer);
   1.271 +            writer.Flush();
   1.272 +            writer.Close();
   1.273 +        }
   1.274 +        
   1.275 +        // Build the XML document to save or transform
   1.276 +        private XmlDocument BuildXml(Army army)
   1.277 +        {
   1.278 +            XmlDocument armyList = new XmlDocument();
   1.279 +           
   1.280 +            // Everything will be a child of the army element
   1.281 +            XmlElement root = armyList.CreateElement("army");
   1.282 +
   1.283 +            // Basic army information
   1.284 +            XmlElement armyRace = armyList.CreateElement("race");
   1.285 +            armyRace.InnerText = army.Race.Name;
   1.286 +            root.AppendChild(armyRace);
   1.287 +
   1.288 +            XmlElement armyName = armyList.CreateElement("name");
   1.289 +            armyName.InnerText = army.Name;
   1.290 +            root.AppendChild(armyName);
   1.291 +
   1.292 +            XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable");
   1.293 +            armyAvailablePoints.InnerText = army.MaxPoints.ToString();
   1.294 +            root.AppendChild(armyAvailablePoints);
   1.295 +
   1.296 +            XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed");
   1.297 +            armyUsedPoints.InnerText = army.Points.ToString();
   1.298 +            root.AppendChild(armyUsedPoints);
   1.299 +
   1.300 +            // Get Categories and interate through each
   1.301 +            foreach(ArmyCategory cat in army.Categories)
   1.302 +            {
   1.303 +                if (cat.GetUnits().Length == 0)
   1.304 +                    continue;
   1.305 +                XmlElement armyCategory = armyList.CreateElement("category");
   1.306 +                armyCategory.SetAttribute("type", cat.Name);
   1.307 +                
   1.308 +
   1.309 +                // Get units and iterate through each
   1.310 +                foreach(Unit uni in cat.GetUnits())
   1.311 +                {
   1.312 +                    XmlElement armyUnit = armyList.CreateElement("unit");
   1.313 +                    armyUnit.SetAttribute("name", uni.UnitType.Name);
   1.314 +
   1.315 +                    foreach (Stat[] stat in uni.UnitStatsArraysWithName)
   1.316 +                    {
   1.317 +                        XmlElement armyStatLine = armyList.CreateElement("statLine");
   1.318 +                        foreach (Stat singleStat in stat)
   1.319 +                        {
   1.320 +                            XmlElement armyStat = armyList.CreateElement("stat");
   1.321 +                            armyStat.SetAttribute("name", singleStat.ParentSlotName);
   1.322 +                            armyStat.SetAttribute("value", singleStat.SlotValueString);
   1.323 +                            armyStatLine.AppendChild(armyStat);
   1.324 +                        }
   1.325 +                        armyUnit.AppendChild(armyStatLine);
   1.326 +                    }
   1.327 +                    armyUnit.SetAttribute("points", uni.Points.ToString());
   1.328 +                    armyUnit.SetAttribute("models", uni.Size.ToString());
   1.329 +
   1.330 +                    foreach (UnitEquipmentItem equip in uni.GetEquipment())
   1.331 +                    {
   1.332 +                        XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
   1.333 +                        armyEquipmentItem.SetAttribute("name", equip.Name);
   1.334 +
   1.335 +                        int armyEquipAmount = 0;
   1.336 +						armyEquipAmount = (int)UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
   1.337 +                        
   1.338 +                        if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
   1.339 +                        {
   1.340 +                            float fraction = (float)(armyEquipAmount / 100.0);
   1.341 +                            armyEquipAmount = (int)(fraction * uni.Size);
   1.342 +                        }
   1.343 +
   1.344 +                        armyEquipmentItem.SetAttribute("count", armyEquipAmount.ToString());
   1.345 +                       
   1.346 +                        armyUnit.AppendChild(armyEquipmentItem);
   1.347 +                    }
   1.348 +
   1.349 +                    foreach (Ability abil in uni.Abilities)
   1.350 +                    {
   1.351 +                        XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
   1.352 +
   1.353 +                        armyAbilityItem.SetAttribute("name", abil.Name);
   1.354 +                        armyAbilityItem.SetAttribute("description", abil.Description);
   1.355 +
   1.356 +                        armyUnit.AppendChild(armyAbilityItem);
   1.357 +                    }
   1.358 +
   1.359 +                    armyCategory.AppendChild(armyUnit);
   1.360 +                }
   1.361 +                root.AppendChild(armyCategory);
   1.362 +            }
   1.363 +
   1.364 +           
   1.365 +           
   1.366 +
   1.367 +            // Append all Categories to the XML doc
   1.368 +
   1.369 +            // Append tree to document
   1.370 +            armyList.AppendChild(root);
   1.371 +
   1.372 +            return armyList;
   1.373 +        }
   1.374 +        private string GetEquipmentAmountRatioTranslation(double amount, int number)
   1.375 +        {
   1.376 +            return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
   1.377 +        }
   1.378 +
   1.379 +        private string GetEquipmentAmountNumberTranslation(int amount)
   1.380 +        {
   1.381 +            return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
   1.382 +        }
   1.383 +
   1.384 +        private string GetEquipmentAmountAllTranslation(Unit unit)
   1.385 +        {
   1.386 +            return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
   1.387 +        }
   1.388 +    }
   1.389 +}
     2.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Sep 01 15:28:26 2012 +0100
     2.2 +++ b/IBBoard.WarFoundry.API.csproj	Wed Nov 28 20:24:36 2012 +0000
     2.3 @@ -180,14 +180,6 @@
     2.4        <Name>ICSharpCode.SharpZLib</Name>
     2.5      </ProjectReference>
     2.6    </ItemGroup>
     2.7 -  <ItemGroup>
     2.8 -    <Content Include="xsl\default_html.xsl">
     2.9 -      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    2.10 -    </Content>
    2.11 -    <Content Include="xsl\unitcard.xsl">
    2.12 -      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    2.13 -    </Content>
    2.14 -  </ItemGroup>
    2.15    <ProjectExtensions>
    2.16      <MonoDevelop>
    2.17        <Properties>
    2.18 @@ -232,5 +224,13 @@
    2.19      </EmbeddedResource>
    2.20      <EmbeddedResource Include="data\Empire.race" />
    2.21      <EmbeddedResource Include="data\SampleSystem.system" />
    2.22 +    <EmbeddedResource Include="xsl\default_html.xsl">
    2.23 +      <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations>
    2.24 +      <LogicalName>IBBoard.WarFoundry.API.xsl.default_html.xsl</LogicalName>
    2.25 +    </EmbeddedResource>
    2.26 +    <EmbeddedResource Include="xsl\unitcard.xsl">
    2.27 +      <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations>
    2.28 +      <LogicalName>IBBoard.WarFoundry.API.xsl.unitcard.xsl</LogicalName>
    2.29 +    </EmbeddedResource>
    2.30    </ItemGroup>
    2.31  </Project>
    2.32 \ No newline at end of file