Skip to content

Commit

Permalink
Improve the implementation of omitMatches so it is not doing n-square…
Browse files Browse the repository at this point in the history
…d tree traversals, instead it is a single pass.
  • Loading branch information
lovettchris committed Sep 27, 2023
1 parent c5a98dd commit 0472d20
Show file tree
Hide file tree
Showing 19 changed files with 324 additions and 85 deletions.
70 changes: 70 additions & 0 deletions src/Common/Updates.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="updates">
<xs:complexType>
<xs:sequence>
<xs:element name="application">
<xs:complexType>
<xs:sequence>
<xs:element name="title" type="xs:string">
<xs:annotation>
<xs:documentation>The name of the application being updated.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="location" type="xs:string">
<xs:annotation>
<xs:documentation>The location of the update xml page.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="download" type="xs:string">
<xs:annotation>
<xs:documentation>Location of the download page.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="installer" type="xs:string">
<xs:annotation>
<xs:documentation>Location of the installation page.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="history" type="xs:string">
<xs:annotation>
<xs:documentation>Location of the page showing update history.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="frequency" type="xs:string" >
<xs:annotation>
<xs:documentation>The frequency we need to check for updates to the application, any valid TimeSpan.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element maxOccurs="unbounded" name="version">
<xs:annotation>
<xs:documentation>This element describes a new version of the application.</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded" >
<xs:element name="feature" type="xs:string">
<xs:annotation>
<xs:documentation>Description of a new feature added in this version.</xs:documentation>
</xs:annotation>
</xs:element>
<xs:element name="bug" type="xs:string" >
<xs:annotation>
<xs:documentation>Description of a bug fixed in this version.</xs:documentation>
</xs:annotation>
</xs:element>
</xs:choice>
<xs:attribute name="number" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>The version number.</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
76 changes: 76 additions & 0 deletions src/Common/Updates.xslt
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="/">
<html>
<head>
<title>
<xsl:value-of select="updates/application/title"/>
</title>
<style>
body, th, td { font-family: Verdana; font-size:x-small;}
</style>
</head>
<body>
<center>
<table width="600" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" bgcolor="teal" style="height: 29px">
<span style="color:white;background:teal;font-size:18pt">
<xsl:value-of select="updates/application/title"/> - Change History
</span>
</td>
</tr>
<tr>
<td>
<br/>
<p>
</p>
</td>
</tr>
<tr>
<td>
<table border="1" cellpadding="2" >
<xsl:apply-templates/>
</table>
</td>
</tr>
</table>
</center>
</body>
</html>
</xsl:template>
<xsl:template match="updates">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="application">
</xsl:template>
<xsl:template match="version">
<xsl:variable name="color">background:teal;color:white</xsl:variable>
<tr>
<td style="{$color}"> </td>
<td style="{$color}" align="center">
Version <xsl:value-of select="@number"/>
</td>
</tr>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="feature">
<xsl:variable name="color">background:MediumTurquoise</xsl:variable>
<tr>
<td style="{$color}" valign="top">Feature</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
<xsl:template match="bug">
<xsl:variable name="color">background:LightPink</xsl:variable>
<tr>
<td style="{$color}" valign="top">Bug</td>
<td>
<xsl:value-of select="."/>
</td>
</tr>
</xsl:template>
</xsl:stylesheet>
11 changes: 11 additions & 0 deletions src/Common/changes.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Updates.xslt"?>
<updates xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Updates.xsd">
<application>
<title>Microsoft XML Diff</title>
<location>https://raw.githubusercontent.com/lovettchris/xmldiff/master/src/Common/changes.xml</location>
<download>https://www.nuget.org/packages/LovettSoftware.XmlDiff</download>
<installer>https://www.nuget.org/packages/LovettSoftware.XmlDiff</installer>
<history>https://github.com/microsoft/XmlNotepad/blob/master/src/Updates/Updates.xml</history>
<frequency>1.00:00:00</frequency>
</application>
<version number="1.1.0">
<feature>Thanks to Mark Roberts for a cool new feature that omits identical output to make the diff HTML output scale better on huge XML files.</feature>
</version>
<version number="1.0.8">
<bug>Add back strong naming for XmlDiffPatch.dll</bug>
</version>
Expand Down
6 changes: 3 additions & 3 deletions src/Common/version.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<PropertyGroup>
<Company>Lovett Software</Company>
<Copyright>Copyright © Lovett Software</Copyright>
<FileVersion>1.0.8</FileVersion>
<AssemblyVersion>1.0.8</AssemblyVersion>
<Version>1.0.8</Version>
<FileVersion>1.1.0</FileVersion>
<AssemblyVersion>1.1.0</AssemblyVersion>
<Version>1.1.0</Version>
</PropertyGroup>
</Project>
2 changes: 1 addition & 1 deletion src/Common/version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8
1.1.0
21 changes: 21 additions & 0 deletions src/UnitTests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public void SideBySideDiffView()
}
}

