XmlAttributeCollection.cs 8.2 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 { 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. DTDAttListDeclaration attList = ownerDocument.DocumentType != null ? ownerDocument.DocumentType.DTD.AttListDecls [ownerElement.Name] : null;
  145. DTDAttributeDefinition def = attList != null ? attList [retAttr.Name] : null;
  146. if (def != null && def.DefaultValue != null) {
  147. XmlAttribute attr = ownerDocument.CreateAttribute (
  148. retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
  149. attr.Value = def.DefaultValue;
  150. attr.SetDefault ();
  151. this.SetNamedItem (attr);
  152. }
  153. retAttr.SetOwnerElement (null);
  154. return retAttr;
  155. }
  156. public virtual void RemoveAll ()
  157. {
  158. int current = 0;
  159. while (current < Count) {
  160. XmlAttribute attr = this [current];
  161. if (!attr.Specified)
  162. current++;
  163. // It is called for the purpose of event support.
  164. Remove (attr);
  165. }
  166. }
  167. public virtual XmlAttribute RemoveAt (int i)
  168. {
  169. if(Nodes.Count <= i)
  170. return null;
  171. return Remove ((XmlAttribute)Nodes [i]);
  172. }
  173. public override XmlNode SetNamedItem (XmlNode node)
  174. {
  175. if(IsReadOnly)
  176. throw new XmlException ("this AttributeCollection is read only.");
  177. XmlAttribute attr = node as XmlAttribute;
  178. if (attr.OwnerElement != null)
  179. throw new InvalidOperationException ("This attribute is already set to another element.");
  180. attr.SetOwnerElement (ownerElement);
  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 (XmlAttribute 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);
  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. }