XmlAttributeCollection.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. //
  2. // System.Xml.XmlAttributeCollection
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2002 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. using Mono.Xml;
  14. namespace System.Xml
  15. {
  16. public class XmlAttributeCollection : XmlNamedNodeMap, ICollection
  17. {
  18. XmlElement ownerElement;
  19. XmlDocument ownerDocument;
  20. internal XmlAttributeCollection (XmlNode parent) : base (parent)
  21. {
  22. ownerElement = parent as XmlElement;
  23. ownerDocument = parent.OwnerDocument;
  24. if(ownerElement == null)
  25. throw new XmlException ("invalid construction for XmlAttributeCollection.");
  26. }
  27. bool ICollection.IsSynchronized {
  28. get { return false; }
  29. }
  30. bool IsReadOnly {
  31. get { return ownerElement.IsReadOnly; }
  32. }
  33. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  34. public virtual XmlAttribute this [string name] {
  35. get {
  36. return (XmlAttribute) GetNamedItem (name);
  37. }
  38. }
  39. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  40. public virtual XmlAttribute this [int i] {
  41. get {
  42. return (XmlAttribute) Nodes [i];
  43. }
  44. }
  45. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  46. public virtual XmlAttribute this [string localName, string namespaceURI] {
  47. get {
  48. return (XmlAttribute) GetNamedItem (localName, namespaceURI);
  49. }
  50. }
  51. object ICollection.SyncRoot {
  52. get { return this; }
  53. }
  54. public virtual XmlAttribute Append (XmlAttribute node)
  55. {
  56. XmlNode xmlNode = this.SetNamedItem (node);
  57. return node;
  58. }
  59. public void CopyTo (XmlAttribute[] array, int index)
  60. {
  61. // assuming that Nodes is a correct collection.
  62. for(int i=0; i<Nodes.Count; i++)
  63. array [index + i] = Nodes [i] as XmlAttribute;
  64. }
  65. void ICollection.CopyTo (Array array, int index)
  66. {
  67. // assuming that Nodes is a correct collection.
  68. array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
  69. }
  70. public virtual XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  71. {
  72. if (refNode == null) {
  73. if (Nodes.Count == 0)
  74. return InsertBefore (newNode, null);
  75. else
  76. return InsertBefore (newNode, this [0]);
  77. }
  78. for (int i = 0; i < Nodes.Count; i++)
  79. if (refNode == Nodes [i])
  80. return InsertBefore (newNode, Nodes.Count == i + 1 ? null : this [i + 1]);
  81. throw new ArgumentException ("refNode not found in this collection.");
  82. }
  83. public virtual XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  84. {
  85. if (newNode.OwnerDocument != ownerDocument)
  86. throw new ArgumentException ("different document created this newNode.");
  87. ownerDocument.onNodeInserting (newNode, null);
  88. int pos = Nodes.Count;
  89. if (refNode != null) {
  90. for (int i = 0; i < Nodes.Count; i++) {
  91. XmlNode n = Nodes [i] as XmlNode;
  92. if (n == refNode) {
  93. pos = i;
  94. break;
  95. }
  96. }
  97. if (pos == Nodes.Count)
  98. throw new ArgumentException ("refNode not found in this collection.");
  99. }
  100. SetNamedItem (newNode, pos, false);
  101. ownerDocument.onNodeInserted (newNode, null);
  102. return newNode;
  103. }
  104. public virtual XmlAttribute Prepend (XmlAttribute node)
  105. {
  106. return this.InsertAfter (node, null);
  107. }
  108. public virtual XmlAttribute Remove (XmlAttribute node)
  109. {
  110. if (node == null)
  111. throw new ArgumentException ("Specified node is null.");
  112. if (node.OwnerDocument != ownerDocument)
  113. throw new ArgumentException ("Specified node is in a different document.");
  114. if (node.OwnerElement != this.ownerElement)
  115. throw new ArgumentException ("The specified attribute is not contained in the element.");
  116. XmlAttribute retAttr = null;
  117. for (int i = 0; i < Nodes.Count; i++) {
  118. XmlAttribute attr = (XmlAttribute) Nodes [i];
  119. if (attr == node) {
  120. retAttr = attr;
  121. break;
  122. }
  123. }
  124. if(retAttr != null) {
  125. ownerDocument.onNodeRemoving (node, ownerElement);
  126. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  127. RemoveIdenticalAttribute (retAttr);
  128. ownerDocument.onNodeRemoved (node, ownerElement);
  129. }
  130. // If it is default, then directly create new attribute.
  131. DTDAttributeDefinition def = retAttr.GetAttributeDefinition ();
  132. if (def != null && def.DefaultValue != null) {
  133. XmlAttribute attr = ownerDocument.CreateAttribute (
  134. retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
  135. attr.Value = def.DefaultValue;
  136. attr.SetDefault ();
  137. this.SetNamedItem (attr);
  138. }
  139. retAttr.SetOwnerElement (null);
  140. return retAttr;
  141. }
  142. public virtual void RemoveAll ()
  143. {
  144. int current = 0;
  145. while (current < Count) {
  146. XmlAttribute attr = this [current];
  147. if (!attr.Specified)
  148. current++;
  149. // It is called for the purpose of event support.
  150. Remove (attr);
  151. }
  152. }
  153. public virtual XmlAttribute RemoveAt (int i)
  154. {
  155. if(Nodes.Count <= i)
  156. return null;
  157. return Remove ((XmlAttribute)Nodes [i]);
  158. }
  159. public override XmlNode SetNamedItem (XmlNode node)
  160. {
  161. if(IsReadOnly)
  162. throw new XmlException ("this AttributeCollection is read only.");
  163. XmlAttribute attr = node as XmlAttribute;
  164. if (attr.OwnerElement != null)
  165. throw new InvalidOperationException ("This attribute is already set to another element.");
  166. ownerElement.OwnerDocument.onNodeInserting (node, ownerElement);
  167. attr.SetOwnerElement (ownerElement);
  168. XmlNode n = AdjustIdenticalAttributes (node as XmlAttribute, base.SetNamedItem (node, -1, false));
  169. ownerElement.OwnerDocument.onNodeInserted (node, ownerElement);
  170. return n as XmlAttribute;
  171. }
  172. internal void AddIdenticalAttribute ()
  173. {
  174. SetIdenticalAttribute (false);
  175. }
  176. internal void RemoveIdenticalAttribute ()
  177. {
  178. SetIdenticalAttribute (true);
  179. }
  180. private void SetIdenticalAttribute (bool remove)
  181. {
  182. if (ownerElement == null)
  183. return;
  184. // Check if new attribute's datatype is ID.
  185. XmlDocumentType doctype = ownerDocument.DocumentType;
  186. if (doctype == null || doctype.DTD == null)
  187. return;
  188. DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];
  189. for (int i = 0; i < Nodes.Count; i++) {
  190. XmlAttribute node = (XmlAttribute) Nodes [i];
  191. DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
  192. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  193. continue;
  194. if (remove) {
  195. if (ownerDocument.GetIdenticalAttribute (node.Value) != null) {
  196. ownerDocument.RemoveIdenticalAttribute (node.Value);
  197. return;
  198. }
  199. } else {
  200. // adding new identical attribute, but
  201. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  202. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  203. throw new XmlException (String.Format (
  204. "ID value {0} already exists in this document.", node.Value));
  205. ownerDocument.AddIdenticalAttribute (node);
  206. return;
  207. }
  208. }
  209. }
  210. private XmlNode AdjustIdenticalAttributes (XmlAttribute node, XmlNode existing)
  211. {
  212. // If owner element is not appended to the document,
  213. // ID table should not be filled.
  214. if (ownerElement == null)
  215. return existing;
  216. RemoveIdenticalAttribute (existing);
  217. // Check if new attribute's datatype is ID.
  218. XmlDocumentType doctype = node.OwnerDocument.DocumentType;
  219. if (doctype == null || doctype.DTD == null)
  220. return existing;
  221. DTDAttListDeclaration attList = doctype.DTD.AttListDecls [ownerElement.Name];
  222. DTDAttributeDefinition attdef = attList == null ? null : attList.Get (node.Name);
  223. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  224. return existing;
  225. // adding new identical attribute, but
  226. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  227. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  228. throw new XmlException (String.Format (
  229. "ID value {0} already exists in this document.", node.Value));
  230. ownerDocument.AddIdenticalAttribute (node);
  231. return existing;
  232. }
  233. private XmlNode RemoveIdenticalAttribute (XmlNode existing)
  234. {
  235. // If owner element is not appended to the document,
  236. // ID table should not be filled.
  237. if (ownerElement == null)
  238. return existing;
  239. if (existing != null) {
  240. // remove identical attribute (if it is).
  241. if (ownerDocument.GetIdenticalAttribute (existing.Value) != null)
  242. ownerDocument.RemoveIdenticalAttribute (existing.Value);
  243. }
  244. return existing;
  245. }
  246. }
  247. }