comparison API/Factories/Xml/SingleXmlObjectLoader.cs @ 36:82cd08385bfe

Fixes #228: Crash on invalid ability ID * Replace zipped Race and System files with unpackaged XML * Add helper class to create WarFoundry objects from XML files for tests * Use new helper classes to make test pass
author IBBoard <dev@ibboard.co.uk>
date Thu, 24 Dec 2009 20:14:28 +0000
parents
children 97ea355f9564
comparison
equal deleted inserted replaced
35:4302e6b2c5c1 36:82cd08385bfe
1 // This file (SingleXmlObjectLoader.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 IBBoard
2 //
3 // 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.
4
5 using System;
6 using System.IO;
7 using System.Xml;
8 using IBBoard.WarFoundry.API.Objects;
9
10 namespace IBBoard.WarFoundry.API.Factories.Xml
11 {
12
13 /// <summary>
14 /// A helper class that loads a known XML file to the known type to centralise common testing code in one method
15 /// </summary>
16 public class SingleXmlObjectLoader
17 {
18 public static Race LoadRaceFromXML(WarFoundryXmlFactory factory, FileInfo file)
19 {
20 Stream stream = file.OpenRead();
21
22 try
23 {
24 return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream (stream));
25 }
26 finally
27 {
28 if (stream !=null)
29 {
30 stream.Close();
31 }
32 }
33 }
34
35 private static XmlElement CreateDocumentElementFromStream (Stream stream)
36 {
37 return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream (stream).DocumentElement;
38 }
39
40 public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
41 {
42 Stream stream = file.OpenRead();
43
44 try
45 {
46 return factory.GetSystemFactory().CreateSystemFromElement(null, CreateDocumentElementFromStream(stream));
47 }
48 finally
49 {
50 if (stream !=null)
51 {
52 stream.Close();
53 }
54 }
55 }
56 }
57 }