* HtmlFormParameterReader.cs, HtmlFormParameterWriter.cs,
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / HttpSimpleTypeStubInfo.cs
1 //
2 // HttpSimpleMethodStubInfo.cs: Information about a method and its mapping to a SOAP web service.
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc.
8 //
9
10 using System.Reflection;
11 using System.Collections;
12 using System.Xml;
13 using System.Xml.Serialization;
14 using System.Web.Services;
15 using System.Web.Services.Description;
16
17 namespace System.Web.Services.Protocols 
18 {
19         enum FormatterKind
20         {
21                 ParameterWriter,
22                 ParameterReader,
23                 ReturnReader,
24                 ReturnWriter
25         }
26         
27         internal class HttpSimpleMethodStubInfo : MethodStubInfo
28         {
29                 public MimeFormatterInfo ParameterWriterType;
30                 public MimeFormatterInfo ParameterReaderType;
31                 public MimeFormatterInfo ReturnReaderType;
32                 public MimeFormatterInfo ReturnWriterType;
33                 
34                 public MimeFormatterInfo GetFormatterInfo (FormatterKind kind)
35                 {
36                         switch (kind)
37                         {
38                                 case FormatterKind.ParameterWriter: return ParameterWriterType;
39                                 case FormatterKind.ParameterReader: return ParameterReaderType;
40                                 case FormatterKind.ReturnReader: return ReturnReaderType;
41                                 case FormatterKind.ReturnWriter: return ReturnWriterType;
42                         }
43                         return null;
44                 }
45                 
46                 public HttpSimpleMethodStubInfo (TypeStubInfo parent, LogicalMethodInfo source): base (parent, source)
47                 {
48                         object[] atts = source.CustomAttributeProvider.GetCustomAttributes (typeof(HttpMethodAttribute), true);
49                         if (atts.Length > 0)
50                         {
51                                 HttpMethodAttribute at = (HttpMethodAttribute) atts[0];
52                                 ParameterWriterType = new MimeFormatterInfo (at.ParameterFormatter);
53                                 ReturnReaderType = new MimeFormatterInfo (at.ReturnFormatter);
54                         }
55                 }
56         }
57         
58         internal class MimeFormatterInfo
59         {
60                 public MimeFormatterInfo (Type type) {
61                         Type = type;
62                 }
63                 
64                 public MimeFormatter Create () {
65                         return MimeFormatter.CreateInstance (Type, Initializer);
66                 }
67                 
68                 public Type Type;
69                 public object Initializer;
70         }
71
72         internal class HttpSimpleTypeStubInfo : TypeStubInfo
73         {
74                 public HttpSimpleTypeStubInfo (Type t): base (t)
75                 {
76                 }
77
78                 public override void BuildTypeMethods ()
79                 {
80                         base.BuildTypeMethods ();
81                         
82                         BuildInitializers (FormatterKind.ParameterWriter);
83                         BuildInitializers (FormatterKind.ParameterReader);
84                         BuildInitializers (FormatterKind.ReturnReader);
85                         BuildInitializers (FormatterKind.ReturnWriter);
86                 }
87                 
88                 void BuildInitializers (FormatterKind formatter)
89                 {
90                         Hashtable types = new Hashtable ();
91
92                         foreach (HttpSimpleMethodStubInfo met in Methods)
93                                 AddType (types, met.GetFormatterInfo (formatter).Type, met);
94                         
95                         foreach (DictionaryEntry ent in types)
96                         {
97                                 Type t = (Type)ent.Key;
98                                 ArrayList list = (ArrayList)ent.Value;
99                                 LogicalMethodInfo[] mets = new LogicalMethodInfo [list.Count];
100                                 for (int n=0; n<list.Count; n++) 
101                                         mets[n] = ((MethodStubInfo)list[n]).MethodInfo;
102
103                                 object[] inits = MimeFormatter.GetInitializers (t, mets);
104
105                                 for (int n=0; n<list.Count; n++)
106                                         ((HttpSimpleMethodStubInfo)list[n]).GetFormatterInfo (formatter).Initializer = inits[n];
107                         }
108                 }
109
110                 void AddType (Hashtable types, Type type, HttpSimpleMethodStubInfo method)
111                 {
112                         ArrayList list = (ArrayList) types [type];
113                         if (list == null)
114                         {
115                                 list = new ArrayList ();
116                                 types [type] = list;
117                         }
118                         list.Add (method);
119                 }
120                 
121                 protected override MethodStubInfo CreateMethodStubInfo (TypeStubInfo typeInfo, LogicalMethodInfo methodInfo, bool isClientProxy)
122                 {
123                         return new HttpSimpleMethodStubInfo (typeInfo, methodInfo);
124                 }
125         }
126 }