New test.
[mono.git] / mcs / class / System.XML / System.Xml / XmlCharacterData.cs
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 //
10
11 //
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.
30 //
31
32 using System;
33 using System.Xml.XPath;
34
35 namespace System.Xml
36 {
37         public abstract class XmlCharacterData : XmlLinkedNode
38         {
39                 private string data;
40
41                 #region Constructor
42
43                 protected internal XmlCharacterData (string data, XmlDocument doc)
44                         : base (doc)
45                 {
46                         if (data == null)
47                                 data = String.Empty;
48
49                         this.data = data;
50                 }
51
52                 #endregion
53                 
54                 #region Properties
55
56                 public virtual string Data {
57                         get { return data; }
58                         
59                         set {
60                                 string old = data;
61                                 OwnerDocument.onNodeChanging (this, this.ParentNode, old, value);
62
63                                 data = value;
64
65                                 OwnerDocument.onNodeChanged (this, this.ParentNode, old, value);
66                         }
67                 }
68
69                 public override string InnerText {
70                         get { return data; }
71
72                         set { Data = value; }   // invokes events
73                 }
74
75                 public virtual int Length {
76                         get { return data != null ? data.Length : 0; }
77                 }
78
79                 public override string Value {
80                         get { return data; }
81
82                         set {
83                                 Data = value;
84                         }
85                 }
86
87                 internal override XPathNodeType XPathNodeType {
88                         get { return XPathNodeType.Text; }
89                 }
90
91                 #endregion
92
93                 #region Methods
94
95                 public virtual void AppendData (string strData)
96                 {
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);
102                 }
103
104                 public virtual void DeleteData (int offset, int count)
105                 {
106                         if (offset < 0)
107                                 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
108
109                         int newCount = data.Length - offset;
110
111                         if ((offset + count) < data.Length)
112                                 newCount = count;
113
114                         string oldValue = data;
115                         string newValue = data.Remove (offset, newCount);
116
117                         OwnerDocument.onNodeChanging (this, this.ParentNode, oldValue, newValue);
118
119                         data = newValue;
120                         
121                         OwnerDocument.onNodeChanged (this, this.ParentNode, oldValue, newValue);
122                 }
123
124                 public virtual void InsertData (int offset, string strData)
125                 {
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.");
128
129                         string oldData = data;
130                         string newData = data.Insert(offset, strData);
131                         
132                         OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
133
134                         data = newData;
135
136                         OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
137                 }
138
139                 public virtual void ReplaceData (int offset, int count, string strData)
140                 {
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.");
143
144                         if (count < 0)
145                                 throw new ArgumentOutOfRangeException ("count", "Must be non-negative.");
146
147                         if (strData == null)
148                                 throw new ArgumentNullException ("strData", "Must be non-null.");
149
150                         string oldData = data;
151                         string newData = data.Substring (0, offset) + strData;
152                         
153                         if ((offset + count) < data.Length)
154                                 newData += data.Substring (offset + count);
155
156                         OwnerDocument.onNodeChanging (this, this.ParentNode, oldData, newData);
157
158                         data = newData;
159
160                         OwnerDocument.onNodeChanged (this, this.ParentNode, oldData, newData);
161                 }
162
163                 public virtual string Substring (int offset, int count)
164                 {
165                         if (data.Length < offset + count)
166                                 return data.Substring (offset);
167                         else
168                                 return data.Substring (offset, count);
169                 }
170                 #endregion
171         }
172 }