changeset 115:de0ed24eb961

* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise * Clean up code (and some automatic changes) * Use XML resources in TranslationXmlLoader
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Jun 2012 20:06:50 +0100
parents e32b5ccda410
children 07660ac09a5f
files IBBoard.csproj Lang/TranslationXmlLoader.cs Xml/XmlResourceResolver.cs Xml/XmlTools.cs
diffstat 4 files changed, 70 insertions(+), 21 deletions(-) [+]
line wrap: on
line diff
--- a/IBBoard.csproj	Sun Jun 24 15:48:25 2012 +0100
+++ b/IBBoard.csproj	Tue Jun 26 20:06:50 2012 +0100
@@ -47,9 +47,9 @@
     <DefineConstants>DEBUG;TRACE</DefineConstants>
     <DocumentationFile>
     </DocumentationFile>
-    <DebugSymbols>true</DebugSymbols>
+    <DebugSymbols>True</DebugSymbols>
     <FileAlignment>4096</FileAlignment>
-    <Optimize>false</Optimize>
+    <Optimize>False</Optimize>
     <RegisterForComInterop>false</RegisterForComInterop>
     <RemoveIntegerChecks>false</RemoveIntegerChecks>
     <WarningLevel>4</WarningLevel>
@@ -65,7 +65,7 @@
     <DocumentationFile>
     </DocumentationFile>
     <FileAlignment>4096</FileAlignment>
-    <Optimize>true</Optimize>
+    <Optimize>True</Optimize>
     <RegisterForComInterop>false</RegisterForComInterop>
     <RemoveIntegerChecks>false</RemoveIntegerChecks>
     <WarningLevel>4</WarningLevel>
@@ -157,6 +157,7 @@
     <Compile Include="EqualityChecker.cs" />
     <Compile Include="IO\StreamUtil.cs" />
     <Compile Include="Collections\Collections.cs" />
+    <Compile Include="Xml\XmlResourceResolver.cs" />
   </ItemGroup>
   <ItemGroup>
     <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
@@ -215,7 +216,7 @@
   </ProjectExtensions>
   <ItemGroup>
     <EmbeddedResource Include="schemas\translation.xsd">
-      <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations>
+      <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations>
     </EmbeddedResource>
   </ItemGroup>
 </Project>
\ No newline at end of file
--- a/Lang/TranslationXmlLoader.cs	Sun Jun 24 15:48:25 2012 +0100
+++ b/Lang/TranslationXmlLoader.cs	Tue Jun 26 20:06:50 2012 +0100
@@ -19,33 +19,33 @@
 	{
 		private XmlReaderSettings settings;
 		private TranslationXmlExtractor extractor;
-		
+
 		public TranslationXmlLoader()
 		{
 			extractor = new TranslationXmlExtractor();
 		}
-		
+
 		public AbstractTranslationSet LoadTranslations(string path)
 		{
 			FileInfo file = new FileInfo(path);
-			
+
 			if (!file.Exists)
 			{
 				throw new TranslationLoadException("Translation file " + file.FullName + " did not exist");
 			}				
-				
+
 			XmlDocument doc = LoadTranslationDocument(file);
 			XmlTranslationSet translations = new XmlTranslationSet(extractor.GetLanguageOfDocument(doc));
 			translations.SetParentLanguage(extractor.GetParentLanguageOfDocument(doc));
 			translations.SetTranslationNodes(extractor.GetTranslationNodes(doc));
 			return translations;
 		}
-		
+
 		private XmlDocument LoadTranslationDocument(FileInfo file)
 		{
 			XmlDocument doc = new XmlDocument();			
 			XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings());
-			
+
 			try
 			{
 				doc.Load(valReader);
@@ -66,10 +66,10 @@
 			{
 				valReader.Close();
 			}
-			
+
 			return doc;
 		}
-		
+
 		/// <summary>
 		/// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema.
 		/// </summary>
@@ -87,7 +87,7 @@
 					settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
 					settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventMethod);
 					XmlSchemaSet cache = new XmlSchemaSet();