[Fact]
public void SideBySideDiffViewCompact()
{
using (var col = new TempFileCollection())
{
var xmlSource = col.CreateTempFile("<root id='3'><apple></apple><banana><temp/></banana><elephant size='big'/></root>");
var xmlChanged = col.CreateTempFile("<root id='3'><apple></apple><banana></banana><carrot/><elephant size='big'/></root>");

var view = new XmlDiffView();
var options = XmlDiffOptions.IgnoreChildOrder | XmlDiffOptions.IgnoreWhitespace;
var results = view.DifferencesSideBySideAsHtml(xmlSource, xmlChanged, false, options, true);

var diff = results.ReadToEnd();
Assert.Contains("root", diff);
Assert.Contains("banana", diff);
Assert.Contains("&lt;temp", diff);
Assert.DoesNotContain("apple", diff);
Assert.DoesNotContain("elephant", diff);
}
}

private string ToComparibleString(XmlDocument doc)
{
// avoid comparing the hash.
Expand Down
2 changes: 1 addition & 1 deletion src/XmlDiffView/XmlDiff.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>LovettSoftware.XmlDiff</id>
<version>1.0.9</version>
<version>1.1.0</version>
<authors>Chris Lovett</authors>
<description>A library that compares and merges XML files.</description>
<projectUrl>https://github.com/lovettchris/xmldiff</projectUrl>
Expand Down
37 changes: 27 additions & 10 deletions src/XmlDiffView/XmlDiffView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,21 +425,24 @@ public XmlDiffViewResults DifferencesAsFormattedText(
/// <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>
/// <param name="omitIdenticalHtmlOutput">omit html output for identical xml elements</param>
/// <returns>Differences were not found after filtering.</returns>
public bool DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
string resultHtmlViewFile,
bool fragment,
XmlDiffOptions options)
XmlDiffOptions options,
bool omitIdenticalHtmlOutput = false)
{
bool identicalData = this.DifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
resultHtmlViewFile,
fragment,
true,
options);
options,
omitIdenticalHtmlOutput);

