XmlCodeExporter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. //
  2. // System.Xml.Serialization.XmlCodeExporter
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.CodeDom;
  31. using System.Collections;
  32. using System.Xml.Schema;
  33. using System.CodeDom.Compiler;
  34. namespace System.Xml.Serialization
  35. {
  36. public class XmlCodeExporter
  37. #if NET_2_0
  38. : CodeExporter
  39. #endif
  40. {
  41. #region Fields
  42. #if NET_2_0
  43. CodeGenerationOptions options;
  44. #else
  45. XmlMapCodeGenerator codeGenerator;
  46. #endif
  47. #endregion
  48. #region Constructors
  49. public XmlCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
  50. {
  51. }
  52. public XmlCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  53. {
  54. #if NET_2_0
  55. codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, CodeGenerationOptions.GenerateProperties);
  56. #else
  57. codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, CodeGenerationOptions.None);
  58. #endif
  59. }
  60. #if NET_2_0
  61. public XmlCodeExporter (CodeNamespace codeNamespace,
  62. CodeCompileUnit codeCompileUnit,
  63. CodeGenerationOptions options)
  64. : this (codeNamespace, codeCompileUnit, null, options, null)
  65. {
  66. }
  67. public XmlCodeExporter (CodeNamespace codeNamespace,
  68. CodeCompileUnit codeCompileUnit,
  69. CodeGenerationOptions options,
  70. Hashtable mappings)
  71. : this (codeNamespace, codeCompileUnit, null, options, mappings)
  72. {
  73. }
  74. [MonoTODO ("mappings?")]
  75. public XmlCodeExporter (CodeNamespace codeNamespace,
  76. CodeCompileUnit codeCompileUnit,
  77. CodeDomProvider codeProvider,
  78. CodeGenerationOptions options,
  79. Hashtable mappings)
  80. {
  81. codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, codeProvider, options, mappings);
  82. }
  83. #endif
  84. #endregion // Constructors
  85. #region Properties
  86. #if !NET_2_0
  87. public CodeAttributeDeclarationCollection IncludeMetadata {
  88. get { return codeGenerator.IncludeMetadata; }
  89. }
  90. #endif
  91. #endregion Properties
  92. #region Methods
  93. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns)
  94. {
  95. AddMappingMetadata (metadata, member, ns, false);
  96. }
  97. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlTypeMapping member, string ns)
  98. {
  99. if ( (member.TypeData.SchemaType == SchemaTypes.Primitive ||
  100. member.TypeData.SchemaType == SchemaTypes.Array)
  101. && member.Namespace != XmlSchema.Namespace)
  102. {
  103. CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRoot");
  104. ratt.Arguments.Add (MapCodeGenerator.GetArg (member.ElementName));
  105. ratt.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", member.Namespace));
  106. ratt.Arguments.Add (MapCodeGenerator.GetArg ("IsNullable", member.IsNullable));
  107. metadata.Add (ratt);
  108. }
  109. }
  110. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName)
  111. {
  112. CodeAttributeDeclaration att;
  113. TypeData memType = member.TypeMapMember.TypeData;
  114. if (member.Any)
  115. {
  116. XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo;
  117. foreach (XmlTypeMapElementInfo info in list)
  118. {
  119. if (info.IsTextElement)
  120. metadata.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlText"));
  121. else {
  122. att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
  123. if (!info.IsUnnamedAnyElement) {
  124. att.Arguments.Add (MapCodeGenerator.GetArg ("Name", info.ElementName));
  125. if (info.Namespace != ns) att.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", member.Namespace));
  126. }
  127. metadata.Add (att);
  128. }
  129. }
  130. }
  131. else if (member.TypeMapMember is XmlTypeMapMemberList)
  132. {
  133. // Array parameter
  134. XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList;
  135. ListMap listMap = (ListMap) list.ListTypeMapping.ObjectMap;
  136. codeGenerator.AddArrayAttributes (metadata, list, ns, forceUseMemberName);
  137. codeGenerator.AddArrayItemAttributes (metadata, listMap, memType.ListItemTypeData, list.Namespace, 0);
  138. }
  139. else if (member.TypeMapMember is XmlTypeMapMemberElement) {
  140. codeGenerator.AddElementMemberAttributes ((XmlTypeMapMemberElement) member.TypeMapMember, ns, metadata, forceUseMemberName);
  141. }
  142. else if (member.TypeMapMember is XmlTypeMapMemberAttribute) {
  143. codeGenerator.AddAttributeMemberAttributes ((XmlTypeMapMemberAttribute) member.TypeMapMember, ns, metadata, forceUseMemberName);
  144. }
  145. else
  146. throw new NotSupportedException ("Schema type not supported");
  147. }
  148. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  149. {
  150. codeGenerator.ExportMembersMapping (xmlMembersMapping);
  151. }
  152. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  153. {
  154. codeGenerator.ExportTypeMapping (xmlTypeMapping, true);
  155. }
  156. #endregion // Methods
  157. }
  158. class XmlMapCodeGenerator : MapCodeGenerator
  159. {
  160. public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeGenerationOptions options)
  161. : base (codeNamespace, codeCompileUnit, options)
  162. {
  163. }
  164. public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, CodeDomProvider codeProvider, CodeGenerationOptions options, Hashtable mappings)
  165. : base (codeNamespace, codeCompileUnit, codeProvider, options, mappings)
  166. {
  167. }
  168. protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass, bool isTopLevel)
  169. {
  170. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTypeAttribute");
  171. if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
  172. if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
  173. if (!map.IncludeInSchema) att.Arguments.Add (GetArg ("IncludeInSchema", false));
  174. AddCustomAttribute (codeClass, att, false);
  175. CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRootAttribute");
  176. if (map.ElementName != map.XmlType) ratt.Arguments.Add (GetArg (map.ElementName));
  177. if (isTopLevel) {
  178. ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
  179. ratt.Arguments.Add (GetArg ("IsNullable", map.IsNullable));
  180. } else {
  181. if (map.Namespace != "")
  182. ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
  183. }
  184. AddCustomAttribute (codeClass, ratt, isTopLevel);
  185. }
  186. protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
  187. {
  188. CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIncludeAttribute");
  189. iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
  190. attributes.Add (iatt);
  191. }
  192. protected override void GenerateAnyAttribute (CodeTypeMember codeField)
  193. {
  194. AddCustomAttribute (codeField, "System.Xml.Serialization.XmlAnyAttribute");
  195. }
  196. protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
  197. {
  198. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAttributeAttribute");
  199. if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
  200. if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
  201. if (attinfo.Form == XmlSchemaForm.Qualified) att.Arguments.Add (GetEnumArg ("Form","System.Xml.Schema.XmlSchemaForm",attinfo.Form.ToString()));
  202. if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType));
  203. attributes.Add (att);
  204. if (attinfo.Ignore)
  205. attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
  206. }
  207. protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
  208. {
  209. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlElementAttribute");
  210. if (forceUseMemberName || einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
  211. if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) att.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
  212. if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  213. if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
  214. if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
  215. if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) att.Arguments.Add (GetArg ("DataType",einfo.TypeData.XmlType));
  216. if (addAlwaysAttr || att.Arguments.Count > 0) attributes.Add (att);
  217. }
  218. protected override void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
  219. {
  220. if (member.ChoiceMember != null) {
  221. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlChoiceIdentifier");
  222. att.Arguments.Add (GetArg(member.ChoiceMember));
  223. attributes.Add (att);
  224. }
  225. if (member.Ignore)
  226. attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
  227. }
  228. protected override void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
  229. {
  230. XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
  231. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArray");
  232. if (forceUseMemberName || (einfo.ElementName != member.Name)) att.Arguments.Add (GetArg ("ElementName", einfo.ElementName));
  233. if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  234. if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
  235. if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
  236. if (att.Arguments.Count > 0) attributes.Add (att);
  237. }
  238. protected override void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
  239. {
  240. bool needsType = (listMap.ItemInfo.Count > 1) ||
  241. (ainfo.TypeData.FullTypeName != type.FullTypeName && !listMap.IsMultiArray);
  242. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArrayItem");
  243. if (ainfo.ElementName != defaultName) att.Arguments.Add (GetArg ("ElementName", ainfo.ElementName));
  244. if (ainfo.Namespace != defaultNamespace && ainfo.Namespace != XmlSchema.Namespace) att.Arguments.Add (GetArg ("Namespace", ainfo.Namespace));
  245. if (needsType) att.Arguments.Add (GetTypeArg ("Type", ainfo.TypeData.FullTypeName));
  246. if (!ainfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", false));
  247. if (ainfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", ainfo.Form.ToString()));
  248. if (att.Arguments.Count > 0 && nestingLevel > 0) att.Arguments.Add (GetArg ("NestingLevel", nestingLevel));
  249. if (att.Arguments.Count > 0) attributes.Add (att);
  250. }
  251. protected override void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
  252. {
  253. CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTextAttribute");
  254. if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) uatt.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
  255. attributes.Add (uatt);
  256. }
  257. protected override void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
  258. {
  259. CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
  260. if (!einfo.IsUnnamedAnyElement) uatt.Arguments.Add (GetArg ("Name", einfo.ElementName));
  261. if (einfo.Namespace != defaultNamespace) uatt.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  262. attributes.Add (uatt);
  263. }
  264. protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum, bool isTopLevel)
  265. {
  266. GenerateClass (map, codeEnum, isTopLevel);
  267. }
  268. protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
  269. {
  270. if (emem.EnumName != emem.XmlName)
  271. {
  272. CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlEnumAttribute");
  273. xatt.Arguments.Add (GetArg (emem.XmlName));
  274. AddCustomAttribute (codeField, xatt, true);
  275. }
  276. }
  277. protected override void GenerateSpecifierMember (CodeTypeMember codeField)
  278. {
  279. AddCustomAttribute (codeField, "System.Xml.Serialization.XmlIgnore");
  280. }
  281. }
  282. }