2005-12-05 Lluis Sanchez Gual <lluis@novell.com>
[mono.git] / mcs / class / System.XML / System.Xml / XmlEntityReference.cs
1 //
2 // System.Xml.XmlEntityReference.cs
3 // Author:
4 //      Duncan Mak  (duncan@ximian.com)
5 //      Atsushi Enomoto  (atsushi@ximian.com)
6 //
7 // (C) Ximian, Inc. http://www.ximian.com
8 // (C) 2004 Novell inc.
9 //
10
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31 using System;
32 using System.Xml.XPath;
33 using Mono.Xml;
34
35 namespace System.Xml
36 {
37         public class XmlEntityReference : XmlLinkedNode
38         {
39                 string entityName;
40                 
41                 // Constructor
42                 protected internal XmlEntityReference (string name, XmlDocument doc)
43                         : base (doc)
44                 {
45                         // LAMESPEC: MS CreateNode() allows null node name.
46                         XmlConvert.VerifyName (name);
47                         entityName = doc.NameTable.Add (name);
48                 }
49
50                 // Properties
51                 public override string BaseURI {
52                         get { return base.BaseURI; }
53                 }
54
55                 private XmlEntity Entity {
56                         get {
57                                 XmlDocumentType doctype = OwnerDocument.DocumentType;
58                                 if (doctype == null)
59                                         return null;
60
61                                 if (doctype.Entities == null)
62                                         return null;
63
64                                 return doctype.Entities.GetNamedItem (Name) as XmlEntity;
65                         }
66                 }
67
68                 internal override string ChildrenBaseURI {
69                         get {
70                                 XmlEntity ent = Entity;
71                                 if (ent == null)
72                                         return string.Empty;
73
74                                 if (ent.SystemId == null || ent.SystemId.Length == 0)
75                                         return ent.BaseURI;
76
77                                 if (ent.BaseURI == null || ent.BaseURI.Length == 0)
78                                         return ent.SystemId;
79                                 
80                                 Uri baseUri = null;
81                                 try {
82                                         baseUri = new Uri (ent.BaseURI);
83                                 } catch (UriFormatException) {
84                                 }
85
86                                 XmlResolver resolver = OwnerDocument.Resolver;
87                                 if (resolver != null)
88                                         return resolver.ResolveUri (baseUri, ent.SystemId).ToString ();
89
90                                 return new Uri (baseUri, ent.SystemId).ToString ();
91                         }
92                 }
93
94                 public override bool IsReadOnly {
95                         get { return true; } 
96                 }
97
98                 public override string LocalName {
99                         get { return entityName; } // name of the entity referenced.
100                 }
101
102                 public override string Name {
103                         get { return entityName; } // name of the entity referenced.
104                 }
105
106                 public override XmlNodeType NodeType {
107                         get { return XmlNodeType.EntityReference; }
108                 }
109
110                 public override string Value {
111                         get { return null; } // always return null here.
112                         set {
113                                 throw new XmlException ("entity reference cannot be set value.");
114                         }
115                 }
116
117                 internal override XPathNodeType XPathNodeType {
118                         get {
119                                 return XPathNodeType.Text;
120                         }
121                 }
122
123
124                 // Methods
125                 public override XmlNode CloneNode (bool deep)
126                 {
127                         
128                         // API docs: "The replacement text is not included." XmlNode.CloneNode
129                         // "The replacement text is set when node is inserted." XmlEntityReference.CloneNode
130                         //
131                         return new XmlEntityReference (Name, OwnerDocument);
132                 }
133
134                 public override void WriteContentTo (XmlWriter w)
135                 {
136                         for (int i = 0; i < ChildNodes.Count; i++)
137                                 ChildNodes [i].WriteTo (w);
138                 }
139
140                 public override void WriteTo (XmlWriter w)
141                 {
142                         w.WriteRaw ("&");
143                         w.WriteName (Name);
144                         w.WriteRaw (";");
145                 }
146
147                 internal void SetReferencedEntityContent ()
148                 {
149                         if (FirstChild != null)
150                                 return;
151
152                         if (OwnerDocument.DocumentType == null)
153                                 return;
154
155                         XmlEntity ent = Entity;
156                         if (ent == null)
157                                 InsertBefore (OwnerDocument.CreateTextNode (String.Empty), null, false, true);
158                         else {
159                                 ent.SetEntityContent ();
160                                 for (int i = 0; i < ent.ChildNodes.Count; i++)
161                                         InsertBefore (ent.ChildNodes [i].CloneNode (true), null, false, true);
162                         }
163                 }
164         }
165 }