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

xml/soap/webservice

开发平台:

Visual C++

  1. //  ---------------------------------------------------------------------------
  2. // <copyright company="Microsoft Corporation" file="XmlDiffViewPI.cs">
  3. //     Copyright (c) Microsoft Corporation 2005
  4. // </copyright>
  5. // <project>
  6. //     XmlDiffView
  7. // </project>
  8. // <summary>
  9. //     Generate output data for programming instruction nodes.
  10. // </summary>
  11. // <history>
  12. //      [barryw] 03MAR05 Created
  13. // </history>
  14. //  ---------------------------------------------------------------------------
  15. namespace Microsoft.XmlDiffPatch
  16. {
  17.     using System;
  18.     using System.Xml;
  19.     using System.IO;
  20.     using System.Diagnostics;
  21.     /// <summary>
  22.     /// Class to generate output data for programming instruction nodes.
  23.     /// </summary>
  24.     internal class XmlDiffViewPI : XmlDiffViewCharData
  25.     {
  26.         #region Member variables section
  27.         /// <summary>
  28.         /// Name of the programing instruction.
  29.         /// </summary>
  30.         private string name;
  31.         #endregion
  32.         #region  Constructors section
  33.         /// <summary>
  34.         /// Constructor
  35.         /// </summary>
  36.         /// <param name="name">name of programming instruction</param>
  37.         /// <param name="value">value of programming instruction</param>
  38.         internal XmlDiffViewPI(
  39.             string name, 
  40.             string value) : 
  41.             base(value, XmlNodeType.ProcessingInstruction)
  42.         {
  43.             this.Name = name;
  44.         }
  45.         #endregion
  46.         #region Properties section
  47.         /// <summary>
  48.         /// Name of the programming instruction.
  49.         /// </summary>
  50.         public string Name
  51.         {
  52.             get
  53.             {
  54.                 return this.name;
  55.             }
  56.             set
  57.             {
  58.                 this.name = value;
  59.             }
  60.         }
  61.         /// <summary>
  62.         /// Gets or sets the programming instruction statement
  63.         /// </summary>
  64.         public override string OuterXml
  65.         {
  66.             get
  67.             {
  68.                 return Tags.XmlErrorHandlingBegin +
  69.                     this.Name + " " +
  70.                     RemoveTabsAndNewlines(InnerText) +
  71.                     Tags.XmlErrorHandlingEnd;
  72.             }
  73.         }
  74.         #endregion
  75.         #region Methods section
  76.         /// <summary>
  77.         /// Creates a complete copy of the current node.
  78.         /// </summary>
  79.         /// <param name="deep">deprecated</param>
  80.         /// <returns>a programming instruction node</returns>
  81.         internal override XmlDiffViewNode Clone(bool deep)
  82.         {
  83.             return new XmlDiffViewPI(this.Name, this.InnerText);
  84.         }
  85.         /// <summary>
  86.         /// Generates output data in html form.
  87.         /// </summary>
  88.         /// <param name="writer">output data stream</param>
  89.         /// <param name="indent">size of indentation</param>
  90.         internal override void DrawHtml(XmlWriter writer, int indent)
  91.         {
  92.             if (Operation == XmlDiffViewOperation.Change)
  93.             {
  94.                 XmlDiffViewOperation nameOp = (this.Name == ChangeInformation.LocalName) ? XmlDiffViewOperation.Match : XmlDiffViewOperation.Change;
  95.                 XmlDiffViewOperation valueOp = (this.InnerText == ChangeInformation.Subset) ? XmlDiffViewOperation.Match : XmlDiffViewOperation.Change;
  96.                 XmlDiffView.HtmlStartRow(writer);
  97.                 this.DrawLinkNode(writer);
  98.                 XmlDiffView.HtmlStartCell(writer, indent);
  99.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlErrorHandlingBegin);
  100.                 XmlDiffView.HtmlWriteString(writer, nameOp, this.Name);
  101.                 XmlDiffView.HtmlWriteString(writer, " ");
  102.                 XmlDiffView.HtmlWriteString(
  103.                     writer,
  104.                     valueOp,
  105.                     RemoveTabsAndNewlines(InnerText));
  106.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlErrorHandlingEnd);
  107.                 XmlDiffView.HtmlEndCell(writer);
  108.                 XmlDiffView.HtmlStartCell(writer, indent);
  109.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlErrorHandlingBegin);
  110.                 XmlDiffView.HtmlWriteString(
  111.                     writer,
  112.                     nameOp,
  113.                     ChangeInformation.LocalName);
  114.                 XmlDiffView.HtmlWriteString(writer, " ");
  115.                 XmlDiffView.HtmlWriteString(
  116.                     writer,
  117.                     valueOp,
  118.                     RemoveTabsAndNewlines(ChangeInformation.Subset));
  119.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlErrorHandlingEnd);
  120.                 XmlDiffView.HtmlEndCell(writer);
  121.                 XmlDiffView.HtmlEndRow(writer);
  122.             }
  123.             else
  124.             {
  125.                 DrawHtmlNoChange(writer, indent);
  126.             }
  127.         }
  128.         /// <summary>
  129.         /// Generates output data in text form.
  130.         /// </summary>
  131.         /// <param name="writer">output data stream</param>
  132.         /// <param name="indent">size of indentation</param>
  133.         internal override void DrawText(TextWriter writer, int indent)
  134.         {
  135.             Debug.Assert(XmlNodeType.ProcessingInstruction == NodeType);
  136.             writer.Write(XmlDiffView.IndentText(indent));
  137.             switch (Operation)
  138.             {
  139.                 case XmlDiffViewOperation.Add:
  140.                     // component name is the new
  141.                     writer.Write(Tags.XmlErrorHandlingBegin +
  142.                         this.Name +
  143.                         " ");
  144.                     // add difference attribute
  145.                     writer.Write(Difference.Tag + Difference.PIAdded);
  146.                     // process other attributes
  147.                     this.DrawAttributes(writer, indent);
  148.                     // close tag
  149.                     writer.Write(Tags.XmlErrorHandlingEnd);
  150.                     break;
  151.                 case XmlDiffViewOperation.Change:
  152.                 {
  153.                     // Determine nature of changes
  154.                     if (this.Name == ChangeInformation.LocalName)
  155.                     {
  156.                         // component name is the same
  157.                         writer.Write(Tags.XmlErrorHandlingBegin +
  158.                             this.Name +
  159.                             " ");
  160.                         // process attributes
  161.                         this.DrawAttributes(writer, indent);
  162.                         // close tag
  163.                         writer.Write(Tags.XmlErrorHandlingEnd);
  164.                     }
  165.                     else
  166.                     {
  167.                         // component name changed
  168.                         //Note: <?{new name of component} xd_="Rename(component)From('original name')" [, {attributes, values} ?>
  169.                         writer.Write(Tags.XmlErrorHandlingBegin +
  170.                             ChangeInformation.LocalName +
  171.                             " ");
  172.                         writer.Write(Difference.Tag + "=" +
  173.                             Difference.PIRenamedBegin +
  174.                             this.Name + Difference.PIRenamedEnd);
  175.                         // process attributes
  176.                         this.DrawAttributes(writer, indent);
  177.                         // close tag
  178.                         writer.Write(Tags.XmlErrorHandlingEnd);
  179.                     }
  180.                     writer.Write(writer.NewLine);
  181.                 }
  182.                     break;
  183.                 case XmlDiffViewOperation.Ignore:
  184.                     Debug.Assert(false);
  185.                     break;
  186.                 case XmlDiffViewOperation.Match:
  187.                     DrawTextNoChange(writer, indent);
  188.                     break;
  189.                 case XmlDiffViewOperation.MoveFrom:
  190.                     Debug.Assert(false);
  191.                     break;
  192.                 case XmlDiffViewOperation.MoveTo:
  193.                     Debug.Assert(false);
  194.                     break;
  195.                 case XmlDiffViewOperation.Remove:
  196.                     // component removed
  197.                     writer.Write(Tags.XmlErrorHandlingBegin +
  198.                         this.Name +
  199.                         " ");
  200.                     // add difference attribute
  201.                     writer.Write(Difference.Tag + Difference.PIDeleted);
  202.                     // process other attributes
  203.                     this.DrawAttributes(writer, indent);
  204.                     // close tag
  205.                     writer.Write(Tags.XmlErrorHandlingEnd);
  206.                     break;
  207.                 default:
  208.                     Debug.Assert(false);
  209.                     break;
  210.             }
  211.             writer.Write(writer.NewLine);
  212.         }
  213.         /// <summary>
  214.         /// Generates output data for attributes in text form.
  215.         /// </summary>
  216.         /// <param name="writer">output data stream</param>
  217.         /// <param name="indent">size of indentation</param>
  218.         private void DrawAttributes(
  219.             TextWriter writer,
  220.             int indent)
  221.         {
  222.             if (Operation == XmlDiffViewOperation.Change)
  223.             {
  224.                 // determine if there was a sudo-attributes change
  225.                 if (InnerText != ChangeInformation.Subset)
  226.                 {
  227.                     writer.Write(Tags.XmlCharacterDataBegin +
  228.                         Difference.Tag + Difference.ChangeBegin +
  229.                         RemoveTabsAndNewlines(ChangeInformation.Subset) +
  230.                         Difference.ChangeTo +
  231.                         RemoveTabsAndNewlines(InnerText) +
  232.                         Difference.ChangeEnd +
  233.                         Tags.XmlCharacterDataEnd);
  234.                     return;
  235.                 }
  236.             }
  237.             // no change in sudo-attributes
  238.             writer.Write(RemoveTabsAndNewlines(InnerText));
  239.         }
  240.         #endregion
  241.     }
  242. }