Mercurial > repos > IBDev-IBBoard.WarFoundry.API
annotate api/Factories/Xml/WarFoundryXmlFactoryUtils.cs @ 217:89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
* Add null check and throw InvalidFileException when getting unit XML by ID
* Change InvalidDataException (which is a core exception about data streams) to InvalidFileException (which is our exception for incorrect data in a file)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Nov 2009 16:06:06 +0000 |
parents | c1caf467dd40 |
children | a1a6b527cd70 |
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.WarFoundry.API.Objects; | |
10 | |
11 namespace IBBoard.WarFoundry.API.Factories.Xml | |
12 { | |
13 /// <summary> | |
14 /// A collection of useful utility methods for loading WarFoundry data from XML files | |
15 /// </summary> | |
16 public class WarFoundryXmlFactoryUtils | |
17 { | |
146
0b32cc40d82f
Re #152: Test and fix extensibility of current schemas
IBBoard <dev@ibboard.co.uk>
parents:
104
diff
changeset
|
18 public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/"; |
52 | 19 private static XmlReaderSettings settings; |
20 private static XmlNamespaceManager nsManager; | |
21 | |
22 public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery) | |
23 { | |
24 return element.SelectNodes(xpathQuery, GetNamespaceManager()); | |
25 } | |
26 | |
27 public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery) | |
28 { | |
29 return element.SelectSingleNode(xpathQuery, GetNamespaceManager()); | |
30 } | |
31 | |
32 public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery) | |
33 { | |
34 XmlNode node = SelectSingleNode(element, xpathQuery); | |
35 return (node is XmlElement) ? (XmlElement) node : null; | |
36 } | |
37 | |
38 public static XmlNamespaceManager GetNamespaceManager() | |
39 { | |
40 if (nsManager == null) | |
41 { | |
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 nsManager.AddNamespace("army", NS_BASE + "army"); |
52 | 48 } |
49 | |
50 return nsManager; | |
51 } | |
52 | |
53 /// <summary> | |
54 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema. | |
55 /// </summary> | |
56 /// <returns> | |
57 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema | |
58 /// </returns> | |
59 public static XmlReaderSettings GetReaderSettings() | |
60 { | |
61 if (settings == null) | |
62 { | |
63 settings = new XmlReaderSettings(); | |
64 settings.ValidationType = ValidationType.Schema; | |
65 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; | |
66 settings.ProhibitDtd = true; | |
67 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); | |
68 XmlSchemaSet cache = new XmlSchemaSet(); | |
69 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
|
70 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
|
71 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
|
72 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
|
73 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
|
74 AddSchemaToCache(cache, NS_BASE + "army", path + "army.xsd"); |
52 | 75 settings.Schemas.Add(cache); |
76 } | |
77 | |
78 return settings; | |
79 } | |
80 | |
81 private static void ValidationEventMethod(object sender, ValidationEventArgs e) | |
82 { | |
217
89e26d51afc2
Fixes #226: "NullReferenceException" for a unitID
IBBoard <dev@ibboard.co.uk>
parents:
179
diff
changeset
|
83 throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); |
52 | 84 } |
85 | |
86 private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) | |
87 { | |
88 try | |
89 { | |
90 cache.Add(xmlNamespace, schemaLocation); | |
91 } | |
92 catch (IOException ex) | |
93 { | |
179
c1caf467dd40
Re #193: Unhandled exception in dictionary?
IBBoard <dev@ibboard.co.uk>
parents:
146
diff
changeset
|
94 //TODO: Warn on schema failure |
52 | 95 } |
96 catch (XmlSchemaException ex) | |
97 { | |
179
c1caf467dd40
Re #193: Unhandled exception in dictionary?
IBBoard <dev@ibboard.co.uk>
parents:
146
diff
changeset
|
98 //TODO: Warn on schema failure |
52 | 99 } |
100 catch (XmlException ex) | |
101 { | |
179
c1caf467dd40
Re #193: Unhandled exception in dictionary?
IBBoard <dev@ibboard.co.uk>
parents:
146
diff
changeset
|
102 //TODO: Warn on schema failure |
52 | 103 } |
104 } | |
105 | |
106 public static XmlDocument CreateXmlDocumentFromStream(Stream stream) | |
107 { | |
108 XmlDocument doc = new XmlDocument(); | |
109 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); | |
110 | |
111 try | |
112 { | |
113 doc.Load(reader); | |
114 } | |
115 //Don't catch XMLSchemaExceptions - let them get thrown out | |
116 finally | |
117 { | |
118 reader.Close(); | |
119 } | |
120 | |
121 return doc; | |
122 } | |
123 | |
124 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) | |
125 { | |
126 bool canLoad = true; | |
127 | |
128 if (obj.IsFullyLoaded) | |
129 { | |
130 canLoad = false; | |
131 } | |
132 else if (obj.IsLoading) | |
133 { | |
134 canLoad = false; | |
135 } | |
136 | |
137 return canLoad; | |
138 } | |
139 } | |
140 } |