-
Notifications
You must be signed in to change notification settings - Fork 1
/
MXLIFFWriter.cs
479 lines (386 loc) · 18.8 KB
/
MXLIFFWriter.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
using Sdl.Core.Globalization;
using Sdl.FileTypeSupport.Framework.BilingualApi;
using Sdl.FileTypeSupport.Framework.Core.Utilities.NativeApi;
using Sdl.FileTypeSupport.Framework.NativeApi;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace Leo.FileTypeSupport.MXLIFF
{
internal class MXLIFFWriter : AbstractBilingualFileTypeComponent, IBilingualWriter, INativeOutputSettingsAware
{
private INativeOutputFileProperties nativeFileProperties;
private XmlNamespaceManager nsmgr;
private IPersistentFileConversionProperties originalFileProperties;
private XmlDocument targetFile;
private MXLIFFTextExtractor textExtractor;
private Dictionary<string, string> users = new Dictionary<string, string>();
private int workflowLevel = 0;
public void Complete()
{
}
public void Dispose()
{
// Don't need to dispose of anything
}
public void FileComplete()
{
using (XmlTextWriter wr = new XmlTextWriter(nativeFileProperties.OutputFilePath, Encoding.UTF8))
{
wr.Formatting = Formatting.None;
targetFile.Save(wr);
targetFile = null;
}
}
public void GetProposedOutputFileInfo(IPersistentFileConversionProperties fileProperties, IOutputFileInfo proposedFileInfo)
{
originalFileProperties = fileProperties;
}
public void Initialize(IDocumentProperties documentInfo)
{
textExtractor = new MXLIFFTextExtractor();
}
public void ProcessParagraphUnit(IParagraphUnit paragraphUnit)
{
string id = paragraphUnit.Properties.Contexts.Contexts[0].GetMetaData("ID");
XmlNode xmlUnit = targetFile.SelectSingleNode("//x:trans-unit[@id='" + id + "']", nsmgr);
CreateParagraphUnit(paragraphUnit, xmlUnit);
}
public void SetFileProperties(IFileProperties fileInfo)
{
targetFile = new XmlDocument();
targetFile.PreserveWhitespace = false;
targetFile.Load(originalFileProperties.OriginalFilePath);
nsmgr = new XmlNamespaceManager(targetFile.NameTable);
nsmgr.AddNamespace("x", "urn:oasis:names:tc:xliff:document:1.2");
nsmgr.AddNamespace("m", "http://www.memsource.com/mxlf/2.0");
var level = targetFile.DocumentElement.Attributes["m:level"];
if (level != null)
{
workflowLevel = Int32.Parse(level.Value);
}
// Acquire users
var memsourceUsers = targetFile.SelectNodes("//m:user", nsmgr);
if (memsourceUsers != null)
{
foreach (XmlElement user in memsourceUsers)
{
var id = user.Attributes["id"]?.Value;
var username = user.Attributes["username"]?.Value;
if (!string.IsNullOrWhiteSpace(username) && !string.IsNullOrWhiteSpace(id) && users.ContainsKey(username))
{
users.Add(username, id);
}
}
}
}
public void SetOutputProperties(INativeOutputFileProperties properties)
{
nativeFileProperties = properties;
}
private static void UpdateConfirmedAttribute(XmlNode transUnit, ISegmentPair segmentPair)
{
if (segmentPair.Target != null && segmentPair.Target.Properties != null)
{
switch (segmentPair.Target.Properties.ConfirmationLevel)
{
case ConfirmationLevel.Unspecified:
transUnit.Attributes["m:confirmed"].Value = "0";
break;
case ConfirmationLevel.Draft:
transUnit.Attributes["m:confirmed"].Value = "0";
break;
case ConfirmationLevel.Translated:
transUnit.Attributes["m:confirmed"].Value = "1";
break;
case ConfirmationLevel.RejectedTranslation:
transUnit.Attributes["m:confirmed"].Value = "0";
break;
case ConfirmationLevel.ApprovedTranslation:
transUnit.Attributes["m:confirmed"].Value = "1";
break;
case ConfirmationLevel.RejectedSignOff:
transUnit.Attributes["m:confirmed"].Value = "0";
break;
case ConfirmationLevel.ApprovedSignOff:
transUnit.Attributes["m:confirmed"].Value = "1";
break;
default:
break;
}
}
}
private void AddComments(XmlNode xmlUnit, List<IComment> comments)
{
var text = string.Empty;
var comment = comments.First();
// We concatenate all comment text if there are multiple
foreach (var c in comments)
{
text += c.Text + " ";
}
var createdat = targetFile.CreateAttribute("created-at");
var createdby = targetFile.CreateAttribute("created-by");
var modifiedat = targetFile.CreateAttribute("modified-at");
modifiedat.Value = "0";
var modifiedby = targetFile.CreateAttribute("modified-by");
var resolved = targetFile.CreateAttribute("resolved");
resolved.Value = "false";
string commentDate = this.GetCommentDate();
// Convert DateTime to Unix timestamp in milliseconds
long milliseconds = (long)(TimeZoneInfo.ConvertTimeToUtc(comment.Date) - new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
createdat.Value = milliseconds.ToString();
// Try to find the user id by author name
// If it fails, just leave it blank
if (users.ContainsKey(comment.Author))
{
createdby.Value = users[comment.Author].ToString();
}
else
{
createdby.Value = "";
}
xmlUnit.Attributes.Append(createdat);
xmlUnit.Attributes.Append(createdby);
xmlUnit.Attributes.Append(modifiedat);
xmlUnit.Attributes.Append(modifiedby);
xmlUnit.Attributes.Append(resolved);
xmlUnit.InnerText = text;
}
private XmlNode CreateNewNode(XmlNode parent, XmlNode transUnit)
{
XmlDocument segDoc = new XmlDocument();
string nodeContent = parent.OuterXml;
segDoc.LoadXml(nodeContent);
XmlNode tuNode = segDoc.CreateNode(XmlNodeType.Element, "trans-unit", nsmgr.LookupNamespace("x"));
XmlNode importNode = parent.OwnerDocument.ImportNode(tuNode, true);
parent.InsertAfter(importNode, transUnit);
return importNode;
}
private void CreateParagraphUnit(IParagraphUnit paragraphUnit, XmlNode transUnit)
{
if (transUnit == null)
{
return;
}
if (paragraphUnit.SegmentPairs.Count() == 1)
{
UpdateSegment(transUnit, paragraphUnit.SegmentPairs.First());
}
else if (paragraphUnit.SegmentPairs.Count() > 1)
{
UpdateSegment(transUnit, paragraphUnit.SegmentPairs.First());
string id = paragraphUnit.Properties.Contexts.Contexts[0].GetMetaData("ID");
transUnit.Attributes["id"].Value = UpdateTopId(transUnit.Attributes["id"].Value);
var topId = transUnit.Attributes["id"].Value;
int count = (int)char.GetNumericValue(topId.Last()) + 1;
// Iterate all segment pairs
foreach (ISegmentPair segmentPair in paragraphUnit.SegmentPairs.Skip(1).Reverse())
{
XmlNode parent = transUnit.ParentNode;
XmlNode newNode = CreateNewNode(parent, transUnit);
AddAttributes(transUnit, newNode);
AddElements(transUnit, newNode);
newNode.Attributes["id"].Value = UpdateId(topId, count);
UpdateSegment(newNode, segmentPair);
count++;
}
}
}
private string UpdateTopId(string value)
{
if (Regex.IsMatch(value, @".*?(:\d+?:\d+)"))
{
return value;
}
else
{
return value + ":0";
}
}
private string UpdateId(string id, int count)
{
return id.Remove(id.Length - 1) + count;
}
private void AddElements(XmlNode transUnit, XmlNode newNode)
{
XmlDocument segDoc = new XmlDocument();
string nodeContent = transUnit.OuterXml;
segDoc.LoadXml(nodeContent);
XmlNode source = segDoc.CreateNode(XmlNodeType.Element, "source", nsmgr.LookupNamespace("x"));
XmlNode target = segDoc.CreateNode(XmlNodeType.Element, "target", nsmgr.LookupNamespace("x"));
XmlNode sourceNode = transUnit.OwnerDocument.ImportNode(source, true);
XmlNode targetNode = transUnit.OwnerDocument.ImportNode(target, true);
newNode.AppendChild(sourceNode);
newNode.AppendChild(targetNode);
// alt-trans
XmlNode altTrans = segDoc.CreateNode(XmlNodeType.Element, "alt-trans", nsmgr.LookupNamespace("x"));
XmlNode altTransNode = transUnit.OwnerDocument.ImportNode(altTrans, true);
altTransNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("origin"));
altTransNode.Attributes["origin"].Value = "machine-trans";
altTransNode.AppendChild(targetNode.Clone());
newNode.AppendChild(altTransNode);
// m:tunit-metadata
// m:tunit-target-metadata
XmlNode meta1 = segDoc.CreateNode(XmlNodeType.Element, "m:tunit-metadata", nsmgr.LookupNamespace("m"));
XmlNode meta2 = segDoc.CreateNode(XmlNodeType.Element, "m:tunit-target-metadata", nsmgr.LookupNamespace("m"));
XmlNode m1 = transUnit.OwnerDocument.ImportNode(meta1, true);
XmlNode m2 = transUnit.OwnerDocument.ImportNode(meta2, true);
newNode.AppendChild(m1);
newNode.AppendChild(m2);
// m:editing-stats
XmlNode mstats = segDoc.CreateNode(XmlNodeType.Element, "m:editing-stats", nsmgr.LookupNamespace("m"));
XmlNode thinkingtime = segDoc.CreateNode(XmlNodeType.Element, "m:thinking-time", nsmgr.LookupNamespace("m"));
thinkingtime.InnerText = "0";
XmlNode editingtime = segDoc.CreateNode(XmlNodeType.Element, "m:editing-time", nsmgr.LookupNamespace("m"));
editingtime.InnerText = "0";
mstats.AppendChild(thinkingtime);
mstats.AppendChild(editingtime);
XmlNode mstatsNode = transUnit.OwnerDocument.ImportNode(mstats, true);
newNode.AppendChild(mstatsNode);
}
private void AddAttributes(XmlNode transUnit, XmlNode newNode)
{
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("id"));
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:para-id", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:para-id"].Value = transUnit.Attributes["m:para-id"].Value;
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("xml:space"));
newNode.Attributes["xml:space"].Value = "preserve";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:trans-origin", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:trans-origin"].Value = "null";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:score", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:score"].Value = "0";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:gross-score", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:gross-score"].Value = "0";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:confirmed", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:confirmed"].Value = "0";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:locked", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:locked"].Value = "false";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:level-edited", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:level-edited"].Value = "true";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:created-by", nsmgr.LookupNamespace("m")));
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:created-at", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:created-at"].Value = "0";
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:modified-by", nsmgr.LookupNamespace("m")));
newNode.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:modified-at", nsmgr.LookupNamespace("m")));
newNode.Attributes["m:modified-at"].Value = "0";
}
private void FillSegment(ISegmentPair segmentPair, XmlNode node, bool source)
{
ISegment seg;
if (source)
{
seg = segmentPair.Source;
}
else
{
seg = segmentPair.Target;
}
node.InnerText = textExtractor.GetPlainText(seg);
}
private string GetCommentDate()
{
string day;
string month;
if (DateTime.UtcNow.Month.ToString().Length == 1)
month = "0" + DateTime.UtcNow.Month;
else
month = "0" + DateTime.UtcNow.Month;
if (DateTime.UtcNow.Day.ToString().Length == 1)
day = "0" + DateTime.UtcNow.Day;
else
day = "0" + DateTime.UtcNow.Day;
return DateTime.UtcNow.Year + month + day + "T" +
DateTime.UtcNow.Hour + DateTime.UtcNow.Minute +
DateTime.UtcNow.Second + "Z";
}
private void UpdateSegment(XmlNode transUnit, ISegmentPair segmentPair)
{
Byte matchPercent = 0;
XmlNode source = transUnit.SelectSingleNode("x:source", nsmgr);
if (source != null)
{
FillSegment(segmentPair, source, true);
}
if (segmentPair.Properties.TranslationOrigin != null)
{
matchPercent = segmentPair.Properties.TranslationOrigin.MatchPercent;
}
if (transUnit.SelectSingleNode("x:target", nsmgr) == null)
{
XmlDocument segDoc = new XmlDocument();
string nodeContent = transUnit.OuterXml;
segDoc.LoadXml(nodeContent);
XmlNode trgNode = segDoc.CreateNode(XmlNodeType.Element, "target", nsmgr.LookupNamespace("x"));
trgNode.InnerText = "";
XmlNode importNode = transUnit.OwnerDocument.ImportNode(trgNode, true);
transUnit.AppendChild(importNode);
XmlNode target = transUnit.SelectSingleNode("x:target", nsmgr);
FillSegment(segmentPair, target, false);
}
else
{
XmlNode target = transUnit.SelectSingleNode("x:target", nsmgr);
FillSegment(segmentPair, target, false);
}
// Add comments (if applicable)
var comments = textExtractor.GetSegmentComment(segmentPair.Target);
if (comments.Count > 0 && transUnit.SelectSingleNode("m:comment", nsmgr) == null)
{
XmlElement commentElement = targetFile.CreateElement("m:comment", nsmgr.LookupNamespace("m"));
var tunitMetaData = transUnit.SelectSingleNode("m:tunit-metadata", nsmgr);
if (tunitMetaData != null)
{
transUnit.InsertBefore(commentElement, tunitMetaData);
AddComments(transUnit.SelectSingleNode("m:comment", nsmgr), comments);
}
}
// Update score value
var dbl = matchPercent / 100.0;
if (transUnit.Attributes["m:score"] != null && transUnit.Attributes["m:gross-score"] != null
&& transUnit.Attributes["m:trans-origin"] != null)
{
transUnit.Attributes["m:score"].Value = dbl.ToString(CultureInfo.InvariantCulture);
transUnit.Attributes["m:gross-score"].Value = dbl.ToString(CultureInfo.InvariantCulture);
}
else
{
transUnit.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:score"));
transUnit.Attributes.Append(transUnit.OwnerDocument.CreateAttribute("m:gross-score"));
transUnit.Attributes["m:score"].Value = dbl.ToString(CultureInfo.InvariantCulture);
transUnit.Attributes["m:gross-score"].Value = dbl.ToString(CultureInfo.InvariantCulture);
}
var transOrigin = segmentPair?.Target?.Properties?.TranslationOrigin;
if (transOrigin != null)
{
var memSourceTransOrigin = transUnit.Attributes["m:trans-origin"];
if (memSourceTransOrigin != null)
{
if (transOrigin.OriginType == DefaultTranslationOrigin.TranslationMemory)
{
memSourceTransOrigin.Value = DefaultTranslationOrigin.TranslationMemory;
}
else if (transOrigin.OriginType == DefaultTranslationOrigin.MachineTranslation)
{
memSourceTransOrigin.Value = DefaultTranslationOrigin.MachineTranslation;
}
}
}
// Update m:locked
if (transUnit.Attributes["m:locked"] != null)
{
var isLocked = segmentPair.Target?.Properties?.IsLocked.ToString();
transUnit.Attributes["m:locked"].Value = isLocked != null ? isLocked.ToLower() : "false";
}
// Update m:confirmed
if (transUnit.Attributes["m:confirmed"] != null)
{
UpdateConfirmedAttribute(transUnit, segmentPair);
}
}
}
}