view 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
line source
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 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.
5 using System;
6 using System.IO;
7 using System.Xml;
8 using System.Xml.Schema;
9 using IBBoard.WarFoundry.API.Objects;
10 using IBBoard.IO;
11 using IBBoard.Xml;
12 using System.Reflection;
14 namespace IBBoard.WarFoundry.API.Factories.Xml
15 {
16 /// <summary>
17 /// A collection of useful utility methods for loading WarFoundry data from XML files
18 /// </summary>
19 public class WarFoundryXmlFactoryUtils
20 {
21 public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/";
22 private static XmlReaderSettings settings;
23 private static XmlNamespaceManager nsManager;
25 public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
26 {
27 return element.SelectNodes(xpathQuery, GetNamespaceManager());
28 }
30 public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery)
31 {
32 return element.SelectSingleNode(xpathQuery, GetNamespaceManager());
33 }
35 public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery)
36 {
37 XmlNode node = SelectSingleNode(element, xpathQuery);
38 return (node is XmlElement) ? (XmlElement)node : null;
39 }
41 public static XmlNamespaceManager GetNamespaceManager()
42 {
43 if (nsManager == null)
44 {
45 nsManager = new XmlNamespaceManager(new NameTable());
46 nsManager.AddNamespace("core", NS_BASE + "core");
47 nsManager.AddNamespace("cat", NS_BASE + "cats");
48 nsManager.AddNamespace("race", NS_BASE + "race");
49 nsManager.AddNamespace("system", NS_BASE + "system");
50 nsManager.AddNamespace("army", NS_BASE + "army");
51 }
53 return nsManager;
54 }
56 /// <summary>
57 /// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
58 /// </summary>
59 /// <returns>
60 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
61 /// </returns>
62 public static XmlReaderSettings GetReaderSettings()
63 {
64 if (settings == null)
65 {
66 settings = new XmlReaderSettings();
67 settings.ValidationType = ValidationType.Schema;
68 //settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
69 settings.ProhibitDtd = true;
70 settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventMethod);
71 XmlSchemaSet cache = new XmlSchemaSet();
72 string path = "IBBoard.WarFoundry.schemas.";
73 Assembly assm = Assembly.GetExecutingAssembly();
74 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "core", assm, path + "warfoundry-core.xsd");
75 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "cats", assm, path + "warfoundry-cats.xsd");
76 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "race", assm, path + "race.xsd");
77 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "system", assm, path + "system.xsd");
78 XmlTools.AddSchemaToSetFromResource(cache, NS_BASE + "army", assm, path + "army.xsd");
79 settings.Schemas.Add(cache);
80 settings.Schemas.CompilationSettings.EnableUpaCheck = false;
81 }
83 return settings;
84 }
86 private static void ValidationEventMethod(object sender, ValidationEventArgs e)
87 {
88 if (e.Severity == XmlSeverityType.Error)
89 {
90 throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception);
91 } else
92 {
93 //TODO: Fire some kind of warning event
94 }
95 }
97 public static XmlDocument CreateXmlDocumentFromStream(Stream stream)
98 {
99 XmlDocument doc = new XmlDocument();
100 XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
102 try
103 {
104 doc.Load(reader);
105 }
106 //Don't catch XMLSchemaExceptions - let them get thrown out
107 finally
108 {
109 reader.Close();
110 }
112 return doc;
113 }
115 public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj)
116 {
117 bool canLoad = true;
119 if (obj.IsFullyLoaded)
120 {
121 canLoad = false;
122 } else if (obj.IsLoading)
123 {
124 canLoad = false;
125 }
127 return canLoad;
128 }
129 }
130 }