XmlAttributeCollection.cs 7.4 KB

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