-
Notifications
You must be signed in to change notification settings - Fork 202
/
XmlDiffView.cs
2076 lines (1867 loc) · 74.8 KB
/
XmlDiffView.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ---------------------------------------------------------------------------
// <copyright company="Microsoft Corporation" file="XmlDiffView.cs">
// Copyright (c) Microsoft Corporation 2005
// </copyright>
// <project>
// XmlDiffView
// </project>
// <summary>
// Public entry point for the library.
// </summary>
// <history>
// [barryw] 01MAR05 Adapted from sample file.
// </history>
// ---------------------------------------------------------------------------
namespace Microsoft.XmlDiffPatch
{
#region Using directives
using System;
using System.Xml;
using System.IO;
using System.Diagnostics;
using System.Collections;
using Microsoft.XmlDiffPatch;
#endregion
#region Library Enums section
/// <summary>
/// Enumerator values for types of differences
/// (Match indicates no difference)
/// </summary>
internal enum XmlDiffViewOperation
{
/// <summary>
/// Data matches
/// </summary>
Match = 0,
/// <summary>
/// Data differences will be ignored
/// </summary>
Ignore = 1,
/// <summary>
/// Data was added
/// </summary>
Add = 2,
/// <summary>
/// Data was moved to here
/// </summary>
MoveTo = 3,
/// <summary>
/// Data was removed
/// </summary>
Remove = 4,
/// <summary>
/// Data was moved from here
/// </summary>
MoveFrom = 5,
/// <summary>
/// Data was changed
/// </summary>
Change = 6,
}
#endregion
/// <summary>
/// Class which provides the external methods
/// and properties to use this library.
/// </summary>
public sealed class XmlDiffView
{
#region Member variables section
// Static methods and data for drawing
/// <summary>
/// Size of the incremental indentation
/// </summary>
internal static readonly int DeltaIndent = 15;
/// <summary>
/// Operation settings to control writing to
/// the baseline and actual "panes" of the html.
/// </summary>
internal static readonly bool[,] HtmlWriteToPane =
{
// Match = 0
{
true, true
},
// Ignore = 1
{
true, true
},
// Add = 2
{
false, true
},
// MoveTo = 3,
{
false, true
},
// Remove = 4,
{
true, false
},
// MoveFrom = 5,
{
true, false
},
// Change = 6,
{
true, true
},
};
/// <summary>
/// String used to set a required number html space characters.
/// </summary>
private static readonly string Nbsp = " " +
" " +
" ";
/// <summary>
/// Declares a view document to hold the merged data.
/// </summary>
private XmlDiffViewDocument viewDocument = null;
/// <summary>
/// Creates a hash table of change short descriptions.
/// </summary>
private Hashtable descriptors = new Hashtable();
/// <summary>
/// Declares a memory stream to hold the diffgram.
/// </summary>
private MemoryStream diffgram = null;
/// <summary>
/// Declares a TextWriter object to hold the output data.
/// </summary>
private TextWriter outputData = null;
/// <summary>
/// Stores the final output.
/// </summary>
private XmlDiffViewResults finalOutput = null;
// options
/// <summary>
/// Initializes the child order option
/// </summary>
private bool ignoreChildOrder = false;
/// <summary>
/// Initializes the comments option
/// </summary>
private bool ignoreComments = false;
/// <summary>
/// Initializes the programming instructions node type option
/// </summary>
private bool ignorePI = false;
/// <summary>
/// Initializes the white space option
/// </summary>
private bool ignoreWhitespace = false;
/// <summary>
/// Initializes the namespaces node type option
/// </summary>
private bool ignoreNamespaces = false;
/// <summary>
/// Initializes the xml prefixes option
/// </summary>
private bool ignorePrefixes = false;
/// <summary>
/// Initializes the declaration node type option
/// </summary>
private bool ignoreXmlDecl = false;
/// <summary>
/// Initializes the DTD node type option
/// </summary>
private bool ignoreDtd = false;
/// <summary>
/// Declares an object with references to the last
/// node and attribute.
/// </summary>
private LoadState loadState;
private static int nextOperationId = 1;
#endregion
#region Constructors section
/// <summary>
/// Constructor
/// </summary>
public XmlDiffView()
{
}
#endregion
#region Destructors section
/// <summary>
/// Destructor
/// </summary>
~XmlDiffView()
{
}
#endregion
#region Methods section
#region Public static methods section
#endregion
#region Public non-static methods section
public static int NextOperationId {
get { return nextOperationId++; }
}
public static int LastOperationId {
get { return nextOperationId; }
}
/// <summary>
/// Loads the diffgram to an XmlDocument object, and drives the
/// process of loading the baseline xml file to a
/// XmlDiffViewDocument object, tagging the nodes and attributes
/// according to the options selected, and finally merges the
/// diffgram data into the XmlDiffViewDocument object.
/// </summary>
/// <param name="sourceXml">baseline data</param>
/// <param name="diffgram">diffgram data stream</param>
public void Load(XmlTextReader sourceXml, XmlReader diffGram)
{
nextOperationId = 1;
if (null == sourceXml)
{
throw new ArgumentNullException("sourceXml");
}
if (null == diffGram)
{
throw new ArgumentNullException("diffgram");
}
// load diffgram to DOM
XmlDocument diffgramDoc = new XmlDocument();
diffgramDoc.Load(diffGram);
// process operation descriptors
this.PreprocessDiffgram(diffgramDoc);
// load document
this.viewDocument = new XmlDiffViewDocument();
this.LoadSourceChildNodes(this.viewDocument, sourceXml, false);
// apply diffgram
this.ApplyDiffgram(diffgramDoc.DocumentElement, this.viewDocument);
}
/// <summary>
/// Write the differences in the Xml data
/// to the output file as formatted xml-like text.
/// </summary>
/// <param name="sourceXmlFile">baseline file</param>
/// <param name="changedXmlFile">actual file</param>
/// <param name="outputTextPath">Output data file for the data in text form</param>
/// <param name="appendToOutputFile">Append output to the file</param>
/// <param name="fragment">This is an xml data frament</param>
/// <param name="options">Comparison options</param>
/// <returns>data is identical</returns>
public bool DifferencesAsFormattedText(
string sourceXmlFile,
string changedXmlFile,
string outputTextPath,
bool appendToOutputFile,
bool fragment,
XmlDiffOptions options)
{
// Append to the specified output file.
FileMode mode;
if (appendToOutputFile)
{
mode = FileMode.Append;
}
else
{
mode = FileMode.Create;
}
this.outputData = new StreamWriter(
new FileStream(
outputTextPath,
mode,
FileAccess.Write));
bool identicalData;
identicalData = this.GetDifferencesAsFormattedText(
sourceXmlFile,
changedXmlFile,
fragment,
options);
// close the output stream to release the file.
this.outputData.Close();
return identicalData;
}
/// <summary>
/// Append the differences in the Xml data
/// to the output file as formatted xml-like text.
/// </summary>
/// <param name="sourceXmlFile">baseline file</param>
/// <param name="changedXmlFile">actual file</param>
/// <param name="outputTextPath">output file for the data in text form</param>
/// <param name="fragment">This is an xml data frament</param>
/// <param name="options">Comparison options</param>
/// <returns>Differences were not found.</returns>
public bool DifferencesAsFormattedText(
string sourceXmlFile,
string changedXmlFile,
string outputTextPath,
bool fragment,
XmlDiffOptions options)
{
bool identicalData = this.DifferencesAsFormattedText(
sourceXmlFile,
changedXmlFile,
outputTextPath,
true,
fragment,
options);
return identicalData;
}
/// <summary>
/// Write the differences in the Xml data
/// as formatted xml-like text return in
/// a memory based TextReader object.
/// </summary>
/// <param name="sourceXmlFile">baseline file</param>
/// <param name="changedXmlFile">actual file</param>
/// <param name="fragment">This is an xml data frament</param>
/// <param name="options">Comparison options</param>
/// <param name="reader">A reference to return readable
/// formatted xml-like text.</param>
/// <returns>data is identical</returns>
public XmlDiffViewResults DifferencesAsFormattedText(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options)
{
MemoryStream data = new MemoryStream();
this.outputData = new StreamWriter(
data,
System.Text.Encoding.Unicode);
bool identicalData;
identicalData = this.GetDifferencesAsFormattedText(
sourceXmlFile,
changedXmlFile,
fragment,
options);
// Move the data to the memory stream
this.outputData.Flush();
this.finalOutput = new XmlDiffViewResults(data, identicalData);
// return result of comparison
return this.finalOutput;
}
/// <summary>
/// Create WinDiff like static comparison in Html.
/// </summary>
/// <param name="sourceXmlFile">the baseline file</param>
/// <param name="changedXmlFile">the actual (or target) file</param>
/// <param name="resultHtmlViewFile">the html output file</param>
/// <param name="fragment">the file is only an Xml fragment</param>
/// <param name="options">comparison filtering options</param>
/// <returns>Differences were not found after filtering.</returns>
public bool DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
string resultHtmlViewFile,
bool fragment,
XmlDiffOptions options)
{
bool identicalData = this.DifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
resultHtmlViewFile,
fragment,
true,
options);
return identicalData;
}
/// <summary>
/// Create WinDiff like static comparison in Html.
/// </summary>
/// <param name="sourceXmlFile">the baseline file</param>
/// <param name="changedXmlFile">the actual (or target) file</param>
/// <param name="resultHtmlViewFile">the html output file</param>
/// <param name="fragment">the file is only an Xml fragment</param>
/// <param name="appendToOutputFile">Append to existing output file</param>
/// <param name="options">comparison filtering options</param>
/// <returns>Differences were not found after filtering.</returns>
public bool DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
string resultHtmlViewFile,
bool fragment,
bool appendToOutputFile,
XmlDiffOptions options)
{
// Append to the specified output file.
FileMode mode;
if (appendToOutputFile)
{
mode = FileMode.Append;
}
else
{
mode = FileMode.Create;
}
this.outputData = new StreamWriter(
new FileStream(
resultHtmlViewFile,
mode,
FileAccess.Write));
bool identicalData;
try
{
identicalData = this.DifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
fragment,
options,
this.outputData);
}
finally
{
this.outputData.Close();
}
return identicalData;
}
/// <summary>
/// Create WinDiff like static comparison in Html.
/// </summary>
/// <param name="sourceXmlFile">the baseline file</param>
/// <param name="changedXmlFile">the actual (or target) file</param>
/// <param name="fragment">the file is only an Xml fragment</param>
/// <param name="options">comparison filtering options</param>
/// <param name="reader">Readable output data stream</param>
/// <returns>Differences were not found after filtering.</returns>
public XmlDiffViewResults DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options)
{
MemoryStream data = new MemoryStream();
try
{
this.outputData = new StreamWriter(
data,
System.Text.Encoding.Unicode);
bool identicalData = this.DifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
fragment,
options,
this.outputData);
// Move the data to the memory stream
this.outputData.Flush();
// Generate the final output using the returned values
// from the differences comparison.
this.finalOutput = new XmlDiffViewResults(data, identicalData);
}
finally
{
if (null != data)
{
data.Close();
}
} // return result of comparison
return this.finalOutput;
}
internal static int LastVisitedOpId = 0;
/// <summary>
/// Converts a copy of the xml data in the
/// XmlDiffViewDocument object
/// to html and writes it out to the
/// TextWriter object (which may be a file).
/// </summary>
/// <param name="htmlOutput">Data stream for output</param>
public void GetHtml(TextWriter htmlOutput)
{
LastVisitedOpId = 0;
XmlDiffViewNode.ResetLineNumbers();
XmlTextWriter writer = new XmlTextWriter(htmlOutput);
this.viewDocument.DrawHtml(writer, 10);
}
#endregion
#region Internal static methods section
/// <summary>
/// Writes to the specified section
/// </summary>
/// <param name="pane">baseline/actual data presentation sections</param>
/// <param name="str">Statement to be written</param>
internal static void HtmlWriteString(
XmlWriter pane,
string str)
{
pane.WriteString(str);
}
/// <summary>
/// Writes to the specified section using
/// highlighting based on hte type of data change
/// </summary>
/// <param name="pane">baseline/actual data presentation sections</param>
/// <param name="op">Type of data change</param>
/// <param name="str">Statement to be written</param>
internal static void HtmlWriteString(
XmlWriter pane,
XmlDiffViewOperation op,
string str)
{
HtmlSetColor(pane, op);
pane.WriteString(str);
HtmlResetColor(pane);
}
/// <summary>
/// Writes an 'empty' space character.
/// </summary>
/// <param name="pane">baseline/actual data presentation sections</param>
internal static void HtmlWriteEmptyString(XmlWriter pane)
{
pane.WriteRaw(" ");
}
/// <summary>
/// Writes the start of a table cell
/// </summary>
/// <param name="writer">output stream</param>
/// <param name="indent">Text indentation characters</param>
internal static void HtmlStartCell(XmlWriter writer, int indent)
{
writer.WriteStartElement("td");
writer.WriteAttributeString("style", "padding-left: " + indent.ToString() + "pt;");
}
/// <summary>
/// Writes the closing tags
/// </summary>
/// <param name="writer">output stream</param>
internal static void HtmlEndCell(XmlWriter writer)
{
writer.WriteFullEndElement();
}
/// <summary>
/// Inserts an html break-line and writes the tags to
/// close the html element
/// </summary>
/// <param name="writer">Output data stream</param>
internal static void HtmlBr(XmlWriter writer)
{
writer.WriteStartElement("br");
writer.WriteEndElement();
}
/// <summary>
/// Starts a new row in the table
/// </summary>
/// <param name="writer">output data stream</param>
internal static void HtmlStartRow(XmlWriter writer)
{
writer.WriteStartElement("tr");
}
/// <summary>
/// Ends a row in the table
/// </summary>
/// <param name="writer">output data stream</param>
internal static void HtmlEndRow(XmlWriter writer)
{
writer.WriteFullEndElement();
}
/// <summary>
/// Gets 'empty' string characters to use as an
/// indentation for formatting the output.
/// </summary>
/// <param name="charCount">Number of indentations</param>
/// <returns>string to use as an indentation</returns>
internal static string GetIndent(int charCount)
{
int nbspCount = charCount * 6;
if (nbspCount <= Nbsp.Length)
{
return Nbsp.Substring(0, nbspCount);
}
else
{
string indent = string.Empty;
while (nbspCount > Nbsp.Length)
{
indent += Nbsp;
nbspCount -= Nbsp.Length;
}
indent += Nbsp.Substring(0, nbspCount);
return indent;
}
}
/// <summary>
/// This functions allows the size of the indentation to be
/// set, and returns one indentation.
/// </summary>
/// <param name="charCount">Size of the indent (spaces)</param>
/// <returns>A string of spaces</returns>
internal static string IndentText(int charCount)
{
const string oneSpace = " ";
string indent = string.Empty;
while (charCount > 0)
{
// increment the spaces
indent += oneSpace;
// decrement the count
charCount -= 1;
}
return indent;
}
/// <summary>
/// Adjusts the format of the data to account for the defined
/// whitespace characters.
/// </summary>
/// <param name="text">data to be formatted</param>
/// <returns>formatted data</returns>
internal static string NormalizeText(string text)
{
char[] chars = text.ToCharArray();
int i = 0;
int j = 0;
for (;;)
{
while (j < chars.Length && IsWhitespace(text[j]))
{
j++;
}
while (j < chars.Length && !IsWhitespace(text[j]))
{
chars[i++] = chars[j++];
}
if (j < chars.Length)
{
chars[i++] = ' ';
j++;
}
else
{
if (j == 0)
{
return string.Empty;
}
if (IsWhitespace(chars[j - 1]))
{
i--;
}
return new string(chars, 0, i);
}
}
}
/// <summary>
/// Determines if the specifed character is a
/// defined whitespace character.
/// </summary>
/// <param name="c">character to test</param>
/// <returns>specifed character is a
/// defined whitespace character</returns>
internal static bool IsWhitespace(char c)
{
return (c == ' ' ||
c == '\t' ||
c == '\n' ||
c == '\r');
}
#endregion
#region Private static methods section
/// <summary>
/// Sets the output style based on the type of change in the data.
/// </summary>
/// <param name="pane">baseline/actual data presentation sections</param>
/// <param name="op">Type of data change</param>
private static void HtmlSetColor(
XmlWriter pane,
XmlDiffViewOperation op)
{
pane.WriteStartElement("span");
pane.WriteAttributeString("class", op.ToString().ToLowerInvariant());
}
/// <summary>
/// Closes the tags after setting the color
/// </summary>
/// <param name="pane">baseline/actual data presentation sections</param>
private static void HtmlResetColor(XmlWriter pane)
{
pane.WriteFullEndElement();
}
/// <include file='doc\XmlDiff.uex' path='docs/doc[@for="XmlDiff.ParseOptions"]/*' />
/// <summary>
/// Translates string representation of XmlDiff options into XmlDiffOptions enum.
/// </summary>
/// <param name="options">Value of the 'options' attribute of the 'xd:xmldiff' element in diffgram.</param>
/// <returns>An object containing the parsing options.</returns>
private static XmlDiffOptions ParseOptions(string options)
{
if (options == null)
throw new ArgumentNullException("options");
if (options == XmlDiffOptions.None.ToString())
return XmlDiffOptions.None;
else
{
XmlDiffOptions optionsEnum = XmlDiffOptions.None;
int j = 0, i = 0;
while (i < options.Length)
{
j = options.IndexOf(' ', i);
if (j == -1)
j = options.Length;
string opt = options.Substring(i, j - i);
switch (opt)
{
case "IgnoreChildOrder": optionsEnum |= XmlDiffOptions.IgnoreChildOrder;
break;
case "IgnoreComments": optionsEnum |= XmlDiffOptions.IgnoreComments;
break;
case "IgnoreNamespaces": optionsEnum |= XmlDiffOptions.IgnoreNamespaces;
break;
case "IgnorePI": optionsEnum |= XmlDiffOptions.IgnorePI;
break;
case "IgnorePrefixes": optionsEnum |= XmlDiffOptions.IgnorePrefixes;
break;
case "IgnoreWhitespace": optionsEnum |= XmlDiffOptions.IgnoreWhitespace;
break;
case "IgnoreXmlDecl": optionsEnum |= XmlDiffOptions.IgnoreXmlDecl;
break;
case "IgnoreDtd": optionsEnum |= XmlDiffOptions.IgnoreDtd;
break;
default:
throw new ArgumentException("options");
}
i = j + 1;
}
return optionsEnum;
}
}
#endregion
#region Private non-static methods section
/// <summary>
/// Determines if the data changed
/// </summary>
/// <param name="sourceXmlFile">baseline file</param>
/// <param name="changedXmlFile">actual file</param>
/// <param name="fragment">xml data fragment</param>
/// <param name="options">Comparison options</param>
/// <returns>data is identical</returns>
private bool GetDifferencesAsFormattedText(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options)
{
bool identicalData = this.MarkupBaselineWithChanges(
sourceXmlFile,
changedXmlFile,
fragment,
options);
// only generate the output if there are differences.
if (!identicalData)
{
// Populate the output
this.GetText(sourceXmlFile, changedXmlFile);
}
return identicalData;
}
/// <summary>
/// Determines if the data changed
/// </summary>
/// <param name="sourceXmlFile">baseline file</param>
/// <param name="changedXmlFile">actual file</param>
/// <param name="fragment">xml data fragment</param>
/// <param name="options">Comparison options</param>
/// <param name="resultHtml">output data</param>
/// <returns>data is identical</returns>
private bool DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options,
TextWriter resultHtml)
{
bool identicalData = this.MarkupBaselineWithChanges(
sourceXmlFile,
changedXmlFile,
fragment,
options);
this.SideBySideHtmlHeader(
sourceXmlFile,
changedXmlFile,
identicalData,
resultHtml);
this.GetHtml(resultHtml);
this.SideBySideHtmlFooter(resultHtml);
return identicalData;
}
/// <summary>
/// Markup the baseline data with changes
/// </summary>
/// <param name="sourceXmlFile">baseline xml data</param>
/// <param name="changedXmlFile">xml data to which to compare</param>
/// <param name="fragment">xml data fragment</param>
/// <param name="options">comparison options</param>
/// <returns>data is identical</returns>
private bool MarkupBaselineWithChanges(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options)
{
// generate the diffgram
bool identicalData = this.GenerateDiffGram(
sourceXmlFile,
changedXmlFile,
fragment,
options);
this.MergeDiffgramAndBaseline(
sourceXmlFile,
fragment);
return identicalData;
}
private int ParseOpId(string value) {
int opid = 0;
if (string.IsNullOrEmpty(value)) {
opid = NextOperationId;
} else {
opid = int.Parse(value);
}
if (XmlDiffView.nextOperationId <= opid) {
XmlDiffView.nextOperationId = opid + 1;
}
return opid;
}
/// <summary>
/// Adjust the diffgram data for the comparison options.
/// </summary>
/// <param name="diffgramDoc">diffgram data</param>
private void PreprocessDiffgram(XmlDocument diffgramDoc)
{
// read xmldiff options
XmlAttribute attr = (XmlAttribute)
diffgramDoc.DocumentElement.Attributes.GetNamedItem("options");
if (attr == null)
{
throw new NullReferenceException(
"Missing 'options' attribute in the diffgram.");
}
string optionsAttr = attr.Value;
XmlDiffOptions options = ParseOptions(optionsAttr);
this.ignoreChildOrder = (((int)options & (int)
(XmlDiffOptions.IgnoreChildOrder)) > 0);
this.ignoreComments = (((int)options &
(int)(XmlDiffOptions.IgnoreComments)) > 0);
this.ignorePI = (((int)options &
(int)(XmlDiffOptions.IgnorePI)) > 0);
this.ignoreWhitespace = (((int)options &
(int)(XmlDiffOptions.IgnoreWhitespace)) > 0);
this.ignoreNamespaces = (((int)options &
(int)(XmlDiffOptions.IgnoreNamespaces)) > 0);
this.ignorePrefixes = (((int)options &
(int)(XmlDiffOptions.IgnorePrefixes)) > 0);
this.ignoreDtd = (((int)options &
(int)(XmlDiffOptions.IgnoreDtd)) > 0);
if (this.ignoreNamespaces)
{
this.ignorePrefixes = true;
}
// read descriptors
XmlNodeList children =
diffgramDoc.DocumentElement.ChildNodes;
IEnumerator e = children.GetEnumerator();
while (e.MoveNext())
{
XmlElement desc = e.Current as XmlElement;
if (desc != null && desc.LocalName == "descriptor")
{
int opid = ParseOpId(desc.GetAttribute("opid"));
OperationDescriptor.Type type;
switch (desc.GetAttribute("type"))
{
case "move":
type =
OperationDescriptor.Type.Move;
break;
case "prefix change":
type =
OperationDescriptor.Type.PrefixChange;
break;
case "namespace change":
type = OperationDescriptor.Type.NamespaceChange;
break;