diff API/Factories/Xml/WarFoundryXmlFactoryUtils.cs @ 337:3c4a6403a88c

* Fix capitalisation so that new files are in the namespace no-open-ticket
author IBBoard <dev@ibboard.co.uk>
date Sun, 03 Apr 2011 18:50:32 +0000
parents
children 6da9db4a9c23
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/API/Factories/Xml/WarFoundryXmlFactoryUtils.cs	Sun Apr 03 18:50:32 2011 +0000
@@ -0,0 +1,149 @@
+//  This file (WarFoundryXmlFactoryUtils.cs) is a part of the IBBoard.WarFoundry.API project and is copyright 2009 IBBoard
+// 
+// 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.
+
+using System;
+using System.IO;
+using System.Xml;
+using System.Xml.Schema;
+using IBBoard.WarFoundry.API.Objects;
+using IBBoard.IO;
+
+namespace IBBoard.WarFoundry.API.Factories.Xml
+{
+	/// <summary>
+	/// A collection of useful utility methods for loading WarFoundry data from XML files
+	/// </summary>
+	public class WarFoundryXmlFactoryUtils
+	{
+		public static readonly string NS_BASE = "http://ibboard.co.uk/warfoundry/";
+		private static XmlReaderSettings settings;
+		private static XmlNamespaceManager nsManager;
+		
+		public static XmlNodeList SelectNodes(XmlNode element, string xpathQuery)
+		{
+			return element.SelectNodes(xpathQuery, GetNamespaceManager());
+		}
+		
+		public static XmlNode SelectSingleNode(XmlNode element, string xpathQuery)
+		{
+			return element.SelectSingleNode(xpathQuery, GetNamespaceManager());
+		}
+		
+		public static XmlElement SelectSingleElement(XmlNode element, string xpathQuery)
+		{
+			XmlNode node = SelectSingleNode(element, xpathQuery);
+			return (node is XmlElement) ? (XmlElement) node : null;
+		}
+				
+		public static XmlNamespaceManager GetNamespaceManager()
+		{
+			if (nsManager == null)
+			{
+				nsManager = new XmlNamespaceManager(new NameTable());
+				nsManager.AddNamespace("core", NS_BASE + "core");
+				nsManager.AddNamespace("cat", NS_BASE + "cats");
+				nsManager.AddNamespace("race", NS_BASE + "race");
+				nsManager.AddNamespace("system", NS_BASE + "system");
+				nsManager.AddNamespace("army", NS_BASE + "army");
+			}
+			
+			return nsManager;
+		}
+		
+		/// <summary>
+		/// Lazy-getter for XML reader settings. May throw a <see cref="InvalidDataException"/> if there is a problem with the translation schema.
+		/// </summary>
+		/// <returns>
+		/// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
+		/// </returns>
+		public static XmlReaderSettings GetReaderSettings()
+		{
+			if (settings == null)
+			{
+				settings = new XmlReaderSettings();
+				settings.ValidationType = ValidationType.Schema;
+				//settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
+				settings.ProhibitDtd = true;
+				settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
+				XmlSchemaSet cache = new XmlSchemaSet();
+				string path =  IBBoard.Constants.ExecutablePath + "/schemas/";
+				AddSchemaToCache(cache, NS_BASE + "core", path + "warfoundry-core.xsd");
+				AddSchemaToCache(cache, NS_BASE + "cats", path + "warfoundry-cats.xsd");
+				AddSchemaToCache(cache, NS_BASE + "race", path + "race.xsd");
+				AddSchemaToCache(cache, NS_BASE + "system", path + "system.xsd");
+				AddSchemaToCache(cache, NS_BASE + "army", path + "army.xsd");
+				settings.Schemas.Add(cache);
+				settings.Schemas.CompilationSettings.EnableUpaCheck = false;
+			}
+			
+			return settings;
+		}
+		
+		private static void ValidationEventMethod(object sender, ValidationEventArgs e)
+		{
+			if (e.Severity == XmlSeverityType.Error)
+			{
+				throw new InvalidFileException("Problem validating against schema for WarFoundry data: " + e.Message, e.Exception);
+			}
+			else
+			{
+				//TODO: Fire some kind of warning event
+			}
+		}
+		
+		private static void AddSchemaToCache(XmlSchemaSet cache, string xmlNamespace, string schemaLocation)
+		{
+			try
+			{
+				cache.Add(xmlNamespace, schemaLocation);
+			}
+			catch (IOException ex)
+			{
+				//TODO: Warn on schema failure
+			}
+			catch (XmlSchemaException ex)
+			{
+				//TODO: Warn on schema failure
+			}
+			catch (XmlException ex)
+			{
+				//TODO: Warn on schema failure
+			}
+		}
+		
+		public static XmlDocument CreateXmlDocumentFromStream(Stream stream)
+		{
+			XmlDocument doc = new XmlDocument();
+			XmlReader reader = XmlReader.Create(stream, GetReaderSettings());
+			
+			try
+			{
+				doc.Load(reader);
+			}
+			//Don't catch XMLSchemaExceptions - let them get thrown out
+			finally
+			{
+				reader.Close();
+			}
+
+			return doc;
+		}
+		
+		public static bool CanCompleteLoading(IWarFoundryStagedLoadObject obj)
+		{
+			bool canLoad = true;			
+			
+			if (obj.IsFullyLoaded)
+			{
+				canLoad = false;
+			}
+			else if (obj.IsLoading)
+			{
+				canLoad = false;
+			}
+			
+			return canLoad;
+		}
+	}
+}