-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathParatextProjectSettingsParserBase.cs
More file actions
161 lines (145 loc) · 6.71 KB
/
ParatextProjectSettingsParserBase.cs
File metadata and controls
161 lines (145 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.IO;
using System.Text;
using System.Xml.Linq;
using SIL.Scripture;
namespace SIL.Machine.Corpora
{
public abstract class ParatextProjectSettingsParserBase
{
private readonly IParatextProjectFileHandler _paratextProjectFileHandler;
private readonly ParatextProjectSettings _parentParatextProjectSettings;
public ParatextProjectSettingsParserBase(
IParatextProjectFileHandler paratextProjectFileHandler,
ParatextProjectSettings parentParatextProjectSettings = null
)
{
_paratextProjectFileHandler = paratextProjectFileHandler;
_parentParatextProjectSettings = parentParatextProjectSettings;
}
public ParatextProjectSettings Parse()
{
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
string settingsFileName = "Settings.xml";
if (!_paratextProjectFileHandler.Exists(settingsFileName))
settingsFileName = _paratextProjectFileHandler.Find(".ssf");
if (string.IsNullOrEmpty(settingsFileName))
throw new InvalidOperationException("The project does not contain a settings file.");
XDocument settingsDoc;
using (Stream stream = _paratextProjectFileHandler.Open(settingsFileName))
{
settingsDoc = XDocument.Load(stream);
}
string guid = settingsDoc.Root.Element("Guid").Value;
string name = settingsDoc.Root.Element("Name").Value;
string fullName = settingsDoc.Root.Element("FullName").Value;
var encodingStr = (string)settingsDoc.Root.Element("Encoding") ?? "65001";
if (!int.TryParse(encodingStr, out int codePage))
{
throw new NotImplementedException(
$"The project uses a legacy encoding that requires TECKit, map file: {encodingStr}."
);
}
var encoding = Encoding.GetEncoding(codePage);
var scrVersType = (int?)settingsDoc.Root.Element("Versification") ?? (int)ScrVersType.English;
var versification = new ScrVers((ScrVersType)scrVersType);
if (_paratextProjectFileHandler.Exists("custom.vrs"))
{
string versName = ((ScrVersType)scrVersType).ToString() + "-" + guid;
if (Versification.Table.Implementation.Exists(versName))
{
versification = new ScrVers(versName);
}
else
{
using (var reader = new StreamReader(_paratextProjectFileHandler.Open("custom.vrs")))
{
versification = Versification.Table.Implementation.Load(
reader,
"custom.vrs",
versification,
versName
);
}
Versification.Table.Implementation.RemoveAllUnknownVersifications();
}
}
var stylesheetFileName = (string)settingsDoc.Root.Element("StyleSheet") ?? "usfm.sty";
if (!_paratextProjectFileHandler.Exists(stylesheetFileName) && stylesheetFileName != "usfm_sb.sty")
stylesheetFileName = "usfm.sty";
UsfmStylesheet stylesheet = _paratextProjectFileHandler.CreateStylesheet(stylesheetFileName);
string prefix = "";
string form = "41MAT";
string suffix = ".SFM";
XElement namingElem = settingsDoc.Root.Element("Naming");
if (namingElem != null)
{
var prePart = (string)namingElem.Attribute("PrePart");
if (!string.IsNullOrEmpty(prePart))
prefix = prePart;
var bookNameForm = (string)namingElem.Attribute("BookNameForm");
if (!string.IsNullOrEmpty(bookNameForm))
form = bookNameForm;
var postPart = (string)namingElem.Attribute("PostPart");
if (!string.IsNullOrEmpty(postPart))
suffix = postPart;
}
string biblicalTermsListSetting = settingsDoc.Root.Element("BiblicalTermsListSetting")?.Value;
if (biblicalTermsListSetting == null)
// Default to Major::BiblicalTerms.xml to mirror Paratext behavior
biblicalTermsListSetting = "Major::BiblicalTerms.xml";
string[] parts = biblicalTermsListSetting.Split(new[] { ':' }, 3);
if (parts.Length != 3)
{
throw new InvalidOperationException(
$"The BiblicalTermsListSetting element in Settings.xml in project {fullName}"
+ $" is not in the expected format (e.g., Major::BiblicalTerms.xml) but is {biblicalTermsListSetting}."
);
}
string languageCode = null;
string languageIsoCodeSetting = settingsDoc.Root.Element("LanguageIsoCode")?.Value;
if (languageIsoCodeSetting != null)
{
string[] languageIsoCodeSettingParts = languageIsoCodeSetting.Split(':');
if (languageIsoCodeSettingParts.Length > 0)
{
languageCode = languageIsoCodeSettingParts[0];
}
}
string translationInfoSetting = settingsDoc.Root.Element("TranslationInfo")?.Value;
string translationType = "Standard";
string parentName = null;
string parentGuid = null;
string[] translationInfoSettingParts = translationInfoSetting?.Split(':');
if (translationInfoSettingParts?.Length == 3)
{
translationType = translationInfoSettingParts[0];
parentName = translationInfoSettingParts[1] != string.Empty ? translationInfoSettingParts[1] : null;
parentGuid = translationInfoSettingParts[2] != string.Empty ? translationInfoSettingParts[2] : null;
}
var settings = new ParatextProjectSettings(
guid,
name,
fullName,
encoding,
versification,
stylesheet,
prefix,
form,
suffix,
parts[0],
parts[1],
parts[2],
languageCode,
translationType,
parentGuid,
parentName
);
if (_parentParatextProjectSettings != null && settings.HasParent)
{
settings.Parent = _parentParatextProjectSettings;
}
return settings;
}
}
}