2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml / XmlCharacterData.cs
blob3623f1611319dfc579051d4a35a2906e76145c00
1 //
2 // System.Xml.XmlCharacterData.cs
3 //
4 // Authors:
5 // Jason Diamond <jason@injektilo.org>
6 // Kral Ferch <kral_ferch@hotmail.com>
7 //
8 // (C) 2002 Jason Diamond, Kral Ferch
9 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 //
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 //
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System;
33 using System.Xml.XPath;
35 namespace System.Xml
37 public abstract class XmlCharacterData : XmlLinkedNode
39 private string data;
41 #region Constructor
43 protected internal XmlCharacterData (string data, XmlDocument doc)
44 : base (doc)
46 if (data == null)
47 data = String.Empty;
49 this.data = data;
52 #endregion
54 #region Properties
56 public virtual string Data {
57 get { return data; }
59 set {
60 string old = data;
61 OwnerDocument.onNodeChanging (this, this.ParentNode, old, value);
63 data = value;
65 OwnerDocument.onNodeChanged (this, this.ParentNode, old, value);
69 public override string InnerText {
70 get { return data; }
72 set { Data = value; } // invokes events
75 public virtual int Length {
76 get { return data != null ? data.Length : 0; }
79 public override string Value {
80 get { return data; }
82 set {
83 Data = value;
87 internal override XPathNodeType XPathNodeType {
88 get { return XPathNodeType.Text; }
91 #endregion
93 #region Methods
95 public virtual void AppendData (string strData)
97 string oldData = data;
98 string newData = data += strData;
99 OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
100 data = newData;
101 OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
104 public virtual void DeleteData (int offset, int count)
106 if (offset < 0)
107 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
109 int newCount = data.Length - offset;
111 if ((offset + count) < data.Length)
112 newCount = count;
114 string oldValue = data;
115 string newValue = data.Remove (offset, newCount);
117 OwnerDocument.onNodeChanging (this, this.ParentNode, oldValue, newValue);
119 data = newValue;
121 OwnerDocument.onNodeChanged (this, this.ParentNode, oldValue, newValue);
124 public virtual void InsertData (int offset, string strData)
126 if ((offset < 0) || (offset > data.Length))
127 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
129 string oldData = data;
130 string newData = data.Insert(offset, strData);
132 OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
134 data = newData;
136 OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
139 public virtual void ReplaceData (int offset, int count, string strData)
141 if ((offset < 0) || (offset > data.Length))
142 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
144 if (count < 0)
145 throw new ArgumentOutOfRangeException ("count", "Must be non-negative.");
147 if (strData == null)
148 throw new ArgumentNullException ("strData", "Must be non-null.");
150 string oldData = data;
151 string newData = data.Substring (0, offset) + strData;
153 if ((offset + count) < data.Length)
154 newData += data.Substring (offset + count);
156 OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
158 data = newData;
160 OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
163 public virtual string Substring (int offset, int count)
165 if (data.Length < offset + count)
166 return data.Substring (offset);
167 else
168 return data.Substring (offset, count);
170 #endregion