XmlCharacterData.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // System.Xml.XmlCharacterData.cs
  3. //
  4. // Authors:
  5. // Jason Diamond <[email protected]>
  6. // Kral Ferch <[email protected]>
  7. //
  8. // (C) 2002 Jason Diamond, Kral Ferch
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Xml.XPath;
  32. namespace System.Xml
  33. {
  34. public abstract class XmlCharacterData : XmlLinkedNode
  35. {
  36. private string data;
  37. #region Constructor
  38. protected internal XmlCharacterData (string data, XmlDocument doc)
  39. : base (doc)
  40. {
  41. if (data == null)
  42. data = String.Empty;
  43. this.data = data;
  44. }
  45. #endregion
  46. #region Properties
  47. public virtual string Data {
  48. get { return data; }
  49. set {
  50. OwnerDocument.onNodeChanging (this, this.ParentNode);
  51. data = value;
  52. OwnerDocument.onNodeChanged (this, this.ParentNode);
  53. }
  54. }
  55. public override string InnerText {
  56. get { return data; }
  57. set { Data = value; } // invokes events
  58. }
  59. public virtual int Length {
  60. get { return data != null ? data.Length : 0; }
  61. }
  62. public override string Value {
  63. get { return data; }
  64. set {
  65. Data = value;
  66. }
  67. }
  68. internal override XPathNodeType XPathNodeType {
  69. get { return XPathNodeType.Text; }
  70. }
  71. #endregion
  72. #region Methods
  73. public virtual void AppendData (string strData)
  74. {
  75. OwnerDocument.onNodeChanging (this, this.ParentNode);
  76. data += strData;
  77. OwnerDocument.onNodeChanged (this, this.ParentNode);
  78. }
  79. public virtual void DeleteData (int offset, int count)
  80. {
  81. OwnerDocument.onNodeChanging (this, this.ParentNode);
  82. if (offset < 0)
  83. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  84. int newCount = data.Length - offset;
  85. if ((offset + count) < data.Length)
  86. newCount = count;
  87. data = data.Remove (offset, newCount);
  88. OwnerDocument.onNodeChanged (this, this.ParentNode);
  89. }
  90. public virtual void InsertData (int offset, string strData)
  91. {
  92. OwnerDocument.onNodeChanging (this, this.ParentNode);
  93. if ((offset < 0) || (offset > data.Length))
  94. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  95. data = data.Insert(offset, strData);
  96. OwnerDocument.onNodeChanged (this, this.ParentNode);
  97. }
  98. public virtual void ReplaceData (int offset, int count, string strData)
  99. {
  100. OwnerDocument.onNodeChanging (this, this.ParentNode);
  101. if ((offset < 0) || (offset > data.Length))
  102. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  103. if (count < 0)
  104. throw new ArgumentOutOfRangeException ("count", "Must be non-negative.");
  105. if (strData == null)
  106. throw new ArgumentNullException ("strData", "Must be non-null.");
  107. string newData = data.Substring (0, offset) + strData;
  108. if ((offset + count) < data.Length)
  109. newData += data.Substring (offset + count);
  110. data = newData;
  111. OwnerDocument.onNodeChanged (this, this.ParentNode);
  112. }
  113. public virtual string Substring (int offset, int count)
  114. {
  115. if (data.Length < offset + count)
  116. return data.Substring (offset);
  117. else
  118. return data.Substring (offset, count);
  119. }
  120. #endregion
  121. }
  122. }