XmlEntityReference.cs 4.4 KB

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