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

xml/soap/webservice

开发平台:

Visual C++

  1. //  ---------------------------------------------------------------------------
  2. // <copyright company="Microsoft Corporation" file="XmlDiffViewXmlDeclaration.cs">
  3. //     Copyright (c) Microsoft Corporation 2005
  4. // </copyright>
  5. // <project>
  6. //     XmlDiffView
  7. // </project>
  8. // <summary>
  9. //     Generate differences data for xml declaration 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 differences data for xml declaration nodes. 
  23.     /// </summary>
  24.     internal class XmlDiffViewXmlDeclaration : XmlDiffViewNode
  25.     {
  26.         #region Member variables section
  27.         private string declarationValue;
  28.         #endregion
  29.         #region  Constructors section
  30.         /// <summary>
  31.         /// Constructor.
  32.         /// </summary>
  33.         /// <param name="value">the declaration's value</param>
  34.         internal XmlDiffViewXmlDeclaration(string value) : base(XmlNodeType.XmlDeclaration)
  35.         {
  36.             this.declarationValue = value;
  37.         }
  38.         #endregion
  39.         #region Properties section
  40.         /// <summary>
  41.         /// Gets the declaration value enclosed within xml declaration tags.
  42.         /// </summary>
  43.         public override string OuterXml
  44.         {
  45.             get
  46.             {
  47.                 return Tags.XmlDeclarationBegin + this.declarationValue + Tags.XmlDeclarationEnd;
  48.             }
  49.         }
  50.         #endregion
  51.         #region Methods section
  52.         /// <summary>
  53.         /// Creates a complete copy of the current node.
  54.         /// </summary>
  55.         /// <param name="deep">deprecated</param>
  56.         /// <returns>a declaration node</returns>
  57.         internal override XmlDiffViewNode Clone(bool deep)
  58.         {
  59.             return new XmlDiffViewXmlDeclaration(this.declarationValue);
  60.         }
  61.         /// <summary>
  62.         /// Generates  output data in html form
  63.         /// </summary>
  64.         /// <param name="writer">output stream</param>
  65.         /// <param name="indent">number of indentations</param>
  66.         internal override void DrawHtml(XmlWriter writer, int indent)
  67.         {
  68.             if (Operation == XmlDiffViewOperation.Change)
  69.             {
  70.                 Debug.Assert(this.declarationValue != ChangeInformation.Subset);
  71.                 XmlDiffView.HtmlStartRow(writer);
  72.                 this.DrawLinkNode(writer);
  73.                 XmlDiffView.HtmlStartCell(writer, indent);
  74.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlDeclarationBegin);
  75.                 XmlDiffView.HtmlWriteString(writer, XmlDiffViewOperation.Change, this.declarationValue);
  76.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlDeclarationEnd);
  77.                 XmlDiffView.HtmlEndCell(writer);
  78.                 XmlDiffView.HtmlStartCell(writer, indent);
  79.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlDeclarationBegin);
  80.                 XmlDiffView.HtmlWriteString(writer, XmlDiffViewOperation.Change, ChangeInformation.Subset);
  81.                 XmlDiffView.HtmlWriteString(writer, Tags.XmlDeclarationEnd);
  82.                 XmlDiffView.HtmlEndCell(writer);
  83.                 XmlDiffView.HtmlEndRow(writer);
  84.             }
  85.             else
  86.             {
  87.                 DrawHtmlNoChange(writer, indent);
  88.             }
  89.         }
  90.         /// <summary>
  91.         /// Generates  output data in text form
  92.         /// </summary>
  93.         /// <param name="writer">output stream</param>
  94.         /// <param name="indent">number of indentations</param>
  95.         internal override void DrawText(
  96.             TextWriter writer,
  97.             int indent)
  98.         {
  99.             Debug.Assert(XmlNodeType.XmlDeclaration == NodeType);
  100.             writer.Write(XmlDiffView.IndentText(indent));
  101.             switch (Operation)
  102.             {
  103.                 case XmlDiffViewOperation.Add:
  104.                     //Note: Can only have one valid declaration
  105.                     //      in an Xml document and it must be on
  106.                     //      the first line if it is present
  107.                     // component name is the new
  108.                     writer.Write(Tags.XmlDeclarationBegin);
  109.                     // add difference attribute
  110.                     writer.Write(Difference.Tag + Difference.DeclarationAdded);
  111.                     // process sudo-attributes
  112.                     //DrawAttributes(writer, indent);
  113.                     writer.Write(this.declarationValue);
  114.                     // close tag
  115.                     writer.Write(Tags.XmlDeclarationEnd);
  116.                     break;
  117.                 case XmlDiffViewOperation.Change:
  118.                     Debug.Assert(this.declarationValue != ChangeInformation.Subset);
  119.                     // component name is the same
  120.                     writer.Write(Tags.XmlDeclarationBegin);
  121.                     // process attributes
  122.                     //DrawAttributes(writer, indent);
  123.                     //Note: the following breaks xml validation but
  124.                     //      could not design a better alternative.
  125.                     writer.Write(Difference.Tag +
  126.                         Difference.ChangeBegin + this.declarationValue +
  127.                         Difference.ChangeTo + ChangeInformation.Subset +
  128.                         Difference.ChangeEnd);
  129.                     // close tag
  130.                     writer.Write(Tags.XmlDeclarationEnd);
  131.                     break;
  132.                 case XmlDiffViewOperation.Ignore:
  133.                     // suppress the output
  134.                     break;
  135.                 case XmlDiffViewOperation.Match:
  136.                     DrawTextNoChange(writer, indent);
  137.                     break;
  138.                 case XmlDiffViewOperation.MoveFrom:
  139.                     Debug.Assert(false);
  140.                     break;
  141.                 case XmlDiffViewOperation.MoveTo:
  142.                     Debug.Assert(false);
  143.                     break;
  144.                 case XmlDiffViewOperation.Remove:
  145.                     // component removed
  146.                     writer.Write(Tags.XmlDeclarationBegin);
  147.                     // add difference attribute
  148.                     writer.Write(Difference.Tag + Difference.DeclarationDeleted);
  149.                     // process sudo-attributes
  150.                     //DrawAttributes(writer, indent);
  151.                     writer.Write(this.declarationValue);
  152.                     // close tag
  153.                     writer.Write(Tags.XmlDeclarationEnd);
  154.                     break;
  155.                 default:
  156.                     Debug.Assert(false);
  157.                     break;
  158.             }
  159.             writer.Write(writer.NewLine);
  160.         }
  161.         #endregion
  162.     }
  163. }