# HG changeset patch # User IBBoard # Date 1236285271 0 # Node ID c8d74202182a4f7d43a98c62713983c1075263b7 # Parent 855b4903b256d4af6392c9a59b8ce073bb7fa755 Closes #14 - Throw specific exceptions from translations * Add TranslationLoadException * Make Translation class catch various exceptions and throw meaninful TranslationLoadExceptions * Note potential exceptions in documentation of Translation diff -r 855b4903b256 -r c8d74202182a IBBoard.csproj --- a/IBBoard.csproj Sat Feb 21 14:28:07 2009 +0000 +++ b/IBBoard.csproj Thu Mar 05 20:34:31 2009 +0000 @@ -126,6 +126,7 @@ + diff -r 855b4903b256 -r c8d74202182a Lang/Translation.cs --- a/Lang/Translation.cs Sat Feb 21 14:28:07 2009 +0000 +++ b/Lang/Translation.cs Thu Mar 05 20:34:31 2009 +0000 @@ -30,7 +30,9 @@ private static Dictionary translationsDefault; /// - /// Initialises the translations for the language specified and the default translations so that the Translation class can be used + /// Initialises the translations for the language specified and the default translations so that the Translation class can be used. + /// Throws a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can + /// still be called but no translation will be performed. /// /// /// The full path that the application is running from. Must contain the "translations" folder. @@ -59,7 +61,7 @@ } else { - throw new ArgumentException("Translation path must exist ("+translationPath+")"); + throw new TranslationLoadException("Translation path not found ("+translationPath+")"); } } @@ -71,9 +73,25 @@ settings.ValidationType = ValidationType.DTD; settings.ProhibitDtd = false; settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); - XmlReader valReader = XmlReader.Create(file.FullName, settings); - doc.Load(valReader); - valReader.Close(); + XmlReader valReader = XmlReader.Create(file.FullName, settings); + + try + { + doc.Load(valReader); + } + 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; } @@ -83,7 +101,7 @@ if (!file.Exists) { - throw new FileNotFoundException(language + "translation could not be found in "+translationDir.FullName); + throw new TranslationLoadException(language + ".translation could not be found in "+translationDir.FullName); } return file; @@ -118,7 +136,7 @@ } catch(Exception ex) { - throw new XmlParseException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message); + throw new TranslationLoadException("Error while parsing " + GetLanguageOfDocument(doc)+" translation: "+ex.Message, ex); } } @@ -134,7 +152,9 @@ } /// - /// Loads translations for a given language and sets them as the local language + /// Loads translations for a given language and sets them as the local language. + /// hrows a TranslationLoadException if a problem occurred while loading translations. If this occurs then the translation methods can + /// still be called but all translations will fall back to the default translation. /// /// /// The new local language to load @@ -151,7 +171,7 @@ private static void LoadTranslationForLanguage(string translationLanguage) { - checkInitialisation(); + CheckInitialisation(); if (translationLanguage != DEFAULT_LANGUAGE && translationLanguage != "" && translationLanguage != null) { @@ -221,7 +241,7 @@ /// public static string GetTranslation(string translationID, string defaultTranslation, params object[] replacements) { - checkInitialisation(); + CheckInitialisation(); string trans = GetTranslationFromTables(translationID); if (trans == null) @@ -271,7 +291,7 @@ return translation; } - private static void checkInitialisation() + private static void CheckInitialisation() { if (translationDir==null) { diff -r 855b4903b256 -r c8d74202182a Lang/TranslationLoadException.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Lang/TranslationLoadException.cs Thu Mar 05 20:34:31 2009 +0000 @@ -0,0 +1,24 @@ +// This file (TranslationLoadException.cs) is a part of the IBBoard project and is copyright 2009 IBBoard +// +// 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.IO; + +namespace IBBoard +{ + /// + /// A sub-class of Exceptions that is thrown when translations fail to load. + /// + public class TranslationLoadException : Exception + { + public TranslationLoadException(string message) : base(message) + { + } + + public TranslationLoadException(string message, Exception ex) : base(message, ex) + { + } + } +}