XmlEntityReference.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. public override bool IsReadOnly {
  51. get { return true; }
  52. }
  53. public override string LocalName {
  54. get { return entityName; } // name of the entity referenced.
  55. }
  56. public override string Name {
  57. get { return entityName; } // name of the entity referenced.
  58. }
  59. public override XmlNodeType NodeType {
  60. get { return XmlNodeType.EntityReference; }
  61. }
  62. public override string Value {
  63. get { return null; } // always return null here.
  64. set {
  65. throw new XmlException ("entity reference cannot be set value.");
  66. }
  67. }
  68. internal override XPathNodeType XPathNodeType {
  69. get {
  70. return XPathNodeType.Text;
  71. }
  72. }
  73. // Methods
  74. public override XmlNode CloneNode (bool deep)
  75. {
  76. // API docs: "The replacement text is not included." XmlNode.CloneNode
  77. // "The replacement text is set when node is inserted." XmlEntityReference.CloneNode
  78. //
  79. return new XmlEntityReference (Name, OwnerDocument);
  80. }
  81. public override void WriteContentTo (XmlWriter w)
  82. {
  83. for (int i = 0; i < ChildNodes.Count; i++)
  84. ChildNodes [i].WriteTo (w);
  85. }
  86. public override void WriteTo (XmlWriter w)
  87. {
  88. w.WriteRaw ("&");
  89. w.WriteName (Name);
  90. w.WriteRaw (";");
  91. }
  92. internal void SetReferencedEntityContent ()
  93. {
  94. if (FirstChild != null)
  95. return;
  96. XmlDocumentType doctype = OwnerDocument.DocumentType;
  97. if (doctype == null)
  98. return;
  99. XmlEntity ent = doctype.Entities.GetNamedItem (Name) as XmlEntity;
  100. if (ent == null)
  101. InsertBefore (OwnerDocument.CreateTextNode (String.Empty), null, false, true);
  102. else {
  103. ent.SetEntityContent ();
  104. for (int i = 0; i < ent.ChildNodes.Count; i++)
  105. InsertBefore (ent.ChildNodes [i].CloneNode (true), null, false, true);
  106. }
  107. }
  108. }
  109. }