return identicalData;
}
Expand All @@ -453,14 +456,16 @@ public bool DifferencesSideBySideAsHtml(
/// <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>
/// <param name="omitIdenticalHtmlOutput">omit html output for identical xml elements</param>
/// <returns>Differences were not found after filtering.</returns>
public bool DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
string resultHtmlViewFile,
bool fragment,
bool appendToOutputFile,
XmlDiffOptions options)
XmlDiffOptions options,
bool omitIdenticalHtmlOutput = false)
{
// Append to the specified output file.
FileMode mode;
Expand All @@ -481,11 +486,12 @@ public bool DifferencesSideBySideAsHtml(
bool identicalData;
try
{
identicalData = this.DifferencesSideBySideAsHtml(
identicalData = this.InternalDifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
fragment,
options,
omitIdenticalHtmlOutput,
this.outputData);
}
finally
Expand All @@ -503,12 +509,14 @@ public bool DifferencesSideBySideAsHtml(
/// <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="omitIdenticalHtmlOutput">omit html output for identical xml elements</param>
/// <returns>Differences were not found after filtering.</returns>
public XmlDiffViewResults DifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options)
XmlDiffOptions options,
bool omitIdenticalHtmlOutput = false)
{
MemoryStream data = new MemoryStream();

Expand All @@ -521,11 +529,12 @@ public XmlDiffViewResults DifferencesSideBySideAsHtml(
bool identicalData = false;
try
{
identicalData = this.DifferencesSideBySideAsHtml(
identicalData = this.InternalDifferencesSideBySideAsHtml(
sourceXmlFile,
changedXmlFile,
fragment,
options,
omitIdenticalHtmlOutput,
this.outputData);

}
Expand Down Expand Up @@ -556,12 +565,18 @@ public XmlDiffViewResults DifferencesSideBySideAsHtml(
/// TextWriter object (which may be a file).
/// </summary>
/// <param name="htmlOutput">Data stream for output</param>
public void GetHtml(TextWriter htmlOutput, bool omitMatches = false)
/// <param name="omitMatches">Omit matching lines to compress the size of the HTML output</param>
/// <param name="indentPoints">HTML indent to show in points - default is padding-left: 10pt;</param>
public void GetHtml(TextWriter htmlOutput, bool omitMatches = false, int indentPoints = 10)
{
LastVisitedOpId = 0;
XmlDiffViewNode.ResetLineNumbers();
XmlTextWriter writer = new XmlTextWriter(htmlOutput);
this.viewDocument.DrawHtml(writer, 10, new XmlDiffViewRenderState { OmitMatches = omitMatches });
if (omitMatches)
{
this.viewDocument.PruneMatchingElements();
}
this.viewDocument.DrawHtml(writer, indentPoints);
}

#endregion
Expand Down Expand Up @@ -907,13 +922,15 @@ private bool GetDifferencesAsFormattedText(
/// <param name="changedXmlFile">actual file</param>
/// <param name="fragment">xml data fragment</param>
/// <param name="options">Comparison options</param>
/// <param name="omitIdenticalHtmlOutput">omit html output for identical xml elements</param>
/// <param name="resultHtml">output data</param>
/// <returns>data is identical</returns>
private bool DifferencesSideBySideAsHtml(
private bool InternalDifferencesSideBySideAsHtml(
string sourceXmlFile,
string changedXmlFile,
bool fragment,
XmlDiffOptions options,
bool omitIdenticalHtmlOutput,
TextWriter resultHtml)
{
bool identicalData = this.MarkupBaselineWithChanges(
Expand All @@ -929,7 +946,7 @@ private bool DifferencesSideBySideAsHtml(
identicalData,
resultHtml);

this.GetHtml(resultHtml);
this.GetHtml(resultHtml, omitIdenticalHtmlOutput);

this.SideBySideHtmlFooter(resultHtml);

Expand Down
2 changes: 1 addition & 1 deletion src/XmlDiffView/XmlDiffViewAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ internal override XmlDiffViewNode Clone(bool deep)
/// <param name="writer">output stream</param>
/// <param name="indent">size of indent</param>
[Obsolete("This method should never be called", true)]
internal override void DrawHtml(XmlWriter writer, int indent, XmlDiffViewRenderState renderState)
internal override void DrawHtml(XmlWriter writer, int indent)

Check warning on line 252 in src/XmlDiffView/XmlDiffViewAttribute.cs

View workflow job for this annotation

GitHub Actions / build

Obsolete member 'XmlDiffViewAttribute.DrawHtml(XmlWriter, int)' overrides non-obsolete member 'XmlDiffViewNode.DrawHtml(XmlWriter, int)'

Check warning on line 252 in src/XmlDiffView/XmlDiffViewAttribute.cs

View workflow job for this annotation

GitHub Actions / build

Obsolete member 'XmlDiffViewAttribute.DrawHtml(XmlWriter, int)' overrides non-obsolete member 'XmlDiffViewNode.DrawHtml(XmlWriter, int)'
{
throw new Exception("This method should never be called.");
}
Expand Down
2 changes: 1 addition & 1 deletion src/XmlDiffView/XmlDiffViewCharData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal override XmlDiffViewNode Clone(bool deep)
/// </summary>
/// <param name="writer">output stream</param>
/// <param name="indent">number of indentations</param>
internal override void DrawHtml(XmlWriter writer, int indent, XmlDiffViewRenderState renderState)
internal override void DrawHtml(XmlWriter writer, int indent)
{
if (Operation == XmlDiffViewOperation.Change)
{
Expand Down
4 changes: 2 additions & 2 deletions src/XmlDiffView/XmlDiffViewDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ internal override XmlDiffViewNode Clone(bool deep)
/// <param name="writer">output stream</param>
/// <param name="indent">number of indentations</param>
/// <param name="renderState"></param>
internal override void DrawHtml(XmlWriter writer, int indent, XmlDiffViewRenderState renderState)
internal override void DrawHtml(XmlWriter writer, int indent)
{
HtmlDrawChildNodes(writer, indent, renderState);
HtmlDrawChildNodes(writer, indent);
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion src/XmlDiffView/XmlDiffViewDocumentType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ internal override XmlDiffViewNode Clone(bool deep)
/// </summary>
/// <param name="writer">output stream</param>
/// <param name="indent">number of indentations</param>
internal override void DrawHtml(XmlWriter writer, int indent, XmlDiffViewRenderState renderState)
internal override void DrawHtml(XmlWriter writer, int indent)
{
if (Operation == XmlDiffViewOperation.Change)
{
Expand Down
Loading

0 comments on commit 0472d20

Please sign in to comment.