changeset 399:9a49d34599fb xsl-output

Basic XSL output
author Dan.Kulinski@dank-laptop.Global.Local
date Sun, 14 Aug 2011 14:33:15 -0600
parents cbe69734f48f
children d7e6012d8302
files API/Exporters/WarFoundryXMLWithXSLExporter.cs
diffstat 1 files changed, 199 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Sun Aug 14 14:33:15 2011 -0600
@@ -0,0 +1,199 @@
+// 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
+        }
+
+        public void ExportArmy(Army army, string path)
+        {
+            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("count", equip.CostMultiplier.ToString());
+                        
+                        // Item Name
+                        XmlElement armyEquipmentName = armyList.CreateElement("name");
+                        armyEquipmentName.InnerText = equip.Name;
+                        armyEquipmentItem.AppendChild(armyEquipmentName);
+
+                        // Item description
+                        XmlElement armyEquipmentDesc = armyList.CreateElement("desc");
+                        double amount = UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
+                        string amountString = "";
+
+                        
+                        // TODO Change to report number, no string needed, add as attribute to armyEquipmentItem
+                        if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
+                        {
+                            if (amount == 100)
+                            {
+                                amountString = GetEquipmentAmountAllTranslation(uni);
+                            }
+                            else
+                            {
+                                int number = UnitEquipmentUtil.GetEquipmentAmountTaken(uni, equip);
+                                amountString = GetEquipmentAmountRatioTranslation(amount, number);
+                            }
+                        }
+                        else
+                        {
+                            if (amount == -1)
+                            {
+                                amountString = GetEquipmentAmountAllTranslation(uni);
+                            }
+                            else
+                            {
+                                amountString = GetEquipmentAmountNumberTranslation((int)amount);
+                            }
+                        }
+                        armyEquipmentDesc.InnerText = Translation.GetTranslation("armyHtmlExportEquipAmountRatio","{0} for {1}", equip.Name, amountString);
+                        armyEquipmentItem.SetAttribute("count", amountString);
+                       
+                        armyEquipmentItem.AppendChild(armyEquipmentDesc);
+                        armyUnit.AppendChild(armyEquipmentItem);
+                    }
+
+                    foreach (Ability abil in uni.Abilities)
+                    {
+                        XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
+                        
+                        XmlElement armyAbilityName = armyList.CreateElement("name");
+                        armyAbilityName.InnerText = abil.Name;
+                        armyAbilityItem.AppendChild(armyAbilityName);
+
+                        XmlElement armyAbilityDesc = armyList.CreateElement("description");
+                        armyAbilityDesc.InnerText = abil.Description;
+                        armyAbilityItem.AppendChild(armyAbilityDesc);
+
+                        armyUnit.AppendChild(armyAbilityItem);
+                    }
+
+                    armyCategory.AppendChild(armyUnit);
+                }
+                root.AppendChild(armyCategory);
+            }
+
+           
+           
+
+            // Append all Categories to the XML doc
+
+            // Append tree to document
+            armyList.AppendChild(root);
+
+            // Simple XML output settings
+            XmlWriterSettings xmlSettings = new XmlWriterSettings();
+            xmlSettings.Indent = true;
+            xmlSettings.IndentChars = "   ";
+
+            // Write XML to file
+            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
+            {
+                armyList.Save(writer);
+                writer.Flush();
+            }
+        }
+        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);
+        }
+    }
+}