-					cache.Add("http://ibboard.co.uk/translation", new XmlTextReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("IBBoard.schemas.translation.xsd")));
+					XmlTools.AddSchemaToSetFromResource(cache, "http://ibboard.co.uk/translation", Assembly.GetExecutingAssembly(), "IBBoard.schemas.translation.xsd");
 					settings.Schemas.Add(cache);
 				}
 				catch (DirectoryNotFoundException ex)
@@ -103,10 +103,10 @@
 					throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex);
 				}
 			}
-			
+
 			return settings;
 		}
-		
+
 		private void ValidationEventMethod(object sender, ValidationEventArgs e)
 		{
 			throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception);
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Xml/XmlResourceResolver.cs	Tue Jun 26 20:06:50 2012 +0100
@@ -0,0 +1,40 @@
+using System;
+using System.Xml;
+using System.Reflection;
+using System.Collections.Generic;
+using System.IO;
+using System.Net;
+
+namespace IBBoard.Xml
+{
+	public class XmlResourceResolver : XmlUrlResolver
+	{
+		private Assembly assm;
+		private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();
+
+		public XmlResourceResolver(Assembly assembly)
+		{
+			assm = assembly;
+		}
+
+		public void AddMapping(string relativeURI, string resourceName)
+		{
+			relativeToUriMap[relativeURI] = resourceName;
+		}
+
+		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
+		{
+			string absoluteUriString = absoluteUri.ToString();
+
+			if (relativeToUriMap.ContainsKey(absoluteUriString))
+			{
+				string file = relativeToUriMap[absoluteUriString];
+				Stream stream = assm.GetManifestResourceStream(file);
+				return stream;
+			}
+
+			return base.GetEntity(absoluteUri, role, ofObjectToReturn);
+		}
+	}
+}
+
--- a/Xml/XmlTools.cs	Sun Jun 24 15:48:25 2012 +0100
+++ b/Xml/XmlTools.cs	Tue Jun 26 20:06:50 2012 +0100
@@ -2,11 +2,13 @@
 // 
 //  The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license.
 // 
-
 using System;
 using System.Globalization;
 using System.Text.RegularExpressions;
 using System.Xml;
+using System.Xml.Schema;
+using System.Reflection;
+using System.IO;
 
 namespace IBBoard.Xml
 {
@@ -37,7 +39,7 @@
 			{
 				return bool.Parse(elem.GetAttribute(attributeName));
 			}
-			catch(FormatException)
+			catch (FormatException)
 			{
 				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id")));
 			}
@@ -61,7 +63,7 @@
 			{
 				return int.Parse(elem.GetAttribute(attributeName));
 			}
-			catch(FormatException)
+			catch (FormatException)
 			{
 				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
 			}
@@ -94,7 +96,7 @@
 				{
 					return double.Parse(attribValue, GetNumberFormatInfo());
 				}
-				catch(FormatException)
+				catch (FormatException)
 				{
 					throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
 				}
@@ -110,13 +112,13 @@
 		
 		public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName, bool ignoreCase)
 		{
-			string attribValue = elem.GetAttribute (attributeName);
+			string attribValue = elem.GetAttribute(attributeName);
 			
 			try
 			{
 				return EnumTools.ParseEnum<T>(attribValue, ignoreCase);
 			}
-			catch(ArgumentException)
+			catch (ArgumentException)
 			{
 				throw new FormatException(String.Format("Attribute '{0}' with value {1} for {2} with ID '{3}' was not a valid {4} enum", attributeName, attribValue, elem.Name, elem.GetAttribute("id"), typeof(T).Name));
 			}
@@ -188,5 +190,11 @@
 			
 			return valid;
 		}
+
+		public static void AddSchemaToSetFromResource(XmlSchemaSet schemaSet, string targetNamespace, Assembly assm, string id)
+		{
+			Stream resStream = assm.GetManifestResourceStream(id);
+			schemaSet.Add(targetNamespace, new XmlTextReader(resStream));
+		}
 	}
 }