XmlCharacterData.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. string old = data;
  51. OwnerDocument.onNodeChanging (this, this.ParentNode, old, value);
  52. data = value;
  53. OwnerDocument.onNodeChanged (this, this.ParentNode, old, value);
  54. }
  55. }
  56. public override string InnerText {
  57. get { return data; }
  58. set { Data = value; } // invokes events
  59. }
  60. public virtual int Length {
  61. get { return data != null ? data.Length : 0; }
  62. }
  63. public override string Value {
  64. get { return data; }
  65. set {
  66. Data = value;
  67. }
  68. }
  69. internal override XPathNodeType XPathNodeType {
  70. get { return XPathNodeType.Text; }
  71. }
  72. #endregion
  73. #region Methods
  74. public virtual void AppendData (string strData)
  75. {
  76. string oldData = data;
  77. string newData = data += strData;
  78. OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
  79. data = newData;
  80. OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
  81. }
  82. public virtual void DeleteData (int offset, int count)
  83. {
  84. if (offset < 0)
  85. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  86. int newCount = data.Length - offset;
  87. if ((offset + count) < data.Length)
  88. newCount = count;
  89. string oldValue = data;
  90. string newValue = data.Remove (offset, newCount);
  91. OwnerDocument.onNodeChanging (this, this.ParentNode, oldValue, newValue);
  92. data = newValue;
  93. OwnerDocument.onNodeChanged (this, this.ParentNode, oldValue, newValue);
  94. }
  95. public virtual void InsertData (int offset, string strData)
  96. {
  97. if ((offset < 0) || (offset > data.Length))
  98. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  99. string oldData = data;
  100. string newData = data.Insert(offset, strData);
  101. OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
  102. data = newData;
  103. OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
  104. }
  105. public virtual void ReplaceData (int offset, int count, string strData)
  106. {
  107. if ((offset < 0) || (offset > data.Length))
  108. throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
  109. if (count < 0)
  110. throw new ArgumentOutOfRangeException ("count", "Must be non-negative.");
  111. if (strData == null)
  112. throw new ArgumentNullException ("strData", "Must be non-null.");
  113. string oldData = data;
  114. string newData = data.Substring (0, offset) + strData;
  115. if ((offset + count) < data.Length)
  116. newData += data.Substring (offset + count);
  117. OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
  118. data = newData;
  119. OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
  120. }
  121. public virtual string Substring (int offset, int count)
  122. {
  123. if (data.Length < offset + count)
  124. return data.Substring (offset);
  125. else
  126. return data.Substring (offset, count);
  127. }
  128. #endregion
  129. }
  130. }