Mercurial > repos > IBBoard
annotate Lang/TranslationXmlLoader.cs @ 80:a23c07137fa4
Re #32: Add staged loading of translations
* Move from passing entire document to XmlTranslationSet to just passing XmlNodeList of translation nodes
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 10 Apr 2010 19:05:26 +0000 |
parents | a70d89de1435 |
children | 90b9a3fe3c18 |
rev | line source |
---|---|
69 | 1 // This file (TranslationXmlLoader.cs) is a part of the IBBoard project and is copyright 2010 IBBoard |
2 // | |
3 // 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. | |
4 | |
5 using System; | |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
6 using System.IO; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
7 using System.Xml; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
8 using System.Xml.Schema; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
9 using System.Collections.Generic; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
10 using IBBoard.IO; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
11 using IBBoard.Xml; |
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; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
21 private string schemaLocation; |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
22 private TranslationXmlExtractor extractor; |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
23 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
24 public TranslationXmlLoader(string schemaLocation) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
25 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
26 this.schemaLocation = schemaLocation; |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
27 extractor = new TranslationXmlExtractor(); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
28 } |
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 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
31 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
|
32 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
33 FileInfo file = new FileInfo(path); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
34 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
35 if (!file.Exists) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
36 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
37 throw new TranslationLoadException("Translation file "+file.FullName+" did not exist"); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
38 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
39 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
40 XmlDocument doc = LoadTranslationDocument(file); |
79
a70d89de1435
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
41 XmlTranslationSet translations = new XmlTranslationSet(extractor.GetLanguageOfDocument(doc)); |
a70d89de1435
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
78
diff
changeset
|
42 translations.SetParentLanguage(extractor.GetParentLanguageOfDocument(doc)); |
80
a23c07137fa4
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
79
diff
changeset
|
43 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
|
44 return translations; |
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 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
47 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
|
48 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
49 XmlDocument doc = new XmlDocument(); |
78
da339d10c5fe
Re #32: Add staged loading of translations
IBBoard <dev@ibboard.co.uk>
parents:
70
diff
changeset
|
50 XmlReader valReader = XmlReader.Create(file.OpenRead(), GetReaderSettings()); |
70
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
51 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
52 try |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
53 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
54 doc.Load(valReader); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
55 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
56 catch (DirectoryNotFoundException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
57 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
58 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
|
59 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
60 catch (XmlSchemaException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
61 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
62 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
|
63 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
64 catch (XmlException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
65 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
66 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
|
67 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
68 finally |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
69 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
70 valReader.Close(); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
71 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
72 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
73 return doc; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
74 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
75 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
76 /// <summary> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
77 /// 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
|
78 /// </summary> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
79 /// <returns> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
80 /// 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
|
81 /// </returns> |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
82 private XmlReaderSettings GetReaderSettings() |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
83 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
84 if (settings == null) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
85 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
86 try |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
87 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
88 settings = new XmlReaderSettings(); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
89 settings.ValidationType = ValidationType.Schema; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
90 settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
91 settings.ValidationEventHandler+= new ValidationEventHandler(ValidationEventMethod); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
92 XmlSchemaSet cache = new XmlSchemaSet(); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
93 cache.Add("http://ibboard.co.uk/translation", schemaLocation); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
94 settings.Schemas.Add(cache); |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
95 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
96 catch (DirectoryNotFoundException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
97 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
98 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
|
99 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
100 catch (XmlSchemaException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
101 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
102 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
|
103 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
104 catch (XmlException ex) |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
105 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
106 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
|
107 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
108 } |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
109 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
110 return settings; |
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 |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
113 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
|
114 { |
753be4b6c3b0
Re #31: Break out Translations for language to own class
IBBoard <dev@ibboard.co.uk>
parents:
69
diff
changeset
|
115 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
|
116 } |
69 | 117 } |
118 } |