XmlDiffViewCharData.cs
上传用户:hbhltzc
上传日期:2022-06-04
资源大小:1925k
文件大小:17k
源码类别:

xml/soap/webservice

开发平台:

Visual C++

  1. //  ---------------------------------------------------------------------------
  2. // <copyright company="Microsoft Corporation" file="XmlDiffViewCharData.cs">
  3. //     Copyright (c) Microsoft Corporation 2005
  4. // </copyright>
  5. // <project>
  6. //     XmlDiffView
  7. // </project>
  8. // <summary>
  9. //     Generate output data for character 
  10. //     based data, e.g., text, CharData, and comments.
  11. // </summary>
  12. // <history>
  13. //      [barryw] 03MAR05 Created
  14. // </history>
  15. //  ---------------------------------------------------------------------------
  16. namespace Microsoft.XmlDiffPatch
  17. {
  18.     #region Using directives
  19.     using System;
  20.     using System.IO;
  21.     using System.Xml;
  22.     using System.Diagnostics;
  23.     
  24.     #endregion
  25.     /// <summary>
  26.     /// Class to generate output data for character 
  27.     /// based data, e.g., text, CharData, and comments.
  28.     /// </summary>
  29.     internal class XmlDiffViewCharData : XmlDiffViewNode
  30.     {
  31.         #region Member variables section
  32.         private string openString;
  33.         private string closeString;
  34.         /// <summary>
  35.         /// Hold the value of the innerText for the node.
  36.         /// </summary>
  37.         private string innerText;
  38.         #endregion
  39.         #region  Constructors section
  40.         /// <summary>
  41.         /// Constructor
  42.         /// </summary>
  43.         /// <param name="value">innerText for this node</param>
  44.         /// <param name="nodeType">type of node</param>
  45.         internal XmlDiffViewCharData(
  46.             string value, 
  47.             XmlNodeType nodeType) : base(nodeType)
  48.         {
  49.             this.InnerText = value;
  50.         }
  51.         
  52.         #endregion
  53.         #region Properties section
  54.         /// <summary>
  55.         /// Gets or sets the inner text value.  Text returned 
  56.         /// is free of tabs and newline characters.
  57.         /// </summary>
  58.         public string InnerText
  59.         {
  60.             get
  61.             {
  62.                 return RemoveTabsAndNewlines(this.innerText);
  63.             }
  64.             set
  65.             {
  66.                 this.innerText = value;
  67.             }
  68.         }
  69.         /// <summary>
  70.         /// Returns innerText of current node.
  71.         /// </summary>
  72.         /// <value>innerText stripped of tabs and newline characters.</value>
  73.         public override string OuterXml
  74.         {
  75.             get
  76.             {
  77.                 switch (NodeType)
  78.                 {
  79.                     case XmlNodeType.Text:
  80.                     case XmlNodeType.Whitespace:
  81.                         return this.InnerText;
  82.                     case XmlNodeType.Comment:
  83.                         return Tags.XmlCommentOldStyleBegin + 
  84.                             this.InnerText + Tags.XmlCommentOldStyleEnd;
  85.                     case XmlNodeType.CDATA:
  86.                         return Tags.XmlCharacterDataBegin + this.InnerText + 
  87.                             Tags.XmlCharacterDataEnd;
  88.                     default:
  89.                         Debug.Assert(false, "Invalid node type.");
  90.                         return string.Empty;
  91.                 }
  92.             }
  93.         }
  94.         #endregion
  95.         #region Methods section
  96.         /// <summary>
  97.         /// Creates a complete copy of the current node.
  98.         /// </summary>
  99.         /// <param name="deep">this parameter is deprecated</param>
  100.         /// <returns>a node object of type XmlDiffViewCharData</returns>
  101.         internal override XmlDiffViewNode Clone(bool deep)
  102.         {
  103.             return new XmlDiffViewCharData(this.InnerText, NodeType);
  104.         }
  105.         /// <summary>
  106.         /// Generates  output data in html form
  107.         /// </summary>
  108.         /// <param name="writer">output stream</param>
  109.         /// <param name="indent">number of indentations</param>
  110.         internal override void DrawHtml(XmlWriter writer, int indent)
  111.         {
  112.             if (Operation == XmlDiffViewOperation.Change)
  113.             {
  114.                 string openString = string.Empty;
  115.                 string closeString = string.Empty;
  116.                 //Note: OuterXml function is not used here 
  117.                 //      in order that the tags
  118.                 //      can correctly wrap the data.
  119.                 if (NodeType == XmlNodeType.CDATA)
  120.                 {
  121.                     openString = Tags.XmlCharacterDataBegin;
  122.                     closeString = Tags.XmlCharacterDataEnd;
  123.                 }
  124.                 else if (NodeType == XmlNodeType.Comment)
  125.                 {
  126.                     openString = Tags.XmlCommentOldStyleBegin;
  127.                     closeString = Tags.XmlCommentOldStyleEnd;
  128.                 }
  129.                 XmlDiffView.HtmlStartRow(writer);
  130.                 this.DrawLinkNode(writer);
  131.                 XmlDiffView.HtmlStartCell(writer, indent);
  132.                 if (openString != string.Empty)
  133.                 {
  134.                     XmlDiffView.HtmlWriteString(writer, openString);
  135.                     XmlDiffView.HtmlWriteString(
  136.                         writer, 
  137.                         XmlDiffViewOperation.Change, 
  138.                         this.InnerText);
  139.                     XmlDiffView.HtmlWriteString(writer, closeString);
  140.                 }
  141.                 else
  142.                 {
  143.                     XmlDiffView.HtmlWriteString(
  144.                         writer, 
  145.                         XmlDiffViewOperation.Change, 
  146.                         this.InnerText);
  147.                 }
  148.                 XmlDiffView.HtmlEndCell(writer);                
  149.                 XmlDiffView.HtmlStartCell(writer, indent);
  150.                 if (openString != string.Empty)
  151.                 {
  152.                     XmlDiffView.HtmlWriteString(writer, openString);
  153.                     XmlDiffView.HtmlWriteString(
  154.                         writer, 
  155.                         XmlDiffViewOperation.Change, 
  156.                         ChangeInformation.Subset);
  157.                     XmlDiffView.HtmlWriteString(writer, closeString);
  158.                 }
  159.                 else
  160.                 {
  161.                     XmlDiffView.HtmlWriteString(
  162.                         writer, 
  163.                         XmlDiffViewOperation.Change, 
  164.                         ChangeInformation.Subset);
  165.                 }
  166.                 XmlDiffView.HtmlEndCell(writer);
  167.                 XmlDiffView.HtmlEndRow(writer);
  168.             }
  169.             else
  170.             {
  171.                 DrawHtmlNoChange(writer, indent);
  172.             }
  173.         }
  174.         
  175.         /// <summary>
  176.         /// Generates  output data in text form
  177.         /// </summary>
  178.         /// <param name="writer">output stream</param>
  179.         /// <param name="indent">number of indentations</param>
  180.         internal override void DrawText(TextWriter writer, int indent)
  181.         {
  182.             Debug.Assert(XmlNodeType.Comment == NodeType ||
  183.                 XmlNodeType.CDATA == NodeType ||
  184.                 XmlNodeType.Text == NodeType);
  185.             
  186.             // indent the text.
  187.             writer.Write(XmlDiffView.IndentText(indent));
  188.             switch (Operation)
  189.             {
  190.                 case XmlDiffViewOperation.Add:
  191.                     this.DrawTextAdd(writer);
  192.                     break;
  193.                 case XmlDiffViewOperation.Change:
  194.                     this.DrawTextChange(writer);
  195.                     break;
  196.                 case XmlDiffViewOperation.Ignore:
  197.                     // suppress the output                    
  198.                     break;
  199.                 case XmlDiffViewOperation.Match:
  200.                     DrawTextNoChange(writer, indent);
  201.                     break;
  202.                 case XmlDiffViewOperation.MoveFrom:
  203.                     Debug.Assert(false);
  204.                     break;
  205.                 case XmlDiffViewOperation.MoveTo:
  206.                     Debug.Assert(false);
  207.                     break;
  208.                 case XmlDiffViewOperation.Remove:
  209.                     this.DrawTextDelete(writer);
  210.                     break;
  211.                 default:
  212.                     Debug.Assert(false);
  213.                     break;
  214.             }
  215.         }
  216.         /// <summary>
  217.         /// Set xml tags based on the type of object.
  218.         /// </summary>
  219.         /// <param name="nodeType">The type of xml node.</param>
  220.         private void SetOpeningAndClosingTags(XmlNodeType nodeType)
  221.         {
  222.             switch (nodeType)
  223.             {
  224.                 case XmlNodeType.CDATA:
  225.                     this.openString = Tags.XmlCharacterDataBegin;
  226.                     this.closeString = Tags.XmlCharacterDataEnd;
  227.                     break;
  228.                 case XmlNodeType.Comment:
  229.                     this.openString = Tags.XmlCommentOldStyleBegin;
  230.                     this.closeString = Tags.XmlCommentOldStyleEnd;
  231.                     break;
  232.                 case XmlNodeType.Attribute:
  233.                     Debug.Assert(false);
  234.                     break;
  235.                 case XmlNodeType.Document:
  236.                     Debug.Assert(false);
  237.                     break;
  238.                 case XmlNodeType.DocumentFragment:
  239.                     Debug.Assert(false);
  240.                     break;
  241.                 case XmlNodeType.DocumentType:
  242.                     Debug.Assert(false);
  243.                     break;
  244.                 case XmlNodeType.Element:
  245.                     Debug.Assert(false);
  246.                     break;
  247.                 case XmlNodeType.EndElement:
  248.                     Debug.Assert(false);
  249.                     break;
  250.                 case XmlNodeType.EndEntity:
  251.                     Debug.Assert(false);
  252.                     break;
  253.                 case XmlNodeType.Entity:
  254.                     Debug.Assert(false);
  255.                     break;
  256.                 case XmlNodeType.EntityReference:
  257.                     Debug.Assert(false);
  258.                     break;
  259.                 case XmlNodeType.None:
  260.                     Debug.Assert(false);
  261.                     break;
  262.                 case XmlNodeType.Notation:
  263.                     Debug.Assert(false);
  264.                     break;
  265.                 case XmlNodeType.ProcessingInstruction:
  266.                     Debug.Assert(false);
  267.                     break;
  268.                 case XmlNodeType.SignificantWhitespace:
  269.                     Debug.Assert(false);
  270.                     break;
  271.                 case XmlNodeType.Text:
  272.                     this.openString = string.Empty;
  273.                     this.closeString = string.Empty;
  274.                     break;
  275.                 case XmlNodeType.Whitespace:
  276.                     Debug.Assert(false);
  277.                     break;
  278.                 case XmlNodeType.XmlDeclaration:
  279.                     Debug.Assert(false);
  280.                     break;
  281.                 default:
  282.                     Debug.Assert(false);
  283.                     break;
  284.             }
  285.         }
  286.         /// <summary>
  287.         /// Generates output text when the difference is due to adding data
  288.         /// </summary>
  289.         /// <param name="writer">output stream</param>
  290.         private void DrawTextAdd(TextWriter writer)
  291.         {
  292.             Debug.Assert(XmlDiffViewOperation.Add == Operation);
  293.             // Remove tabs and newlines from values
  294.             this.InnerText = this.InnerText;
  295.             this.SetOpeningAndClosingTags(NodeType);
  296.             if (this.openString != string.Empty)
  297.             {
  298.                 writer.Write(this.openString);
  299.                 writer.Write(Difference.Tag + 
  300.                     Difference.TextAddedBegin + 
  301.                     this.InnerText + 
  302.                     Difference.TextAddedEnd);
  303.                 writer.Write(this.closeString);
  304.             }
  305.             else
  306.             {
  307.                 writer.Write(Difference.Tag + 
  308.                     Difference.TextAddedBegin +
  309.                     this.InnerText +
  310.                     Difference.TextAddedEnd);
  311.             }
  312.             writer.Write(writer.NewLine);
  313.         }
  314.         /// <summary>
  315.         /// Generates output text when the difference is due to deleting data
  316.         /// </summary>
  317.         /// <param name="writer">output stream</param>
  318.         private void DrawTextDelete(TextWriter writer)
  319.         {
  320.             Debug.Assert(XmlDiffViewOperation.Remove == Operation);
  321.             // Remove tabs and newlines from values
  322.             this.InnerText = this.InnerText;
  323.             this.SetOpeningAndClosingTags(NodeType);
  324.             if (this.openString != string.Empty)
  325.             {
  326.                 writer.Write(this.openString);
  327.                 writer.Write(Difference.Tag + 
  328.                     Difference.TextDeletedBegin +
  329.                     this.InnerText +
  330.                     Difference.TextDeletedEnd);
  331.                 writer.Write(this.closeString);
  332.             }
  333.             else
  334.             {
  335.                 writer.Write(Difference.Tag + 
  336.                     Difference.TextDeletedBegin +
  337.                     this.InnerText +
  338.                     Difference.TextDeletedEnd);
  339.             }
  340.             writer.Write(writer.NewLine);
  341.         }
  342.         /// <summary>
  343.         /// Generates output text when the difference
  344.         ///  is due to changing existing data
  345.         /// </summary>
  346.         /// <param name="writer">output stream</param>
  347.         private void DrawTextChange(TextWriter writer)
  348.         {
  349.             Debug.Assert(XmlDiffViewOperation.Change == Operation);
  350.             // Remove tabs and newlines from values
  351.             this.InnerText = this.InnerText;
  352.             ChangeInformation.Subset = RemoveTabsAndNewlines(
  353.                 ChangeInformation.Subset);
  354.             this.SetOpeningAndClosingTags(NodeType);
  355.             if (this.openString != string.Empty)
  356.             {
  357.                 writer.Write(this.openString);
  358.                 writer.Write(Difference.Tag + 
  359.                     Difference.ChangeBegin + 
  360.                     this.InnerText +
  361.                     Difference.ChangeTo + 
  362.                     ChangeInformation.Subset + 
  363.                     Difference.ChangeEnd);
  364.                 writer.Write(this.closeString);
  365.             }
  366.             else
  367.             {
  368.                 writer.Write(Difference.Tag +
  369.                     Difference.ChangeBegin +
  370.                     this.InnerText +
  371.                     Difference.ChangeTo +
  372.                     ChangeInformation.Subset +
  373.                     Difference.ChangeEnd);
  374.             }
  375.             writer.Write(writer.NewLine);
  376.         }
  377.         /// <summary>
  378.         /// Generates output text when the difference is 
  379.         /// due to moving data from a location.
  380.         /// </summary>
  381.         /// <param name="writer">output stream</param>
  382.         private void DrawTextMoveFrom(TextWriter writer)
  383.         {
  384.             Debug.Assert(XmlDiffViewOperation.MoveFrom == Operation);
  385.             // Remove tabs and newlines from values
  386.             this.InnerText = this.InnerText;
  387.             this.SetOpeningAndClosingTags(NodeType);
  388.             if (this.openString != string.Empty)
  389.             {
  390.                 writer.Write(this.openString);
  391.                 writer.Write(Difference.Tag + 
  392.                     Difference.TextMovedFromBegin + 
  393.                     this.InnerText +
  394.                     Difference.TextMovedFromEnd);
  395.                 writer.Write(this.closeString);
  396.             }
  397.             else
  398.             {
  399.                 writer.Write(Difference.Tag +
  400.                     Difference.TextMovedFromBegin +
  401.                     this.InnerText +
  402.                     Difference.TextMovedFromEnd);
  403.             }
  404.             writer.Write(writer.NewLine);
  405.         }
  406.         /// <summary>
  407.         /// Generates output text when the difference is 
  408.         /// due to moving data to a location.
  409.         /// </summary>
  410.         /// <param name="writer">output stream</param>
  411.         private void DrawTextMoveTo(TextWriter writer)
  412.         {
  413.             Debug.Assert(XmlDiffViewOperation.MoveTo == Operation);
  414.             // Remove tabs and newlines from values
  415.             this.InnerText = this.InnerText;
  416.             this.SetOpeningAndClosingTags(NodeType);
  417.             if (this.openString != string.Empty)
  418.             {
  419.                 writer.Write(this.openString);
  420.                 writer.Write(Difference.Tag +
  421.                     Difference.TextMovedToBegin +
  422.                     this.InnerText +
  423.                     Difference.TextMovedToEnd);
  424.                 writer.Write(this.closeString);
  425.             }
  426.             else
  427.             {
  428.                 writer.Write(Difference.Tag +
  429.                     Difference.TextMovedToBegin +
  430.                     this.InnerText +
  431.                     Difference.TextMovedToEnd);
  432.             }
  433.             writer.Write(writer.NewLine);
  434.         }
  435.         #endregion
  436.      }
  437. }