XmlAttributeCollection.cs 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using Mono.Xml;
  34. namespace System.Xml
  35. {
  36. public class XmlAttributeCollection : XmlNamedNodeMap, ICollection
  37. {
  38. XmlElement ownerElement;
  39. XmlDocument ownerDocument;
  40. internal XmlAttributeCollection (XmlNode parent) : base (parent)
  41. {
  42. ownerElement = parent as XmlElement;
  43. ownerDocument = parent.OwnerDocument;
  44. if(ownerElement == null)
  45. throw new XmlException ("invalid construction for XmlAttributeCollection.");
  46. }
  47. bool ICollection.IsSynchronized {
  48. get { return false; }
  49. }
  50. bool IsReadOnly {
  51. get { return ownerElement.IsReadOnly; }
  52. }
  53. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  54. public virtual XmlAttribute this [string name] {
  55. get {
  56. return (XmlAttribute) GetNamedItem (name);
  57. }
  58. }
  59. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  60. public virtual XmlAttribute this [int i] {
  61. get {
  62. return (XmlAttribute) Nodes [i];
  63. }
  64. }
  65. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  66. public virtual XmlAttribute this [string localName, string namespaceURI] {
  67. get {
  68. return (XmlAttribute) GetNamedItem (localName, namespaceURI);
  69. }
  70. }
  71. object ICollection.SyncRoot {
  72. get { return this; }
  73. }
  74. public virtual XmlAttribute Append (XmlAttribute node)
  75. {
  76. XmlNode xmlNode = this.SetNamedItem (node);
  77. return node;
  78. }
  79. public void CopyTo (XmlAttribute[] array, int index)
  80. {
  81. // assuming that Nodes is a correct collection.
  82. for(int i=0; i<Nodes.Count; i++)
  83. array [index + i] = Nodes [i] as XmlAttribute;
  84. }
  85. void ICollection.CopyTo (Array array, int index)
  86. {
  87. // assuming that Nodes is a correct collection.
  88. array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
  89. }
  90. public virtual XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  91. {
  92. if (refNode == null) {
  93. if (Nodes.Count == 0)
  94. return InsertBefore (newNode, null);
  95. else
  96. return InsertBefore (newNode, this [0]);
  97. }
  98. for (int i = 0; i < Nodes.Count; i++)
  99. if (refNode == Nodes [i])
  100. return InsertBefore (newNode, Nodes.Count == i + 1 ? null : this [i + 1]);
  101. throw new ArgumentException ("refNode not found in this collection.");
  102. }
  103. public virtual XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  104. {
  105. if (newNode.OwnerDocument != ownerDocument)
  106. throw new ArgumentException ("different document created this newNode.");
  107. ownerDocument.onNodeInserting (newNode, null);
  108. int pos = Nodes.Count;
  109. if (refNode != null) {
  110. for (int i = 0; i < Nodes.Count; i++) {
  111. XmlNode n = Nodes [i] as XmlNode;
  112. if (n == refNode) {
  113. pos = i;
  114. break;
  115. }
  116. }
  117. if (pos == Nodes.Count)
  118. throw new ArgumentException ("refNode not found in this collection.");
  119. }
  120. SetNamedItem (newNode, pos, false);
  121. ownerDocument.onNodeInserted (newNode, null);
  122. return newNode;
  123. }
  124. public virtual XmlAttribute Prepend (XmlAttribute node)
  125. {
  126. return this.InsertAfter (node, null);
  127. }
  128. public virtual XmlAttribute Remove (XmlAttribute node)
  129. {
  130. if (IsReadOnly)
  131. throw new ArgumentException ("This attribute collection is read-only.");
  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. if (node.OwnerElement != this.ownerElement)
  137. throw new ArgumentException ("The specified attribute is not contained in the element.");
  138. XmlAttribute retAttr = null;
  139. for (int i = 0; i < Nodes.Count; i++) {
  140. XmlAttribute attr = (XmlAttribute) Nodes [i];
  141. if (attr == node) {
  142. retAttr = attr;
  143. break;
  144. }
  145. }
  146. if(retAttr != null) {
  147. ownerDocument.onNodeRemoving (node, ownerElement);
  148. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  149. RemoveIdenticalAttribute (retAttr);
  150. ownerDocument.onNodeRemoved (node, ownerElement);
  151. }
  152. // If it is default, then directly create new attribute.
  153. DTDAttributeDefinition def = retAttr.GetAttributeDefinition ();
  154. if (def != null && def.DefaultValue != null) {
  155. XmlAttribute attr = ownerDocument.CreateAttribute (
  156. retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
  157. attr.Value = def.DefaultValue;
  158. attr.SetDefault ();
  159. this.SetNamedItem (attr);
  160. }
  161. retAttr.SetOwnerElement (null);
  162. return retAttr;
  163. }
  164. public virtual void RemoveAll ()
  165. {
  166. int current = 0;
  167. while (current < Count) {
  168. XmlAttribute attr = this [current];
  169. if (!attr.Specified)
  170. current++;
  171. // It is called for the purpose of event support.
  172. Remove (attr);
  173. }
  174. }
  175. public virtual XmlAttribute RemoveAt (int i)
  176. {
  177. if(Nodes.Count <= i)
  178. return null;
  179. return Remove ((XmlAttribute)Nodes [i]);
  180. }
  181. public override XmlNode SetNamedItem (XmlNode node)
  182. {
  183. if(IsReadOnly)
  184. throw new ArgumentException ("this AttributeCollection is read only.");
  185. XmlAttribute attr = node as XmlAttribute;
  186. if (attr.OwnerElement != null)
  187. throw new ArgumentException ("This attribute is already set to another element.");
  188. ownerElement.OwnerDocument.onNodeInserting (node, ownerElement);
  189. attr.SetOwnerElement (ownerElement);
  190. XmlNode n = AdjustIdenticalAttributes (node as XmlAttribute, base.SetNamedItem (node, -1, false));
  191. ownerElement.OwnerDocument.onNodeInserted (node, ownerElement);
  192. return n as XmlAttribute;
  193. }
  194. internal void AddIdenticalAttribute ()
  195. {
  196. SetIdenticalAttribute (false);
  197. }
  198. internal void RemoveIdenticalAttribute ()
  199. {
  200. SetIdenticalAttribute (true);
  201. }
  202. private void SetIdenticalAttribute (bool remove)
  203. {
  204. if (ownerElement == null)
  205. return;
  206. // Check if new attribute's datatype is ID.
  207. XmlDocumentType doctype = ownerDocument.DocumentType;
  208. if (doctype == null || doctype.DTD == null)
  209. return;
  210. DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];
  211. for (int i = 0; i < Nodes.Count; i++) {
  212. XmlAttribute node = (XmlAttribute) Nodes [i];
  213. DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
  214. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  215. continue;
  216. if (remove) {
  217. if (ownerDocument.GetIdenticalAttribute (node.Value) != null) {
  218. ownerDocument.RemoveIdenticalAttribute (node.Value);
  219. return;
  220. }
  221. } else {
  222. // adding new identical attribute, but
  223. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  224. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  225. throw new XmlException (String.Format (
  226. "ID value {0} already exists in this document.", node.Value));
  227. ownerDocument.AddIdenticalAttribute (node);
  228. return;
  229. }
  230. }
  231. }
  232. private XmlNode AdjustIdenticalAttributes (XmlAttribute node, XmlNode existing)
  233. {
  234. // If owner element is not appended to the document,
  235. // ID table should not be filled.
  236. if (ownerElement == null)
  237. return existing;
  238. RemoveIdenticalAttribute (existing);
  239. // Check if new attribute's datatype is ID.
  240. XmlDocumentType doctype = node.OwnerDocument.DocumentType;
  241. if (doctype == null || doctype.DTD == null)
  242. return existing;
  243. DTDAttListDeclaration attList = doctype.DTD.AttListDecls [ownerElement.Name];
  244. DTDAttributeDefinition attdef = attList == null ? null : attList.Get (node.Name);
  245. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  246. return existing;
  247. // adding new identical attribute, but
  248. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  249. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  250. throw new XmlException (String.Format (
  251. "ID value {0} already exists in this document.", node.Value));
  252. ownerDocument.AddIdenticalAttribute (node);
  253. return existing;
  254. }
  255. private XmlNode RemoveIdenticalAttribute (XmlNode existing)
  256. {
  257. // If owner element is not appended to the document,
  258. // ID table should not be filled.
  259. if (ownerElement == null)
  260. return existing;
  261. if (existing != null) {
  262. // remove identical attribute (if it is).
  263. if (ownerDocument.GetIdenticalAttribute (existing.Value) != null)
  264. ownerDocument.RemoveIdenticalAttribute (existing.Value);
  265. }
  266. return existing;
  267. }
  268. }
  269. }