[corlib] Update ValueTuple implementation
[mono.git] / mcs / class / System.Runtime.Serialization / Test / System.Runtime.Serialization / WsdlHelper.cs
1 //
2 // WsdlHelper.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2012 Xamarin, Inc.
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26
27 #if !MOBILE
28
29 using System;
30 using System.IO;
31 using System.Linq;
32 using System.Reflection;
33 using System.ServiceModel.Description;
34 using System.Web.Services.Discovery;
35 using System.Runtime.Serialization;
36 using WebServices = System.Web.Services;
37 using System.CodeDom;
38
39 namespace MonoTests.System.Runtime.Serialization
40 {
41         public static class WsdlHelper
42         {
43                 /*
44                  * This reads a normal .wsdl file from an embedded resource.
45                  * 
46                  * You can simply fetch them from your server using
47                  * 'curl http://yourserver/YourService.svc?singleWsdl > YourService.wsdl',
48                  * add the .wsdl file to Test/Resources/WSDL and add it to `TEST_RESOURCE_FILES'
49                  * in the Makefile.
50                  */
51
52                 public static MetadataSet GetMetadataSet (string name)
53                 {
54                         var asm = Assembly.GetExecutingAssembly ();
55                         using (var stream = asm.GetManifestResourceStream (name)) {
56                                 if (stream == null)
57                                         throw new InvalidOperationException (string.Format (
58                                                 "Cannot find resource file '{0}'.", name));
59                                 return GetMetadataSet (stream);
60                         }
61                 }
62                 
63                 public static MetadataSet GetMetadataSet (Stream stream)
64                 {
65                         var dr = new ContractReference ();
66                         var doc = (WebServices.Description.ServiceDescription) dr.ReadDocument (stream);
67                         
68                         var metadata = new MetadataSet ();
69                         metadata.MetadataSections.Add (
70                                 new MetadataSection (MetadataSection.ServiceDescriptionDialect, "", doc));
71                         return metadata;
72                 }
73
74                 public static CodeCompileUnit Import (MetadataSet metadata, ImportOptions options)
75                 {
76                         var importer = new WsdlImporter (metadata);
77                         var xsdImporter = new XsdDataContractImporter ();
78                         xsdImporter.Options = options;
79                         importer.State.Add (typeof(XsdDataContractImporter), xsdImporter);
80                         
81                         var contracts = importer.ImportAllContracts ();
82                         
83                         CodeCompileUnit ccu = new CodeCompileUnit ();
84                         var generator = new ServiceContractGenerator (ccu);
85
86                         if (contracts.Count != 1)
87                                 throw new InvalidOperationException (string.Format (
88                                         "Metadata import failed: found {0} contracts.", contracts.Count));
89                         
90                         var contract = contracts.First ();
91                         generator.GenerateServiceContractType (contract);
92                         
93                         return ccu;
94                 }
95
96                 public static CodeNamespace Find (this CodeNamespaceCollection collection, string name)
97                 {
98                         foreach (CodeNamespace ns in collection) {
99                                 if (ns.Name == name)
100                                         return ns;
101                         }
102                         
103                         return null;
104                 }
105                 
106                 public static CodeNamespace FindNamespace (this CodeCompileUnit unit, string name)
107                 {
108                         foreach (CodeNamespace ns in unit.Namespaces) {
109                                 if (ns.Name == name)
110                                         return ns;
111                         }
112                         
113                         return null;
114                 }
115                 
116                 public static CodeTypeDeclaration FindType (this CodeNamespace ns, string name)
117                 {
118                         foreach (CodeTypeDeclaration type in ns.Types) {
119                                 if (type.Name == name)
120                                         return type;
121                         }
122                         
123                         return null;
124                 }
125                 
126                 public static CodeTypeDeclaration FindType (this CodeCompileUnit unit, string name)
127                 {
128                         foreach (CodeNamespace ns in unit.Namespaces) {
129                                 foreach (CodeTypeDeclaration type in ns.Types) {
130                                         if (type.Name == name)
131                                                 return type;
132                                 }
133                         }
134                         
135                         return null;
136                 }
137                 
138                 public static CodeMemberMethod FindMethod (this CodeTypeDeclaration type, string name)
139                 {
140                         foreach (var member in type.Members) {
141                                 var method = member as CodeMemberMethod;
142                                 if (method == null)
143                                         continue;
144                                 if (method.Name == name)
145                                         return method;
146                         }
147                         
148                         return null;
149                 }
150                 
151                 public static CodeMemberMethod FindMethod (this CodeCompileUnit unit, string typeName,
152                                                            string methodName)
153                 {
154                         var type = unit.FindType (typeName);
155                         if (type == null)
156                                 return null;
157                         return type.FindMethod (methodName);
158                 }
159
160                 public static CodeAttributeDeclaration FindAttribute (this CodeTypeDeclaration type, string name)
161                 {
162                         foreach (CodeAttributeDeclaration attr in type.CustomAttributes) {
163                                 if (attr.Name == name)
164                                         return attr;
165                         }
166
167                         return null;
168                 }
169
170                 public static CodeAttributeArgument FindArgument (this CodeAttributeDeclaration attr, string name)
171                 {
172                         foreach (CodeAttributeArgument arg in attr.Arguments) {
173                                 if (arg.Name == name)
174                                         return arg;
175                         }
176
177                         return null;
178                 }
179         }
180 }
181
182 #endif
183