-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathDictionaryConfigurationHandlerStrategy.cs
More file actions
94 lines (84 loc) · 3.08 KB
/
DictionaryConfigurationHandlerStrategy.cs
File metadata and controls
94 lines (84 loc) · 3.08 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
// --------------------------------------------------------------------------------------------
// Copyright (C) 2015-2017 SIL International. All rights reserved.
//
// Distributable under the terms of the MIT License.
// --------------------------------------------------------------------------------------------
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using Chorus.FileTypeHandlers;
using Chorus.merge;
using Chorus.merge.xml.generic;
using Chorus.VcsDrivers.Mercurial;
using LibFLExBridgeChorusPlugin.Infrastructure;
using SIL.IO;
namespace LibFLExBridgeChorusPlugin.Handling.ConfigLayout
{
[Export(typeof(IFieldWorksFileHandler))]
internal sealed class DictionaryConfigurationHandlerStrategy : IFieldWorksFileHandler
{
private string _xsdPathname;
private string GetXsdPathname(string configFilePathname)
{
if (_xsdPathname != null)
return _xsdPathname;
var innerPath = Path.Combine("Temp", FlexBridgeConstants.DictConfigSchemaFilename);
var parentDir = Path.GetDirectoryName(configFilePathname);
while (!string.IsNullOrEmpty(parentDir))
{
if (File.Exists(_xsdPathname = Path.Combine(parentDir, innerPath)))
return _xsdPathname;
parentDir = Path.GetDirectoryName(parentDir);
}
return _xsdPathname = string.Empty;
}
public bool CanValidateFile(string pathToFile)
{
return PathHelper.CheckValidPathname(pathToFile, Extension) && File.Exists(GetXsdPathname(pathToFile));
}
public string ValidateFile(string pathToFile)
{
var schemas = new XmlSchemaSet();
using(var reader = XmlReader.Create(GetXsdPathname(pathToFile)))
{
try
{
schemas.Add("", reader);
string result = null;
XDocument.Load(pathToFile).Validate(schemas, (sender, args) => result = args.Message);
return result;
}
catch (XmlException e)
{
return e.Message;
}
}
}
public IChangePresenter GetChangePresenter(IChangeReport report, HgRepository repository)
{
return FieldWorksChangePresenter.GetCommonChangePresenter(report, repository);
}
public IEnumerable<IChangeReport> Find2WayDifferences(FileInRevision parent, FileInRevision child, HgRepository repository)
{
return Xml2WayDiffService.ReportDifferences(repository, parent, child, null, FlexBridgeConstants.ConfigurationItem, FlexBridgeConstants.GuidStr);
}
public void Do3WayMerge(MetadataCache mdc, MergeOrder mergeOrder)
{
var merger = new XmlMerger(mergeOrder.MergeSituation) { EventListener = mergeOrder.EventListener };
var rootStrategy = ElementStrategy.CreateSingletonElement();
rootStrategy.IsAtomic = true;
merger.MergeStrategies.SetStrategy("DictionaryConfiguration", rootStrategy);
var mergeResults = merger.MergeFiles(mergeOrder.pathToOurs, mergeOrder.pathToTheirs, mergeOrder.pathToCommonAncestor);
// Write merged data
File.WriteAllText(mergeOrder.pathToOurs, mergeResults.MergedNode.OuterXml, Encoding.UTF8);
}
public string Extension
{
get { return FlexBridgeConstants.fwdictconfig; }
}
}
}