XmlAttributes.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlAttributes.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. //------------------------------------------------------------------------------
  7. namespace System.Xml.Serialization {
  8. using System;
  9. using System.Reflection;
  10. using System.Collections;
  11. using System.ComponentModel;
  12. internal enum XmlAttributeFlags {
  13. Enum = 0x1,
  14. Array = 0x2,
  15. Text = 0x4,
  16. ArrayItems = 0x8,
  17. Elements = 0x10,
  18. Attribute = 0x20,
  19. Root = 0x40,
  20. Type = 0x80,
  21. AnyElements = 0x100,
  22. AnyAttribute = 0x200,
  23. ChoiceIdentifier = 0x400,
  24. XmlnsDeclarations = 0x800,
  25. }
  26. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes"]/*' />
  27. /// <devdoc>
  28. /// <para>[To be supplied.]</para>
  29. /// </devdoc>
  30. public class XmlAttributes {
  31. XmlElementAttributes xmlElements = new XmlElementAttributes();
  32. XmlArrayItemAttributes xmlArrayItems = new XmlArrayItemAttributes();
  33. XmlAnyElementAttributes xmlAnyElements = new XmlAnyElementAttributes();
  34. XmlArrayAttribute xmlArray;
  35. XmlAttributeAttribute xmlAttribute;
  36. XmlTextAttribute xmlText;
  37. XmlEnumAttribute xmlEnum;
  38. bool xmlIgnore;
  39. bool xmlns;
  40. object xmlDefaultValue = null;
  41. XmlRootAttribute xmlRoot;
  42. XmlTypeAttribute xmlType;
  43. XmlAnyAttributeAttribute xmlAnyAttribute;
  44. XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
  45. static volatile Type ignoreAttributeType;
  46. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlAttributes"]/*' />
  47. /// <devdoc>
  48. /// <para>[To be supplied.]</para>
  49. /// </devdoc>
  50. public XmlAttributes() {
  51. }
  52. internal XmlAttributeFlags XmlFlags {
  53. get {
  54. XmlAttributeFlags flags = 0;
  55. if (xmlElements.Count > 0) flags |= XmlAttributeFlags.Elements;
  56. if (xmlArrayItems.Count > 0) flags |= XmlAttributeFlags.ArrayItems;
  57. if (xmlAnyElements.Count > 0) flags |= XmlAttributeFlags.AnyElements;
  58. if (xmlArray != null) flags |= XmlAttributeFlags.Array;
  59. if (xmlAttribute != null) flags |= XmlAttributeFlags.Attribute;
  60. if (xmlText != null) flags |= XmlAttributeFlags.Text;
  61. if (xmlEnum != null) flags |= XmlAttributeFlags.Enum;
  62. if (xmlRoot != null) flags |= XmlAttributeFlags.Root;
  63. if (xmlType != null) flags |= XmlAttributeFlags.Type;
  64. if (xmlAnyAttribute != null) flags |= XmlAttributeFlags.AnyAttribute;
  65. if (xmlChoiceIdentifier != null) flags |= XmlAttributeFlags.ChoiceIdentifier;
  66. if (xmlns) flags |= XmlAttributeFlags.XmlnsDeclarations;
  67. return flags;
  68. }
  69. }
  70. private static Type IgnoreAttribute {
  71. get {
  72. if (ignoreAttributeType == null) {
  73. ignoreAttributeType = typeof(object).Assembly.GetType("System.XmlIgnoreMemberAttribute");
  74. if (ignoreAttributeType == null) {
  75. ignoreAttributeType = typeof(XmlIgnoreAttribute);
  76. }
  77. }
  78. return ignoreAttributeType;
  79. }
  80. }
  81. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlAttributes1"]/*' />
  82. /// <devdoc>
  83. /// <para>[To be supplied.]</para>
  84. /// </devdoc>
  85. public XmlAttributes(ICustomAttributeProvider provider) {
  86. object[] attrs = provider.GetCustomAttributes(false);
  87. // most generic <any/> matches everithig
  88. XmlAnyElementAttribute wildcard = null;
  89. for (int i = 0; i < attrs.Length; i++) {
  90. if (attrs[i] is XmlIgnoreAttribute || attrs[i] is ObsoleteAttribute || attrs[i].GetType() == IgnoreAttribute) {
  91. xmlIgnore = true;
  92. break;
  93. }
  94. else if (attrs[i] is XmlElementAttribute) {
  95. this.xmlElements.Add((XmlElementAttribute)attrs[i]);
  96. }
  97. else if (attrs[i] is XmlArrayItemAttribute) {
  98. this.xmlArrayItems.Add((XmlArrayItemAttribute)attrs[i]);
  99. }
  100. else if (attrs[i] is XmlAnyElementAttribute) {
  101. XmlAnyElementAttribute any = (XmlAnyElementAttribute)attrs[i];
  102. if ((any.Name == null || any.Name.Length == 0) && any.NamespaceSpecified && any.Namespace == null) {
  103. // ignore duplicate wildcards
  104. wildcard = any;
  105. }
  106. else {
  107. this.xmlAnyElements.Add((XmlAnyElementAttribute)attrs[i]);
  108. }
  109. }
  110. else if (attrs[i] is DefaultValueAttribute) {
  111. this.xmlDefaultValue = ((DefaultValueAttribute)attrs[i]).Value;
  112. }
  113. else if (attrs[i] is XmlAttributeAttribute) {
  114. this.xmlAttribute = (XmlAttributeAttribute)attrs[i];
  115. }
  116. else if (attrs[i] is XmlArrayAttribute) {
  117. this.xmlArray = (XmlArrayAttribute)attrs[i];
  118. }
  119. else if (attrs[i] is XmlTextAttribute) {
  120. this.xmlText = (XmlTextAttribute)attrs[i];
  121. }
  122. else if (attrs[i] is XmlEnumAttribute) {
  123. this.xmlEnum = (XmlEnumAttribute)attrs[i];
  124. }
  125. else if (attrs[i] is XmlRootAttribute) {
  126. this.xmlRoot = (XmlRootAttribute)attrs[i];
  127. }
  128. else if (attrs[i] is XmlTypeAttribute) {
  129. this.xmlType = (XmlTypeAttribute)attrs[i];
  130. }
  131. else if (attrs[i] is XmlAnyAttributeAttribute) {
  132. this.xmlAnyAttribute = (XmlAnyAttributeAttribute)attrs[i];
  133. }
  134. else if (attrs[i] is XmlChoiceIdentifierAttribute) {
  135. this.xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute)attrs[i];
  136. }
  137. else if (attrs[i] is XmlNamespaceDeclarationsAttribute) {
  138. this.xmlns = true;
  139. }
  140. }
  141. if (xmlIgnore) {
  142. this.xmlElements.Clear();
  143. this.xmlArrayItems.Clear();
  144. this.xmlAnyElements.Clear();
  145. this.xmlDefaultValue = null;
  146. this.xmlAttribute = null;
  147. this.xmlArray = null;
  148. this.xmlText = null;
  149. this.xmlEnum = null;
  150. this.xmlType = null;
  151. this.xmlAnyAttribute = null;
  152. this.xmlChoiceIdentifier = null;
  153. this.xmlns = false;
  154. }
  155. else {
  156. if (wildcard != null) {
  157. this.xmlAnyElements.Add(wildcard);
  158. }
  159. }
  160. }
  161. internal static object GetAttr(ICustomAttributeProvider provider, Type attrType) {
  162. object[] attrs = provider.GetCustomAttributes(attrType, false);
  163. if (attrs.Length == 0) return null;
  164. return attrs[0];
  165. }
  166. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlElements"]/*' />
  167. /// <devdoc>
  168. /// <para>[To be supplied.]</para>
  169. /// </devdoc>
  170. public XmlElementAttributes XmlElements {
  171. get { return xmlElements; }
  172. }
  173. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlAttribute"]/*' />
  174. /// <devdoc>
  175. /// <para>[To be supplied.]</para>
  176. /// </devdoc>
  177. public XmlAttributeAttribute XmlAttribute {
  178. get { return xmlAttribute; }
  179. set { xmlAttribute = value; }
  180. }
  181. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlEnum"]/*' />
  182. /// <devdoc>
  183. /// <para>[To be supplied.]</para>
  184. /// </devdoc>
  185. public XmlEnumAttribute XmlEnum {
  186. get { return xmlEnum; }
  187. set { xmlEnum = value; }
  188. }
  189. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlText"]/*' />
  190. /// <devdoc>
  191. /// <para>[To be supplied.]</para>
  192. /// </devdoc>
  193. public XmlTextAttribute XmlText {
  194. get { return xmlText; }
  195. set { xmlText = value; }
  196. }
  197. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlArray"]/*' />
  198. /// <devdoc>
  199. /// <para>[To be supplied.]</para>
  200. /// </devdoc>
  201. public XmlArrayAttribute XmlArray {
  202. get { return xmlArray; }
  203. set { xmlArray = value; }
  204. }
  205. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlArrayItems"]/*' />
  206. /// <devdoc>
  207. /// <para>[To be supplied.]</para>
  208. /// </devdoc>
  209. public XmlArrayItemAttributes XmlArrayItems {
  210. get { return xmlArrayItems; }
  211. }
  212. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlDefaultValue"]/*' />
  213. /// <devdoc>
  214. /// <para>[To be supplied.]</para>
  215. /// </devdoc>
  216. public object XmlDefaultValue {
  217. get { return xmlDefaultValue; }
  218. set { xmlDefaultValue = value; }
  219. }
  220. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlIgnore"]/*' />
  221. /// <devdoc>
  222. /// <para>[To be supplied.]</para>
  223. /// </devdoc>
  224. public bool XmlIgnore {
  225. get { return xmlIgnore; }
  226. set { xmlIgnore = value; }
  227. }
  228. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlType"]/*' />
  229. /// <devdoc>
  230. /// <para>[To be supplied.]</para>
  231. /// </devdoc>
  232. public XmlTypeAttribute XmlType {
  233. get { return xmlType; }
  234. set { xmlType = value; }
  235. }
  236. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlRoot"]/*' />
  237. /// <devdoc>
  238. /// <para>[To be supplied.]</para>
  239. /// </devdoc>
  240. public XmlRootAttribute XmlRoot {
  241. get { return xmlRoot; }
  242. set { xmlRoot = value; }
  243. }
  244. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlAnyElement"]/*' />
  245. /// <devdoc>
  246. /// <para>[To be supplied.]</para>
  247. /// </devdoc>
  248. public XmlAnyElementAttributes XmlAnyElements {
  249. get { return xmlAnyElements; }
  250. }
  251. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlAnyAttribute"]/*' />
  252. /// <devdoc>
  253. /// <para>[To be supplied.]</para>
  254. /// </devdoc>
  255. public XmlAnyAttributeAttribute XmlAnyAttribute {
  256. get { return xmlAnyAttribute; }
  257. set { xmlAnyAttribute = value; }
  258. }
  259. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.XmlChoiceIdentifier"]/*' />
  260. public XmlChoiceIdentifierAttribute XmlChoiceIdentifier {
  261. get { return xmlChoiceIdentifier; }
  262. }
  263. /// <include file='doc\XmlAttributes.uex' path='docs/doc[@for="XmlAttributes.Xmlns"]/*' />
  264. /// <devdoc>
  265. /// <para>[To be supplied.]</para>
  266. /// </devdoc>
  267. public bool Xmlns {
  268. get { return xmlns; }
  269. set { xmlns = value; }
  270. }
  271. }
  272. }