XmlAttributes.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. //
  2. // XmlAttributes.cs:
  3. //
  4. // Author:
  5. // John Donagher ([email protected])
  6. //
  7. // (C) 2002 John Donagher
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System.Reflection;
  30. using System;
  31. using System.ComponentModel;
  32. using System.Collections;
  33. namespace System.Xml.Serialization
  34. {
  35. /// <summary>
  36. /// Summary description for XmlAttributes.
  37. /// </summary>
  38. public class XmlAttributes
  39. {
  40. private XmlAnyAttributeAttribute xmlAnyAttribute;
  41. private XmlAnyElementAttributes xmlAnyElements = new XmlAnyElementAttributes();
  42. private XmlArrayAttribute xmlArray;
  43. private XmlArrayItemAttributes xmlArrayItems = new XmlArrayItemAttributes();
  44. private XmlAttributeAttribute xmlAttribute;
  45. private XmlChoiceIdentifierAttribute xmlChoiceIdentifier;
  46. private object xmlDefaultValue = System.DBNull.Value;
  47. private XmlElementAttributes xmlElements = new XmlElementAttributes();
  48. private XmlEnumAttribute xmlEnum;
  49. private bool xmlIgnore;
  50. private bool xmlns;
  51. private XmlRootAttribute xmlRoot;
  52. private XmlTextAttribute xmlText;
  53. private XmlTypeAttribute xmlType;
  54. public XmlAttributes ()
  55. {
  56. }
  57. public XmlAttributes (ICustomAttributeProvider provider)
  58. {
  59. object[] attributes = provider.GetCustomAttributes(false);
  60. foreach(object obj in attributes)
  61. {
  62. if(obj is XmlAnyAttributeAttribute)
  63. xmlAnyAttribute = (XmlAnyAttributeAttribute) obj;
  64. else if(obj is XmlAnyElementAttribute)
  65. xmlAnyElements.Add((XmlAnyElementAttribute) obj);
  66. else if(obj is XmlArrayAttribute)
  67. xmlArray = (XmlArrayAttribute) obj;
  68. else if(obj is XmlArrayItemAttribute)
  69. xmlArrayItems.Add((XmlArrayItemAttribute) obj);
  70. else if(obj is XmlAttributeAttribute)
  71. xmlAttribute = (XmlAttributeAttribute) obj;
  72. else if(obj is XmlChoiceIdentifierAttribute)
  73. xmlChoiceIdentifier = (XmlChoiceIdentifierAttribute) obj;
  74. else if(obj is DefaultValueAttribute)
  75. xmlDefaultValue = ((DefaultValueAttribute)obj).Value;
  76. else if(obj is XmlElementAttribute )
  77. xmlElements.Add((XmlElementAttribute ) obj);
  78. else if(obj is XmlEnumAttribute)
  79. xmlEnum = (XmlEnumAttribute) obj;
  80. else if(obj is XmlIgnoreAttribute)
  81. xmlIgnore = true;
  82. else if(obj is XmlNamespaceDeclarationsAttribute)
  83. xmlns = true;
  84. else if(obj is XmlRootAttribute)
  85. xmlRoot = (XmlRootAttribute) obj;
  86. else if(obj is XmlTextAttribute)
  87. xmlText = (XmlTextAttribute) obj;
  88. else if(obj is XmlTypeAttribute)
  89. xmlType = (XmlTypeAttribute) obj;
  90. }
  91. }
  92. #region public properties
  93. public XmlAnyAttributeAttribute XmlAnyAttribute
  94. {
  95. get
  96. {
  97. return xmlAnyAttribute;
  98. }
  99. set
  100. {
  101. xmlAnyAttribute = value;
  102. }
  103. }
  104. public XmlAnyElementAttributes XmlAnyElements
  105. {
  106. get
  107. {
  108. return xmlAnyElements;
  109. }
  110. }
  111. public XmlArrayAttribute XmlArray
  112. {
  113. get
  114. {
  115. return xmlArray;
  116. }
  117. set
  118. {
  119. xmlArray = value;
  120. }
  121. }
  122. public XmlArrayItemAttributes XmlArrayItems
  123. {
  124. get
  125. {
  126. return xmlArrayItems;
  127. }
  128. }
  129. public XmlAttributeAttribute XmlAttribute
  130. {
  131. get
  132. {
  133. return xmlAttribute;
  134. }
  135. set
  136. {
  137. xmlAttribute = value;
  138. }
  139. }
  140. public XmlChoiceIdentifierAttribute XmlChoiceIdentifier
  141. {
  142. get
  143. {
  144. return xmlChoiceIdentifier;
  145. }
  146. }
  147. public object XmlDefaultValue
  148. {
  149. get
  150. {
  151. return xmlDefaultValue;
  152. }
  153. set
  154. {
  155. xmlDefaultValue = value;
  156. }
  157. }
  158. public XmlElementAttributes XmlElements
  159. {
  160. get
  161. {
  162. return xmlElements;
  163. }
  164. }
  165. public XmlEnumAttribute XmlEnum
  166. {
  167. get
  168. {
  169. return xmlEnum;
  170. }
  171. set
  172. {
  173. xmlEnum = value;
  174. }
  175. }
  176. public bool XmlIgnore
  177. {
  178. get
  179. {
  180. return xmlIgnore;
  181. }
  182. set
  183. {
  184. xmlIgnore = value;
  185. }
  186. }
  187. public bool Xmlns
  188. {
  189. get
  190. {
  191. return xmlns;
  192. }
  193. set
  194. {
  195. xmlns = value;
  196. }
  197. }
  198. public XmlRootAttribute XmlRoot
  199. {
  200. get
  201. {
  202. return xmlRoot;}
  203. set
  204. {
  205. xmlRoot = value;
  206. }
  207. }
  208. public XmlTextAttribute XmlText
  209. {
  210. get
  211. {
  212. return xmlText;
  213. }
  214. set
  215. {
  216. xmlText = value;
  217. }
  218. }
  219. public XmlTypeAttribute XmlType
  220. {
  221. get
  222. {
  223. return xmlType;
  224. }
  225. set
  226. {
  227. xmlType = value;
  228. }
  229. }
  230. #endregion
  231. internal void AddKeyHash (System.Text.StringBuilder sb)
  232. {
  233. sb.Append ("XA ");
  234. KeyHelper.AddField (sb, 1, xmlIgnore);
  235. KeyHelper.AddField (sb, 2, xmlns);
  236. KeyHelper.AddField (sb, 3, xmlAnyAttribute!=null);
  237. xmlAnyElements.AddKeyHash (sb);
  238. xmlArrayItems.AddKeyHash (sb);
  239. xmlElements.AddKeyHash (sb);
  240. if (xmlArray != null)
  241. xmlArray.AddKeyHash (sb);
  242. if (xmlAttribute != null)
  243. xmlAttribute.AddKeyHash (sb);
  244. if (xmlDefaultValue == null) {
  245. sb.Append ("n");
  246. }
  247. else if (!(xmlDefaultValue is System.DBNull)) {
  248. string v = XmlCustomFormatter.ToXmlString (TypeTranslator.GetTypeData (xmlDefaultValue.GetType()), xmlDefaultValue);
  249. sb.Append ("v" + v);
  250. }
  251. if (xmlEnum != null)
  252. xmlEnum.AddKeyHash (sb);
  253. if (xmlRoot != null)
  254. xmlRoot.AddKeyHash (sb);
  255. if (xmlText != null)
  256. xmlText.AddKeyHash (sb);
  257. if (xmlType != null)
  258. xmlType.AddKeyHash (sb);
  259. if (xmlChoiceIdentifier != null)
  260. xmlChoiceIdentifier.AddKeyHash (sb);
  261. sb.Append ("|");
  262. }
  263. }
  264. }