annotate API/Factories/Xml/SingleXmlObjectLoader.cs @ 75:72479ca6feb4

Fixes #311: can't read ZIP file packed by Linux app Archive Manager/File Roller * Add a unit test for the failure
author IBBoard <dev@ibboard.co.uk>
date Sat, 30 Oct 2010 14:45:45 +0000
parents b2517bb113d0
children 3725fd449c77
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
69
b2517bb113d0 Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
35 public static XmlElement CreateDocumentElementFromFile(string filePath)
b2517bb113d0 Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
36 {
b2517bb113d0 Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
37 return CreateDocumentElementFromFile(new FileInfo(filePath));
b2517bb113d0 Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
38 }
b2517bb113d0 Re #290: Migrate to using key/keyref definitions instead of ID/IDREF type
IBBoard <dev@ibboard.co.uk>
parents: 52
diff changeset
39
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
40 public static XmlElement CreateDocumentElementFromFile(FileInfo file)
36
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 {
52
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
42 return CreateDocumentElementFromStream(file.OpenRead());
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
43 }
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
44
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
45 private static XmlElement CreateDocumentElementFromStream(Stream stream)
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
46 {
97ea355f9564 Re #270: Add multiple categories to API
IBBoard <dev@ibboard.co.uk>
parents: 36
diff changeset
47 return WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream).DocumentElement;
36
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
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50 public static GameSystem LoadGameSystemFromXML(WarFoundryXmlFactory factory, FileInfo file)
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 Stream stream = file.OpenRead();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 try
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 {
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 return factory.GetSystemFactory().CreateSystemFromElement(null, CreateDocumentElementFromStream(stream));
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 finally
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 if (stream !=null)
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 stream.Close();
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 }
82cd08385bfe Fixes #228: Crash on invalid ability ID
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 }