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