XmlAttributeCollection.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. #if NET_2_0
  37. public sealed class XmlAttributeCollection : XmlNamedNodeMap, ICollection
  38. #else
  39. public class XmlAttributeCollection : XmlNamedNodeMap, ICollection
  40. #endif
  41. {
  42. XmlElement ownerElement;
  43. XmlDocument ownerDocument;
  44. internal XmlAttributeCollection (XmlNode parent) : base (parent)
  45. {
  46. ownerElement = parent as XmlElement;
  47. ownerDocument = parent.OwnerDocument;
  48. if(ownerElement == null)
  49. throw new XmlException ("invalid construction for XmlAttributeCollection.");
  50. }
  51. bool ICollection.IsSynchronized {
  52. get { return false; }
  53. }
  54. bool IsReadOnly {
  55. get { return ownerElement.IsReadOnly; }
  56. }
  57. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  58. #if NET_2_0
  59. public XmlAttribute this [string name] {
  60. #else
  61. public virtual XmlAttribute this [string name] {
  62. #endif
  63. get {
  64. return (XmlAttribute) GetNamedItem (name);
  65. }
  66. }
  67. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  68. #if NET_2_0
  69. public XmlAttribute this [int i] {
  70. #else
  71. public virtual XmlAttribute this [int i] {
  72. #endif
  73. get {
  74. return (XmlAttribute) Nodes [i];
  75. }
  76. }
  77. [System.Runtime.CompilerServices.IndexerName ("ItemOf")]
  78. #if NET_2_0
  79. public XmlAttribute this [string localName, string namespaceURI] {
  80. #else
  81. public virtual XmlAttribute this [string localName, string namespaceURI] {
  82. #endif
  83. get {
  84. return (XmlAttribute) GetNamedItem (localName, namespaceURI);
  85. }
  86. }
  87. object ICollection.SyncRoot {
  88. get { return this; }
  89. }
  90. #if NET_2_0
  91. public XmlAttribute Append (XmlAttribute node)
  92. #else
  93. public virtual XmlAttribute Append (XmlAttribute node)
  94. #endif
  95. {
  96. SetNamedItem (node);
  97. return node;
  98. }
  99. public void CopyTo (XmlAttribute[] array, int index)
  100. {
  101. // assuming that Nodes is a correct collection.
  102. for(int i=0; i<Nodes.Count; i++)
  103. array [index + i] = Nodes [i] as XmlAttribute;
  104. }
  105. void ICollection.CopyTo (Array array, int index)
  106. {
  107. // assuming that Nodes is a correct collection.
  108. array.CopyTo (Nodes.ToArray (typeof(XmlAttribute)), index);
  109. }
  110. #if NET_2_0
  111. public XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  112. #else
  113. public virtual XmlAttribute InsertAfter (XmlAttribute newNode, XmlAttribute refNode)
  114. #endif
  115. {
  116. if (refNode == null) {
  117. if (Nodes.Count == 0)
  118. return InsertBefore (newNode, null);
  119. else
  120. return InsertBefore (newNode, this [0]);
  121. }
  122. for (int i = 0; i < Nodes.Count; i++)
  123. if (refNode == Nodes [i])
  124. return InsertBefore (newNode, Nodes.Count == i + 1 ? null : this [i + 1]);
  125. throw new ArgumentException ("refNode not found in this collection.");
  126. }
  127. #if NET_2_0
  128. public XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  129. #else
  130. public virtual XmlAttribute InsertBefore (XmlAttribute newNode, XmlAttribute refNode)
  131. #endif
  132. {
  133. if (newNode.OwnerDocument != ownerDocument)
  134. throw new ArgumentException ("different document created this newNode.");
  135. ownerDocument.onNodeInserting (newNode, null);
  136. int pos = Nodes.Count;
  137. if (refNode != null) {
  138. for (int i = 0; i < Nodes.Count; i++) {
  139. XmlNode n = Nodes [i] as XmlNode;
  140. if (n == refNode) {
  141. pos = i;
  142. break;
  143. }
  144. }
  145. if (pos == Nodes.Count)
  146. throw new ArgumentException ("refNode not found in this collection.");
  147. }
  148. SetNamedItem (newNode, pos, false);
  149. ownerDocument.onNodeInserted (newNode, null);
  150. return newNode;
  151. }
  152. #if NET_2_0
  153. public XmlAttribute Prepend (XmlAttribute node)
  154. #else
  155. public virtual XmlAttribute Prepend (XmlAttribute node)
  156. #endif
  157. {
  158. return this.InsertAfter (node, null);
  159. }
  160. #if NET_2_0
  161. public XmlAttribute Remove (XmlAttribute node)
  162. #else
  163. public virtual XmlAttribute Remove (XmlAttribute node)
  164. #endif
  165. {
  166. if (IsReadOnly)
  167. throw new ArgumentException ("This attribute collection is read-only.");
  168. if (node == null)
  169. throw new ArgumentException ("Specified node is null.");
  170. if (node.OwnerDocument != ownerDocument)
  171. throw new ArgumentException ("Specified node is in a different document.");
  172. if (node.OwnerElement != this.ownerElement)
  173. throw new ArgumentException ("The specified attribute is not contained in the element.");
  174. XmlAttribute retAttr = null;
  175. for (int i = 0; i < Nodes.Count; i++) {
  176. XmlAttribute attr = (XmlAttribute) Nodes [i];
  177. if (attr == node) {
  178. retAttr = attr;
  179. break;
  180. }
  181. }
  182. if(retAttr != null) {
  183. ownerDocument.onNodeRemoving (node, ownerElement);
  184. base.RemoveNamedItem (retAttr.LocalName, retAttr.NamespaceURI);
  185. RemoveIdenticalAttribute (retAttr);
  186. ownerDocument.onNodeRemoved (node, ownerElement);
  187. }
  188. // If it is default, then directly create new attribute.
  189. DTDAttributeDefinition def = retAttr.GetAttributeDefinition ();
  190. if (def != null && def.DefaultValue != null) {
  191. XmlAttribute attr = ownerDocument.CreateAttribute (
  192. retAttr.Prefix, retAttr.LocalName, retAttr.NamespaceURI);
  193. attr.Value = def.DefaultValue;
  194. attr.SetDefault ();
  195. this.SetNamedItem (attr);
  196. }
  197. retAttr.AttributeOwnerElement = null;
  198. return retAttr;
  199. }
  200. #if NET_2_0
  201. public void RemoveAll ()
  202. #else
  203. public virtual void RemoveAll ()
  204. #endif
  205. {
  206. int current = 0;
  207. while (current < Count) {
  208. XmlAttribute attr = this [current];
  209. if (!attr.Specified)
  210. current++;
  211. // It is called for the purpose of event support.
  212. Remove (attr);
  213. }
  214. }
  215. #if NET_2_0
  216. public XmlAttribute RemoveAt (int i)
  217. #else
  218. public virtual XmlAttribute RemoveAt (int i)
  219. #endif
  220. {
  221. if(Nodes.Count <= i)
  222. return null;
  223. return Remove ((XmlAttribute)Nodes [i]);
  224. }
  225. public override XmlNode SetNamedItem (XmlNode node)
  226. {
  227. if(IsReadOnly)
  228. throw new ArgumentException ("this AttributeCollection is read only.");
  229. XmlAttribute attr = node as XmlAttribute;
  230. if (attr.OwnerElement != null)
  231. throw new ArgumentException ("This attribute is already set to another element.");
  232. ownerElement.OwnerDocument.onNodeInserting (node, ownerElement);
  233. attr.AttributeOwnerElement = ownerElement;
  234. XmlNode n = AdjustIdenticalAttributes (node as XmlAttribute, base.SetNamedItem (node, -1, false));
  235. ownerElement.OwnerDocument.onNodeInserted (node, ownerElement);
  236. return n as XmlAttribute;
  237. }
  238. internal void AddIdenticalAttribute ()
  239. {
  240. SetIdenticalAttribute (false);
  241. }
  242. internal void RemoveIdenticalAttribute ()
  243. {
  244. SetIdenticalAttribute (true);
  245. }
  246. private void SetIdenticalAttribute (bool remove)
  247. {
  248. if (ownerElement == null)
  249. return;
  250. // Check if new attribute's datatype is ID.
  251. XmlDocumentType doctype = ownerDocument.DocumentType;
  252. if (doctype == null || doctype.DTD == null)
  253. return;
  254. DTDElementDeclaration elem = doctype.DTD.ElementDecls [ownerElement.Name];
  255. for (int i = 0; i < Nodes.Count; i++) {
  256. XmlAttribute node = (XmlAttribute) Nodes [i];
  257. DTDAttributeDefinition attdef = elem == null ? null : elem.Attributes [node.Name];
  258. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  259. continue;
  260. if (remove) {
  261. if (ownerDocument.GetIdenticalAttribute (node.Value) != null) {
  262. ownerDocument.RemoveIdenticalAttribute (node.Value);
  263. return;
  264. }
  265. } else {
  266. // adding new identical attribute, but
  267. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  268. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  269. throw new XmlException (String.Format (
  270. "ID value {0} already exists in this document.", node.Value));
  271. ownerDocument.AddIdenticalAttribute (node);
  272. return;
  273. }
  274. }
  275. }
  276. private XmlNode AdjustIdenticalAttributes (XmlAttribute node, XmlNode existing)
  277. {
  278. // If owner element is not appended to the document,
  279. // ID table should not be filled.
  280. if (ownerElement == null)
  281. return existing;
  282. RemoveIdenticalAttribute (existing);
  283. // Check if new attribute's datatype is ID.
  284. XmlDocumentType doctype = node.OwnerDocument.DocumentType;
  285. if (doctype == null || doctype.DTD == null)
  286. return existing;
  287. DTDAttListDeclaration attList = doctype.DTD.AttListDecls [ownerElement.Name];
  288. DTDAttributeDefinition attdef = attList == null ? null : attList.Get (node.Name);
  289. if (attdef == null || attdef.Datatype.TokenizedType != XmlTokenizedType.ID)
  290. return existing;
  291. // adding new identical attribute, but
  292. // MS.NET is pity for ID support, so I'm wondering how to correct it...
  293. if (ownerDocument.GetIdenticalAttribute (node.Value) != null)
  294. throw new XmlException (String.Format (
  295. "ID value {0} already exists in this document.", node.Value));
  296. ownerDocument.AddIdenticalAttribute (node);
  297. return existing;
  298. }
  299. private XmlNode RemoveIdenticalAttribute (XmlNode existing)
  300. {
  301. // If owner element is not appended to the document,
  302. // ID table should not be filled.
  303. if (ownerElement == null)
  304. return existing;
  305. if (existing != null) {
  306. // remove identical attribute (if it is).
  307. if (ownerDocument.GetIdenticalAttribute (existing.Value) != null)
  308. ownerDocument.RemoveIdenticalAttribute (existing.Value);
  309. }
  310. return existing;
  311. }
  312. }
  313. }