annotate API/Factories/Xml/SingleXmlObjectLoader.cs @ 52:97ea355f9564

Re #270: Add multiple categories to API * Add tests for army loading to make sure that units go back in same category
author IBBoard <dev@ibboard.co.uk>
date Wed, 19 May 2010 20:05:52 +0000
parents 82cd08385bfe
children b2517bb113d0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (SingleXmlObjectLoader.cs) is a part of the IBBoard.WarFoundry.API.Tests project and is copyright 2009 IBBoard
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5 using System;
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System.IO;
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.Xml;
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using IBBoard.WarFoundry.API.Objects;
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 namespace IBBoard.WarFoundry.API.Factories.Xml
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 /// <summary>
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 /// A helper class that loads a known XML file to the known type to centralise common testing code in one method
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// </summary>
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 public class SingleXmlObjectLoader
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 public static Race LoadRaceFromXML(WarFoundryXmlFactory factory, FileInfo file)
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 Stream stream = file.OpenRead();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 try
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 {
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
24 return factory.GetRaceFactory().CreateRaceFromElement(null, CreateDocumentElementFromStream(stream));
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 }
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
26 finally
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27 {
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
28 if (stream != null)
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 stream.Close();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
35 public static XmlElement CreateDocumentElementFromFile(FileInfo file)
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 {
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
37 return CreateDocumentElementFromStream(file.OpenRead());
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
38 }
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
39
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
40 private static XmlElement CreateDocumentElementFromStream(Stream stream)
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
41 {
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
42 return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream).DocumentElement;
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 Stream stream = file.OpenRead();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 try
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 return factory.GetSystemFactory().CreateSystemFromElement(null, CreateDocumentElementFromStream(stream));
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53 finally
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 if (stream !=null)
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 stream.Close();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 }