Mercurial > repos > IBBoard
annotate Lang/TranslationXmlLoader.cs @ 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 |
rev | line source |
---|---|
69 | 1 // This file (TranslationXmlLoader.cs) is a part of the IBBoard project and is copyright 2010 IBBoard |
2 // | |
87
90b9a3fe3c18
* Correct licensing comments at top of files
IBBoard <dev@ibboard.co.uk>
parents:
80
diff
changeset
|
3 // The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license. |
69 | 4 using System; |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
5 using System.IO; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
6 using System.Xml; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
7 using System.Xml.Schema; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
8 using System.Collections.Generic; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
9 using IBBoard.IO; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
10 using IBBoard.Xml; |
113
e32b5ccda410
* Update Translations to use built-in schema
IBBoard <dev@ibboard.co.uk>
parents:
87
diff
changeset
|
11 using System.Reflection; |
69 | 12 |
13 namespace IBBoard.Lang | |
14 { | |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
15 /// <summary> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
16 /// A simple loader of translations from XML files |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
17 /// </summary> |
69 | 18 public class TranslationXmlLoader |
19 { | |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
20 private XmlReaderSettings settings; |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
21 private TranslationXmlExtractor extractor; |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
22 |
113
e32b5ccda410
* Update Translations to use built-in schema
IBBoard <dev@ibboard.co.uk>
parents:
87
diff
changeset
|
23 public TranslationXmlLoader() |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
24 { |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
25 extractor = new TranslationXmlExtractor(); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
26 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
27 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
28 public AbstractTranslationSet LoadTranslations(string path) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
29 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
30 FileInfo file = new FileInfo(path); |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
31 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
32 if (!file.Exists) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
33 { |
113
e32b5ccda410
* Update Translations to use built-in schema
IBBoard <dev@ibboard.co.uk>
parents:
87
diff
changeset
|
34 throw new TranslationLoadException("Translation file " + file.FullName + " did not exist"); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
35 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
36 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
37 XmlDocument doc = LoadTranslationDocument(file); |
79
a70d89de1435
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
38 XmlTranslationSet translations = new XmlTranslationSet(extractor.GetLanguageOfDocument(doc)); |
a70d89de1435
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
39 translations.SetParentLanguage(extractor.GetParentLanguageOfDocument(doc)); |
80
a23c07137fa4
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
79
diff
changeset
|
40 translations.SetTranslationNodes(extractor.GetTranslationNodes(doc)); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
41 return translations; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
42 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
43 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
44 private XmlDocument LoadTranslationDocument(FileInfo file) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
45 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
46 XmlDocument doc = new XmlDocument(); |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
47 XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings()); |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
48 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
49 try |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
50 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
51 doc.Load(valReader); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
52 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 catch (DirectoryNotFoundException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
54 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
55 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
56 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
57 catch (XmlSchemaException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
58 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
59 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
60 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
61 catch (XmlException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
62 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
63 throw new TranslationLoadException("Problem reading data for translation: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
64 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
65 finally |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
66 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
67 valReader.Close(); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
68 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
69 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
70 return doc; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
71 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
72 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
73 /// <summary> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
74 /// Lazy-getter for XML reader settings. May throw a <see cref="TranslationLoadException"/> if there is a problem with the translation schema. |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
75 /// </summary> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
76 /// <returns> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
77 /// A <see cref="XmlReaderSettings"/> with the default values for validating the translation document against the translation schema |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
78 /// </returns> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
79 private XmlReaderSettings GetReaderSettings() |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
80 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
81 if (settings == null) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
82 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
83 try |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
84 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
85 settings = new XmlReaderSettings(); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
86 settings.ValidationType = ValidationType.Schema; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
87 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
113
e32b5ccda410
* Update Translations to use built-in schema
IBBoard <dev@ibboard.co.uk>
parents:
87
diff
changeset
|
88 settings.ValidationEventHandler += new ValidationEventHandler(ValidationEventMethod); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
89 XmlSchemaSet cache = new XmlSchemaSet(); |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
90 XmlTools.AddSchemaToSetFromResource(cache, "http://ibboard.co.uk/translation", Assembly.GetExecutingAssembly(), "IBBoard.schemas.translation.xsd"); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
91 settings.Schemas.Add(cache); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
92 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
93 catch (DirectoryNotFoundException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
94 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
95 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
96 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
97 catch (XmlSchemaException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
98 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
99 throw new TranslationLoadException("Problem validating schema for translation: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
100 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
101 catch (XmlException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
102 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
103 throw new TranslationLoadException("Problem reading data for schema: " + ex.Message, ex); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
104 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
105 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
106 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
107 return settings; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
108 } |
115
de0ed24eb961
* Reimplement 7ca4acc659bbdd/IBBoard without the excess noise
IBBoard <dev@ibboard.co.uk>
parents:
113
diff
changeset
|
109 |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
110 private void ValidationEventMethod(object sender, ValidationEventArgs e) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
111 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
112 throw new TranslationLoadException("Problem validating schema for translation: " + e.Exception.Message, e.Exception); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
113 } |
69 | 114 } |
115 } |