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