/// <summary>
/// Updates the state of the paragraphs located in source according to the original content, located in original
/// </summary>
/// <param name="original">The original file content</param>
public void UpdateState(Document original)
{
// Find paragraphs that have been inserted and those that have been modified
foreach (Paragraph p in Paragraphs.Values)
{
Paragraph originalParagraph = original.FindParagraph(p.Id);
if (originalParagraph != null)
{
if (originalParagraph.Text.Equals(p.Text))
{
p.State = Paragraph.ParagraphState.NoChange;
}
else
{
p.OriginalText = originalParagraph.Text;
p.State = Paragraph.ParagraphState.Changed;
// Maybe the row has been moved (new row inserted before), or this is a new row
TableRow row = p as TableRow;
TableRow originalRow = originalParagraph as TableRow;
if (originalRow != null)
{
// Try to determine if the paragraph as been moved (instead of changed)
// The paragraph has been moved when we can find it, at another location in the original table
foreach (Paragraph otherParagraph in original.Paragraphs.Values)
{
TableRow otherRow = otherParagraph as TableRow;
if (otherRow != null && otherRow.EnclosingParagraph == originalRow.EnclosingParagraph)
{
if (otherRow.Text.Equals(p.Text))
{
p.State = Paragraph.ParagraphState.Moved;
p.OriginalText = otherRow.Id;
break;
}
}
}
if (p.State == Paragraph.ParagraphState.Changed)
{
// If the paragraph has not been moved, try to determine if the paragraph has been inserted
// The paragraph has been inserted if the corresponding paragraph in the original table
// can be found at another location of this table
foreach (Paragraph otherParagraph in Paragraphs.Values)
{
TableRow otherRow = otherParagraph as TableRow;
if (otherRow != null && otherRow.EnclosingParagraph == row.EnclosingParagraph)
{
if (otherRow.Text.Equals(originalRow.Text))
{
p.State = Paragraph.ParagraphState.Inserted;
break;
}
}
}
}
}
}
}
else
{
// Original paragraph could not be found => This is a new paragraph
p.State = Paragraph.ParagraphState.Inserted;
TableRow originalRow = originalParagraph as TableRow;
if (originalRow != null)
{
// Try to determine if the paragraph as been moved (instead of changed)
// The paragraph has been moved when we can find it, at another location in the original table
foreach (Paragraph otherParagraph in original.Paragraphs.Values)
{
TableRow otherRow = otherParagraph as TableRow;
if (otherRow != null && otherRow.EnclosingParagraph == originalRow.EnclosingParagraph)
{
if (otherRow.Text.Equals(p.Text))
{
p.State = Paragraph.ParagraphState.Moved;
p.OriginalText = otherRow.Id;
break;
}
}
}
}
}
}
// Find paragraphs that have been deleted
foreach (Paragraph p in original.Paragraphs.Values)
{
Paragraph newParagraph = FindParagraph(p.Id);
if (newParagraph == null)
{
p.State = Paragraph.ParagraphState.Deleted;
Paragraphs.Add(p.Id, p);
}
}
}