XmlAttributeCollection.cs 8.0 KB

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