2002-08-25 Tim Coleman <tim@timcoleman.com>
[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 using System;
12
13 namespace System.Xml
14 {
15         public abstract class XmlCharacterData : XmlLinkedNode
16         {
17                 private string data;
18
19                 #region Constructor
20
21                 protected internal XmlCharacterData (string data, XmlDocument doc)
22                         : base (doc)
23                 {
24                         if (data == null)
25                                 data = String.Empty;
26
27                         this.data = data;
28                 }
29
30                 #endregion
31                 
32                 #region Properties
33
34                 public virtual string Data {
35                         get { return data; }
36                         
37                         set { data = value; }
38                 }
39
40                 public override string InnerText {
41                         get { return data; }
42
43                         set { data = value; }
44                 }
45
46                 public virtual int Length {
47                         get { return data != null ? data.Length : 0; }
48                 }
49
50                 public override string Value {
51                         get { return data; }
52
53                         set {
54                                 OwnerDocument.onNodeChanging (this, this.ParentNode);
55
56                                 if (IsReadOnly)
57                                         throw new ArgumentException ("Node is read-only.");
58
59                                 data = value;
60
61                                 OwnerDocument.onNodeChanged (this, this.ParentNode);
62                         }
63                 }
64
65                 #endregion
66
67                 #region Methods
68
69                 public virtual void AppendData (string strData)
70                 {
71                         OwnerDocument.onNodeChanging (this, this.ParentNode);
72                         data += strData;
73                         OwnerDocument.onNodeChanged (this, this.ParentNode);
74                 }
75
76                 public virtual void DeleteData (int offset, int count)
77                 {
78                         OwnerDocument.onNodeChanging (this, this.ParentNode);
79
80                         if (offset < 0)
81                                 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
82
83                         int newCount = data.Length - offset;
84
85                         if ((offset + count) < data.Length)
86                                 newCount = count;
87
88                         data = data.Remove (offset, newCount);
89                         
90                         OwnerDocument.onNodeChanged (this, this.ParentNode);
91                 }
92
93                 public virtual void InsertData (int offset, string strData)
94                 {
95                         OwnerDocument.onNodeChanging (this, this.ParentNode);
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
100                         data = data.Insert(offset, strData);
101                         
102                         OwnerDocument.onNodeChanged (this, this.ParentNode);
103                 }
104
105                 public virtual void ReplaceData (int offset, int count, string strData)
106                 {
107                         OwnerDocument.onNodeChanging (this, this.ParentNode);
108
109                         if ((offset < 0) || (offset > data.Length))
110                                 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
111
112                         if (strData == null)
113                                 throw new ArgumentNullException ("strData", "Must be non-null.");
114
115                         string newData = data.Substring (0, offset) + strData;
116                         
117                         if ((offset + count) < data.Length)
118                                 newData += data.Substring (offset + count);
119
120                         data = newData;
121
122                         OwnerDocument.onNodeChanged (this, this.ParentNode);
123                 }
124
125                 public virtual string Substring (int offset, int count)
126                 {
127                         return data.Substring (offset, count);
128                 }
129
130                 #endregion
131         }
132 }