-
Notifications
You must be signed in to change notification settings - Fork 202
/
XmlDiffPath.cs
356 lines (316 loc) · 13.1 KB
/
XmlDiffPath.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
// ---------------------------------------------------------------------------
// <copyright company="Microsoft Corporation" file="XmlDiffPath.cs">
// Copyright (c) Microsoft Corporation 2005
// </copyright>
// <project>
// XmlDiffView
// </project>
// <summary>
// Provide lists of nodes and attributes
// based on proprietary alphanumeric path statements.
// </summary>
// <history>
// [barryw] 03MAR15 Created
// </history>
// ---------------------------------------------------------------------------
namespace Microsoft.XmlDiffPatch {
#region Using directives
using System;
using System.Diagnostics;
#endregion
/// <summary>
/// Class to provide lists of nodes and attributes
/// based on proprietary alphanumeric path statements
/// </summary>
internal class XmlDiffPath {
#region Member variables section
private static char[] delimites = new
char[] {
'|', '-', '/'
};
#endregion
#region Methods section
/// <summary>
/// Gets the list of nodes or attributes below the position indicated
/// </summary>
/// <param name="rootNode">The root node for the xml data</param>
/// <param name="currentParentNode">The current node</param>
/// <param name="xmlDiffPathExpr">Proprietary path statement separator or character</param>
/// <returns>List of nodes or attributes</returns>
public static XmlDiffPathNodeList SelectNodes(
XmlDiffViewParentNode rootNode,
XmlDiffViewParentNode currentParentNode,
string xmlDiffPathExpr) {
switch (xmlDiffPathExpr[0]) {
case '/':
return SelectAbsoluteNodes(rootNode, xmlDiffPathExpr);
case '@':
if (xmlDiffPathExpr.Length < 2) {
OnInvalidExpression(xmlDiffPathExpr);
}
if (xmlDiffPathExpr[1] == '*') {
return SelectAllAttributes(
(XmlDiffViewElement)currentParentNode);
} else {
return SelectAttributes(
(XmlDiffViewElement)currentParentNode, xmlDiffPathExpr);
}
case '*':
if (xmlDiffPathExpr.Length == 1) {
return SelectAllChildren(currentParentNode);
} else {
OnInvalidExpression(xmlDiffPathExpr);
return null;
}
default:
int startPosition = 0;
return SelectChildNodes(
currentParentNode,
xmlDiffPathExpr,
startPosition);
}
}
/// <summary>
/// Gets a list of node objects corresponding to
/// the proprietary path reference provided.
/// </summary>
/// <param name="rootNode">The starting node</param>
/// <param name="path">Absolute path reference to node of interest</param>
/// <returns>list of node objects</returns>
private static XmlDiffPathNodeList SelectAbsoluteNodes(
XmlDiffViewParentNode rootNode,
string path) {
Debug.Assert(path[0] == '/');
int pos = 1;
XmlDiffViewNode node = rootNode;
for (; ; ) {
int startPos = pos;
int nodePos = ReadPosition(path, ref pos);
if (pos == path.Length || path[pos] == '/') {
if (node.FirstChildNode == null) {
OnNoMatchingNode(path);
}
XmlDiffViewParentNode parentNode = (XmlDiffViewParentNode)node;
if (nodePos <= 0 || nodePos > parentNode.
SourceChildNodesCount) {
OnNoMatchingNode(path);
}
node = parentNode.GetSourceChildNode(nodePos - 1);
if (pos == path.Length) {
XmlDiffPathNodeList list = new
XmlDiffPathSingleNodeList();
list.AddNode(node);
return list;
}
pos++;
} else {
if (path[pos] == '-' || path[pos] == '|') {
if (node.FirstChildNode == null) {
OnNoMatchingNode(path);
}
return SelectChildNodes(
((XmlDiffViewParentNode)node),
path,
startPos);
}
OnInvalidExpression(path);
}
}
}
/// <summary>
/// Gets a list of the attributes for the specifed node
/// and if applicable, its children.
/// </summary>
/// <param name="parentElement">The node which
/// contains the attributes</param>
/// <returns>List of attributes</returns>
private static XmlDiffPathNodeList SelectAllAttributes(
XmlDiffViewElement parentElement) {
if (parentElement.Attributes == null) {
OnNoMatchingNode("@*");
return null;
} else if (parentElement.Attributes.NextSibling == null) {
XmlDiffPathNodeList nodeList = new XmlDiffPathSingleNodeList();
nodeList.AddNode(parentElement.Attributes);
return nodeList;
} else {
XmlDiffPathNodeList nodeList = new XmlDiffPathMultiNodeList();
XmlDiffViewAttribute curAttr = parentElement.Attributes;
while (curAttr != null) {
nodeList.AddNode(curAttr);
}
return nodeList;
}
}
/// <summary>
/// Gets a list of attribute objects based on the location
/// specified.
/// </summary>
/// <param name="parentElement">Node at which to start the path search</param>
/// <param name="path">Proprietary alphanumeric path statement</param>
/// <returns>list of attribute objects</returns>
private static XmlDiffPathNodeList SelectAttributes(
XmlDiffViewElement parentElement,
string path) {
Debug.Assert(path[0] == '@');
int pos = 1;
XmlDiffPathNodeList nodeList = null;
for (; ; ) {
string name = ReadAttrName(path, ref pos);
if (nodeList == null) {
if (pos == path.Length) {
nodeList = new XmlDiffPathSingleNodeList();
} else {
nodeList = new XmlDiffPathMultiNodeList();
}
}
XmlDiffViewAttribute attr = parentElement.GetAttribute(name);
if (attr == null) {
OnNoMatchingNode(path);
}
nodeList.AddNode(attr);
if (pos == path.Length) {
break;
} else if (path[pos] == '|') {
pos++;
if (path[pos] != '@') {
OnInvalidExpression(path);
}
pos++;
} else {
OnInvalidExpression(path);
}
}
return nodeList;
}
/// <summary>
/// Gets a list of all node objects at and below the location
/// specified.
/// </summary>
/// <param name="parentNode">Node at which to start</param>
/// <returns>list of node objects</returns>
private static XmlDiffPathNodeList SelectAllChildren(
XmlDiffViewParentNode parentNode) {
if (parentNode.ChildNodes == null) {
OnNoMatchingNode("*");
return null;
} else if (parentNode.ChildNodes.NextSibling == null) {
XmlDiffPathNodeList nodeList = new XmlDiffPathSingleNodeList();
nodeList.AddNode(parentNode.ChildNodes);
return nodeList;
} else {
XmlDiffPathNodeList nodeList = new XmlDiffPathMultiNodeList();
XmlDiffViewNode childNode = parentNode.ChildNodes;
while (childNode != null) {
nodeList.AddNode(childNode);
childNode = childNode.NextSibling;
}
return nodeList;
}
}
/// <summary>
/// Gets the list of child nodes below the position indicated
/// </summary>
/// <param name="parentNode">The current node</param>
/// <param name="path">Proprietary path statement</param>
/// <param name="startPos">Position in the path statement
/// at which to start collecting node objects.</param>
/// <returns>list of child nodes</returns>
/// <returns>List of nodes or attributes</returns>
private static XmlDiffPathNodeList SelectChildNodes(
XmlDiffViewParentNode parentNode,
string path,
int startPos) {
int pos = startPos;
XmlDiffPathNodeList nodeList = null;
for (; ; ) {
int nodePos = ReadPosition(path, ref pos);
if (pos == path.Length) {
nodeList = new XmlDiffPathSingleNodeList();
} else {
nodeList = new XmlDiffPathMultiNodeList();
}
if (nodePos <= 0 || nodePos > parentNode.SourceChildNodesCount) {
OnNoMatchingNode(path);
}
nodeList.AddNode(parentNode.GetSourceChildNode(nodePos - 1));
if (pos == path.Length) {
break;
} else if (path[pos] == '|') {
pos++;
} else if (path[pos] == '-') {
pos++;
int endNodePos = ReadPosition(path, ref pos);
if (endNodePos <= 0 || endNodePos > parentNode.SourceChildNodesCount) {
OnNoMatchingNode(path);
}
while (nodePos < endNodePos) {
nodePos++;
nodeList.AddNode(parentNode.GetSourceChildNode(nodePos - 1));
}
if (pos == path.Length) {
break;
} else if (path[pos] == '|') {
pos++;
} else {
OnInvalidExpression(path);
}
}
}
return nodeList;
}
/// <summary>
/// Gets the numeric value at the specified
/// position in the statement
/// </summary>
/// <param name="str">Statement to search</param>
/// <param name="pos">Position at which to start the search</param>
/// <returns>Representation of the position in the absolute path to the node</returns>
private static int ReadPosition(string str, ref int pos) {
Debug.Assert(pos <= str.Length);
int end = str.IndexOfAny(delimites, pos);
if (end < 0) {
end = str.Length;
}
// TODO: better error handling if this should be shipped
int nodePos = int.Parse(str.Substring(pos, end - pos));
pos = end;
return nodePos;
}
/// <summary>
/// Returns the sub-string representing the attribute name which
/// starts at the specified position in the provided statement
/// and ends just before a vertical bar character or the end
/// of the specifed statement.
/// </summary>
/// <param name="str">Statement to search</param>
/// <param name="pos">Position at which to start the search</param>
/// <returns>attribute name</returns>
private static string ReadAttrName(string str, ref int pos) {
Debug.Assert(pos <= str.Length);
int end = str.IndexOf('|', pos);
if (end < 0) {
end = str.Length;
}
// TODO: better error handling if this should be shipped
string name = str.Substring(pos, end - pos);
pos = end;
return name;
}
/// <summary>
/// Throws an 'invalid XmlDiffPath expression' exception.
/// </summary>
/// <param name="path">Proprietary alphanumeric path statement</param>
private static void OnInvalidExpression(string path) {
throw new Exception("Invalid XmlDiffPath expression: " + path);
}
/// <summary>
/// Throws an 'no matching node' exception.
/// </summary>
/// <param name="path">Proprietary alphanumeric path statement</param>
private static void OnNoMatchingNode(string path) {
throw new Exception("No matching node:" + path);
}
#endregion
}
}