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, 100 insertions(+), 68 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.WarFoundry.API.csproj	Sat Aug 15 10:36:50 2009 +0000
+++ b/IBBoard.WarFoundry.API.csproj	Mon Aug 17 19:08:44 2009 +0000
@@ -132,6 +132,7 @@
     <Compile Include="api\Commands\AbstractSetUnitEquipmentAmountCommand.cs" />
     <Compile Include="api\Commands\ReplaceUnitEquipmentWithNumericAmountItemCommand.cs" />
     <Compile Include="api\Commands\ReplaceUnitEquipmentWithRatioAmountItemCommand.cs" />
+    <Compile Include="api\Factories\Xml\Zip\StringZipEntrySource.cs" />
   </ItemGroup>
   <ItemGroup>
     <Content Include="libs\ICSharpCode.SharpZipLib.dll" />
--- a/api/Factories/Xml/WarFoundryXmlSaver.cs	Sat Aug 15 10:36:50 2009 +0000
+++ b/api/Factories/Xml/WarFoundryXmlSaver.cs	Mon Aug 17 19:08:44 2009 +0000
@@ -5,92 +5,93 @@
 using System;
 using System.Collections.Generic;
 using System.IO;
+using System.Xml;
+using System.Xml.Schema;
 using IBBoard.Lang;
+using IBBoard.WarFoundry.API.Factories.Xml.Zip;
 using IBBoard.WarFoundry.API.Objects;
+using IBBoard.WarFoundry.API.Savers;
+using ICSharpCode.SharpZipLib.Zip;
 
 namespace IBBoard.WarFoundry.API.Factories.Xml
 {
-	public class WarFoundryXmlSaver
+	public class WarFoundryXmlSaver : IWarFoundryFileSaver
 	{
-		//FIXME: Rework to saving files in Zips
-		private static WarFoundryXmlSaver saver;
-		private static List<Type> saveAsTypes;
-		private Dictionary<WarFoundryObject, string> savePaths = new Dictionary<WarFoundryObject,string>();
-		
-		public static WarFoundryXmlSaver GetSaver()
+		public bool Save(Army toSave, string savePath)
 		{
-			if (saver == null)
+			bool success = false;
+			ZipFile file = null;
+
+			try
 			{
-				saveAsTypes = new List<Type>();
-				saveAsTypes.Add(typeof(Army));
-				saver = new WarFoundryXmlSaver();
+				file = ZipFile.Create(savePath);
+				file.BeginUpdate();
+				file.Add(new StringZipEntrySource(CreateXmlString(toSave)), "data.army");
+				file.CommitUpdate();
+				success = true;
 			}
-			
-			return saver;
-		}
-		
-		public bool SaveAs(WarFoundryObject toSave, string saveAsPath)
-		{
-			if (CanSaveType(toSave))
+			finally
 			{
-				FileStream fs = null;
-				bool success = false;
-				
-				try
+				if (file != null)
 				{
-					fs = new FileStream(saveAsPath, FileMode.Create, FileAccess.Write);
-					byte[] bytes = StringManipulation.StringToBytes(CreateXmlString(toSave));
-					fs.Write(bytes, 0, bytes.Length);
-					fs.Flush();
-					savePaths.Add(toSave, saveAsPath);
-					success = true;
+					file.Close();
 				}
-				finally
-				{
-					if (fs!=null && fs.CanWrite)
-					{
-						fs.Close();
-					}
-				}
-
-				return success;
 			}
-			else
-			{
-				throw new ArgumentException("Cannot directly save objects of type "+toSave.GetType());
-			}
+
+			return success;
 		}
-		
-		public bool Save(WarFoundryObject toSave)
-		{
-			if (CanSave(toSave))
-			{
-				return SaveAs(toSave, savePaths[toSave]);
-			}
-			else
-			{
-				throw new InvalidOperationException("Cannot save an object that has not previously been saved using SaveAs");
-			}
-		}
-		
+
 		private string CreateXmlString(WarFoundryObject toSave)
 		{
-			return ""; //TODO: Create XML string as appropriate
-		}
-		
-		public bool CanSaveAs(WarFoundryObject obj)
-		{
-			return CanSaveType(obj);
+			/*
+<army id="12345" name="Sample Army" system="sampleSystem" race="Empire" maxPoints="500">
+	<units>
+		<unit id="unit1" unitType="Empire1" unitName="General Eustace" size="1">
+			<equipment>
+				<equipItem id="equip1" amount="1"/>
+			</equipment>
+		</unit>
+		<unit id="unit2" unitType="Empire2" unitName="First Swordsmen" size="20">
+			<equipment>
+				<equipItem id="equip1" amount="1"/>
+				<equipItem id="equip2" amount="1"/>
+			</equipment>
+		</unit>
+		<unit id="unit3" unitType="Empire2" unitName="First Greatswords" size="15">
+			<equipment>
+				<equipItem id="equip3" amount="1"/>
+			</equipment>
+		</unit>
+	</units>
+</army>
+*/
+			string xmlString = "";
+
+			if (toSave is Army)
+			{
+				xmlString = CreateArmyXmlString((Army)toSave);
+			}
+
+			return xmlString;
 		}
-		
-		public bool CanSave(WarFoundryObject obj)
+
+		private string CreateArmyXmlString(Army toSave)
 		{
-			return savePaths.ContainsKey(obj);
-		}
-		
-		public bool CanSaveType(WarFoundryObject obj)
-		{
-			return saveAsTypes.Contains(obj.GetType());
+			XmlDocument doc = new XmlDocument();
+			XmlDeclaration declaration = doc.CreateXmlDeclaration("1.0", null, null);
+			doc.AppendChild(declaration);
+			XmlSchema schema = new XmlSchema();
+			schema.Namespaces.Add("xmlns", "http://ibboard.co.uk/warfoundry/army");
+			schema.Namespaces.Add("xmlns:core", "http://ibboard.co.uk/warfoundry/core");
+			doc.Schemas.Add(schema);
+			XmlElement root = doc.CreateElement("army");
+			doc.AppendChild(root);
+			root.SetAttribute("id", toSave.ID);
+			root.SetAttribute("name", toSave.Name);
+			root.SetAttribute("system", toSave.GameSystem.ID);
+			root.SetAttribute("race", toSave.Race.ID);
+			root.SetAttribute("maxPoints", toSave.MaxPoints.ToString());
+			return doc.OuterXml;
 		}
 	}
 }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/api/Factories/Xml/Zip/StringZipEntrySource.cs	Mon Aug 17 19:08:44 2009 +0000
@@ -0,0 +1,30 @@
+//  This file (StringZipEntrySource.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
+// 
+//  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.
+// 
+
+using System;
+using System.IO;
+using IBBoard.Lang;
+using ICSharpCode.SharpZipLib.Zip;
+
+namespace IBBoard.WarFoundry.API.Factories.Xml.Zip
+{
+	/// <summary>
+	/// A simple implementation of IStaticDataSource that lets us add a string directly to a Zip file
+	/// </summary>
+	public class StringZipEntrySource : IStaticDataSource
+	{
+		private byte[] entryContent;
+		
+		public StringZipEntrySource(String content)
+		{
+			entryContent = StringManipulation.StringToBytes(content);
+		}
+
+		public Stream GetSource()
+		{
+			return new MemoryStream(entryContent);
+		}
+	}
+}