XmlEntityReference.cs 4.2 KB

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