2002-12-24 Atsushi Enomoto <ginga@kit.hi-ho.ne.jp>
[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 {
38                                 OwnerDocument.onNodeChanging (this, this.ParentNode);
39
40                                 if (IsReadOnly)
41                                         throw new ArgumentException ("Node is read-only.");
42
43                                 data = value;
44
45                                 OwnerDocument.onNodeChanged (this, this.ParentNode);
46                         }
47                 }
48
49                 public override string InnerText {
50                         get { return data; }
51
52                         set { Data = value; }   // invokes events
53                 }
54
55                 public virtual int Length {
56                         get { return data != null ? data.Length : 0; }
57                 }
58
59                 public override string Value {
60                         get { return data; }
61
62                         set {
63                                 Data = value;
64                         }
65                 }
66
67                 #endregion
68
69                 #region Methods
70
71                 public virtual void AppendData (string strData)
72                 {
73                         OwnerDocument.onNodeChanging (this, this.ParentNode);
74                         data += strData;
75                         OwnerDocument.onNodeChanged (this, this.ParentNode);
76                 }
77
78                 public virtual void DeleteData (int offset, int count)
79                 {
80                         OwnerDocument.onNodeChanging (this, this.ParentNode);
81
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
85                         int newCount = data.Length - offset;
86
87                         if ((offset + count) < data.Length)
88                                 newCount = count;
89
90                         data = data.Remove (offset, newCount);
91                         
92                         OwnerDocument.onNodeChanged (this, this.ParentNode);
93                 }
94
95                 public virtual void InsertData (int offset, string strData)
96                 {
97                         OwnerDocument.onNodeChanging (this, this.ParentNode);
98
99                         if ((offset < 0) || (offset > data.Length))
100                                 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
101
102                         data = data.Insert(offset, strData);
103                         
104                         OwnerDocument.onNodeChanged (this, this.ParentNode);
105                 }
106
107                 public virtual void ReplaceData (int offset, int count, string strData)
108                 {
109                         OwnerDocument.onNodeChanging (this, this.ParentNode);
110
111                         if ((offset < 0) || (offset > data.Length))
112                                 throw new ArgumentOutOfRangeException ("offset", "Must be non-negative and must not be greater than the length of this instance.");
113
114                         if (strData == null)
115                                 throw new ArgumentNullException ("strData", "Must be non-null.");
116
117                         string newData = data.Substring (0, offset) + strData;
118                         
119                         if ((offset + count) < data.Length)
120                                 newData += data.Substring (offset + count);
121
122                         data = newData;
123
124                         OwnerDocument.onNodeChanged (this, this.ParentNode);
125                 }
126
127                 public virtual string Substring (int offset, int count)
128                 {
129                         return data.Substring (offset, count);
130                 }
131
132                 #endregion
133         }
134 }