ProtocolImporter.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. //
  2. // System.Web.Services.Description.ProtocolImporter.cs
  3. //
  4. // Author:
  5. // Tim Coleman ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // Copyright (C) Tim Coleman, 2002
  9. //
  10. using System;
  11. using System.CodeDom;
  12. using System.Web.Services;
  13. using System.Web.Services.Protocols;
  14. using System.Xml.Serialization;
  15. using System.Xml;
  16. using System.Collections;
  17. using System.Configuration;
  18. namespace System.Web.Services.Description {
  19. public abstract class ProtocolImporter {
  20. #region Fields
  21. Binding binding;
  22. string className;
  23. CodeIdentifiers classNames;
  24. CodeNamespace codeNamespace;
  25. CodeCompileUnit codeCompileUnit;
  26. CodeTypeDeclaration codeTypeDeclaration;
  27. Message inputMessage;
  28. string methodName;
  29. Operation operation;
  30. OperationBinding operationBinding;
  31. Message outputMessage;
  32. Port port;
  33. PortType portType;
  34. string protocolName;
  35. Service service;
  36. ServiceDescriptionImportWarnings warnings = (ServiceDescriptionImportWarnings)0;
  37. ServiceDescriptionImporter descriptionImporter;
  38. ImportInfo iinfo;
  39. #endregion // Fields
  40. #region Constructors
  41. protected ProtocolImporter ()
  42. {
  43. }
  44. #endregion // Constructors
  45. #region Properties
  46. [MonoTODO]
  47. public XmlSchemas AbstractSchemas {
  48. get { return descriptionImporter.Schemas; }
  49. }
  50. public Binding Binding {
  51. get { return binding; }
  52. }
  53. public string ClassName {
  54. get { return className; }
  55. }
  56. public CodeIdentifiers ClassNames {
  57. get { return classNames; }
  58. }
  59. public CodeNamespace CodeNamespace {
  60. get { return codeNamespace; }
  61. }
  62. public CodeTypeDeclaration CodeTypeDeclaration {
  63. get { return codeTypeDeclaration; }
  64. }
  65. [MonoTODO]
  66. public XmlSchemas ConcreteSchemas {
  67. get { return descriptionImporter.Schemas; }
  68. }
  69. public Message InputMessage {
  70. get { return inputMessage; }
  71. }
  72. public string MethodName {
  73. get { return methodName; }
  74. }
  75. public Operation Operation {
  76. get { return operation; }
  77. }
  78. public OperationBinding OperationBinding {
  79. get { return operationBinding; }
  80. }
  81. public Message OutputMessage {
  82. get { return outputMessage; }
  83. }
  84. public Port Port {
  85. get { return port; }
  86. }
  87. public PortType PortType {
  88. get { return portType; }
  89. }
  90. public abstract string ProtocolName {
  91. get;
  92. }
  93. public XmlSchemas Schemas {
  94. get { return descriptionImporter.Schemas; }
  95. }
  96. public Service Service {
  97. get { return service; }
  98. }
  99. public ServiceDescriptionCollection ServiceDescriptions {
  100. get { return descriptionImporter.ServiceDescriptions; }
  101. }
  102. public ServiceDescriptionImportStyle Style {
  103. get { return descriptionImporter.Style; }
  104. }
  105. public ServiceDescriptionImportWarnings Warnings {
  106. get { return warnings; }
  107. set { warnings = value; }
  108. }
  109. internal ImportInfo ImportInfo
  110. {
  111. get { return iinfo; }
  112. }
  113. #endregion // Properties
  114. #region Methods
  115. internal bool Import (ServiceDescriptionImporter descriptionImporter, CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, ArrayList importInfo)
  116. {
  117. this.descriptionImporter = descriptionImporter;
  118. this.classNames = new CodeIdentifiers();;
  119. this.codeNamespace = codeNamespace;
  120. this.codeCompileUnit = codeCompileUnit;
  121. warnings = (ServiceDescriptionImportWarnings) 0;
  122. bool found = false;
  123. BeginNamespace ();
  124. foreach (ImportInfo info in importInfo)
  125. {
  126. foreach (Service service in info.ServiceDescription.Services)
  127. {
  128. this.service = service;
  129. foreach (Port port in service.Ports)
  130. {
  131. this.iinfo = info;
  132. this.port = port;
  133. binding = ServiceDescriptions.GetBinding (port.Binding);
  134. if (!IsBindingSupported ()) continue;
  135. found = true;
  136. ImportPortBinding ();
  137. }
  138. }
  139. }
  140. EndNamespace ();
  141. return true;
  142. }
  143. void ImportPortBinding ()
  144. {
  145. if (service.Ports.Count > 1) className = port.Name;
  146. else className = service.Name;
  147. className = classNames.AddUnique (CodeIdentifier.MakeValid (className), port);
  148. try
  149. {
  150. portType = ServiceDescriptions.GetPortType (binding.Type);
  151. if (portType == null) throw new Exception ("Port type not found: " + binding.Type);
  152. CodeTypeDeclaration codeClass = BeginClass ();
  153. codeTypeDeclaration = codeClass;
  154. AddCodeType (codeClass, port.Documentation);
  155. codeClass.Attributes = MemberAttributes.Public;
  156. if (service.Documentation != null && service.Documentation != "")
  157. AddComments (codeClass, service.Documentation);
  158. CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Diagnostics.DebuggerStepThroughAttribute");
  159. AddCustomAttribute (codeClass, att, true);
  160. att = new CodeAttributeDeclaration ("System.ComponentModel.DesignerCategoryAttribute");
  161. att.Arguments.Add (GetArg ("code"));
  162. AddCustomAttribute (codeClass, att, true);
  163. if (binding.Operations.Count == 0) {
  164. warnings |= ServiceDescriptionImportWarnings.NoMethodsGenerated;
  165. return;
  166. }
  167. foreach (OperationBinding oper in binding.Operations)
  168. {
  169. operationBinding = oper;
  170. operation = FindPortOperation ();
  171. if (operation == null) throw new Exception ("Operation " + operationBinding.Name + " not found in portType " + PortType.Name);
  172. foreach (OperationMessage omsg in operation.Messages)
  173. {
  174. Message msg = ServiceDescriptions.GetMessage (omsg.Message);
  175. if (msg == null) throw new Exception ("Message not found: " + omsg.Message);
  176. if (omsg is OperationInput)
  177. inputMessage = msg;
  178. else
  179. outputMessage = msg;
  180. }
  181. CodeMemberMethod method = GenerateMethod ();
  182. if (method != null)
  183. {
  184. methodName = method.Name;
  185. if (operation.Documentation != null && operation.Documentation != "")
  186. AddComments (method, operation.Documentation);
  187. }
  188. }
  189. EndClass ();
  190. }
  191. catch (Exception ex)
  192. {
  193. warnings |= ServiceDescriptionImportWarnings.NoCodeGenerated;
  194. UnsupportedBindingWarning (ex.Message);
  195. }
  196. }
  197. Operation FindPortOperation ()
  198. {
  199. string inMessage = (operationBinding.Input.Name != "") ? operationBinding.Input.Name : operationBinding.Name;
  200. string outMessage = (operationBinding.Output.Name != "") ? operationBinding.Output.Name : operationBinding.Name;
  201. string operName = operationBinding.Name;
  202. foreach (Operation oper in PortType.Operations)
  203. {
  204. if (oper.Name == operName)
  205. {
  206. int hits = 0;
  207. foreach (OperationMessage omsg in oper.Messages)
  208. {
  209. if (omsg is OperationInput && GetOperMessageName (omsg, operName) == inMessage) hits++;
  210. if (omsg is OperationOutput && GetOperMessageName (omsg, operName) == outMessage) hits++;
  211. }
  212. if (hits == 2) return oper;
  213. }
  214. }
  215. return null;
  216. }
  217. string GetOperMessageName (OperationMessage msg, string operName)
  218. {
  219. if (msg.Name == null) return operName;
  220. else return msg.Name;
  221. }
  222. internal string GetServiceUrl (string location)
  223. {
  224. if (ImportInfo.AppSettingUrlKey == null || ImportInfo.AppSettingUrlKey == string.Empty)
  225. return location;
  226. else
  227. {
  228. string url;
  229. if (Style == ServiceDescriptionImportStyle.Server) throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
  230. url = ConfigurationSettings.AppSettings [ImportInfo.AppSettingUrlKey];
  231. if (ImportInfo.AppSettingBaseUrl != null && ImportInfo.AppSettingBaseUrl != string.Empty)
  232. url += "/" + ImportInfo.AppSettingBaseUrl + "/" + location;
  233. return url;
  234. }
  235. }
  236. [MonoTODO]
  237. public void AddExtensionWarningComments (CodeCommentStatementCollection comments, ServiceDescriptionFormatExtensionCollection extensions)
  238. {
  239. throw new NotImplementedException ();
  240. }
  241. protected abstract CodeTypeDeclaration BeginClass ();
  242. protected virtual void BeginNamespace ()
  243. {
  244. }
  245. protected virtual void EndClass ()
  246. {
  247. }
  248. protected virtual void EndNamespace ()
  249. {
  250. }
  251. protected abstract CodeMemberMethod GenerateMethod ();
  252. protected abstract bool IsBindingSupported ();
  253. protected abstract bool IsOperationFlowSupported (OperationFlow flow);
  254. [MonoTODO]
  255. public Exception OperationBindingSyntaxException (string text)
  256. {
  257. throw new NotImplementedException ();
  258. }
  259. [MonoTODO]
  260. public Exception OperationSyntaxException (string text)
  261. {
  262. throw new NotImplementedException ();
  263. }
  264. public void UnsupportedBindingWarning (string text)
  265. {
  266. AddGlobalComments ("WARNING: Could not generate proxy for binding " + binding.Name + ". " + text);
  267. }
  268. public void UnsupportedOperationBindingWarning (string text)
  269. {
  270. AddGlobalComments ("WARNING: Could not generate operation " + OperationBinding.Name + ". " + text);
  271. }
  272. public void UnsupportedOperationWarning (string text)
  273. {
  274. AddGlobalComments ("WARNING: Could not generate operation " + OperationBinding.Name + ". " + text);
  275. }
  276. void AddGlobalComments (string comments)
  277. {
  278. codeNamespace.Comments.Add (new CodeCommentStatement (comments, false));
  279. }
  280. void AddComments (CodeTypeMember member, string comments)
  281. {
  282. if (comments == null || comments == "") member.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
  283. else member.Comments.Add (new CodeCommentStatement ("<remarks>\n" + comments + "\n</remarks>", true));
  284. }
  285. void AddCodeType (CodeTypeDeclaration type, string comments)
  286. {
  287. AddComments (type, comments);
  288. codeNamespace.Types.Add (type);
  289. }
  290. internal void AddCustomAttribute (CodeTypeMember ctm, CodeAttributeDeclaration att, bool addIfNoParams)
  291. {
  292. if (att.Arguments.Count == 0 && !addIfNoParams) return;
  293. if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
  294. ctm.CustomAttributes.Add (att);
  295. }
  296. internal void AddCustomAttribute (CodeTypeMember ctm, string name, params CodeAttributeArgument[] args)
  297. {
  298. if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
  299. ctm.CustomAttributes.Add (new CodeAttributeDeclaration (name, args));
  300. }
  301. internal CodeAttributeArgument GetArg (string name, object value)
  302. {
  303. return new CodeAttributeArgument (name, new CodePrimitiveExpression(value));
  304. }
  305. internal CodeAttributeArgument GetEnumArg (string name, string enumType, string enumValue)
  306. {
  307. return new CodeAttributeArgument (name, new CodeFieldReferenceExpression (new CodeTypeReferenceExpression(enumType), enumValue));
  308. }
  309. internal CodeAttributeArgument GetArg (object value)
  310. {
  311. return new CodeAttributeArgument (new CodePrimitiveExpression(value));
  312. }
  313. internal CodeAttributeArgument GetTypeArg (string name, string typeName)
  314. {
  315. return new CodeAttributeArgument (name, new CodeTypeOfExpression(typeName));
  316. }
  317. #endregion
  318. }
  319. }