2
0

XmlNamedNodeMap.cs 5.3 KB

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