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

xml/soap/webservice

开发平台:

Visual C++

  1. //  ---------------------------------------------------------------------------
  2. // <copyright company="Microsoft Corporation" file="XmlDiffViewParentNode.cs">
  3. //     Copyright (c) Microsoft Corporation 2005
  4. // </copyright>
  5. // <project>
  6. //     XmlDiffView
  7. // </project>
  8. // <summary>
  9. //     Provides access to the parent node object and its child node objects.
  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 access the parent node object and its child node objects.
  23.     /// </summary>
  24.     internal abstract class XmlDiffViewParentNode : XmlDiffViewNode
  25.     {
  26.         #region Member variables section
  27.         // child nodes
  28.         private XmlDiffViewNode childNodes;
  29.         // number of source child nodes
  30.         private int sourceChildNodesCount;
  31.         // source nodes indexed by their relative position
  32.         private XmlDiffViewNode[] sourceChildNodesIndex;
  33.         
  34.         #endregion
  35.         
  36.         #region  Constructors section
  37.         /// <summary>
  38.         /// Constructor
  39.         /// </summary>
  40.         /// <param name="nodeType">Type of xml node</param>
  41.         internal XmlDiffViewParentNode(
  42.             XmlNodeType nodeType) : base(nodeType) 
  43.         {
  44.         }
  45.         
  46.         #endregion
  47.         #region Properties section
  48.         /// <summary>
  49.         /// Gets or sets a reference to the childnodes
  50.         /// </summary>
  51.         public XmlDiffViewNode ChildNodes
  52.         {
  53.             get
  54.             {
  55.                 return this.childNodes;
  56.             }
  57.             set
  58.             {
  59.                 this.childNodes = value;
  60.             }
  61.         }
  62.         /// <summary>
  63.         /// Gets or sets the number of child nodes
  64.         /// </summary>
  65.         public int SourceChildNodesCount
  66.         {
  67.             get
  68.             {
  69.                 return this.sourceChildNodesCount;
  70.             }
  71.             set
  72.             {
  73.                 this.sourceChildNodesCount = value;
  74.             }
  75.         }
  76.         /// <summary>
  77.         /// Gets the first child node
  78.         /// </summary>
  79.         internal override XmlDiffViewNode FirstChildNode
  80.         { 
  81.             get 
  82.             { 
  83.                 return this.ChildNodes; 
  84.             } 
  85.         }
  86.         /// <summary>
  87.         /// Gets or sets the collection of baseline child nodes
  88.         /// </summary>
  89.         private XmlDiffViewNode[] SourceChildNodesIndex
  90.         {
  91.             get
  92.             {
  93.                 return this.sourceChildNodesIndex;
  94.             }
  95.             set
  96.             {
  97.                 this.sourceChildNodesIndex = value;
  98.             }
  99.         }
  100.         #endregion
  101.         
  102.         #region Methods section
  103.         /// <summary>
  104.         /// Gets a particular child node based on its index.
  105.         /// </summary>
  106.         /// <param name="index">index of the child node</param>
  107.         /// <returns>child node</returns>
  108.         /// <exception cref="ArgumentException">Thrown when the
  109.         /// index value is out of bounds (Has the CreateSourceNodesIndex
  110.         ///  method been called?)</exception>
  111.         internal XmlDiffViewNode GetSourceChildNode(int index) 
  112.         { 
  113.             if (index < 0 || 
  114.                 index >= this.SourceChildNodesCount || 
  115.                 this.SourceChildNodesCount == 0)
  116.             {
  117.                 throw new ArgumentException("index");
  118.             }
  119.             if (this.SourceChildNodesCount == 0)
  120.             {
  121.                     return null;
  122.             }
  123.             if (this.SourceChildNodesIndex == null)
  124.             {
  125.                     this.CreateSourceNodesIndex();
  126.             }
  127.             return this.SourceChildNodesIndex[index];
  128.         }
  129.         /// <summary>
  130.         /// Creates an indexed collection of child nodes.
  131.         /// </summary>
  132.         internal void CreateSourceNodesIndex()
  133.         {
  134.             if (this.SourceChildNodesIndex != null || 
  135.                 this.SourceChildNodesCount == 0)
  136.             {
  137.                     return;
  138.             }
  139.             this.SourceChildNodesIndex = new 
  140.                 XmlDiffViewNode[this.SourceChildNodesCount];
  141.         
  142.             XmlDiffViewNode curChild = this.ChildNodes;
  143.             for (int i = 0; i < this.SourceChildNodesCount; i++, curChild = curChild.NextSibbling) 
  144.             {
  145.                 Debug.Assert(curChild != null);
  146.                 this.SourceChildNodesIndex[i] = curChild;
  147.             }
  148.             Debug.Assert(curChild == null);
  149.         }
  150.         /// <summary>
  151.         /// Inserts a node after the specified node
  152.         /// </summary>
  153.         /// <param name="newChild">node to insert</param>
  154.         /// <param name="referenceChild">node to insert after</param>
  155.         /// <param name="sourceNode">This is a baseline node</param>
  156.         internal void InsertChildAfter(
  157.             XmlDiffViewNode newChild, 
  158.             XmlDiffViewNode referenceChild, 
  159.             bool sourceNode) 
  160.         {
  161.             Debug.Assert(newChild != null);
  162.             if (referenceChild == null) 
  163.             {
  164.                 newChild.NextSibbling = this.ChildNodes;
  165.                 this.ChildNodes = newChild;
  166.             }
  167.             else 
  168.             {
  169.                 newChild.NextSibbling = referenceChild.NextSibbling;
  170.                 referenceChild.NextSibbling = newChild;
  171.             }
  172.             if (sourceNode)
  173.             {
  174.                 this.SourceChildNodesCount++;
  175.             }
  176.             newChild.Parent = this;
  177.         }
  178.         /// <summary>
  179.         /// Generates  output data in html form
  180.         /// </summary>
  181.         /// <param name="writer">output stream</param>
  182.         /// <param name="indent">number of indentations</param>
  183.         internal void HtmlDrawChildNodes(XmlWriter writer, int indent) 
  184.         {
  185.             XmlDiffViewNode curChild = this.ChildNodes;
  186.             while (curChild != null) 
  187.             {
  188.                 curChild.DrawHtml(writer, indent);
  189.                 curChild = curChild.NextSibbling;
  190.             }
  191.         }
  192.         /// <summary>
  193.         /// Generates output data in text form
  194.         /// </summary>
  195.         /// <param name="writer">output stream</param>
  196.         /// <param name="indent">number of indentations</param>
  197.         internal void TextDrawChildNodes(
  198.             TextWriter writer, 
  199.             int indent)
  200.         {
  201.             indent += Indent.IncrementSize;
  202.             XmlDiffViewNode curChild = this.ChildNodes;
  203.             while (curChild != null) 
  204.             {
  205.                 curChild.DrawText(writer, indent);
  206.                 curChild = curChild.NextSibbling;
  207.             }
  208.         }
  209.         #endregion
  210.         
  211.     }
  212. }