2 // System.Xml.XmlEntityReference.cs
4 // Duncan Mak (duncan@ximian.com)
5 // Atsushi Enomoto (atsushi@ximian.com)
7 // (C) Ximian, Inc. http://www.ximian.com
8 // (C) 2004 Novell inc.
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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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.
32 using System.Xml.XPath;
37 public class XmlEntityReference : XmlLinkedNode
42 protected internal XmlEntityReference (string name, XmlDocument doc)
45 // LAMESPEC: MS CreateNode() allows null node name.
46 XmlConvert.VerifyName (name);
47 entityName = doc.NameTable.Add (name);
51 public override string BaseURI {
52 get { return base.BaseURI; }
55 private XmlEntity Entity {
57 XmlDocumentType doctype = OwnerDocument.DocumentType;
61 if (doctype.Entities == null)
64 return doctype.Entities.GetNamedItem (Name) as XmlEntity;
68 internal override string ChildrenBaseURI {
70 XmlEntity ent = Entity;
74 if (ent.SystemId == null || ent.SystemId.Length == 0)
77 if (ent.BaseURI == null || ent.BaseURI.Length == 0)
82 baseUri = new Uri (ent.BaseURI);
83 } catch (UriFormatException) {
86 XmlResolver resolver = OwnerDocument.Resolver;
88 return resolver.ResolveUri (baseUri, ent.SystemId).ToString ();
90 return new Uri (baseUri, ent.SystemId).ToString ();
94 public override bool IsReadOnly {
98 public override string LocalName {
99 get { return entityName; } // name of the entity referenced.
102 public override string Name {
103 get { return entityName; } // name of the entity referenced.
106 public override XmlNodeType NodeType {
107 get { return XmlNodeType.EntityReference; }
110 public override string Value {
111 get { return null; } // always return null here.
113 throw new XmlException ("entity reference cannot be set value.");
117 internal override XPathNodeType XPathNodeType {
119 return XPathNodeType.Text;
125 public override XmlNode CloneNode (bool deep)
128 // API docs: "The replacement text is not included." XmlNode.CloneNode
129 // "The replacement text is set when node is inserted." XmlEntityReference.CloneNode
131 return new XmlEntityReference (Name, OwnerDocument);
134 public override void WriteContentTo (XmlWriter w)
136 for (int i = 0; i < ChildNodes.Count; i++)
137 ChildNodes [i].WriteTo (w);
140 public override void WriteTo (XmlWriter w)
147 internal void SetReferencedEntityContent ()
149 if (FirstChild != null)
152 if (OwnerDocument.DocumentType == null)
155 XmlEntity ent = Entity;
157 InsertBefore (OwnerDocument.CreateTextNode (String.Empty), null, false, true);
159 ent.SetEntityContent ();
160 for (int i = 0; i < ent.ChildNodes.Count; i++)
161 InsertBefore (ent.ChildNodes [i].CloneNode (true), null, false, true);