2006-12-12 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Description / ProtocolImporter.cs
index 824d9b2943d2d0a7af79ec428cfd6035d97e6537..c6420a15569c4bfa674bcd1f1980be4fd10932d2 100644 (file)
@@ -185,7 +185,7 @@ namespace System.Web.Services.Description {
                        get { return descriptionImporter.CodeGenerationOptions; }
                }
                
-               internal ICodeGenerator CodeGenerator {
+               internal CodeDomProvider CodeGenerator {
                        get { return descriptionImporter.CodeGenerator; }
                }
 
@@ -237,6 +237,26 @@ namespace System.Web.Services.Description {
                                        }
                                }
                        }
+                       
+                       if (!found)
+                       {
+                               // Looks like MS.NET generates classes for all bindings if
+                               // no services are present
+                               
+                               foreach (ImportInfo info in importInfo)
+                               {
+                                       this.iinfo = info;
+                                       foreach (Binding b in info.ServiceDescription.Bindings)
+                                       {
+                                               this.binding = b;
+                                               this.service = null;
+                                               this.port = null;
+                                               if (!IsBindingSupported ()) continue;
+                                               found = true;
+                                               ImportPortBinding (true);
+                                       }
+                               }
+                       }
 
                        EndNamespace ();
                        
@@ -246,8 +266,12 @@ namespace System.Web.Services.Description {
 
                void ImportPortBinding (bool multipleBindings)
                {
-                       if (multipleBindings) className = port.Name;
-                       else className = service.Name;
+                       if (port != null) {
+                               if (multipleBindings) className = port.Name;
+                               else className = service.Name;
+                       }
+                       else
+                               className = binding.Name;
                        
                        className = classNames.AddUnique (CodeIdentifier.MakeValid (className), port);
                        className = className.Replace ("_x0020_", "");  // MS.NET seems to do this
@@ -259,10 +283,10 @@ namespace System.Web.Services.Description {
 
                                CodeTypeDeclaration codeClass = BeginClass ();
                                codeTypeDeclaration = codeClass;
-                               AddCodeType (codeClass, port.Documentation);
+                               AddCodeType (codeClass, port != null ? port.Documentation : null);
                                codeClass.Attributes = MemberAttributes.Public;
-                               
-                               if (service.Documentation != null && service.Documentation != "")
+                       
+                               if (service != null && service.Documentation != null && service.Documentation != "")
                                        AddComments (codeClass, service.Documentation);
 
                                if (Style == ServiceDescriptionImportStyle.Client) {
@@ -285,7 +309,11 @@ namespace System.Web.Services.Description {
                                {
                                        operationBinding = oper;
                                        operation = FindPortOperation ();
-                                       if (operation == null) throw new Exception ("Operation " + operationBinding.Name + " not found in portType " + PortType.Name);
+                                       if (operation == null)
+                                               throw new Exception ("Operation " + operationBinding.Name + " not found in portType " + PortType.Name);
+
+                                       inputMessage = null;
+                                       outputMessage = null;
 
                                        foreach (OperationMessage omsg in operation.Messages)
                                        {
@@ -366,21 +394,42 @@ namespace System.Web.Services.Description {
                        else return msg.Name;
                }
                
-               internal string GetServiceUrl (string location)
+               internal void GenerateServiceUrl (string location, CodeStatementCollection stms)
                {
-                       if (ImportInfo.AppSettingUrlKey == null || ImportInfo.AppSettingUrlKey == string.Empty)
-                               return location;
+                       if (ImportInfo.AppSettingUrlKey == null || ImportInfo.AppSettingUrlKey == string.Empty) {
+                               if (location != null) {
+                                       CodeExpression ce = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
+                                       CodeAssignStatement cas = new CodeAssignStatement (ce, new CodePrimitiveExpression (location));
+                                       stms.Add (cas);
+                               }
+                       }
                        else
                        {
-                               string url;
-                               if (Style == ServiceDescriptionImportStyle.Server) throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
-                               url = ConfigurationSettings.AppSettings [ImportInfo.AppSettingUrlKey];
-                               if (ImportInfo.AppSettingBaseUrl != null && ImportInfo.AppSettingBaseUrl != string.Empty)
-                                       url += "/" + ImportInfo.AppSettingBaseUrl + "/" + location;
-                               return url;
+                               CodeExpression prop = new CodePropertyReferenceExpression (new CodeTypeReferenceExpression ("System.Configuration.ConfigurationSettings"), "AppSettings");
+                               prop = new CodeIndexerExpression (prop, new CodePrimitiveExpression (ImportInfo.AppSettingUrlKey));
+                               stms.Add (new CodeVariableDeclarationStatement (typeof(string), "urlSetting", prop));
+                               
+                               CodeExpression urlSetting = new CodeVariableReferenceExpression ("urlSetting");
+                               CodeExpression thisUrl = new CodeFieldReferenceExpression (new CodeThisReferenceExpression(), "Url");
+                               
+                               CodeStatement[] trueStms = new CodeStatement [1];
+                               CodeExpression ce = urlSetting;
+                               CodeExpression cond = new CodeBinaryOperatorExpression (urlSetting, CodeBinaryOperatorType.IdentityInequality, new CodePrimitiveExpression (null));
+                               
+                               if (ImportInfo.AppSettingBaseUrl != null)
+                                       ce = new CodeMethodInvokeExpression (new CodeTypeReferenceExpression (typeof(string)), "Concat", ce, new CodePrimitiveExpression (ImportInfo.AppSettingBaseUrl));
+                               trueStms [0] = new CodeAssignStatement (thisUrl, ce);
+                               
+                               if (location != null) {
+                                       CodeStatement[] falseStms = new CodeStatement [1];
+                                       falseStms [0] = new CodeAssignStatement (thisUrl, new CodePrimitiveExpression (location));
+                                       stms.Add (new CodeConditionStatement (cond, trueStms, falseStms));
+                               }
+                               else
+                                       stms.Add (new CodeConditionStatement (cond, trueStms));
                        }
                }
-
+               
                void ClasifySchemas (ArrayList importInfo)
                {
                        // I don't like this, but I could not find any other way of clasifying
@@ -496,7 +545,7 @@ namespace System.Web.Services.Description {
                        foreach (XmlSchemaObject ob in sc.Includes)
                        {
                                XmlSchemaImport import = ob as XmlSchemaImport;
-                               if (import.Namespace == SoapProtocolReflector.EncodingNamespace) return true;
+                               if (import != null && import.Namespace == SoapProtocolReflector.EncodingNamespace) return true;
                        }
                        return false;
                }