XmlAttributes.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. //
  2. // XmlAttributes.cs:
  3. //
  4. // Author:
  5. // John Donagher ([email protected])
  6. //
  7. // (C) 2002 John Donagher
  8. //
  9. using System.Reflection;
  10. using System;
  11. using System.ComponentModel;
  12. using System.Collections;
  13. namespace System.Xml.Serialization
  14. {
  15. /// <summary>
  16. /// Summary description for XmlAttributes.
  17. /// </summary>
  18. public class XmlAttributes
  19. {
  20. private XmlAnyAttributeAttribute xmlAnyAttribute;
  21. private XmlAnyElementAttributes xmlAnyElements = new XmlAnyElementAttributes();
  22. private XmlArrayAttribute xmlArray;
  23. private XmlArrayItemAttributes xmlArrayItems = new XmlArrayItemAttributes();
  24. private XmlAttributeAttribute xmlAttribute;
  25. private XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
  26. private object xmlDefaultValue = System.DBNull.Value;
  27. private XmlElementAttributes xmlElements = new XmlElementAttributes();
  28. private XmlEnumAttribute xmlEnum;
  29. private bool xmlIgnore;
  30. private bool xmlns;
  31. private XmlRootAttribute xmlRoot;
  32. private XmlTextAttribute xmlText;
  33. private XmlTypeAttribute xmlType;
  34. public XmlAttributes ()
  35. {
  36. }
  37. public XmlAttributes (ICustomAttributeProvider provider)
  38. {
  39. object[] attributes = provider.GetCustomAttributes(false);
  40. foreach(object obj in attributes)
  41. {
  42. if(obj is XmlAnyAttributeAttribute)
  43. xmlAnyAttribute = (XmlAnyAttributeAttribute) obj;
  44. else if(obj is XmlAnyElementAttribute)
  45. xmlAnyElements.Add((XmlAnyElementAttribute) obj);
  46. else if(obj is XmlArrayAttribute)
  47. xmlArray = (XmlArrayAttribute) obj;
  48. else if(obj is XmlArrayItemAttribute)
  49. xmlArrayItems.Add((XmlArrayItemAttribute) obj);
  50. else if(obj is XmlAttributeAttribute)
  51. xmlAttribute = (XmlAttributeAttribute) obj;
  52. else if(obj is XmlChoiceIdentifierAttribute)
  53. xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
  54. else if(obj is DefaultValueAttribute)
  55. xmlDefaultValue = ((DefaultValueAttribute)obj).Value;
  56. else if(obj is XmlElementAttribute )
  57. xmlElements.Add((XmlElementAttribute ) obj);
  58. else if(obj is XmlEnumAttribute)
  59. xmlEnum = (XmlEnumAttribute) obj;
  60. else if(obj is XmlIgnoreAttribute)
  61. xmlIgnore = true;
  62. else if(obj is XmlNamespaceDeclarationsAttribute)
  63. xmlns = true;
  64. else if(obj is XmlRootAttribute)
  65. xmlRoot = (XmlRootAttribute) obj;
  66. else if(obj is XmlTextAttribute)
  67. xmlText = (XmlTextAttribute) obj;
  68. else if(obj is XmlTypeAttribute)
  69. xmlType = (XmlTypeAttribute) obj;
  70. }
  71. }
  72. #region public properties
  73. public XmlAnyAttributeAttribute XmlAnyAttribute
  74. {
  75. get
  76. {
  77. return xmlAnyAttribute;
  78. }
  79. set
  80. {
  81. xmlAnyAttribute = value;
  82. }
  83. }
  84. public XmlAnyElementAttributes XmlAnyElements
  85. {
  86. get
  87. {
  88. return xmlAnyElements;
  89. }
  90. }
  91. public XmlArrayAttribute XmlArray
  92. {
  93. get
  94. {
  95. return xmlArray;
  96. }
  97. set
  98. {
  99. xmlArray = value;
  100. }
  101. }
  102. public XmlArrayItemAttributes XmlArrayItems
  103. {
  104. get
  105. {
  106. return xmlArrayItems;
  107. }
  108. }
  109. public XmlAttributeAttribute XmlAttribute
  110. {
  111. get
  112. {
  113. return xmlAttribute;
  114. }
  115. set
  116. {
  117. xmlAttribute = value;
  118. }
  119. }
  120. public XmlChoiceIdentifierAttribute XmlChoiceIdentifier
  121. {
  122. get
  123. {
  124. return xmlChoiceIdentifier;
  125. }
  126. }
  127. public object XmlDefaultValue
  128. {
  129. get
  130. {
  131. return xmlDefaultValue;
  132. }
  133. set
  134. {
  135. xmlDefaultValue = value;
  136. }
  137. }
  138. public XmlElementAttributes XmlElements
  139. {
  140. get
  141. {
  142. return xmlElements;
  143. }
  144. }
  145. public XmlEnumAttribute XmlEnum
  146. {
  147. get
  148. {
  149. return xmlEnum;
  150. }
  151. set
  152. {
  153. xmlEnum = value;
  154. }
  155. }
  156. public bool XmlIgnore
  157. {
  158. get
  159. {
  160. return xmlIgnore;
  161. }
  162. set
  163. {
  164. xmlIgnore = value;
  165. }
  166. }
  167. public bool Xmlns
  168. {
  169. get
  170. {
  171. return xmlns;
  172. }
  173. set
  174. {
  175. xmlns = value;
  176. }
  177. }
  178. public XmlRootAttribute XmlRoot
  179. {
  180. get
  181. {
  182. return xmlRoot;}
  183. set
  184. {
  185. xmlRoot = value;
  186. }
  187. }
  188. public XmlTextAttribute XmlText
  189. {
  190. get
  191. {
  192. return xmlText;
  193. }
  194. set
  195. {
  196. xmlText = value;
  197. }
  198. }
  199. public XmlTypeAttribute XmlType
  200. {
  201. get
  202. {
  203. return xmlType;
  204. }
  205. set
  206. {
  207. xmlType = value;
  208. }
  209. }
  210. #endregion
  211. internal void AddKeyHash (System.Text.StringBuilder sb)
  212. {
  213. sb.Append ("XA ");
  214. KeyHelper.AddField (sb, 1, xmlIgnore);
  215. KeyHelper.AddField (sb, 2, xmlns);
  216. KeyHelper.AddField (sb, 3, xmlAnyAttribute!=null);
  217. xmlAnyElements.AddKeyHash (sb);
  218. xmlArrayItems.AddKeyHash (sb);
  219. xmlElements.AddKeyHash (sb);
  220. if (xmlArray != null)
  221. xmlArray.AddKeyHash (sb);
  222. if (xmlAttribute != null)
  223. xmlAttribute.AddKeyHash (sb);
  224. if (xmlDefaultValue == null) {
  225. sb.Append ("n");
  226. }
  227. else if (!(xmlDefaultValue is System.DBNull)) {
  228. string v = XmlCustomFormatter.ToXmlString (TypeTranslator.GetTypeData (xmlDefaultValue.GetType()), xmlDefaultValue);
  229. sb.Append ("v" + v);
  230. }
  231. if (xmlEnum != null)
  232. xmlEnum.AddKeyHash (sb);
  233. if (xmlRoot != null)
  234. xmlRoot.AddKeyHash (sb);
  235. if (xmlText != null)
  236. xmlText.AddKeyHash (sb);
  237. if (xmlType != null)
  238. xmlType.AddKeyHash (sb);
  239. if (xmlChoiceIdentifier != null)
  240. xmlChoiceIdentifier.AddKeyHash (sb);
  241. sb.Append ("|");
  242. }
  243. }
  244. }