changeset 105:f5aec697b8ea

Re #53: Make WarFoundry XML saver * Make XMLSaver implement Saver interface * Strip out old code * Start to code converting of objects to XML files * Add class to store strings in to Zip files
author IBBoard <dev@ibboard.co.uk>
date Mon, 17 Aug 2009 19:08:44 +0000
parents 2f3cafb69799
children 9bf5eff0992a
files IBBoard.WarFoundry.API.csproj api/Factories/Xml/WarFoundryXmlSaver.cs api/Factories/Xml/Zip/StringZipEntrySource.cs
diffstat 3 files changed, 103 insertions(+), 71 deletions(-) [+]
line diff
     1.1 --- a/IBBoard.WarFoundry.API.csproj	Sat Aug 15 10:36:50 2009 +0000
     1.2 +++ b/IBBoard.WarFoundry.API.csproj	Mon Aug 17 19:08:44 2009 +0000
     1.3 @@ -132,6 +132,7 @@
     1.4      <Compile Include="api\Commands\AbstractSetUnitEquipmentAmountCommand.cs" />
     1.5      <Compile Include="api\Commands\ReplaceUnitEquipmentWithNumericAmountItemCommand.cs" />
     1.6      <Compile Include="api\Commands\ReplaceUnitEquipmentWithRatioAmountItemCommand.cs" />
     1.7 +    <Compile Include="api\Factories\Xml\Zip\StringZipEntrySource.cs" />
     1.8    </ItemGroup>
     1.9    <ItemGroup>
    1.10      <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
     2.1 --- a/api/Factories/Xml/WarFoundryXmlSaver.cs	Sat Aug 15 10:36:50 2009 +0000
     2.2 +++ b/api/Factories/Xml/WarFoundryXmlSaver.cs	Mon Aug 17 19:08:44 2009 +0000
     2.3 @@ -5,92 +5,93 @@
     2.4  using System;
     2.5  using System.Collections.Generic;
     2.6  using System.IO;
     2.7 +using System.Xml;
     2.8 +using System.Xml.Schema;
     2.9  using IBBoard.Lang;
    2.10 +using IBBoard.WarFoundry.API.Factories.Xml.Zip;
    2.11  using IBBoard.WarFoundry.API.Objects;
    2.12 +using IBBoard.WarFoundry.API.Savers;
    2.13 +using ICSharpCode.SharpZipLib.Zip;
    2.14  
    2.15  namespace IBBoard.WarFoundry.API.Factories.Xml
    2.16  {
    2.17 -	public class WarFoundryXmlSaver
    2.18 +	public class WarFoundryXmlSaver : IWarFoundryFileSaver
    2.19  	{
    2.20 -		//FIXME: Rework to saving files in Zips
    2.21 -		private static WarFoundryXmlSaver saver;
    2.22 -		private static List<Type> saveAsTypes;
    2.23 -		private Dictionary<WarFoundryObject, string> savePaths = new Dictionary<WarFoundryObject,string>();
    2.24 -		
    2.25 -		public static WarFoundryXmlSaver GetSaver()
    2.26 +		public bool Save(Army toSave, string savePath)
    2.27  		{
    2.28 -			if (saver == null)
    2.29 +			bool success = false;
    2.30 +			ZipFile file = null;
    2.31 +
    2.32 +			try
    2.33  			{
    2.34 -				saveAsTypes = new List<Type>();
    2.35 -				saveAsTypes.Add(typeof(Army));
    2.36 -				saver = new WarFoundryXmlSaver();
    2.37 +				file = ZipFile.Create(savePath);
    2.38 +				file.BeginUpdate();
    2.39 +				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.army");
    2.40 +				file.CommitUpdate();
    2.41 +				success = true;
    2.42  			}
    2.43 -			
    2.44 -			return saver;
    2.45 +			finally
    2.46 +			{
    2.47 +				if (file != null)
    2.48 +				{
    2.49 +					file.Close();
    2.50 +				}
    2.51 +			}
    2.52 +
    2.53 +			return success;
    2.54  		}
    2.55 -		
    2.56 -		public bool SaveAs(WarFoundryObject toSave, string saveAsPath)
    2.57 -		{
    2.58 -			if (CanSaveType(toSave))
    2.59 -			{
    2.60 -				FileStream fs = null;
    2.61 -				bool success = false;
    2.62 -				
    2.63 -				try
    2.64 -				{
    2.65 -					fs = new FileStream(saveAsPath, FileMode.Create, FileAccess.Write);
    2.66 -					byte[] bytes = StringManipulation.StringToBytes(CreateXmlString(toSave));
    2.67 -					fs.Write(bytes, 0, bytes.Length);
    2.68 -					fs.Flush();
    2.69 -					savePaths.Add(toSave, saveAsPath);
    2.70 -					success = true;
    2.71 -				}
    2.72 -				finally
    2.73 -				{
    2.74 -					if (fs!=null && fs.CanWrite)
    2.75 -					{
    2.76 -						fs.Close();
    2.77 -					}
    2.78 -				}
    2.79  
    2.80 -				return success;
    2.81 -			}
    2.82 -			else
    2.83 -			{
    2.84 -				throw new ArgumentException("Cannot directly save objects of type "+toSave.GetType());
    2.85 -			}
    2.86 -		}
    2.87 -		
    2.88 -		public bool Save(WarFoundryObject toSave)
    2.89 -		{
    2.90 -			if (CanSave(toSave))
    2.91 -			{
    2.92 -				return SaveAs(toSave, savePaths[toSave]);
    2.93 -			}
    2.94 -			else
    2.95 -			{
    2.96 -				throw new InvalidOperationException("Cannot save an object that has not previously been saved using SaveAs");
    2.97 -			}
    2.98 -		}
    2.99 -		
   2.100  		private string CreateXmlString(WarFoundryObject toSave)
   2.101  		{
   2.102 -			return ""; //TODO: Create XML string as appropriate
   2.103 +			/*
   2.104 +<army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
   2.105 +	<units>
   2.106 +		<unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
   2.107 +			<equipment>
   2.108 +				<equipItem id="equip1" amount="1"/>
   2.109 +			</equipment>
   2.110 +		</unit>
   2.111 +		<unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20">
   2.112 +			<equipment>
   2.113 +				<equipItem id="equip1" amount="1"/>
   2.114 +				<equipItem id="equip2" amount="1"/>
   2.115 +			</equipment>
   2.116 +		</unit>
   2.117 +		<unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15">
   2.118 +			<equipment>
   2.119 +				<equipItem id="equip3" amount="1"/>
   2.120 +			</equipment>
   2.121 +		</unit>
   2.122 +	</units>
   2.123 +</army>
   2.124 +*/
   2.125 +			string xmlString = "";
   2.126 +
   2.127 +			if (toSave is Army)
   2.128 +			{
   2.129 +				xmlString = CreateArmyXmlString((Army)toSave);
   2.130 +			}
   2.131 +
   2.132 +			return xmlString;
   2.133  		}
   2.134 -		
   2.135 -		public bool CanSaveAs(WarFoundryObject obj)
   2.136 +
   2.137 +		private string CreateArmyXmlString(Army toSave)
   2.138  		{
   2.139 -			return CanSaveType(obj);
   2.140 -		}
   2.141 -		
   2.142 -		public bool CanSave(WarFoundryObject obj)
   2.143 -		{
   2.144 -			return savePaths.ContainsKey(obj);
   2.145 -		}
   2.146 -		
   2.147 -		public bool CanSaveType(WarFoundryObject obj)
   2.148 -		{
   2.149 -			return saveAsTypes.Contains(obj.GetType());
   2.150 +			XmlDocument doc = new XmlDocument();
   2.151 +			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
   2.152 +			doc.AppendChild(declaration);
   2.153 +			XmlSchema schema = new XmlSchema();
   2.154 +			schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army");
   2.155 +			schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
   2.156 +			doc.Schemas.Add(schema);
   2.157 +			XmlElement root = doc.CreateElement("army");
   2.158 +			doc.AppendChild(root);
   2.159 +			root.SetAttribute("id", toSave.ID);
   2.160 +			root.SetAttribute("name", toSave.Name);
   2.161 +			root.SetAttribute("system", toSave.GameSystem.ID);
   2.162 +			root.SetAttribute("race", toSave.Race.ID);
   2.163 +			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
   2.164 +			return doc.OuterXml;
   2.165  		}
   2.166  	}
   2.167  }
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/api/Factories/Xml/Zip/StringZipEntrySource.cs	Mon Aug 17 19:08:44 2009 +0000
     3.3 @@ -0,0 +1,30 @@
     3.4 +//  This file (StringZipEntrySource.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
     3.5 +// 
     3.6 +//  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
     3.7 +// 
     3.8 +
     3.9 +using System;
    3.10 +using System.IO;
    3.11 +using IBBoard.Lang;
    3.12 +using ICSharpCode.SharpZipLib.Zip;
    3.13 +
    3.14 +namespace IBBoard.WarFoundry.API.Factories.Xml.Zip
    3.15 +{
    3.16 +	/// <summary>
    3.17 +	/// A simple implementation of IStaticDataSource that lets us add a string directly to a Zip file
    3.18 +	/// </summary>
    3.19 +	public class StringZipEntrySource : IStaticDataSource
    3.20 +	{
    3.21 +		private byte[] entryContent;
    3.22 +		
    3.23 +		public StringZipEntrySource(String content)
    3.24 +		{
    3.25 +			entryContent = StringManipulation.StringToBytes(content);
    3.26 +		}
    3.27 +
    3.28 +		public Stream GetSource()
    3.29 +		{
    3.30 +			return new MemoryStream(entryContent);
    3.31 +		}
    3.32 +	}
    3.33 +}