XmlAttributeCollection.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. // If it is default, then directly create new attribute.
  150. if (!retAttr.Specified) {
  151. XmlAttribute attr = ownerDocument.CreateAttribute (retAttr.Prefix,
  152. retAttr.LocalName, retAttr.NamespaceURI);
  153. attr.SetDefault ();
  154. foreach (XmlNode child in retAttr.ChildNodes)
  155. attr.AppendChild (child);
  156. this.SetNamedItem (attr);
  157. }
  158. return retAttr;
  159. }
  160. public virtual void RemoveAll ()
  161. {
  162. int current = 0;
  163. while (current < Count) {
  164. XmlAttribute attr = this [current];
  165. if (!attr.Specified)
  166. current++;
  167. // It is called for the purpose of event support.
  168. Remove (attr);
  169. }
  170. }
  171. public virtual XmlAttribute RemoveAt (int i)
  172. {
  173. if(Nodes.Count <= i)
  174. return null;
  175. return Remove ((XmlAttribute)Nodes [i]);
  176. }
  177. public override XmlNode SetNamedItem (XmlNode node)
  178. {
  179. if(IsReadOnly)
  180. throw new XmlException ("this AttributeCollection is read only.");
  181. return AdjustIdenticalAttributes (node as XmlAttribute, base.SetNamedItem (node, -1) as XmlAttribute);
  182. }
  183. internal void AddIdenticalAttribute ()
  184. {
  185. SetIdenticalAttribute (false);
  186. }
  187. internal void RemoveIdenticalAttribute ()
  188. {
  189. SetIdenticalAttribute (true);
  190. }
  191. private void SetIdenticalAttribute (bool remove)
  192. {
  193. if (ownerElement == null)
  194. return;
  195. // Check if new attribute's datatype is ID.
  196. XmlDocumentType doctype = ownerDocument.DocumentType;
  197. if (doctype == null || doctype.DTD == null)
  198. return;
  199. DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];
  200. foreach (XmlAttribute node in this) {
  201. DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
  202. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  203. continue;
  204. if (remove) {
  205. if (ownerDocument.GetIdenticalAttribute (node.Value) != null) {
  206. ownerDocument.RemoveIdenticalAttribute (node.Value);
  207. return;
  208. }
  209. } else {
  210. // adding new identical attribute, but
  211. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  212. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  213. throw new XmlException (String.Format (
  214. "ID value {0} already exists in this document.", node.Value));
  215. ownerDocument.AddIdenticalAttribute (node);
  216. return;
  217. }
  218. }
  219. }
  220. private XmlNode AdjustIdenticalAttributes (XmlNode node, XmlNode existing)
  221. {
  222. // If owner element is not appended to the document,
  223. // ID table should not be filled.
  224. if (ownerElement == null)
  225. return existing;
  226. RemoveIdenticalAttribute (existing);
  227. // Check if new attribute's datatype is ID.
  228. XmlDocumentType doctype = node.OwnerDocument.DocumentType;
  229. if (doctype == null || doctype.DTD == null)
  230. return existing;
  231. DTDAttListDeclaration attList = doctype.DTD.AttListDecls [ownerElement.Name];
  232. DTDAttributeDefinition attdef = attList == null ? null : attList.Get (node.Name);
  233. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  234. return existing;
  235. // adding new identical attribute, but
  236. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  237. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  238. throw new XmlException (String.Format (
  239. "ID value {0} already exists in this document.", node.Value));
  240. ownerDocument.AddIdenticalAttribute (node as XmlAttribute);
  241. return existing;
  242. }
  243. private XmlNode RemoveIdenticalAttribute (XmlNode existing)
  244. {
  245. // If owner element is not appended to the document,
  246. // ID table should not be filled.
  247. if (ownerElement == null)
  248. return existing;
  249. if (existing != null) {
  250. // remove identical attribute (if it is).
  251. if (ownerDocument.GetIdenticalAttribute (existing.Value) != null)
  252. ownerDocument.RemoveIdenticalAttribute (existing.Value);
  253. }
  254. return existing;
  255. }
  256. }
  257. }