2005-12-05 Lluis Sanchez Gual <lluis@novell.com>
[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
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
61                 #endregion
62
63                 #region Properties
64
65                 public override string BaseURI {
66                         get {  return baseUri; }
67                 }
68
69                 public override string InnerText {
70                         get { return base.InnerText; }
71                         set { throw new InvalidOperationException ("This operation is not supported."); }
72                 }
73
74                 public override string InnerXml {
75                         get { return base.InnerXml; }
76                         set { throw new InvalidOperationException ("This operation is not supported."); }
77                 }
78
79                 public override bool IsReadOnly {
80                         get { return true; } // always read-only.
81                 }
82
83                 public override string LocalName {
84                         get { return name; }
85                 }
86
87                 public override string Name {
88                         get { return name; }
89                 }
90
91                 public override XmlNodeType NodeType {
92                         get { return XmlNodeType.Entity; }
93                 }
94
95                 public string NotationName {
96                         get {
97                                 if (NDATA == null)
98                                         return null;
99                                 else
100                                         return NDATA;
101                         }
102                 }
103
104                 public override string OuterXml {
105                         get { return String.Empty; }
106                 }
107
108                 public string PublicId {
109                         get { return publicId; }
110                 }
111
112                 public string SystemId {
113                         get { return systemId; }
114                 }
115                 #endregion
116
117                 #region Methods
118
119                 public override XmlNode CloneNode (bool deep)
120                 {
121                         throw new InvalidOperationException ("This operation is not supported.");
122                 }
123
124                 public override void WriteContentTo (XmlWriter w)
125                 {
126                         // No effect.
127                 }
128
129                 public override void WriteTo (XmlWriter w)
130                 {
131                         // No effect.
132                 }
133
134                 internal void SetEntityContent ()
135                 {
136                         if (FirstChild != null)
137                                 return;
138
139                         XmlDocumentType doctype = OwnerDocument.DocumentType;
140
141                         if (doctype == null)
142                                 return;
143
144                         DTDEntityDeclaration decl = doctype.DTD.EntityDecls [name];
145                         if (decl == null)
146                                 return;
147
148                         XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
149                         XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
150                                 doctype != null ? doctype.DTD : null,
151                                 BaseURI, XmlLang, XmlSpace, null);
152                         XmlTextReader xmlReader = new XmlTextReader (decl.EntityValue, XmlNodeType.Element, ctx);
153                         xmlReader.XmlResolver = OwnerDocument.Resolver;
154
155                         do {
156                                 XmlNode n = OwnerDocument.ReadNode (xmlReader);
157                                 if(n == null) break;
158                                 InsertBefore (n, null, false, false);
159                         } while (true);
160                 }
161                 #endregion
162         }
163 }