XmlCodeExporter.cs 15 KB

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