2 // System.Web.Services.Description.ProtocolImporter.cs
5 // Tim Coleman (tim@timcoleman.com)
6 // Lluis Sanchez Gual (lluis@ximian.com)
8 // Copyright (C) Tim Coleman, 2002
13 using System.Web.Services;
14 using System.Web.Services.Protocols;
15 using System.Xml.Serialization;
17 using System.Xml.Schema;
18 using System.Collections;
19 using System.Configuration;
21 namespace System.Web.Services.Description {
22 public abstract class ProtocolImporter {
28 CodeIdentifiers classNames;
29 CodeNamespace codeNamespace;
30 CodeCompileUnit codeCompileUnit;
31 CodeTypeDeclaration codeTypeDeclaration;
35 OperationBinding operationBinding;
36 Message outputMessage;
41 ServiceDescriptionImportWarnings warnings = (ServiceDescriptionImportWarnings)0;
42 ServiceDescriptionImporter descriptionImporter;
44 XmlSchemas xmlSchemas;
45 XmlSchemas soapSchemas;
51 protected ProtocolImporter ()
55 #endregion // Constructors
60 public XmlSchemas AbstractSchemas {
61 get { return descriptionImporter.Schemas; }
64 public Binding Binding {
65 get { return binding; }
68 public string ClassName {
69 get { return className; }
72 public CodeIdentifiers ClassNames {
73 get { return classNames; }
76 public CodeNamespace CodeNamespace {
77 get { return codeNamespace; }
80 public CodeTypeDeclaration CodeTypeDeclaration {
81 get { return codeTypeDeclaration; }
85 public XmlSchemas ConcreteSchemas {
86 get { return descriptionImporter.Schemas; }
89 public Message InputMessage {
90 get { return inputMessage; }
93 public string MethodName {
94 get { return methodName; }
97 public Operation Operation {
98 get { return operation; }
101 public OperationBinding OperationBinding {
102 get { return operationBinding; }
105 public Message OutputMessage {
106 get { return outputMessage; }
113 public PortType PortType {
114 get { return portType; }
117 public abstract string ProtocolName {
121 public XmlSchemas Schemas {
122 get { return descriptionImporter.Schemas; }
125 public Service Service {
126 get { return service; }
129 public ServiceDescriptionCollection ServiceDescriptions {
130 get { return descriptionImporter.ServiceDescriptions; }
133 public ServiceDescriptionImportStyle Style {
134 get { return descriptionImporter.Style; }
137 public ServiceDescriptionImportWarnings Warnings {
138 get { return warnings; }
139 set { warnings = value; }
142 internal ImportInfo ImportInfo
144 get { return iinfo; }
147 internal XmlSchemas LiteralSchemas
149 get { return xmlSchemas; }
152 internal XmlSchemas EncodedSchemas
154 get { return soapSchemas; }
157 #endregion // Properties
161 internal bool Import (ServiceDescriptionImporter descriptionImporter, CodeNamespace codeNamespace, CodeCompileUnit codeCompileUnit, ArrayList importInfo)
163 this.descriptionImporter = descriptionImporter;
164 this.classNames = new CodeIdentifiers();;
165 this.codeNamespace = codeNamespace;
166 this.codeCompileUnit = codeCompileUnit;
168 warnings = (ServiceDescriptionImportWarnings) 0;
172 ClasifySchemas (importInfo);
176 foreach (ImportInfo info in importInfo)
178 foreach (Service service in info.ServiceDescription.Services)
180 this.service = service;
181 int bindingCount = 0;
182 foreach (Port port in service.Ports)
184 binding = ServiceDescriptions.GetBinding (port.Binding);
185 if (IsBindingSupported ()) bindingCount ++;
188 foreach (Port port in service.Ports)
192 binding = ServiceDescriptions.GetBinding (port.Binding);
193 if (!IsBindingSupported ()) continue;
196 ImportPortBinding (bindingCount > 1);
206 void ImportPortBinding (bool multipleBindings)
208 if (multipleBindings) className = port.Name;
209 else className = service.Name;
211 className = classNames.AddUnique (CodeIdentifier.MakeValid (className), port);
215 portType = ServiceDescriptions.GetPortType (binding.Type);
216 if (portType == null) throw new Exception ("Port type not found: " + binding.Type);
218 CodeTypeDeclaration codeClass = BeginClass ();
219 codeTypeDeclaration = codeClass;
220 AddCodeType (codeClass, port.Documentation);
221 codeClass.Attributes = MemberAttributes.Public;
223 if (service.Documentation != null && service.Documentation != "")
224 AddComments (codeClass, service.Documentation);
226 CodeAttributeDeclaration att = new CodeAttributeDeclaration ("System.Diagnostics.DebuggerStepThroughAttribute");
227 AddCustomAttribute (codeClass, att, true);
229 att = new CodeAttributeDeclaration ("System.ComponentModel.DesignerCategoryAttribute");
230 att.Arguments.Add (GetArg ("code"));
231 AddCustomAttribute (codeClass, att, true);
233 if (binding.Operations.Count == 0) {
234 warnings |= ServiceDescriptionImportWarnings.NoMethodsGenerated;
238 foreach (OperationBinding oper in binding.Operations)
240 operationBinding = oper;
241 operation = FindPortOperation ();
242 if (operation == null) throw new Exception ("Operation " + operationBinding.Name + " not found in portType " + PortType.Name);
244 foreach (OperationMessage omsg in operation.Messages)
246 Message msg = ServiceDescriptions.GetMessage (omsg.Message);
247 if (msg == null) throw new Exception ("Message not found: " + omsg.Message);
249 if (omsg is OperationInput)
255 CodeMemberMethod method = GenerateMethod ();
259 methodName = method.Name;
260 if (operation.Documentation != null && operation.Documentation != "")
261 AddComments (method, operation.Documentation);
267 catch (InvalidOperationException ex)
269 warnings |= ServiceDescriptionImportWarnings.NoCodeGenerated;
270 UnsupportedBindingWarning (ex.Message);
274 Operation FindPortOperation ()
276 string inMessage = null;
277 string outMessage = null;
280 if (operationBinding.Input == null) throw new InvalidOperationException ("Input operation binding not found");
281 inMessage = (operationBinding.Input.Name != null) ? operationBinding.Input.Name : operationBinding.Name;
283 if (operationBinding.Output != null) {
284 outMessage = (operationBinding.Output.Name != null) ? operationBinding.Output.Name : operationBinding.Name;
288 string operName = operationBinding.Name;
290 Operation foundOper = null;
291 foreach (Operation oper in PortType.Operations)
293 if (oper.Name == operName)
296 foreach (OperationMessage omsg in oper.Messages)
298 if (omsg is OperationInput && GetOperMessageName (omsg, operName) == inMessage) hits++;
299 if (omsg is OperationOutput && GetOperMessageName (omsg, operName) == outMessage) hits++;
301 if (hits == numMsg) return oper;
308 string GetOperMessageName (OperationMessage msg, string operName)
310 if (msg.Name == null) return operName;
311 else return msg.Name;
314 internal string GetServiceUrl (string location)
316 if (ImportInfo.AppSettingUrlKey == null || ImportInfo.AppSettingUrlKey == string.Empty)
321 if (Style == ServiceDescriptionImportStyle.Server) throw new InvalidOperationException ("Cannot set appSettingUrlKey if Style is Server");
322 url = ConfigurationSettings.AppSettings [ImportInfo.AppSettingUrlKey];
323 if (ImportInfo.AppSettingBaseUrl != null && ImportInfo.AppSettingBaseUrl != string.Empty)
324 url += "/" + ImportInfo.AppSettingBaseUrl + "/" + location;
329 void ClasifySchemas (ArrayList importInfo)
331 // I don't like this, but I could not find any other way of clasifying
332 // schemas between encoded and literal.
334 xmlSchemas = new XmlSchemas ();
335 soapSchemas = new XmlSchemas ();
337 foreach (ImportInfo info in importInfo)
339 foreach (Service service in info.ServiceDescription.Services)
341 foreach (Port port in service.Ports)
345 binding = ServiceDescriptions.GetBinding (port.Binding);
346 if (binding == null) continue;
347 portType = ServiceDescriptions.GetPortType (binding.Type);
348 if (portType == null) continue;
350 foreach (OperationBinding oper in binding.Operations)
352 operationBinding = oper;
353 operation = FindPortOperation ();
354 if (operation == null) continue;
356 foreach (OperationMessage omsg in operation.Messages)
358 Message msg = ServiceDescriptions.GetMessage (omsg.Message);
359 if (msg == null) continue;
361 if (omsg is OperationInput)
367 if (GetMessageEncoding (oper.Input) == SoapBindingUse.Encoded)
368 AddMessageSchema (soapSchemas, oper.Input, inputMessage);
370 AddMessageSchema (xmlSchemas, oper.Input, inputMessage);
372 if (oper.Output != null) {
373 if (GetMessageEncoding (oper.Output) == SoapBindingUse.Encoded)
374 AddMessageSchema (soapSchemas, oper.Output, outputMessage);
376 AddMessageSchema (xmlSchemas, oper.Output, outputMessage);
383 XmlSchemas defaultList = xmlSchemas;
385 if (xmlSchemas.Count == 0 && soapSchemas.Count > 0)
386 defaultList = soapSchemas;
388 // Schemas not referenced by any message
389 foreach (XmlSchema sc in Schemas)
391 if (ImportsEncodedNamespace (sc))
392 soapSchemas.Add (sc);
393 else if (!soapSchemas.Contains (sc) && !xmlSchemas.Contains (sc))
394 defaultList.Add (sc);
398 void AddMessageSchema (XmlSchemas schemas, MessageBinding mb, Message msg)
400 foreach (MessagePart part in msg.Parts)
402 if (part.Element != XmlQualifiedName.Empty)
403 AddIncludingSchema (schemas, part.Element.Namespace);
404 else if (part.Type != XmlQualifiedName.Empty)
405 AddIncludingSchema (schemas, part.Type.Namespace);
407 SoapBodyBinding sbb = mb.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
408 if (sbb != null) AddIncludingSchema (schemas, sbb.Namespace);
411 void AddIncludingSchema (XmlSchemas list, string ns)
413 XmlSchema sc = Schemas [ns];
414 if (sc == null || list.Contains (sc)) return;
416 foreach (XmlSchemaObject ob in sc.Includes)
418 XmlSchemaImport import = ob as XmlSchemaImport;
419 if (import != null) AddIncludingSchema (list, import.Namespace);
423 SoapBindingUse GetMessageEncoding (MessageBinding mb)
425 SoapBodyBinding sbb = mb.Extensions.Find (typeof(SoapBodyBinding)) as SoapBodyBinding;
428 if (mb is InputBinding) return SoapBindingUse.Encoded;
429 else return SoapBindingUse.Literal;
432 if (sbb.Use == SoapBindingUse.Encoded) return SoapBindingUse.Encoded;
434 return SoapBindingUse.Literal;
437 bool ImportsEncodedNamespace (XmlSchema sc)
439 foreach (XmlSchemaObject ob in sc.Includes)
441 XmlSchemaImport import = ob as XmlSchemaImport;
442 if (import.Namespace == SoapProtocolReflector.EncodingNamespace) return true;
448 public void AddExtensionWarningComments (CodeCommentStatementCollection comments, ServiceDescriptionFormatExtensionCollection extensions)
450 throw new NotImplementedException ();
453 protected abstract CodeTypeDeclaration BeginClass ();
455 protected virtual void BeginNamespace ()
459 protected virtual void EndClass ()
463 protected virtual void EndNamespace ()
467 protected abstract CodeMemberMethod GenerateMethod ();
468 protected abstract bool IsBindingSupported ();
469 protected abstract bool IsOperationFlowSupported (OperationFlow flow);
472 public Exception OperationBindingSyntaxException (string text)
474 throw new NotImplementedException ();
478 public Exception OperationSyntaxException (string text)
480 throw new NotImplementedException ();
483 public void UnsupportedBindingWarning (string text)
485 warnings |= ServiceDescriptionImportWarnings.UnsupportedBindingsIgnored;
486 AddGlobalComments ("WARNING: Could not generate proxy for binding " + binding.Name + ". " + text);
489 public void UnsupportedOperationBindingWarning (string text)
491 warnings |= ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored;
492 AddGlobalComments ("WARNING: Could not generate operation " + OperationBinding.Name + ". " + text);
495 public void UnsupportedOperationWarning (string text)
497 warnings |= ServiceDescriptionImportWarnings.UnsupportedOperationsIgnored;
498 AddGlobalComments ("WARNING: Could not generate operation " + OperationBinding.Name + ". " + text);
501 void AddGlobalComments (string comments)
503 codeNamespace.Comments.Add (new CodeCommentStatement (comments, false));
506 void AddComments (CodeTypeMember member, string comments)
508 if (comments == null || comments == "") member.Comments.Add (new CodeCommentStatement ("<remarks/>", true));
509 else member.Comments.Add (new CodeCommentStatement ("<remarks>\n" + comments + "\n</remarks>", true));
512 void AddCodeType (CodeTypeDeclaration type, string comments)
514 AddComments (type, comments);
515 codeNamespace.Types.Add (type);
518 internal void AddCustomAttribute (CodeTypeMember ctm, CodeAttributeDeclaration att, bool addIfNoParams)
520 if (att.Arguments.Count == 0 && !addIfNoParams) return;
522 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
523 ctm.CustomAttributes.Add (att);
526 internal void AddCustomAttribute (CodeTypeMember ctm, string name, params CodeAttributeArgument[] args)
528 if (ctm.CustomAttributes == null) ctm.CustomAttributes = new CodeAttributeDeclarationCollection ();
529 ctm.CustomAttributes.Add (new CodeAttributeDeclaration (name, args));
532 internal CodeAttributeArgument GetArg (string name, object value)
534 return new CodeAttributeArgument (name, new CodePrimitiveExpression(value));
537 internal CodeAttributeArgument GetEnumArg (string name, string enumType, string enumValue)
539 return new CodeAttributeArgument (name, new CodeFieldReferenceExpression (new CodeTypeReferenceExpression(enumType), enumValue));
542 internal CodeAttributeArgument GetArg (object value)
544 return new CodeAttributeArgument (new CodePrimitiveExpression(value));
547 internal CodeAttributeArgument GetTypeArg (string name, string typeName)
549 return new CodeAttributeArgument (name, new CodeTypeOfExpression(typeName));