Mercurial > repos > IBDev-IBBoard.WarFoundry.API
comparison API/Factories/Xml/WarFoundryXmlFactoryUtils.cs @ 481:81f32062c9fa
Re #419: Remove assumptions of a file-based install
* Convert schemas to resources and load them from there
* Remove use of "data" folder relative to exe location from default "Hacks" (GTK/WinForms GUIs can re-add it locally)
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Mon, 25 Jun 2012 21:04:02 +0100 |
parents | 71fceea2725b |
children |
comparison
equal
deleted
inserted
replaced
480:e0641e0eb86c | 481:81f32062c9fa |
---|---|
6 using System.IO; | 6 using System.IO; |
7 using System.Xml; | 7 using System.Xml; |
8 using System.Xml.Schema; | 8 using System.Xml.Schema; |
9 using IBBoard.WarFoundry.API.Objects; | 9 using IBBoard.WarFoundry.API.Objects; |
10 using IBBoard.IO; | 10 using IBBoard.IO; |
11 using IBBoard.Xml; | |
12 using System.Reflection; | |
11 | 13 |
12 namespace IBBoard.WarFoundry.API.Factories.Xml | 14 namespace IBBoard.WarFoundry.API.Factories.Xml |
13 { | 15 { |
14 /// <summary> | 16 /// <summary> |
15 /// A collection of useful utility methods for loading WarFoundry data from XML files | 17 /// A collection of useful utility methods for loading WarFoundry data from XML files |
16 /// </summary> | 18 /// </summary> |
17 public class WarFoundryXmlFactoryUtils | 19 public class WarFoundryXmlFactoryUtils |
18 { | 20 { |
19 public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/"; | 21 public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/"; |
20 private static XmlReaderSettings settings; | 22 private static XmlReaderSettings settings; |
21 private static XmlNamespaceManager nsManager; | 23 private static XmlNamespaceManager nsManager; |
22 | 24 |
23 public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery) | 25 public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery) |
24 { | 26 { |
25 return element.SelectNodes(xpathQuery, GetNamespaceManager()); | 27 return element.SelectNodes(xpathQuery, GetNamespaceManager()); |
26 } | 28 } |
27 | 29 |
28 public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery) | 30 public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery) |
29 { | 31 { |
30 return element.SelectSingleNode(xpathQuery, GetNamespaceManager()); | 32 return element.SelectSingleNode(xpathQuery, GetNamespaceManager()); |
31 } | 33 } |
32 | 34 |
33 public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery) | 35 public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery) |
34 { | 36 { |
35 XmlNode node = SelectSingleNode(element, xpathQuery); | 37 XmlNode node = SelectSingleNode(element, xpathQuery); |
36 return (node is XmlElement) ? (XmlElement) node : null; | 38 return (node is XmlElement) ? (XmlElement)node : null; |
37 } | 39 } |
38 | 40 |
39 public static XmlNamespaceManager GetNamespaceManager() | 41 public static XmlNamespaceManager GetNamespaceManager() |
40 { | 42 { |
41 if (nsManager == null) | 43 if (nsManager == null) |
42 { | 44 { |
43 nsManager = new XmlNamespaceManager(new NameTable()); | 45 nsManager = new XmlNamespaceManager(new NameTable()); |
44 nsManager.AddNamespace("core", NS_BASE + "core"); | 46 nsManager.AddNamespace("core", NS_BASE + "core"); |
45 nsManager.AddNamespace("cat", NS_BASE + "cats"); | 47 nsManager.AddNamespace("cat", NS_BASE + "cats"); |
46 nsManager.AddNamespace("race", NS_BASE + "race"); | 48 nsManager.AddNamespace("race", NS_BASE + "race"); |
47 nsManager.AddNamespace("system", NS_BASE + "system"); | 49 nsManager.AddNamespace("system", NS_BASE + "system"); |
48 nsManager.AddNamespace("army", NS_BASE + "army"); | 50 nsManager.AddNamespace("army", NS_BASE + "army"); |
49 } | 51 } |
50 | 52 |
51 return nsManager; | 53 return nsManager; |
52 } | 54 } |
53 | 55 |
54 /// <summary> | 56 /// <summary> |
55 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema. | 57 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema. |
56 /// </summary> | 58 /// </summary> |
57 /// <returns> | 59 /// <returns> |
58 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema | 60 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema |
59 /// </returns> | 61 /// </returns> |
60 public static XmlReaderSettings GetReaderSettings() | 62 public static XmlReaderSettings GetReaderSettings() |
61 { | 63 { |
62 if (settings == null) | 64 if (settings == null) |
63 { | 65 { |
64 settings = new XmlReaderSettings(); | 66 settings = new XmlReaderSettings(); |
65 settings.ValidationType = ValidationType.Schema; | 67 settings.ValidationType = ValidationType.Schema; |
66 //settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; | 68 //settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
67 settings.ProhibitDtd = true; | 69 settings.ProhibitDtd = true; |
68 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); | 70 settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventMethod); |
69 XmlSchemaSet cache = new XmlSchemaSet(); | 71 XmlSchemaSet cache = new XmlSchemaSet(); |
70 string path = Path.Combine(IBBoard.Constants.ExecutablePath, "schemas"); | 72 string path = "IBBoard.WarFoundry.schemas."; |
71 AddSchemaToCache(cache, NS_BASE + "core", Path.Combine(path, "warfoundry-core.xsd")); | 73 Assembly assm = Assembly.GetExecutingAssembly(); |
72 AddSchemaToCache(cache, NS_BASE + "cats", Path.Combine(path, "warfoundry-cats.xsd")); | 74 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "core", assm, path + "warfoundry-core.xsd"); |
73 AddSchemaToCache(cache, NS_BASE + "race", Path.Combine(path, "race.xsd")); | 75 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "cats", assm, path + "warfoundry-cats.xsd"); |
74 AddSchemaToCache(cache, NS_BASE + "system", Path.Combine(path, "system.xsd")); | 76 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "race", assm, path + "race.xsd"); |
75 AddSchemaToCache(cache, NS_BASE + "army", Path.Combine(path, "army.xsd")); | 77 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "system", assm, path + "system.xsd"); |
76 settings.Schemas.Add(cache); | 78 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "army", assm, path + "army.xsd"); |
77 settings.Schemas.CompilationSettings.EnableUpaCheck = false; | 79 settings.Schemas.Add(cache); |
78 } | 80 settings.Schemas.CompilationSettings.EnableUpaCheck = false; |
81 } | |
79 | 82 |
80 return settings; | 83 return settings; |
81 } | 84 } |
82 | 85 |
83 private static void ValidationEventMethod(object sender, ValidationEventArgs e) | 86 private static void ValidationEventMethod(object sender, ValidationEventArgs e) |
84 { | 87 { |
85 if (e.Severity == XmlSeverityType.Error) | 88 if (e.Severity == XmlSeverityType.Error) |
86 { | 89 { |
87 throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); | 90 throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception); |
88 } | 91 } else |
89 else | 92 { |
90 { | 93 //TODO: Fire some kind of warning event |
91 //TODO: Fire some kind of warning event | 94 } |
92 } | 95 } |
93 } | |
94 | 96 |
95 private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation) | 97 public static XmlDocument CreateXmlDocumentFromStream(Stream stream) |
96 { | 98 { |
97 try | 99 XmlDocument doc = new XmlDocument(); |
98 { | 100 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); |
99 cache.Add(xmlNamespace, schemaLocation); | |
100 } | |
101 catch (IOException) | |
102 { | |
103 //TODO: Warn on schema failure | |
104 } | |
105 catch (XmlSchemaException) | |
106 { | |
107 //TODO: Warn on schema failure | |
108 } | |
109 catch (XmlException) | |
110 { | |
111 //TODO: Warn on schema failure | |
112 } | |
113 } | |
114 | |
115 public static XmlDocument CreateXmlDocumentFromStream(Stream stream) | |
116 { | |
117 XmlDocument doc = new XmlDocument(); | |
118 XmlReader reader = XmlReader.Create(stream, GetReaderSettings()); | |
119 | 101 |
120 try | 102 try |
121 { | 103 { |
122 doc.Load(reader); | 104 doc.Load(reader); |
123 } | 105 } |
124 //Don't catch XMLSchemaExceptions - let them get thrown out | 106 //Don't catch XMLSchemaExceptions - let them get thrown out |
125 finally | 107 finally |
126 { | 108 { |
127 reader.Close(); | 109 reader.Close(); |
128 } | 110 } |
129 | 111 |
130 return doc; | 112 return doc; |
113 } | |
114 | |
115 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) | |
116 { | |
117 bool canLoad = true; | |
118 | |
119 if (obj.IsFullyLoaded) | |
120 { | |
121 canLoad = false; | |
122 } else if (obj.IsLoading) | |
123 { | |
124 canLoad = false; | |
125 } | |
126 | |
127 return canLoad; | |
128 } | |
131 } | 129 } |
132 | |
133 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj) | |
134 { | |
135 bool canLoad = true; | |
136 | |
137 if (obj.IsFullyLoaded) | |
138 { | |
139 canLoad = false; | |
140 } | |
141 else if (obj.IsLoading) | |
142 { | |
143 canLoad = false; | |
144 } | |
145 | |
146 return canLoad; | |
147 } | |
148 } | |
149 } | 130 } |