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