XmlCodeExporter.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. CodeNamespace codeNamespace;
  43. CodeCompileUnit codeCompileUnit;
  44. bool encodedFormat;
  45. #if NET_2_0
  46. CodeGenerationOptions options;
  47. #else
  48. XmlMapCodeGenerator codeGenerator;
  49. #endif
  50. Hashtable exportedMaps = new Hashtable ();
  51. #endregion
  52. #region Constructors
  53. public XmlCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
  54. {
  55. }
  56. public XmlCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  57. {
  58. this.codeCompileUnit = codeCompileUnit;
  59. this.codeNamespace = codeNamespace;
  60. codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit);
  61. }
  62. #if NET_2_0
  63. public XmlCodeExporter (CodeNamespace codeNamespace,
  64. CodeCompileUnit codeCompileUnit,
  65. CodeGenerationOptions options)
  66. : this (codeNamespace, codeCompileUnit, null, options, null)
  67. {
  68. }
  69. public XmlCodeExporter (CodeNamespace codeNamespace,
  70. CodeCompileUnit codeCompileUnit,
  71. CodeGenerationOptions options,
  72. Hashtable mappings)
  73. : this (codeNamespace, codeCompileUnit, null, options, mappings)
  74. {
  75. }
  76. [MonoTODO ("mappings?")]
  77. public XmlCodeExporter (CodeNamespace codeNamespace,
  78. CodeCompileUnit codeCompileUnit,
  79. ICodeGenerator codeGen,
  80. CodeGenerationOptions options,
  81. Hashtable mappings)
  82. {
  83. this.codeCompileUnit = codeCompileUnit;
  84. this.codeNamespace = codeNamespace;
  85. codeGenerator = new XmlMapCodeGenerator (codeNamespace, codeCompileUnit, codeGen, options, mappings);
  86. }
  87. #endif
  88. #endregion // Constructors
  89. #region Properties
  90. #if !NET_2_0
  91. public CodeAttributeDeclarationCollection IncludeMetadata {
  92. get { return codeGenerator.IncludeMetadata; }
  93. }
  94. #endif
  95. #endregion Properties
  96. #region Methods
  97. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns)
  98. {
  99. AddMappingMetadata (metadata, member, ns, false);
  100. }
  101. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlTypeMapping member, string ns)
  102. {
  103. if (member.Namespace != ns && member.Namespace != "") {
  104. CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRoot");
  105. ratt.Arguments.Add (MapCodeGenerator.GetArg (member.ElementName));
  106. ratt.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", member.Namespace));
  107. ratt.Arguments.Add (MapCodeGenerator.GetArg ("IsNullable", member.IsNullable));
  108. metadata.Add (ratt);
  109. }
  110. }
  111. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, string ns, bool forceUseMemberName)
  112. {
  113. CodeAttributeDeclaration att;
  114. TypeData memType = member.TypeMapMember.TypeData;
  115. if (member.Any)
  116. {
  117. XmlTypeMapElementInfoList list = (XmlTypeMapElementInfoList)((XmlTypeMapMemberElement)member.TypeMapMember).ElementInfo;
  118. foreach (XmlTypeMapElementInfo info in list)
  119. {
  120. if (info.IsTextElement)
  121. metadata.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlText"));
  122. else {
  123. att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
  124. if (!info.IsUnnamedAnyElement) {
  125. att.Arguments.Add (MapCodeGenerator.GetArg ("Name", info.ElementName));
  126. if (info.Namespace != ns) att.Arguments.Add (MapCodeGenerator.GetArg ("Namespace", member.Namespace));
  127. }
  128. metadata.Add (att);
  129. }
  130. }
  131. }
  132. else if (member.TypeMapMember is XmlTypeMapMemberList)
  133. {
  134. // Array parameter
  135. XmlTypeMapMemberList list = member.TypeMapMember as XmlTypeMapMemberList;
  136. ListMap listMap = (ListMap) list.ListTypeMapping.ObjectMap;
  137. codeGenerator.AddArrayAttributes (metadata, list, ns, forceUseMemberName);
  138. codeGenerator.AddArrayItemAttributes (metadata, listMap, memType.ListItemTypeData, list.Namespace, 0);
  139. }
  140. else if (member.TypeMapMember is XmlTypeMapMemberElement) {
  141. codeGenerator.AddElementMemberAttributes ((XmlTypeMapMemberElement) member.TypeMapMember, ns, metadata, forceUseMemberName);
  142. }
  143. else if (member.TypeMapMember is XmlTypeMapMemberAttribute) {
  144. codeGenerator.AddAttributeMemberAttributes ((XmlTypeMapMemberAttribute) member.TypeMapMember, ns, metadata, forceUseMemberName);
  145. }
  146. else
  147. throw new NotSupportedException ("Schema type not supported");
  148. }
  149. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  150. {
  151. codeGenerator.ExportMembersMapping (xmlMembersMapping);
  152. }
  153. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  154. {
  155. codeGenerator.ExportTypeMapping (xmlTypeMapping);
  156. }
  157. #endregion // Methods
  158. }
  159. class XmlMapCodeGenerator : MapCodeGenerator
  160. {
  161. public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  162. : base (codeNamespace, codeCompileUnit)
  163. {
  164. }
  165. public XmlMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, ICodeGenerator codeGen, CodeGenerationOptions options, Hashtable mappings)
  166. : base (codeNamespace, codeCompileUnit, codeGen, options, mappings)
  167. {
  168. }
  169. protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass)
  170. {
  171. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlType");
  172. if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
  173. if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
  174. if (!map.IncludeInSchema) att.Arguments.Add (GetArg ("IncludeInSchema", false));
  175. AddCustomAttribute (codeClass, att, false);
  176. CodeAttributeDeclaration ratt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlRoot");
  177. if (map.ElementName != map.XmlType) ratt.Arguments.Add (GetArg (map.ElementName));
  178. if (map.Namespace != "") ratt.Arguments.Add (GetArg ("Namespace", map.Namespace));
  179. AddCustomAttribute (codeClass, ratt, false);
  180. }
  181. protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
  182. {
  183. CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlInclude");
  184. iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
  185. attributes.Add (iatt);
  186. }
  187. protected override void GenerateAnyAttribute (CodeTypeMember codeField)
  188. {
  189. AddCustomAttribute (codeField, "System.Xml.Serialization.XmlAnyAttribute");
  190. }
  191. protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
  192. {
  193. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAttribute");
  194. if (forceUseMemberName || attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
  195. if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
  196. if (attinfo.Form != XmlSchemaForm.None) att.Arguments.Add (GetEnumArg ("Form","System.Xml.Schema.XmlSchemaForm",attinfo.Form.ToString()));
  197. if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType));
  198. attributes.Add (att);
  199. if (attinfo.Ignore)
  200. attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
  201. }
  202. protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
  203. {
  204. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlElement");
  205. if (forceUseMemberName || einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
  206. if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) att.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
  207. if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  208. if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
  209. if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
  210. if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) att.Arguments.Add (GetArg ("DataType",einfo.TypeData.XmlType));
  211. if (addAlwaysAttr || att.Arguments.Count > 0) attributes.Add (att);
  212. }
  213. protected override void GenerateElementMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member)
  214. {
  215. if (member.ChoiceMember != null) {
  216. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlChoiceIdentifier");
  217. att.Arguments.Add (GetArg(member.ChoiceMember));
  218. attributes.Add (att);
  219. }
  220. if (member.Ignore)
  221. attributes.Add (new CodeAttributeDeclaration ("System.Xml.Serialization.XmlIgnoreAttribute"));
  222. }
  223. protected override void GenerateArrayElement (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, string defaultNamespace, bool forceUseMemberName)
  224. {
  225. XmlTypeMapElementInfo einfo = (XmlTypeMapElementInfo) member.ElementInfo[0];
  226. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArray");
  227. if (forceUseMemberName || (einfo.ElementName != member.Name)) att.Arguments.Add (GetArg ("ElementName", einfo.ElementName));
  228. if (einfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  229. if (einfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", einfo.Form.ToString()));
  230. if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true));
  231. if (att.Arguments.Count > 0) attributes.Add (att);
  232. }
  233. protected override void GenerateArrayItemAttributes (CodeAttributeDeclarationCollection attributes, ListMap listMap, TypeData type, XmlTypeMapElementInfo ainfo, string defaultName, string defaultNamespace, int nestingLevel)
  234. {
  235. bool needsType = (listMap.ItemInfo.Count > 1) ||
  236. (ainfo.TypeData.FullTypeName != type.FullTypeName && !listMap.IsMultiArray);
  237. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlArrayItem");
  238. if (ainfo.ElementName != defaultName) att.Arguments.Add (GetArg ("ElementName", ainfo.ElementName));
  239. if (ainfo.Namespace != defaultNamespace && ainfo.Namespace != XmlSchema.Namespace) att.Arguments.Add (GetArg ("Namespace", ainfo.Namespace));
  240. if (needsType) att.Arguments.Add (GetTypeArg ("Type", ainfo.TypeData.FullTypeName));
  241. if (!ainfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", false));
  242. if (ainfo.Form == XmlSchemaForm.Unqualified) att.Arguments.Add (MapCodeGenerator.GetEnumArg ("Form", "System.Xml.Schema.XmlSchemaForm", ainfo.Form.ToString()));
  243. if (att.Arguments.Count > 0 && nestingLevel > 0) att.Arguments.Add (GetArg ("NestingLevel", nestingLevel));
  244. if (att.Arguments.Count > 0) attributes.Add (att);
  245. }
  246. protected override void GenerateTextElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, TypeData defaultType)
  247. {
  248. CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlText");
  249. if (einfo.TypeData.FullTypeName != defaultType.FullTypeName) uatt.Arguments.Add (GetTypeArg ("Type", einfo.TypeData.FullTypeName));
  250. attributes.Add (uatt);
  251. }
  252. protected override void GenerateUnnamedAnyElementAttribute (CodeAttributeDeclarationCollection attributes, XmlTypeMapElementInfo einfo, string defaultNamespace)
  253. {
  254. CodeAttributeDeclaration uatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlAnyElement");
  255. if (!einfo.IsUnnamedAnyElement) uatt.Arguments.Add (GetArg ("Name", einfo.ElementName));
  256. if (einfo.Namespace != defaultNamespace) uatt.Arguments.Add (GetArg ("Namespace", einfo.Namespace));
  257. attributes.Add (uatt);
  258. }
  259. protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum)
  260. {
  261. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlTypeAttribute");
  262. if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg ("TypeName", map.XmlType));
  263. if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
  264. AddCustomAttribute (codeEnum, att, false);
  265. }
  266. protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
  267. {
  268. if (emem.EnumName != emem.XmlName)
  269. {
  270. CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.XmlEnum");
  271. xatt.Arguments.Add (GetArg ("Name", emem.XmlName));
  272. AddCustomAttribute (codeField, xatt, true);
  273. }
  274. }
  275. protected override void GenerateSpecifierMember (CodeTypeMember codeField)
  276. {
  277. AddCustomAttribute (codeField, "System.Xml.Serialization.XmlIgnore");
  278. }
  279. }
  280. }