New test.
[mono.git] / mcs / class / System.XML / System.Xml / XmlEntity.cs
1 //
2 // System.Xml.XmlEntity.cs
3 //
4 // Author:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Atsushi Enomoto  (atsushi@ximian.com)
7 //
8 // (C) Ximian, Inc.
9 // (C) 2004 Novell Inc.
10 //
11
12 //
13 // Permission is hereby granted, free of charge, to any person obtaining
14 // a copy of this software and associated documentation files (the
15 // "Software"), to deal in the Software without restriction, including
16 // without limitation the rights to use, copy, modify, merge, publish,
17 // distribute, sublicense, and/or sell copies of the Software, and to
18 // permit persons to whom the Software is furnished to do so, subject to
19 // the following conditions:
20 // 
21 // The above copyright notice and this permission notice shall be
22 // included in all copies or substantial portions of the Software.
23 // 
24 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 //
32 using Mono.Xml;
33
34 namespace System.Xml
35 {
36         public class XmlEntity : XmlNode, IHasXmlChildNode
37         {
38                 #region Constructors
39
40                 internal XmlEntity (string name, string NDATA, string publicId, string systemId,
41                                     XmlDocument doc)
42                         : base (doc)
43                 {
44                         this.name = doc.NameTable.Add (name);
45                         this.NDATA = NDATA;
46                         this.publicId = publicId;
47                         this.systemId = systemId;
48                         this.baseUri = doc.BaseURI;
49                 }
50
51                 #endregion
52                 
53                 #region Fields
54
55                 string name;
56                 string NDATA;
57                 string publicId;
58                 string systemId;
59                 string baseUri;
60                 XmlLinkedNode lastLinkedChild;
61                 bool contentAlreadySet;
62
63                 #endregion
64
65                 #region Properties
66
67                 XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
68                         get {
69                                 if (lastLinkedChild != null)
70                                         return lastLinkedChild;
71                                 if (!contentAlreadySet) {
72                                         contentAlreadySet = true;
73                                         SetEntityContent ();
74                                 }
75                                 return lastLinkedChild;
76                         }
77                         set { lastLinkedChild = value; }
78                 }
79
80                 public override string BaseURI {
81                         get {  return baseUri; }
82                 }
83
84                 public override string InnerText {
85                         get { return base.InnerText; }
86                         set { throw new InvalidOperationException ("This operation is not supported."); }
87                 }
88
89                 public override string InnerXml {
90                         get { return base.InnerXml; }
91                         set { throw new InvalidOperationException ("This operation is not supported."); }
92                 }
93
94                 public override bool IsReadOnly {
95                         get { return true; } // always read-only.
96                 }
97
98                 public override string LocalName {
99                         get { return name; }
100                 }
101
102                 public override string Name {
103                         get { return name; }
104                 }
105
106                 public override XmlNodeType NodeType {
107                         get { return XmlNodeType.Entity; }
108                 }
109
110                 public string NotationName {
111                         get {
112                                 if (NDATA == null)
113                                         return null;
114                                 else
115                                         return NDATA;
116                         }
117                 }
118
119                 public override string OuterXml {
120                         get { return String.Empty; }
121                 }
122
123                 public string PublicId {
124                         get { return publicId; }
125                 }
126
127                 public string SystemId {
128                         get { return systemId; }
129                 }
130                 #endregion
131
132                 #region Methods
133
134                 public override XmlNode CloneNode (bool deep)
135                 {
136                         throw new InvalidOperationException ("This operation is not supported.");
137                 }
138
139                 public override void WriteContentTo (XmlWriter w)
140                 {
141                         // No effect.
142                 }
143
144                 public override void WriteTo (XmlWriter w)
145                 {
146                         // No effect.
147                 }
148
149                 void SetEntityContent ()
150                 {
151                         if (lastLinkedChild != null)
152                                 return;
153
154                         XmlDocumentType doctype = OwnerDocument.DocumentType;
155
156                         if (doctype == null)
157                                 return;
158
159                         DTDEntityDeclaration decl = doctype.DTD.EntityDecls [name];
160                         if (decl == null)
161                                 return;
162
163                         XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
164                         XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
165                                 doctype != null ? doctype.DTD : null,
166                                 BaseURI, XmlLang, XmlSpace, null);
167                         XmlTextReader xmlReader = new XmlTextReader (decl.EntityValue, XmlNodeType.Element, ctx);
168                         xmlReader.XmlResolver = OwnerDocument.Resolver;
169
170                         do {
171                                 XmlNode n = OwnerDocument.ReadNode (xmlReader);
172                                 if(n == null) break;
173                                 InsertBefore (n, null, false, false);
174                         } while (true);
175                 }
176                 #endregion
177         }
178 }