15
|
1 // This file (WarFoundryXmlSaver.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard.
|
|
2 //
|
|
3 // 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.
|
|
4
|
0
|
5 using System;
|
|
6 using System.Collections.Generic;
|
|
7 using System.IO;
|
|
8 using IBBoard.Lang;
|
|
9 using IBBoard.WarFoundry.API.Objects;
|
|
10
|
|
11 namespace IBBoard.WarFoundry.API.Factories.Xml
|
|
12 {
|
|
13 public class WarFoundryXmlSaver
|
|
14 {
|
|
15 //FIXME: Rework to saving files in Zips
|
|
16 private static WarFoundryXmlSaver saver;
|
|
17 private static List<Type> saveAsTypes;
|
|
18 private Dictionary<WarFoundryObject, string> savePaths = new Dictionary<WarFoundryObject,string>();
|
|
19
|
|
20 public static WarFoundryXmlSaver GetSaver()
|
|
21 {
|
|
22 if (saver == null)
|
|
23 {
|
|
24 saveAsTypes = new List<Type>();
|
|
25 saveAsTypes.Add(typeof(Army));
|
|
26 saver = new WarFoundryXmlSaver();
|
|
27 }
|
|
28
|
|
29 return saver;
|
|
30 }
|
|
31
|
82
|
32 public bool SaveAs(WarFoundryObject toSave, string saveAsPath)
|
0
|
33 {
|
|
34 if (CanSaveType(toSave))
|
|
35 {
|
|
36 FileStream fs = null;
|
|
37 bool success = false;
|
|
38
|
|
39 try
|
82
|
40 {
|
|
41 fs = new FileStream(saveAsPath, FileMode.Create, FileAccess.Write);
|
|
42 byte[] bytes = StringManipulation.StringToBytes(CreateXmlString(toSave));
|
|
43 fs.Write(bytes, 0, bytes.Length);
|
0
|
44 fs.Flush();
|
82
|
45 savePaths.Add(toSave, saveAsPath);
|
0
|
46 success = true;
|
|
47 }
|
|
48 finally
|
|
49 {
|
|
50 if (fs!=null && fs.CanWrite)
|
|
51 {
|
|
52 fs.Close();
|
|
53 }
|
|
54 }
|
|
55
|
|
56 return success;
|
|
57 }
|
|
58 else
|
|
59 {
|
|
60 throw new ArgumentException("Cannot directly save objects of type "+toSave.GetType());
|
82
|
61 }
|
0
|
62 }
|
|
63
|
|
64 public bool Save(WarFoundryObject toSave)
|
|
65 {
|
|
66 if (CanSave(toSave))
|
|
67 {
|
|
68 return SaveAs(toSave, savePaths[toSave]);
|
|
69 }
|
|
70 else
|
|
71 {
|
|
72 throw new InvalidOperationException("Cannot save an object that has not previously been saved using SaveAs");
|
|
73 }
|
|
74 }
|
|
75
|
|
76 private string CreateXmlString(WarFoundryObject toSave)
|
|
77 {
|
|
78 return ""; //TODO: Create XML string as appropriate
|
|
79 }
|
|
80
|
|
81 public bool CanSaveAs(WarFoundryObject obj)
|
|
82 {
|
|
83 return CanSaveType(obj);
|
|
84 }
|
|
85
|
|
86 public bool CanSave(WarFoundryObject obj)
|
|
87 {
|
|
88 return savePaths.ContainsKey(obj);
|
|
89 }
|
|
90
|
|
91 public bool CanSaveType(WarFoundryObject obj)
|
|
92 {
|
|
93 return saveAsTypes.Contains(obj.GetType());
|
|
94 }
|
|
95 }
|
|
96 }
|