-
Notifications
You must be signed in to change notification settings - Fork 2
/
UsfmDataProvider.cs
157 lines (137 loc) · 5.28 KB
/
UsfmDataProvider.cs
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
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Xml;
using System.Xml.XPath;
using Paranext.DataProvider.JsonUtils;
using Paranext.DataProvider.MessageHandlers;
using Paranext.DataProvider.MessageTransports;
using Paranext.DataProvider.ParatextUtils;
using Paratext.Data;
using SIL.Scripture;
namespace Paranext.DataProvider.NetworkObjects;
internal class UsfmDataProvider : DataProvider
{
private readonly string _collectionName;
private ScrText? _scrText;
public UsfmDataProvider(PapiClient papiClient, string dataFolderPath, string collectionName)
: base("usfm", papiClient)
{
_collectionName = collectionName;
ParatextGlobals.Initialize(dataFolderPath);
}
protected override Task StartDataProvider()
{
_scrText = ScrTextCollection.Find(_collectionName);
return Task.CompletedTask;
}
protected override ResponseToRequest HandleRequest(string functionName, JsonArray args)
{
if (_scrText == null)
{
Console.Error.WriteLine("StartDataProvider must be called first");
return ResponseToRequest.Failed("Data provider must be started first");
}
try
{
return functionName switch
{
"getBookNames" => GetBookNames(),
"getChapter" => GetChapter(args[0]!.ToJsonString()),
"getChapterUsx" => GetChapterUsx(args[0]!.ToJsonString()),
"setChapterUsx" => SetChapterUsx(args[0]!.ToJsonString(), args[1]!.ToString()),
"getVerse" => GetVerse(args[0]!.ToJsonString()),
_ => ResponseToRequest.Failed($"Unexpected function: {functionName}")
};
}
catch (Exception e)
{
Console.Error.WriteLine(e.ToString());
return ResponseToRequest.Failed(e.Message);
}
}
private static ResponseToRequest GetBookNames()
{
return ResponseToRequest.Succeeded(JsonSerializer.Serialize(Canon.AllBookIds));
}
private ResponseToRequest GetChapter(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(_scrText!.GetText(verseRef, true, true))
: ResponseToRequest.Failed(errorMsg);
}
private ResponseToRequest GetChapterUsx(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(GetUsx(verseRef))
: ResponseToRequest.Failed(errorMsg);
}
private ResponseToRequest SetChapterUsx(string argVref, string argNewUsx)
{
return VerseRefConverter.TryCreateVerseRef(argVref, out var verseRef, out string errorMsg)
? SetUsx(verseRef, argNewUsx)
: ResponseToRequest.Failed(errorMsg);
}
private ResponseToRequest GetVerse(string args)
{
return VerseRefConverter.TryCreateVerseRef(args, out var verseRef, out string errorMsg)
? ResponseToRequest.Succeeded(_scrText!.GetVerseText(verseRef))
: ResponseToRequest.Failed(errorMsg);
}
public string GetUsx(VerseRef vref)
{
XmlDocument usx = GetUsxForChapter(vref.BookNum, vref.ChapterNum);
string contents = usx.OuterXml ?? string.Empty;
return contents;
}
public ResponseToRequest SetUsx(VerseRef vref, string newUsx)
{
try
{
XmlDocument doc = new() { PreserveWhitespace = true };
doc.LoadXml(newUsx);
if (doc.FirstChild?.Name != "usx")
return ResponseToRequest.Failed("Invalid USX");
UsxFragmenter.FindFragments(
_scrText!.ScrStylesheet(vref.BookNum),
doc.CreateNavigator(),
XPathExpression.Compile("*[false()]"),
out string usfm
);
usfm = UsfmToken.NormalizeUsfm(_scrText, vref.BookNum, usfm);
_scrText.PutText(vref.BookNum, vref.ChapterNum, false, usfm, null);
SendDataUpdateEvent("*");
}
catch (Exception e)
{
return ResponseToRequest.Failed(e.Message);
}
return ResponseToRequest.Succeeded();
}
private XmlDocument GetUsxForChapter(int bookNum, int chapterNum)
{
return ConvertUsfmToUsx(GetUsfmForChapter(bookNum, chapterNum), bookNum);
}
/// <summary>
/// Converts usfm to usx, but does not annotate
/// </summary>
private XmlDocument ConvertUsfmToUsx(string usfm, int bookNum)
{
ScrStylesheet scrStylesheet = _scrText!.ScrStylesheet(bookNum);
// Tokenize usfm
List<UsfmToken> tokens = UsfmToken.Tokenize(scrStylesheet, usfm ?? string.Empty, true);
XmlDocument doc = new XmlDocument();
using (XmlWriter xmlw = doc.CreateNavigator()!.AppendChild())
{
// Convert to XML
UsfmToUsx.ConvertToXmlWriter(scrStylesheet, tokens, xmlw, false);
xmlw.Flush();
}
return doc;
}
private string GetUsfmForChapter(int bookNum, int chapterNum)
{
VerseRef vref = new(bookNum, chapterNum, 0, _scrText!.Settings.Versification);
ScrText projectToUse = _scrText!.GetJoinedText(bookNum);
return projectToUse.GetText(vref, true, true);
}
}