2
0

SoapCodeExporter.cs 7.9 KB

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