SoapCodeExporter.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // System.Xml.Serialization.SoapCodeExporter
  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.CodeDom.Compiler;
  32. using System.Collections;
  33. namespace System.Xml.Serialization
  34. {
  35. public class SoapCodeExporter
  36. #if NET_2_0
  37. : CodeExporter
  38. #endif
  39. {
  40. #region Fields
  41. CodeNamespace codeNamespace;
  42. CodeCompileUnit codeCompileUnit;
  43. #if !NET_2_0
  44. SoapMapCodeGenerator codeGenerator;
  45. #endif
  46. #endregion
  47. #region Constructors
  48. public SoapCodeExporter (CodeNamespace codeNamespace): this (codeNamespace, null)
  49. {
  50. }
  51. public SoapCodeExporter (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  52. {
  53. this.codeCompileUnit = codeCompileUnit;
  54. this.codeNamespace = codeNamespace;
  55. codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit);
  56. }
  57. #if NET_2_0
  58. public SoapCodeExporter (CodeNamespace codeNamespace,
  59. CodeCompileUnit codeCompileUnit,
  60. CodeGenerationOptions options)
  61. : this (codeNamespace, codeCompileUnit, null, options, null)
  62. {
  63. }
  64. public SoapCodeExporter (CodeNamespace codeNamespace,
  65. CodeCompileUnit codeCompileUnit,
  66. CodeGenerationOptions options,
  67. Hashtable mappings)
  68. : this (codeNamespace, codeCompileUnit, null, options, mappings)
  69. {
  70. }
  71. [MonoTODO ("mappings?")]
  72. public SoapCodeExporter (CodeNamespace codeNamespace,
  73. CodeCompileUnit codeCompileUnit,
  74. ICodeGenerator codeGen,
  75. CodeGenerationOptions options,
  76. Hashtable mappings)
  77. {
  78. this.codeCompileUnit = codeCompileUnit;
  79. this.codeNamespace = codeNamespace;
  80. codeGenerator = new SoapMapCodeGenerator (codeNamespace, codeCompileUnit, codeGen, options, mappings);
  81. }
  82. #endif
  83. #endregion // Constructors
  84. #region Properties
  85. #if !NET_2_0
  86. public CodeAttributeDeclarationCollection IncludeMetadata {
  87. get { return codeGenerator.IncludeMetadata; }
  88. }
  89. #endif
  90. #endregion // Properties
  91. #region Methods
  92. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member)
  93. {
  94. AddMappingMetadata (metadata, member, false);
  95. }
  96. public void AddMappingMetadata (CodeAttributeDeclarationCollection metadata, XmlMemberMapping member, bool forceUseMemberName)
  97. {
  98. TypeData memType = member.TypeMapMember.TypeData;
  99. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapElement");
  100. if (forceUseMemberName || (member.ElementName != member.MemberName))
  101. att.Arguments.Add (new CodeAttributeArgument (new CodePrimitiveExpression(member.ElementName)));
  102. if (!TypeTranslator.IsDefaultPrimitiveTpeData (memType))
  103. att.Arguments.Add (new CodeAttributeArgument ("DataType", new CodePrimitiveExpression(member.TypeName)));
  104. if (att.Arguments.Count > 0)
  105. metadata.Add (att);
  106. }
  107. public void ExportMembersMapping (XmlMembersMapping xmlMembersMapping)
  108. {
  109. codeGenerator.ExportMembersMapping (xmlMembersMapping);
  110. }
  111. public void ExportTypeMapping (XmlTypeMapping xmlTypeMapping)
  112. {
  113. codeGenerator.ExportTypeMapping (xmlTypeMapping);
  114. }
  115. #endregion // Methods
  116. }
  117. class SoapMapCodeGenerator : MapCodeGenerator
  118. {
  119. public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit)
  120. : base (codeNamespace, codeCompileUnit)
  121. {
  122. includeArrayTypes = true;
  123. }
  124. public SoapMapCodeGenerator (CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, ICodeGenerator codeGen, CodeGenerationOptions options, Hashtable mappings)
  125. : base (codeNamespace, codeCompileUnit, codeGen, options, mappings)
  126. {
  127. }
  128. protected override void GenerateClass (XmlTypeMapping map, CodeTypeDeclaration codeClass)
  129. {
  130. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapType");
  131. if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
  132. if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
  133. AddCustomAttribute (codeClass, att, false);
  134. }
  135. protected override void GenerateClassInclude (CodeAttributeDeclarationCollection attributes, XmlTypeMapping map)
  136. {
  137. CodeAttributeDeclaration iatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapInclude");
  138. iatt.Arguments.Add (new CodeAttributeArgument (new CodeTypeOfExpression(map.TypeData.FullTypeName)));
  139. attributes.Add (iatt);
  140. }
  141. protected override void GenerateAttributeMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberAttribute attinfo, string defaultNamespace, bool forceUseMemberName)
  142. {
  143. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapAttribute");
  144. if (attinfo.Name != attinfo.AttributeName) att.Arguments.Add (GetArg (attinfo.AttributeName));
  145. if (attinfo.Namespace != defaultNamespace) att.Arguments.Add (GetArg ("Namespace", attinfo.Namespace));
  146. if (!TypeTranslator.IsDefaultPrimitiveTpeData(attinfo.TypeData)) att.Arguments.Add (GetArg ("DataType",attinfo.TypeData.XmlType));
  147. attributes.Add (att);
  148. }
  149. protected override void GenerateElementInfoMember (CodeAttributeDeclarationCollection attributes, XmlTypeMapMemberElement member, XmlTypeMapElementInfo einfo, TypeData defaultType, string defaultNamespace, bool addAlwaysAttr, bool forceUseMemberName)
  150. {
  151. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapElement");
  152. if (forceUseMemberName || einfo.ElementName != member.Name) att.Arguments.Add (GetArg (einfo.ElementName));
  153. // if (einfo.IsNullable) att.Arguments.Add (GetArg ("IsNullable", true)); MS seems to ignore this
  154. if (!TypeTranslator.IsDefaultPrimitiveTpeData(einfo.TypeData)) att.Arguments.Add (GetArg ("DataType",einfo.TypeData.XmlType));
  155. if (addAlwaysAttr || att.Arguments.Count > 0) attributes.Add (att);
  156. }
  157. protected override void GenerateEnum (XmlTypeMapping map, CodeTypeDeclaration codeEnum)
  158. {
  159. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapType");
  160. if (map.XmlType != map.TypeData.TypeName) att.Arguments.Add (GetArg (map.XmlType));
  161. if (map.XmlTypeNamespace != "") att.Arguments.Add (GetArg ("Namespace", map.XmlTypeNamespace));
  162. AddCustomAttribute (codeEnum, att, false);
  163. }
  164. protected override void GenerateEnumItem (CodeMemberField codeField, EnumMap.EnumMapMember emem)
  165. {
  166. if (emem.EnumName != emem.XmlName)
  167. {
  168. CodeAttributeDeclaration xatt = new CodeAttributeDeclaration ("System.Xml.Serialization.SoapEnum");
  169. xatt.Arguments.Add (GetArg ("Name", emem.XmlName));
  170. AddCustomAttribute (codeField, xatt, true);
  171. }
  172. }
  173. protected override void GenerateSpecifierMember (CodeTypeMember codeField)
  174. {
  175. AddCustomAttribute (codeField, "System.Xml.Serialization.SoapIgnore");
  176. }
  177. }
  178. }