annotate api/Factories/Xml/WarFoundryXmlFactoryUtils.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
children 2f3cafb69799
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
1 // This file (WarFoundryXmlFactoryUtils.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
2 //
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
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.
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
4 //
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
5
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
6 using System;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
7 using System.IO;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
8 using System.Xml;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
9 using System.Xml.Schema;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
10 using IBBoard.Logging;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
11 using IBBoard.WarFoundry.API.Objects;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
12
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
13 namespace IBBoard.WarFoundry.API.Factories.Xml
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
14 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
15 /// <summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
16 /// A collection of useful utility methods for loading WarFoundry data from XML files
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 /// </summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
18 public class WarFoundryXmlFactoryUtils
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 private static XmlReaderSettings settings;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
21 private static XmlNamespaceManager nsManager;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
24 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 return element.SelectNodes(xpathQuery, GetNamespaceManager());
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
27
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
29 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 return element.SelectSingleNode(xpathQuery, GetNamespaceManager());
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 XmlNode node = SelectSingleNode(element, xpathQuery);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 return (node is XmlElement) ? (XmlElement) node : null;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
37 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
38
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
39 public static XmlNamespaceManager GetNamespaceManager()
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
41 if (nsManager == null)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
42 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
43 nsManager = new XmlNamespaceManager(new NameTable());
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
44 nsManager.AddNamespace("core", "http://ibboard.co.uk/warfoundry/core");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
45 nsManager.AddNamespace("cat", "http://ibboard.co.uk/warfoundry/cats");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
46 nsManager.AddNamespace("race", "http://ibboard.co.uk/warfoundry/race");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
47 nsManager.AddNamespace("system", "http://ibboard.co.uk/warfoundry/system");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
48 nsManager.AddNamespace("army", "http://ibboard.co.uk/warfoundry/army");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
49 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
50
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 return nsManager;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
52 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
53
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 /// <summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56 /// </summary>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 /// <returns>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 /// </returns>
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 public static XmlReaderSettings GetReaderSettings()
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
62 if (settings == null)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
63 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 settings = new XmlReaderSettings();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 settings.ValidationType = ValidationType.Schema;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 settings.ProhibitDtd = true;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 XmlSchemaSet cache = new XmlSchemaSet();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
70 string path = IBBoard.Constants.ExecutablePath + "/dtds/";
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
71 string nsBase = "http://ibboard.co.uk/warfoundry/";
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
72 AddSchemaToCache(cache, nsBase + "core", path + "warfoundry-core.xsd");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 AddSchemaToCache(cache, nsBase + "cats", path + "warfoundry-cats.xsd");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
74 AddSchemaToCache(cache, nsBase + "race", path + "race.xsd");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
75 AddSchemaToCache(cache, nsBase + "system", path + "system.xsd");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 AddSchemaToCache(cache, nsBase + "army", path + "army.xsd");
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
77 settings.Schemas.Add(cache);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
78 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
79
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
80 return settings;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
81 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
82
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
83 private static void ValidationEventMethod(object sender, ValidationEventArgs e)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
84 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
85 throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Exception.Message, e.Exception);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
86 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
87
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
88 private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
89 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
90 try
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
91 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
92 cache.Add(xmlNamespace, schemaLocation);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
93 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
94 catch (IOException ex)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
95 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
96 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading schema: " + ex.Message, ex);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
97 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
98 catch (XmlSchemaException ex)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
99 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
100 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem validating schema for WarFoundry data: " + ex.Message, ex);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
101 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
102 catch (XmlException ex)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
103 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
104 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading data for schema: " + ex.Message, ex);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
105 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
106 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
107
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
108 public static XmlDocument CreateXmlDocumentFromStream(Stream stream)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
109 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
110 XmlDocument doc = new XmlDocument();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
111 XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
112
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
113 try
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
114 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
115 doc.Load(reader);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
116 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
117 //Don't catch XMLSchemaExceptions - let them get thrown out
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
118 finally
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
119 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
120 reader.Close();
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
121 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
122
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
123 return doc;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
124 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
125
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
126 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
127 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
128 bool canLoad = true;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
129
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
130 if (obj.IsFullyLoaded)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
131 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
132 LogNotifier.DebugFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
133 canLoad = false;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
134 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
135 else if (obj.IsLoading)
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
136 {
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
137 LogNotifier.WarnFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID);
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
138 canLoad = false;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
139 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
140
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
141 return canLoad;
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
142 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
143 }
64ef178c18aa Re #10 - Refactor for readability
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
144 }