XmlAttributeCollection.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 (IsReadOnly)
  111. throw new ArgumentException ("This attribute collection is read-only.");
  112. if (node == null)
  113. throw new ArgumentException ("Specified node is null.");
  114. if (node.OwnerDocument != ownerDocument)
  115. throw new ArgumentException ("Specified node is in a different document.");
  116. if (node.OwnerElement != this.ownerElement)
  117. throw new ArgumentException ("The specified attribute is not contained in the element.");
  118. XmlAttribute retAttr = null;
  119. for (int i = 0; i < Nodes.Count; i++) {
  120. XmlAttribute attr = (XmlAttribute) Nodes [i];
  121. if (attr == node) {
  122. retAttr = attr;
  123. break;
  124. }
  125. }
  126. if(retAttr != null) {
  127. ownerDocument.onNodeRemoving (node, ownerElement);
  128. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  129. RemoveIdenticalAttribute (retAttr);
  130. ownerDocument.onNodeRemoved (node, ownerElement);
  131. }
  132. // If it is default, then directly create new attribute.
  133. DTDAttributeDefinition def = retAttr.GetAttributeDefinition ();
  134. if (def != null && def.DefaultValue != null) {
  135. XmlAttribute attr = ownerDocument.CreateAttribute (
  136. retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
  137. attr.Value = def.DefaultValue;
  138. attr.SetDefault ();
  139. this.SetNamedItem (attr);
  140. }
  141. retAttr.SetOwnerElement (null);
  142. return retAttr;
  143. }
  144. public virtual void RemoveAll ()
  145. {
  146. int current = 0;
  147. while (current < Count) {
  148. XmlAttribute attr = this [current];
  149. if (!attr.Specified)
  150. current++;
  151. // It is called for the purpose of event support.
  152. Remove (attr);
  153. }
  154. }
  155. public virtual XmlAttribute RemoveAt (int i)
  156. {
  157. if(Nodes.Count <= i)
  158. return null;
  159. return Remove ((XmlAttribute)Nodes [i]);
  160. }
  161. public override XmlNode SetNamedItem (XmlNode node)
  162. {
  163. if(IsReadOnly)
  164. throw new ArgumentException ("this AttributeCollection is read only.");
  165. XmlAttribute attr = node as XmlAttribute;
  166. if (attr.OwnerElement != null)
  167. throw new ArgumentException ("This attribute is already set to another element.");
  168. ownerElement.OwnerDocument.onNodeInserting (node, ownerElement);
  169. attr.SetOwnerElement (ownerElement);
  170. XmlNode n = AdjustIdenticalAttributes (node as XmlAttribute, base.SetNamedItem (node, -1, false));
  171. ownerElement.OwnerDocument.onNodeInserted (node, ownerElement);
  172. return n as XmlAttribute;
  173. }
  174. internal void AddIdenticalAttribute ()
  175. {
  176. SetIdenticalAttribute (false);
  177. }
  178. internal void RemoveIdenticalAttribute ()
  179. {
  180. SetIdenticalAttribute (true);
  181. }
  182. private void SetIdenticalAttribute (bool remove)
  183. {
  184. if (ownerElement == null)
  185. return;
  186. // Check if new attribute's datatype is ID.
  187. XmlDocumentType doctype = ownerDocument.DocumentType;
  188. if (doctype == null || doctype.DTD == null)
  189. return;
  190. DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];
  191. for (int i = 0; i < Nodes.Count; i++) {
  192. XmlAttribute node = (XmlAttribute) Nodes [i];
  193. DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
  194. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  195. continue;
  196. if (remove) {
  197. if (ownerDocument.GetIdenticalAttribute (node.Value) != null) {
  198. ownerDocument.RemoveIdenticalAttribute (node.Value);
  199. return;
  200. }
  201. } else {
  202. // adding new identical attribute, but
  203. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  204. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  205. throw new XmlException (String.Format (
  206. "ID value {0} already exists in this document.", node.Value));
  207. ownerDocument.AddIdenticalAttribute (node);
  208. return;
  209. }
  210. }
  211. }
  212. private XmlNode AdjustIdenticalAttributes (XmlAttribute node, XmlNode existing)
  213. {
  214. // If owner element is not appended to the document,
  215. // ID table should not be filled.
  216. if (ownerElement == null)
  217. return existing;
  218. RemoveIdenticalAttribute (existing);
  219. // Check if new attribute's datatype is ID.
  220. XmlDocumentType doctype = node.OwnerDocument.DocumentType;
  221. if (doctype == null || doctype.DTD == null)
  222. return existing;
  223. DTDAttListDeclaration attList = doctype.DTD.AttListDecls [ownerElement.Name];
  224. DTDAttributeDefinition attdef = attList == null ? null : attList.Get (node.Name);
  225. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  226. return existing;
  227. // adding new identical attribute, but
  228. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  229. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  230. throw new XmlException (String.Format (
  231. "ID value {0} already exists in this document.", node.Value));
  232. ownerDocument.AddIdenticalAttribute (node);
  233. return existing;
  234. }
  235. private XmlNode RemoveIdenticalAttribute (XmlNode existing)
  236. {
  237. // If owner element is not appended to the document,
  238. // ID table should not be filled.
  239. if (ownerElement == null)
  240. return existing;
  241. if (existing != null) {
  242. // remove identical attribute (if it is).
  243. if (ownerDocument.GetIdenticalAttribute (existing.Value) != null)
  244. ownerDocument.RemoveIdenticalAttribute (existing.Value);
  245. }
  246. return existing;
  247. }
  248. }
  249. }