XmlNamedNodeMap.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // System.Xml.XmlNamedNodeMap
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Duncan Mak ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. //
  10. using System;
  11. using System.Collections;
  12. using Mono.Xml;
  13. namespace System.Xml
  14. {
  15. public class XmlNamedNodeMap : IEnumerable
  16. {
  17. XmlNode parent;
  18. ArrayList nodeList;
  19. bool readOnly = false;
  20. internal XmlNamedNodeMap (XmlNode parent)
  21. {
  22. this.parent = parent;
  23. nodeList = new ArrayList ();
  24. }
  25. public virtual int Count {
  26. get { return nodeList.Count; }
  27. }
  28. public virtual IEnumerator GetEnumerator ()
  29. {
  30. return nodeList.GetEnumerator ();
  31. }
  32. public virtual XmlNode GetNamedItem (string name)
  33. {
  34. for (int i = 0; i < nodeList.Count; i++) {
  35. XmlNode node = (XmlNode) nodeList [i];
  36. if (node.Name == name)
  37. return node;
  38. }
  39. return null;
  40. }
  41. public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
  42. {
  43. for (int i = 0; i < nodeList.Count; i++) {
  44. XmlNode node = (XmlNode) nodeList [i];
  45. if ((node.LocalName == localName)
  46. && (node.NamespaceURI == namespaceURI))
  47. return node;
  48. }
  49. return null;
  50. }
  51. public virtual XmlNode Item (int index)
  52. {
  53. if (index < 0 || index >= nodeList.Count)
  54. return null;
  55. else
  56. return (XmlNode) nodeList [index];
  57. }
  58. public virtual XmlNode RemoveNamedItem (string name)
  59. {
  60. for (int i = 0; i < nodeList.Count; i++) {
  61. XmlNode node = (XmlNode) nodeList [i];
  62. if (node.Name == name) {
  63. if (node.IsReadOnly)
  64. throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
  65. nodeList.Remove (node);
  66. // Since XmlAttributeCollection does not override
  67. // it while attribute have to keep it in the
  68. // collection, it adds to the collection immediately.
  69. XmlAttribute attr = node as XmlAttribute;
  70. if (attr != null) {
  71. DTDAttributeDefinition def = attr.GetAttributeDefinition ();
  72. if (def != null && def.DefaultValue != null) {
  73. XmlAttribute newAttr = attr.OwnerDocument.CreateAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, true, false);
  74. newAttr.Value = def.DefaultValue;
  75. newAttr.SetDefault ();
  76. attr.OwnerElement.SetAttributeNode (newAttr);
  77. }
  78. }
  79. return node;
  80. }
  81. }
  82. return null;
  83. }
  84. public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
  85. {
  86. for (int i = 0; i < nodeList.Count; i++) {
  87. XmlNode node = (XmlNode) nodeList [i];
  88. if ((node.LocalName == localName)
  89. && (node.NamespaceURI == namespaceURI)) {
  90. nodeList.Remove (node);
  91. return node;
  92. }
  93. }
  94. return null;
  95. }
  96. public virtual XmlNode SetNamedItem (XmlNode node)
  97. {
  98. return SetNamedItem (node, -1, true);
  99. }
  100. internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
  101. {
  102. return SetNamedItem (node, -1, raiseEvent);
  103. }
  104. internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
  105. {
  106. if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
  107. throw new ArgumentException ("Cannot add to NodeMap.");
  108. if (raiseEvent)
  109. parent.OwnerDocument.onNodeInserting (node, parent);
  110. try {
  111. for (int i = 0; i < nodeList.Count; i++) {
  112. XmlNode x = (XmlNode) nodeList [i];
  113. if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
  114. nodeList.Remove (x);
  115. if (pos < 0)
  116. nodeList.Add (node);
  117. else
  118. nodeList.Insert (pos, node);
  119. return x;
  120. }
  121. }
  122. if(pos < 0)
  123. nodeList.Add (node);
  124. else
  125. nodeList.Insert (pos, node);
  126. return node;
  127. } finally {
  128. if (raiseEvent)
  129. parent.OwnerDocument.onNodeInserted (node, parent);
  130. }
  131. }
  132. internal ArrayList Nodes { get { return nodeList; } }
  133. }
  134. }