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 diff
     1.1 --- a/IBBoard.csproj	Sun Jun 24 15:48:25 2012 +0100
     1.2 +++ b/IBBoard.csproj	Tue Jun 26 20:06:50 2012 +0100
     1.3 @@ -47,9 +47,9 @@
     1.4      <DefineConstants>DEBUG;TRACE</DefineConstants>
     1.5      <DocumentationFile>
     1.6      </DocumentationFile>
     1.7 -    <DebugSymbols>true</DebugSymbols>
     1.8 +    <DebugSymbols>True</DebugSymbols>
     1.9      <FileAlignment>4096</FileAlignment>
    1.10 -    <Optimize>false</Optimize>
    1.11 +    <Optimize>False</Optimize>
    1.12      <RegisterForComInterop>false</RegisterForComInterop>
    1.13      <RemoveIntegerChecks>false</RemoveIntegerChecks>
    1.14      <WarningLevel>4</WarningLevel>
    1.15 @@ -65,7 +65,7 @@
    1.16      <DocumentationFile>
    1.17      </DocumentationFile>
    1.18      <FileAlignment>4096</FileAlignment>
    1.19 -    <Optimize>true</Optimize>
    1.20 +    <Optimize>True</Optimize>
    1.21      <RegisterForComInterop>false</RegisterForComInterop>
    1.22      <RemoveIntegerChecks>false</RemoveIntegerChecks>
    1.23      <WarningLevel>4</WarningLevel>
    1.24 @@ -157,6 +157,7 @@
    1.25      <Compile Include="EqualityChecker.cs" />
    1.26      <Compile Include="IO\StreamUtil.cs" />
    1.27      <Compile Include="Collections\Collections.cs" />
    1.28 +    <Compile Include="Xml\XmlResourceResolver.cs" />
    1.29    </ItemGroup>
    1.30    <ItemGroup>
    1.31      <BootstrapperPackage Include="Microsoft.Net.Client.3.5">
    1.32 @@ -215,7 +216,7 @@
    1.33    </ProjectExtensions>
    1.34    <ItemGroup>
    1.35      <EmbeddedResource Include="schemas\translation.xsd">
    1.36 -      <Gettext-ScanForTranslations>false</Gettext-ScanForTranslations>
    1.37 +      <Gettext-ScanForTranslations>False</Gettext-ScanForTranslations>
    1.38      </EmbeddedResource>
    1.39    </ItemGroup>
    1.40  </Project>
    1.41 \ No newline at end of file
     2.1 --- a/Lang/TranslationXmlLoader.cs	Sun Jun 24 15:48:25 2012 +0100
     2.2 +++ b/Lang/TranslationXmlLoader.cs	Tue Jun 26 20:06:50 2012 +0100
     2.3 @@ -19,33 +19,33 @@
     2.4  	{
     2.5  		private XmlReaderSettings settings;
     2.6  		private TranslationXmlExtractor extractor;
     2.7 -		
     2.8 +
     2.9  		public TranslationXmlLoader()
    2.10  		{
    2.11  			extractor = new TranslationXmlExtractor();
    2.12  		}
    2.13 -		
    2.14 +
    2.15  		public AbstractTranslationSet LoadTranslations(string path)
    2.16  		{
    2.17  			FileInfo file = new FileInfo(path);
    2.18 -			
    2.19 +
    2.20  			if (!file.Exists)
    2.21  			{
    2.22  				throw new TranslationLoadException("Translation file " + file.FullName + " did not exist");
    2.23  			}				
    2.24 -				
    2.25 +
    2.26  			XmlDocument doc = LoadTranslationDocument(file);
    2.27  			XmlTranslationSet translations = new XmlTranslationSet(extractor.GetLanguageOfDocument(doc));
    2.28  			translations.SetParentLanguage(extractor.GetParentLanguageOfDocument(doc));
    2.29  			translations.SetTranslationNodes(extractor.GetTranslationNodes(doc));
    2.30  			return translations;
    2.31  		}
    2.32 -		
    2.33 +
    2.34  		private XmlDocument LoadTranslationDocument(FileInfo file)
    2.35  		{
    2.36  			XmlDocument doc = new XmlDocument();			
    2.37  			XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings());
    2.38 -			
    2.39 +
    2.40  			try
    2.41  			{
    2.42  				doc.Load(valReader);
    2.43 @@ -66,10 +66,10 @@
    2.44  			{
    2.45  				valReader.Close();
    2.46  			}
    2.47 -			
    2.48 +
    2.49  			return doc;
    2.50  		}
    2.51 -		
    2.52 +
    2.53  		/// <summary>
    2.54  		/// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema.
    2.55  		/// </summary>
    2.56 @@ -87,7 +87,7 @@
    2.57  					settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
    2.58  					settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventMethod);
    2.59  					XmlSchemaSet cache = new XmlSchemaSet();
    2.60 -					cache.Add("http://ibboard.co.uk/translation", new XmlTextReader(Assembly.GetExecutingAssembly().GetManifestResourceStream("IBBoard.schemas.translation.xsd")));
    2.61 +					XmlTools.AddSchemaToSetFromResource(cache, "http://ibboard.co.uk/translation", Assembly.GetExecutingAssembly(), "IBBoard.schemas.translation.xsd");
    2.62  					settings.Schemas.Add(cache);
    2.63  				}
    2.64  				catch (DirectoryNotFoundException ex)
    2.65 @@ -103,10 +103,10 @@
    2.66  					throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex);
    2.67  				}
    2.68  			}
    2.69 -			
    2.70 +
    2.71  			return settings;
    2.72  		}
    2.73 -		
    2.74 +
    2.75  		private void ValidationEventMethod(object sender, ValidationEventArgs e)
    2.76  		{
    2.77  			throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception);
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/Xml/XmlResourceResolver.cs	Tue Jun 26 20:06:50 2012 +0100
     3.3 @@ -0,0 +1,40 @@
     3.4 +using System;
     3.5 +using System.Xml;
     3.6 +using System.Reflection;
     3.7 +using System.Collections.Generic;
     3.8 +using System.IO;
     3.9 +using System.Net;
    3.10 +
    3.11 +namespace IBBoard.Xml
    3.12 +{
    3.13 +	public class XmlResourceResolver : XmlUrlResolver
    3.14 +	{
    3.15 +		private Assembly assm;
    3.16 +		private Dictionary<string, string> relativeToUriMap = new Dictionary<string, string>();
    3.17 +
    3.18 +		public XmlResourceResolver(Assembly assembly)
    3.19 +		{
    3.20 +			assm = assembly;
    3.21 +		}
    3.22 +
    3.23 +		public void AddMapping(string relativeURI, string resourceName)
    3.24 +		{
    3.25 +			relativeToUriMap[relativeURI] = resourceName;
    3.26 +		}
    3.27 +
    3.28 +		public override object GetEntity(Uri absoluteUri, string role, Type ofObjectToReturn)
    3.29 +		{
    3.30 +			string absoluteUriString = absoluteUri.ToString();
    3.31 +
    3.32 +			if (relativeToUriMap.ContainsKey(absoluteUriString))
    3.33 +			{
    3.34 +				string file = relativeToUriMap[absoluteUriString];
    3.35 +				Stream stream = assm.GetManifestResourceStream(file);
    3.36 +				return stream;
    3.37 +			}
    3.38 +
    3.39 +			return base.GetEntity(absoluteUri, role, ofObjectToReturn);
    3.40 +		}
    3.41 +	}
    3.42 +}
    3.43 +
     4.1 --- a/Xml/XmlTools.cs	Sun Jun 24 15:48:25 2012 +0100
     4.2 +++ b/Xml/XmlTools.cs	Tue Jun 26 20:06:50 2012 +0100
     4.3 @@ -2,11 +2,13 @@
     4.4  // 
     4.5  //  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.
     4.6  // 
     4.7 -
     4.8  using System;
     4.9  using System.Globalization;
    4.10  using System.Text.RegularExpressions;
    4.11  using System.Xml;
    4.12 +using System.Xml.Schema;
    4.13 +using System.Reflection;
    4.14 +using System.IO;
    4.15  
    4.16  namespace IBBoard.Xml
    4.17  {
    4.18 @@ -37,7 +39,7 @@
    4.19  			{
    4.20  				return bool.Parse(elem.GetAttribute(attributeName));
    4.21  			}
    4.22 -			catch(FormatException)
    4.23 +			catch (FormatException)
    4.24  			{
    4.25  				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id")));
    4.26  			}
    4.27 @@ -61,7 +63,7 @@
    4.28  			{
    4.29  				return int.Parse(elem.GetAttribute(attributeName));
    4.30  			}
    4.31 -			catch(FormatException)
    4.32 +			catch (FormatException)
    4.33  			{
    4.34  				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
    4.35  			}
    4.36 @@ -94,7 +96,7 @@
    4.37  				{
    4.38  					return double.Parse(attribValue, GetNumberFormatInfo());
    4.39  				}
    4.40 -				catch(FormatException)
    4.41 +				catch (FormatException)
    4.42  				{
    4.43  					throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id")));
    4.44  				}
    4.45 @@ -110,13 +112,13 @@
    4.46  		
    4.47  		public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName, bool ignoreCase)
    4.48  		{
    4.49 -			string attribValue = elem.GetAttribute (attributeName);
    4.50 +			string attribValue = elem.GetAttribute(attributeName);
    4.51  			
    4.52  			try
    4.53  			{
    4.54  				return EnumTools.ParseEnum<T>(attribValue, ignoreCase);
    4.55  			}
    4.56 -			catch(ArgumentException)
    4.57 +			catch (ArgumentException)
    4.58  			{
    4.59  				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));
    4.60  			}
    4.61 @@ -188,5 +190,11 @@
    4.62  			
    4.63  			return valid;
    4.64  		}
    4.65 +
    4.66 +		public static void AddSchemaToSetFromResource(XmlSchemaSet schemaSet, string targetNamespace, Assembly assm, string id)
    4.67 +		{
    4.68 +			Stream resStream = assm.GetManifestResourceStream(id);
    4.69 +			schemaSet.Add(targetNamespace, new XmlTextReader(resStream));
    4.70 +		}
    4.71  	}
    4.72  }