2008-03-30 Carlos Alberto Cortez <calberto.cortez@gmail.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, IHasXmlChildNode
38         {
39                 string entityName;
40                 XmlLinkedNode lastLinkedChild;
41
42                 // Constructor
43                 protected internal XmlEntityReference (string name, XmlDocument doc)
44                         : base (doc)
45                 {
46                         // LAMESPEC: MS CreateNode() allows null node name.
47                         XmlConvert.VerifyName (name);
48                         entityName = doc.NameTable.Add (name);
49                 }
50
51                 // Properties
52
53                 XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
54                         get { return lastLinkedChild; }
55                         set { lastLinkedChild = value; }
56                 }
57
58                 public override string BaseURI {
59                         get { return base.BaseURI; }
60                 }
61
62                 private XmlEntity Entity {
63                         get {
64                                 XmlDocumentType doctype = OwnerDocument.DocumentType;
65                                 if (doctype == null)
66                                         return null;
67
68                                 if (doctype.Entities == null)
69                                         return null;
70
71                                 return doctype.Entities.GetNamedItem (Name) as XmlEntity;
72                         }
73                 }
74
75                 internal override string ChildrenBaseURI {
76                         get {
77                                 XmlEntity ent = Entity;
78                                 if (ent == null)
79                                         return string.Empty;
80
81                                 if (ent.SystemId == null || ent.SystemId.Length == 0)
82                                         return ent.BaseURI;
83
84                                 if (ent.BaseURI == null || ent.BaseURI.Length == 0)
85                                         return ent.SystemId;
86                                 
87                                 Uri baseUri = null;
88                                 try {
89                                         baseUri = new Uri (ent.BaseURI);
90                                 } catch (UriFormatException) {
91                                 }
92
93                                 XmlResolver resolver = OwnerDocument.Resolver;
94                                 if (resolver != null)
95                                         return resolver.ResolveUri (baseUri, ent.SystemId).ToString ();
96
97                                 return new Uri (baseUri, ent.SystemId).ToString ();
98                         }
99                 }
100
101                 public override bool IsReadOnly {
102                         get { return true; } 
103                 }
104
105                 public override string LocalName {
106                         get { return entityName; } // name of the entity referenced.
107                 }
108
109                 public override string Name {
110                         get { return entityName; } // name of the entity referenced.
111                 }
112
113                 public override XmlNodeType NodeType {
114                         get { return XmlNodeType.EntityReference; }
115                 }
116
117                 public override string Value {
118                         get { return null; } // always return null here.
119                         set {
120                                 throw new XmlException ("entity reference cannot be set value.");
121                         }
122                 }
123
124                 internal override XPathNodeType XPathNodeType {
125                         get {
126                                 return XPathNodeType.Text;
127                         }
128                 }
129
130
131                 // Methods
132                 public override XmlNode CloneNode (bool deep)
133                 {
134                         
135                         // API docs: "The replacement text is not included." XmlNode.CloneNode
136                         // "The replacement text is set when node is inserted." XmlEntityReference.CloneNode
137                         //
138                         return new XmlEntityReference (Name, OwnerDocument);
139                 }
140
141                 public override void WriteContentTo (XmlWriter w)
142                 {
143                         for (int i = 0; i < ChildNodes.Count; i++)
144                                 ChildNodes [i].WriteTo (w);
145                 }
146
147                 public override void WriteTo (XmlWriter w)
148                 {
149                         w.WriteRaw ("&");
150                         w.WriteName (Name);
151                         w.WriteRaw (";");
152                 }
153
154                 internal void SetReferencedEntityContent ()
155                 {
156                         if (FirstChild != null)
157                                 return;
158
159                         if (OwnerDocument.DocumentType == null)
160                                 return;
161
162                         XmlEntity ent = Entity;
163                         if (ent == null)
164                                 InsertBefore (OwnerDocument.CreateTextNode (String.Empty), null, false, true);
165                         else {
166                                 for (int i = 0; i < ent.ChildNodes.Count; i++)
167                                         InsertBefore (ent.ChildNodes [i].CloneNode (true), null, false, true);
168                         }
169                 }
170         }
171 }