Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlFactory.cs @ 52:64ef178c18aa
Re #10 - Refactor for readability
* Break WarFoundryXMLFactory out in to GameSystem, Race and Army factories
* Create factory utils classes with methods from WarFoundryXMLFactory for getting node lists etc
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 30 Mar 2009 19:44:03 +0000 |
parents | b271a2252758 |
children | 3ea0ab04352b |
rev | line source |
---|---|
15 | 1 // This file (WarFoundryXmlFactory.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard. |
0 | 2 // |
15 | 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. |
0 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Xml; | |
8 using System.Xml.Schema; | |
34 | 9 using System.Xml.XPath; |
0 | 10 using System.Collections.Generic; |
11 using System.Text; | |
12 using IBBoard; | |
13 using IBBoard.IO; | |
14 using IBBoard.Lang; | |
15 using IBBoard.Logging; | |
16 using IBBoard.Xml; | |
17 using IBBoard.WarFoundry.API.Requirements; | |
18 using IBBoard.WarFoundry.API.Objects; | |
19 using ICSharpCode.SharpZipLib.Zip; | |
20 | |
21 namespace IBBoard.WarFoundry.API.Factories.Xml | |
22 { | |
23 /// <summary> | |
10 | 24 /// The WarFoundryXmlFactory loads WarFoundry classes from the native "XML in a zip" file format. Files are validated using the schema for the file type, so structurally invalid files should be identified at initial load. |
0 | 25 /// </summary> |
26 public class WarFoundryXmlFactory : AbstractNativeWarFoundryFactory | |
27 { | |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
28 private static WarFoundryXmlFactory factory; |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
29 private WarFoundryXmlGameSystemFactory gameSystemFactory; |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
30 private WarFoundryXmlRaceFactory raceFactory; |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
31 private WarFoundryXmlArmyFactory armyFactory; |
0 | 32 |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
33 public static AbstractNativeWarFoundryFactory GetFactory() |
0 | 34 { |
17
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
35 if (factory == null) |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
36 { |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
37 factory = new WarFoundryXmlFactory(); |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
38 } |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
39 |
a99d3b8466ba
Change "CreateFactory" method to "GetFactory" method to allow for caching
IBBoard <dev@ibboard.co.uk>
parents:
15
diff
changeset
|
40 return factory; |
0 | 41 } |
42 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
43 private WarFoundryXmlFactory() : base() |
0 | 44 { |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
45 gameSystemFactory = new WarFoundryXmlGameSystemFactory(this); |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
46 raceFactory = new WarFoundryXmlRaceFactory(this); |
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
47 armyFactory = new WarFoundryXmlArmyFactory(); |
0 | 48 } |
49 | |
50 protected override bool CheckCanFindArmyFileContent(ZipFile file) | |
51 { | |
52 return file.FindEntry("data.armyx", true) > -1; | |
53 } | |
54 | |
55 protected override bool CheckCanFindSystemFileContent(ZipFile file) | |
56 { | |
57 return file.FindEntry("data.systemx", true) > -1; | |
58 } | |
59 | |
60 protected override bool CheckCanFindRaceFileContent(ZipFile file) | |
61 { | |
62 return file.FindEntry("data.racex", true) > -1; | |
63 } | |
64 | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
65 protected override Stream GetArmyDataStream(ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
66 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
67 return file.GetInputStream(file.FindEntry("data.armyx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
68 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
69 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
70 protected override Army CreateArmyFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
71 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
72 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.ARMY_ELEMENT); |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
73 return armyFactory.CreateArmyFromElement(file, elem); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
74 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
75 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
76 private XmlElement GetRootElementFromStream(Stream stream, WarFoundryXmlElementName elementName) |
0 | 77 { |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
78 XmlDocument doc = WarFoundryXmlFactoryUtils.CreateXmlDocumentFromStream(stream); |
0 | 79 XmlElement elem = (XmlElement)doc.LastChild; |
80 | |
43
d0812d7de39d
Re #49 - Resolve namespace issues
IBBoard <dev@ibboard.co.uk>
parents:
42
diff
changeset
|
81 if (!elem.LocalName.Equals(elementName.Value)) |
0 | 82 { |
83 throw new InvalidFileException(String.Format("Root element of XML was not valid. Expected {0} but got {1}", elementName.Value, elem.Name)); | |
84 } | |
85 | |
86 return elem; | |
87 } | |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
88 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
89 protected override Stream GetGameSystemDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
90 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
91 return file.GetInputStream(file.FindEntry("data.systemx", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
92 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
93 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
94 protected override GameSystem CreateGameSystemFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
95 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
96 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.SYSTEM_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
97 LogNotifier.Debug(GetType(), "Create GameSystem"); |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
98 return gameSystemFactory.CreateSystemFromElement(file, elem); |
50
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
99 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
100 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
101 protected override Stream GetRaceDataStream (ZipFile file) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
102 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
103 return file.GetInputStream(file.FindEntry("data.racex", true)); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
104 } |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
105 |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
106 protected override Race CreateRaceFromStream (ZipFile file, Stream dataStream) |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
107 { |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
108 XmlElement elem = GetRootElementFromStream(dataStream, WarFoundryXmlElementName.RACE_ELEMENT); |
bb6b993b98bf
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
49
diff
changeset
|
109 LogNotifier.Debug(GetType(), "Create Race"); |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
110 return raceFactory.CreateRaceFromElement(file, elem); |
49
9d31d063b194
Re #10 - Refactor source code for readability
IBBoard <dev@ibboard.co.uk>
parents:
48
diff
changeset
|
111 } |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
112 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
113 public override void CompleteLoading(IWarFoundryStagedLoadObject obj) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
114 { |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
115 LogNotifier.DebugFormat(GetType(), "Complete loading of {0} with ID {1}", obj.GetType().Name, obj.ID); |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
116 |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
117 if (obj is GameSystem) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
118 { |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
119 gameSystemFactory.CompleteLoading((GameSystem)obj); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
120 } |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
121 else if (obj is Race) |
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
122 { |
52
64ef178c18aa
Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
51
diff
changeset
|
123 raceFactory.CompleteLoading((Race)obj); |
48
b49372dd8afa
Re #50 - Complete loading of XML files
IBBoard <dev@ibboard.co.uk>
parents:
47
diff
changeset
|
124 } |
47
85f2b9c3609c
Re #13 - Use XPath for file loading
IBBoard <dev@ibboard.co.uk>
parents:
45
diff
changeset
|
125 } |
0 | 126 } |
127 } |