changeset 401:0cfa43fd9020 xml-basic

Basic exporter completed
author Dan.Kulinski@dank-laptop.Global.Local
date Sun, 14 Aug 2011 14:40:16 -0600
parents d7e6012d8302
children 3a71f8af5bde
files API/Exporters/WarFoundryXMLWithXSLExporter.cs
diffstat 1 files changed, 199 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/API/Exporters/WarFoundryXMLWithXSLExporter.cs	Sun Aug 14 14:40:16 2011 -0600
     1.3 @@ -0,0 +1,199 @@
     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 +        public void ExportArmy(Army army, string path)
    1.47 +        {
    1.48 +            XmlDocument armyList = new XmlDocument();
    1.49 +           
    1.50 +            // Everything will be a child of the army element
    1.51 +            XmlElement root = armyList.CreateElement("army");
    1.52 +
    1.53 +            // Basic army information
    1.54 +            XmlElement armyRace = armyList.CreateElement("race");
    1.55 +            armyRace.InnerText = army.Race.Name;
    1.56 +            root.AppendChild(armyRace);
    1.57 +
    1.58 +            XmlElement armyName = armyList.CreateElement("name");
    1.59 +            armyName.InnerText = army.Name;
    1.60 +            root.AppendChild(armyName);
    1.61 +
    1.62 +            XmlElement armyAvailablePoints = armyList.CreateElement("pointsAvailable");
    1.63 +            armyAvailablePoints.InnerText = army.MaxPoints.ToString();
    1.64 +            root.AppendChild(armyAvailablePoints);
    1.65 +
    1.66 +            XmlElement armyUsedPoints = armyList.CreateElement("pointsUsed");
    1.67 +            armyUsedPoints.InnerText = army.Points.ToString();
    1.68 +            root.AppendChild(armyUsedPoints);
    1.69 +
    1.70 +            // Get Categories and interate through each
    1.71 +            foreach(ArmyCategory cat in army.Categories)
    1.72 +            {
    1.73 +                if (cat.GetUnits().Length == 0)
    1.74 +                    continue;
    1.75 +                XmlElement armyCategory = armyList.CreateElement("category");
    1.76 +                armyCategory.SetAttribute("type", cat.Name);
    1.77 +                
    1.78 +
    1.79 +                // Get units and iterate through each
    1.80 +                foreach(Unit uni in cat.GetUnits())
    1.81 +                {
    1.82 +                    XmlElement armyUnit = armyList.CreateElement("unit");
    1.83 +                    armyUnit.SetAttribute("name", uni.UnitType.Name);
    1.84 +
    1.85 +                    foreach (Stat[] stat in uni.UnitStatsArraysWithName)
    1.86 +                    {
    1.87 +                        XmlElement armyStatLine = armyList.CreateElement("statLine");
    1.88 +                        foreach (Stat singleStat in stat)
    1.89 +                        {
    1.90 +                            XmlElement armyStat = armyList.CreateElement("stat");
    1.91 +                            armyStat.SetAttribute("name", singleStat.ParentSlotName);
    1.92 +                            armyStat.SetAttribute("value", singleStat.SlotValueString);
    1.93 +                            armyStatLine.AppendChild(armyStat);
    1.94 +                        }
    1.95 +                        armyUnit.AppendChild(armyStatLine);
    1.96 +                    }
    1.97 +                    armyUnit.SetAttribute("points", uni.Points.ToString());
    1.98 +                    armyUnit.SetAttribute("models", uni.Size.ToString());
    1.99 +
   1.100 +                    foreach (UnitEquipmentItem equip in uni.GetEquipment())
   1.101 +                    {
   1.102 +                        XmlElement armyEquipmentItem = armyList.CreateElement("equipmentItem");
   1.103 +                        armyEquipmentItem.SetAttribute("count", equip.CostMultiplier.ToString());
   1.104 +                        
   1.105 +                        // Item Name
   1.106 +                        XmlElement armyEquipmentName = armyList.CreateElement("name");
   1.107 +                        armyEquipmentName.InnerText = equip.Name;
   1.108 +                        armyEquipmentItem.AppendChild(armyEquipmentName);
   1.109 +
   1.110 +                        // Item description
   1.111 +                        XmlElement armyEquipmentDesc = armyList.CreateElement("desc");
   1.112 +                        double amount = UnitEquipmentUtil.GetEquipmentAmount(uni, equip);
   1.113 +                        string amountString = "";
   1.114 +
   1.115 +                        
   1.116 +                        // TODO Change to report number, no string needed, add as attribute to armyEquipmentItem
   1.117 +                        if (UnitEquipmentUtil.GetEquipmentAmountIsRatio(uni, equip))
   1.118 +                        {
   1.119 +                            if (amount == 100)
   1.120 +                            {
   1.121 +                                amountString = GetEquipmentAmountAllTranslation(uni);
   1.122 +                            }
   1.123 +                            else
   1.124 +                            {
   1.125 +                                int number = UnitEquipmentUtil.GetEquipmentAmountTaken(uni, equip);
   1.126 +                                amountString = GetEquipmentAmountRatioTranslation(amount, number);
   1.127 +                            }
   1.128 +                        }
   1.129 +                        else
   1.130 +                        {
   1.131 +                            if (amount == -1)
   1.132 +                            {
   1.133 +                                amountString = GetEquipmentAmountAllTranslation(uni);
   1.134 +                            }
   1.135 +                            else
   1.136 +                            {
   1.137 +                                amountString = GetEquipmentAmountNumberTranslation((int)amount);
   1.138 +                            }
   1.139 +                        }
   1.140 +                        armyEquipmentDesc.InnerText = Translation.GetTranslation("armyHtmlExportEquipAmountRatio","{0} for {1}", equip.Name, amountString);
   1.141 +                        armyEquipmentItem.SetAttribute("count", amountString);
   1.142 +                       
   1.143 +                        armyEquipmentItem.AppendChild(armyEquipmentDesc);
   1.144 +                        armyUnit.AppendChild(armyEquipmentItem);
   1.145 +                    }
   1.146 +
   1.147 +                    foreach (Ability abil in uni.Abilities)
   1.148 +                    {
   1.149 +                        XmlElement armyAbilityItem = armyList.CreateElement("abilityItem");
   1.150 +                        
   1.151 +                        XmlElement armyAbilityName = armyList.CreateElement("name");
   1.152 +                        armyAbilityName.InnerText = abil.Name;
   1.153 +                        armyAbilityItem.AppendChild(armyAbilityName);
   1.154 +
   1.155 +                        XmlElement armyAbilityDesc = armyList.CreateElement("description");
   1.156 +                        armyAbilityDesc.InnerText = abil.Description;
   1.157 +                        armyAbilityItem.AppendChild(armyAbilityDesc);
   1.158 +
   1.159 +                        armyUnit.AppendChild(armyAbilityItem);
   1.160 +                    }
   1.161 +
   1.162 +                    armyCategory.AppendChild(armyUnit);
   1.163 +                }
   1.164 +                root.AppendChild(armyCategory);
   1.165 +            }
   1.166 +
   1.167 +           
   1.168 +           
   1.169 +
   1.170 +            // Append all Categories to the XML doc
   1.171 +
   1.172 +            // Append tree to document
   1.173 +            armyList.AppendChild(root);
   1.174 +
   1.175 +            // Simple XML output settings
   1.176 +            XmlWriterSettings xmlSettings = new XmlWriterSettings();
   1.177 +            xmlSettings.Indent = true;
   1.178 +            xmlSettings.IndentChars = "   ";
   1.179 +
   1.180 +            // Write XML to file
   1.181 +            using (XmlWriter writer = XmlWriter.Create(path, xmlSettings))
   1.182 +            {
   1.183 +                armyList.Save(writer);
   1.184 +                writer.Flush();
   1.185 +            }
   1.186 +        }
   1.187 +        private string GetEquipmentAmountRatioTranslation(double amount, int number)
   1.188 +        {
   1.189 +            return Translation.GetTranslation("armyHtmlExportEquipAmountPercentage", "{0}% ({1})", amount, number);
   1.190 +        }
   1.191 +
   1.192 +        private string GetEquipmentAmountNumberTranslation(int amount)
   1.193 +        {
   1.194 +            return Translation.GetTranslation("armyHtmlExportEquipAmountNumber", "{0}", amount);
   1.195 +        }
   1.196 +
   1.197 +        private string GetEquipmentAmountAllTranslation(Unit unit)
   1.198 +        {
   1.199 +            return Translation.GetTranslation("armyHtmlExportEquipAmountAll", "all ({1})", 100, unit.Size);
   1.200 +        }
   1.201 +    }
   1.202 +}