2
0

XmlCodeExporter.cs 14 KB

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