2010-04-07 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml / XmlEntity.cs
blob9366805c6eab1b4a1e4c015e1c45d2294eb8b03c
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.
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.
32 using Mono.Xml;
34 namespace System.Xml
36 public class XmlEntity : XmlNode, IHasXmlChildNode
38 #region Constructors
40 internal XmlEntity (string name, string NDATA, string publicId, string systemId,
41 XmlDocument doc)
42 : base (doc)
44 this.name = doc.NameTable.Add (name);
45 this.NDATA = NDATA;
46 this.publicId = publicId;
47 this.systemId = systemId;
48 this.baseUri = doc.BaseURI;
51 #endregion
53 #region Fields
55 string name;
56 string NDATA;
57 string publicId;
58 string systemId;
59 string baseUri;
60 XmlLinkedNode lastLinkedChild;
61 bool contentAlreadySet;
63 #endregion
65 #region Properties
67 XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
68 get {
69 if (lastLinkedChild != null)
70 return lastLinkedChild;
71 if (!contentAlreadySet) {
72 contentAlreadySet = true;
73 SetEntityContent ();
75 return lastLinkedChild;
77 set { lastLinkedChild = value; }
80 public override string BaseURI {
81 get { return baseUri; }
84 public override string InnerText {
85 get { return base.InnerText; }
86 set { throw new InvalidOperationException ("This operation is not supported."); }
89 public override string InnerXml {
90 get { return base.InnerXml; }
91 set { throw new InvalidOperationException ("This operation is not supported."); }
94 public override bool IsReadOnly {
95 get { return true; } // always read-only.
98 public override string LocalName {
99 get { return name; }
102 public override string Name {
103 get { return name; }
106 public override XmlNodeType NodeType {
107 get { return XmlNodeType.Entity; }
110 public string NotationName {
111 get {
112 if (NDATA == null)
113 return null;
114 else
115 return NDATA;
119 public override string OuterXml {
120 get { return String.Empty; }
123 public string PublicId {
124 get { return publicId; }
127 public string SystemId {
128 get { return systemId; }
130 #endregion
132 #region Methods
134 public override XmlNode CloneNode (bool deep)
136 throw new InvalidOperationException ("This operation is not supported.");
139 public override void WriteContentTo (XmlWriter w)
141 // No effect.
144 public override void WriteTo (XmlWriter w)
146 // No effect.
149 void SetEntityContent ()
151 if (lastLinkedChild != null)
152 return;
154 XmlDocumentType doctype = OwnerDocument.DocumentType;
156 if (doctype == null)
157 return;
159 DTDEntityDeclaration decl = doctype.DTD.EntityDecls [name];
160 if (decl == null)
161 return;
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;
170 do {
171 XmlNode n = OwnerDocument.ReadNode (xmlReader);
172 if(n == null) break;
173 InsertBefore (n, null, false, false);
174 } while (true);
176 #endregion