Mercurial > repos > IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlFactoryUtils.cs @ 146:0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
* Add extensibility to Race schema
Also:
* Code tidy for WarFoundryXmlFactoryUtils
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Wed, 23 Sep 2009 19:58:21 +0000 |
parents | 2f3cafb69799 |
children | c1caf467dd40 |
rev | line source |
---|---|
52 | 1 // This file (WarFoundryXmlFactoryUtils.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard |
2 // | |
104
2f3cafb69799
Re #121: Migrate to AGPL license
IBBoard <dev@ibboard.co.uk>
parents:
52
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. |
52 | 4 |
5 using System; | |
6 using System.IO; | |
7 using System.Xml; | |
8 using System.Xml.Schema; | |
9 using IBBoard.Logging; | |
10 using IBBoard.WarFoundry.API.Objects; | |
11 | |
12 namespace IBBoard.WarFoundry.API.Factories.Xml | |
13 { | |
14 /// <summary> | |
15 /// A collection of useful utility methods for loading WarFoundry data from XML files | |
16 /// </summary> | |
17 public class WarFoundryXmlFactoryUtils | |
18 { | |
146
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
19 public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/"; |
52 | 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()); | |
146
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
44 nsManager.AddNamespace("core", NS_BASE + "core"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
45 nsManager.AddNamespace("cat", NS_BASE + "cats"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
46 nsManager.AddNamespace("race", NS_BASE + "race"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
47 nsManager.AddNamespace("system", NS_BASE + "system"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
48 nsManager.AddNamespace("army", NS_BASE + "army"); |
52 | 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/"; | |
146
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
71 AddSchemaToCache(cache, NS_BASE + "core", path + "warfoundry-core.xsd"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
72 AddSchemaToCache(cache, NS_BASE + "cats", path + "warfoundry-cats.xsd"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
73 AddSchemaToCache(cache, NS_BASE + "race", path + "race.xsd"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
74 AddSchemaToCache(cache, NS_BASE + "system", path + "system.xsd"); |
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
75 AddSchemaToCache(cache, NS_BASE + "army", path + "army.xsd"); |
52 | 76 settings.Schemas.Add(cache); |
77 } | |
78 | |
79 return settings; | |
80 } | |
81 | |
82 private static void ValidationEventMethod(object sender, ValidationEventArgs e) | |
83 { | |
146
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
84 throw new InvalidDataException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); |
52 | 85 } |
86 | |
87 private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) | |
88 { | |
89 try | |
90 { | |
91 cache.Add(xmlNamespace, schemaLocation); | |
92 } | |
93 catch (IOException ex) | |
94 { | |
95 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading schema: " + ex.Message, ex); | |
96 } | |
97 catch (XmlSchemaException ex) | |
98 { | |
99 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem validating schema for WarFoundry data: " + ex.Message, ex); | |
100 } | |
101 catch (XmlException ex) | |
102 { | |
103 LogNotifier.Warn(typeof(WarFoundryXmlFactoryUtils), "Problem reading data for schema: " + ex.Message, ex); | |
104 } | |
105 } | |
106 | |
107 public static XmlDocument CreateXmlDocumentFromStream(Stream stream) | |
108 { | |
109 XmlDocument doc = new XmlDocument(); | |
110 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); | |
111 | |
112 try | |
113 { | |
114 doc.Load(reader); | |
115 } | |
116 //Don't catch XMLSchemaExceptions - let them get thrown out | |
117 finally | |
118 { | |
119 reader.Close(); | |
120 } | |
121 | |
122 return doc; | |
123 } | |
124 | |
125 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) | |
126 { | |
127 bool canLoad = true; | |
128 | |
129 if (obj.IsFullyLoaded) | |
130 { | |
131 LogNotifier.DebugFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already fully loaded", obj.GetType().Name, obj.ID); | |
132 canLoad = false; | |
133 } | |
134 else if (obj.IsLoading) | |
135 { | |
136 LogNotifier.WarnFormat(typeof(WarFoundryXmlFactoryUtils), "Object of type {0} with ID {1} is already being loaded", obj.GetType().Name, obj.ID); | |
137 canLoad = false; | |
138 } | |
139 | |
140 return canLoad; | |
141 } | |
142 } | |
143 } |