2
0

XmlNamedNodeMap.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //
  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.Collections;
  32. using Mono.Xml;
  33. namespace System.Xml
  34. {
  35. public class XmlNamedNodeMap : IEnumerable
  36. {
  37. XmlNode parent;
  38. ArrayList nodeList;
  39. bool readOnly = false;
  40. internal XmlNamedNodeMap (XmlNode parent)
  41. {
  42. this.parent = parent;
  43. nodeList = new ArrayList ();
  44. }
  45. public virtual int Count {
  46. get { return nodeList.Count; }
  47. }
  48. public virtual IEnumerator GetEnumerator ()
  49. {
  50. return nodeList.GetEnumerator ();
  51. }
  52. public virtual XmlNode GetNamedItem (string name)
  53. {
  54. for (int i = 0; i < nodeList.Count; i++) {
  55. XmlNode node = (XmlNode) nodeList [i];
  56. if (node.Name == name)
  57. return node;
  58. }
  59. return null;
  60. }
  61. public virtual XmlNode GetNamedItem (string localName, string namespaceURI)
  62. {
  63. for (int i = 0; i < nodeList.Count; i++) {
  64. XmlNode node = (XmlNode) nodeList [i];
  65. if ((node.LocalName == localName)
  66. && (node.NamespaceURI == namespaceURI))
  67. return node;
  68. }
  69. return null;
  70. }
  71. public virtual XmlNode Item (int index)
  72. {
  73. if (index < 0 || index >= nodeList.Count)
  74. return null;
  75. else
  76. return (XmlNode) nodeList [index];
  77. }
  78. public virtual XmlNode RemoveNamedItem (string name)
  79. {
  80. for (int i = 0; i < nodeList.Count; i++) {
  81. XmlNode node = (XmlNode) nodeList [i];
  82. if (node.Name == name) {
  83. if (node.IsReadOnly)
  84. throw new InvalidOperationException ("Cannot remove. This node is read only: " + name);
  85. nodeList.Remove (node);
  86. // Since XmlAttributeCollection does not override
  87. // it while attribute have to keep it in the
  88. // collection, it adds to the collection immediately.
  89. XmlAttribute attr = node as XmlAttribute;
  90. if (attr != null) {
  91. DTDAttributeDefinition def = attr.GetAttributeDefinition ();
  92. if (def != null && def.DefaultValue != null) {
  93. XmlAttribute newAttr = attr.OwnerDocument.CreateAttribute (attr.Prefix, attr.LocalName, attr.NamespaceURI, true, false);
  94. newAttr.Value = def.DefaultValue;
  95. newAttr.SetDefault ();
  96. attr.OwnerElement.SetAttributeNode (newAttr);
  97. }
  98. }
  99. return node;
  100. }
  101. }
  102. return null;
  103. }
  104. public virtual XmlNode RemoveNamedItem (string localName, string namespaceURI)
  105. {
  106. for (int i = 0; i < nodeList.Count; i++) {
  107. XmlNode node = (XmlNode) nodeList [i];
  108. if ((node.LocalName == localName)
  109. && (node.NamespaceURI == namespaceURI)) {
  110. nodeList.Remove (node);
  111. return node;
  112. }
  113. }
  114. return null;
  115. }
  116. public virtual XmlNode SetNamedItem (XmlNode node)
  117. {
  118. return SetNamedItem (node, -1, true);
  119. }
  120. internal XmlNode SetNamedItem (XmlNode node, bool raiseEvent)
  121. {
  122. return SetNamedItem (node, -1, raiseEvent);
  123. }
  124. internal XmlNode SetNamedItem (XmlNode node, int pos, bool raiseEvent)
  125. {
  126. if (readOnly || (node.OwnerDocument != parent.OwnerDocument))
  127. throw new ArgumentException ("Cannot add to NodeMap.");
  128. if (raiseEvent)
  129. parent.OwnerDocument.onNodeInserting (node, parent);
  130. try {
  131. for (int i = 0; i < nodeList.Count; i++) {
  132. XmlNode x = (XmlNode) nodeList [i];
  133. if(x.LocalName == node.LocalName && x.NamespaceURI == node.NamespaceURI) {
  134. nodeList.Remove (x);
  135. if (pos < 0)
  136. nodeList.Add (node);
  137. else
  138. nodeList.Insert (pos, node);
  139. return x;
  140. }
  141. }
  142. if(pos < 0)
  143. nodeList.Add (node);
  144. else
  145. nodeList.Insert (pos, node);
  146. return node;
  147. } finally {
  148. if (raiseEvent)
  149. parent.OwnerDocument.onNodeInserted (node, parent);
  150. }
  151. }
  152. internal ArrayList Nodes { get { return nodeList; } }
  153. }
  154. }