view Lang/TranslationXmlLoader.cs @ 78:da339d10c5fe

Re #32: Add staged loading of translations * Add "XML Translation Set" that lazy loads translations from XML * Extract some methods out to an XML extractor * Fix header in abstract translation set * Extract method in Translation class to add a translation set (allows combined file loading and in-code creation)
author IBBoard <dev@ibboard.co.uk>
date Fri, 09 Apr 2010 19:35:18 +0000
parents 753be4b6c3b0
children a70d89de1435
line wrap: on
line source

// This file (TranslationXmlLoader.cs) is a part of the IBBoard project and is copyright 2010 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 System.Collections.Generic;
using IBBoard.IO;
using IBBoard.Xml;

namespace IBBoard.Lang
{
	/// <summary>
	/// A simple loader of translations from XML files
	/// </summary>
	public class TranslationXmlLoader
	{
		private XmlReaderSettings settings;
		private string schemaLocation;
		private TranslationXmlExtractor extractor;
		
		public TranslationXmlLoader(string schemaLocation)
		{
			this.schemaLocation = schemaLocation;
			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);
			ModifiableTranslationSet translations = new ModifiableTranslationSet(extractor.GetLanguageOfDocument(doc));
			LoadTranslationsFromDocument(doc, translations);
			return translations;
		}
		
		private XmlDocument LoadTranslationDocument(FileInfo file)
		{
			XmlDocument doc = new XmlDocument();			
			XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings());
			
			try
			{
				doc.Load(valReader);
			}
			catch (DirectoryNotFoundException ex)
			{
				throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex);
			}
			catch (XmlSchemaException ex)
			{
				throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex);
			}
			catch (XmlException ex)
			{
				throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex);
			}
			finally
			{
				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>
		/// <returns>
		/// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema
		/// </returns>
		private XmlReaderSettings GetReaderSettings()
		{
			if (settings == null)
			{
				try
				{
					settings = new XmlReaderSettings();
					settings.ValidationType = ValidationType.Schema;
					settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
					settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod);
					XmlSchemaSet cache = new XmlSchemaSet();
					cache.Add("http://ibboard.co.uk/translation", schemaLocation);
					settings.Schemas.Add(cache);
				}
				catch (DirectoryNotFoundException ex)
				{
					throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex);
				}
				catch (XmlSchemaException ex)
				{
					throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex);
				}
				catch (XmlException ex)
				{
					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);
		}
		
		private void LoadTranslationsFromDocument(XmlDocument doc, ModifiableTranslationSet translations)
		{
			Dictionary<string, string> translationStrings = extractor.ExtractTranslationsFromDocument(doc);
			
			foreach (KeyValuePair<string, string> translation in translationStrings)
			{
				translations.SetTranslation(translation.Key, translation.Value);
			}
		}
	}
}