-
Notifications
You must be signed in to change notification settings - Fork 536
Expand file tree
/
Copy pathComparisonUnitAtom.cs
More file actions
215 lines (177 loc) · 7.04 KB
/
ComparisonUnitAtom.cs
File metadata and controls
215 lines (177 loc) · 7.04 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
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using DocumentFormat.OpenXml.Packaging;
namespace OpenXmlPowerTools
{
public class ComparisonUnitAtom : ComparisonUnit
{
public ComparisonUnitAtom(
XElement contentElement,
XElement[] ancestorElements,
OpenXmlPart part,
WmlComparerSettings settings)
{
ContentElement = contentElement;
AncestorElements = ancestorElements;
Part = part;
RevTrackElement = GetRevisionTrackingElementFromAncestors(contentElement, AncestorElements);
if (RevTrackElement == null)
{
CorrelationStatus = CorrelationStatus.Equal;
}
else
{
if (RevTrackElement.Name == W.del)
{
CorrelationStatus = CorrelationStatus.Deleted;
}
else if (RevTrackElement.Name == W.ins)
{
CorrelationStatus = CorrelationStatus.Inserted;
}
}
var sha1Hash = (string) contentElement.Attribute(PtOpenXml.SHA1Hash);
if (sha1Hash != null)
{
SHA1Hash = sha1Hash;
}
else
{
string shaHashString = GetSha1HashStringForElement(ContentElement, settings);
SHA1Hash = WmlComparerUtil.SHA1HashStringForUTF8String(shaHashString);
}
}
// AncestorElements are kept in order from the body to the leaf, because this is the order in which we need to access in order
// to reassemble the document. However, in many places in the code, it is necessary to find the nearest ancestor, i.e. cell
// so it is necessary to reverse the order when looking for it, i.e. look from the leaf back to the body element.
public XElement[] AncestorElements { get; }
public XElement ContentElement { get; }
public XElement RevTrackElement { get; }
public string[] AncestorUnids { get; set; }
public ComparisonUnitAtom ComparisonUnitAtomBefore { get; set; }
public XElement ContentElementBefore { get; set; }
public OpenXmlPart Part { get; }
private static string GetSha1HashStringForElement(XElement contentElement, WmlComparerSettings settings)
{
string text = contentElement.Value;
if (settings.CaseInsensitive)
{
text = text.ToUpper(settings.CultureInfo);
}
return contentElement.Name.LocalName + text;
}
private static XElement GetRevisionTrackingElementFromAncestors(
XElement contentElement,
IEnumerable<XElement> ancestors)
{
return contentElement.Name == W.pPr
? contentElement.Elements(W.rPr).Elements().FirstOrDefault(e => e.Name == W.del || e.Name == W.ins)
: ancestors.FirstOrDefault(a => a.Name == W.del || a.Name == W.ins);
}
public override string ToString()
{
return ToString(0);
}
public override string ToString(int indent)
{
const int xNamePad = 16;
string indentString = "".PadRight(indent);
var sb = new StringBuilder();
sb.Append(indentString);
var correlationStatus = "";
if (CorrelationStatus != CorrelationStatus.Nil)
{
correlationStatus = $"[{CorrelationStatus.ToString().PadRight(8)}] ";
}
if (ContentElement.Name == W.t || ContentElement.Name == W.delText)
{
sb.AppendFormat(
"Atom {0}: {1} {2} SHA1:{3} ",
PadLocalName(xNamePad, this),
ContentElement.Value,
correlationStatus,
SHA1Hash.Substring(0, 8));
AppendAncestorsDump(sb, this);
}
else
{
sb.AppendFormat(
"Atom {0}: {1} SHA1:{2} ",
PadLocalName(xNamePad, this),
correlationStatus,
SHA1Hash.Substring(0, 8));
AppendAncestorsDump(sb, this);
}
return sb.ToString();
}
public string ToStringAncestorUnids()
{
return ToStringAncestorUnids(0);
}
private string ToStringAncestorUnids(int indent)
{
const int xNamePad = 16;
string indentString = "".PadRight(indent);
var sb = new StringBuilder();
sb.Append(indentString);
var correlationStatus = "";
if (CorrelationStatus != CorrelationStatus.Nil)
{
correlationStatus = $"[{CorrelationStatus.ToString().PadRight(8)}] ";
}
if (ContentElement.Name == W.t || ContentElement.Name == W.delText)
{
sb.AppendFormat(
"Atom {0}: {1} {2} SHA1:{3} ",
PadLocalName(xNamePad, this),
ContentElement.Value,
correlationStatus,
SHA1Hash.Substring(0, 8));
AppendAncestorsUnidsDump(sb, this);
}
else
{
sb.AppendFormat(
"Atom {0}: {1} SHA1:{2} ",
PadLocalName(xNamePad, this),
correlationStatus,
SHA1Hash.Substring(0, 8));
AppendAncestorsUnidsDump(sb, this);
}
return sb.ToString();
}
private static string PadLocalName(int xNamePad, ComparisonUnitAtom item)
{
return (item.ContentElement.Name.LocalName + " ").PadRight(xNamePad, '-') + " ";
}
private static void AppendAncestorsDump(StringBuilder sb, ComparisonUnitAtom sr)
{
string s = sr
.AncestorElements.Select(p => p.Name.LocalName + GetUnid(p) + "/")
.StringConcatenate()
.TrimEnd('/');
sb.Append("Ancestors:" + s);
}
private static void AppendAncestorsUnidsDump(StringBuilder sb, ComparisonUnitAtom sr)
{
var zipped = sr.AncestorElements.Zip(sr.AncestorUnids, (a, u) => new
{
AncestorElement = a,
AncestorUnid = u
});
string s = zipped
.Select(p => p.AncestorElement.Name.LocalName + "[" + p.AncestorUnid.Substring(0, 8) + "]/")
.StringConcatenate().TrimEnd('/');
sb.Append("Ancestors:" + s);
}
private static string GetUnid(XElement p)
{
var unid = (string) p.Attribute(PtOpenXml.Unid);
return unid == null ? "" : "[" + unid.Substring(0, 8) + "]";
}
}
}