XmlCodeExporter.cs 15 KB

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