MetaData.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // System.Runtime.Remoting.MetadataServices.MetaData
  3. //
  4. // Authors:
  5. // Martin Willemoes Hansen ([email protected])
  6. // Lluis Sanchez Gual ([email protected])
  7. //
  8. // (C) 2003 Martin Willemoes Hansen
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Collections;
  31. using System.IO;
  32. using System.Text;
  33. using System.Xml;
  34. using System.Reflection;
  35. using System.Net;
  36. using System.CodeDom.Compiler;
  37. using Microsoft.CSharp;
  38. namespace System.Runtime.Remoting.MetadataServices
  39. {
  40. public class MetaData
  41. {
  42. internal const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
  43. internal const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
  44. internal const string SchemaNamespace = "http://www.w3.org/2001/XMLSchema";
  45. internal const string SchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  46. internal const string SudsNamespace = "http://www.w3.org/2000/wsdl/suds";
  47. internal const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  48. internal const string SoapNamespace = "http://schemas.xmlsoap.org/wsdl/soap/";
  49. public MetaData()
  50. {
  51. }
  52. [MonoTODO ("strong name")]
  53. public static void ConvertCodeSourceFileToAssemblyFile (
  54. string codePath,
  55. string assemblyPath,
  56. string strongNameFilename)
  57. {
  58. CSharpCodeProvider prov = new CSharpCodeProvider ();
  59. ICodeCompiler comp = prov.CreateCompiler ();
  60. CompilerParameters pars = new CompilerParameters ();
  61. pars.OutputAssembly = assemblyPath;
  62. CompilerResults cr = comp.CompileAssemblyFromFile(pars, codePath);
  63. CheckResult (cr);
  64. }
  65. [MonoTODO ("strong name")]
  66. public static void ConvertCodeSourceStreamToAssemblyFile (
  67. ArrayList outCodeStreamList,
  68. string assemblyPath,
  69. string strongNameFilename)
  70. {
  71. CSharpCodeProvider prov = new CSharpCodeProvider ();
  72. ICodeCompiler comp = prov.CreateCompiler ();
  73. CompilerParameters pars = new CompilerParameters ();
  74. pars.OutputAssembly = assemblyPath;
  75. CompilerResults cr = comp.CompileAssemblyFromFileBatch (pars, (string[]) outCodeStreamList.ToArray(typeof(string)));
  76. CheckResult (cr);
  77. }
  78. static void CheckResult (CompilerResults cr)
  79. {
  80. if (cr.Errors.Count > 0)
  81. {
  82. foreach (string s in cr.Output)
  83. Console.WriteLine (s);
  84. string errs = "";
  85. foreach (CompilerError error in cr.Errors)
  86. if (error.FileName != "")
  87. errs += error.ToString () + "\n";
  88. throw new Exception ("There where errors during compilation of the assembly:\n" + errs);
  89. }
  90. }
  91. public static void ConvertSchemaStreamToCodeSourceStream (
  92. bool clientProxy,
  93. string outputDirectory,
  94. Stream inputStream,
  95. ArrayList outCodeStreamList)
  96. {
  97. ConvertSchemaStreamToCodeSourceStream (clientProxy, outputDirectory, inputStream, outCodeStreamList, null, null);
  98. }
  99. public static void ConvertSchemaStreamToCodeSourceStream (
  100. bool clientProxy,
  101. string outputDirectory,
  102. Stream inputStream,
  103. ArrayList outCodeStreamList,
  104. string proxyUrl)
  105. {
  106. ConvertSchemaStreamToCodeSourceStream (clientProxy, outputDirectory, inputStream, outCodeStreamList, proxyUrl, null);
  107. }
  108. public static void ConvertSchemaStreamToCodeSourceStream (
  109. bool clientProxy,
  110. string outputDirectory,
  111. Stream inputStream,
  112. ArrayList outCodeStreamList,
  113. string proxyUrl,
  114. string proxyNamespace)
  115. {
  116. MetaDataCodeGenerator cg = new MetaDataCodeGenerator ();
  117. MemoryStream memStream = new MemoryStream ();
  118. CopyStream (inputStream, memStream);
  119. memStream.Position = 0;
  120. cg.GenerateCode (clientProxy, outputDirectory, memStream, outCodeStreamList, proxyUrl, proxyNamespace);
  121. }
  122. public static void ConvertTypesToSchemaToFile (ServiceType[] servicetypes, SdlType sdltype, string path)
  123. {
  124. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  125. ConvertTypesToSchemaToStream (servicetypes, sdltype, fs);
  126. fs.Close ();
  127. }
  128. public static void ConvertTypesToSchemaToFile (Type[] types, SdlType sdltype, string path)
  129. {
  130. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  131. ConvertTypesToSchemaToStream (types, sdltype, fs);
  132. fs.Close ();
  133. }
  134. public static void ConvertTypesToSchemaToStream (Type[] types, SdlType sdltype, Stream stream)
  135. {
  136. ServiceType[] st = new ServiceType [types.Length];
  137. for (int n=0; n<types.Length; n++)
  138. st [n] = new ServiceType (types[n]);
  139. ConvertTypesToSchemaToStream (st, sdltype, stream);
  140. }
  141. public static void ConvertTypesToSchemaToStream (ServiceType[] servicetypes, SdlType sdltype, Stream stream)
  142. {
  143. MetaDataExporter exporter = new MetaDataExporter ();
  144. MemoryStream memStream = new MemoryStream ();
  145. StreamWriter sw = new StreamWriter (memStream);
  146. XmlTextWriter tw = new XmlTextWriter (sw);
  147. exporter.ExportTypes (servicetypes, sdltype, tw);
  148. tw.Flush ();
  149. memStream.Position = 0;
  150. CopyStream (memStream, stream);
  151. }
  152. public static void RetrieveSchemaFromUrlToFile (string url, string path)
  153. {
  154. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  155. RetrieveSchemaFromUrlToStream (url, fs);
  156. fs.Close ();
  157. }
  158. public static void RetrieveSchemaFromUrlToStream (string url, Stream outputStream)
  159. {
  160. WebRequest req = WebRequest.Create (url);
  161. Stream st = req.GetResponse().GetResponseStream();
  162. CopyStream (st, outputStream);
  163. st.Close ();
  164. }
  165. public static void SaveStreamToFile (Stream inputStream, string path)
  166. {
  167. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  168. CopyStream (inputStream, fs);
  169. fs.Close ();
  170. }
  171. static void CopyStream (Stream inputStream, Stream outputStream)
  172. {
  173. byte[] buffer = new byte [1024*5];
  174. int nr = 0;
  175. while ((nr = inputStream.Read (buffer, 0, buffer.Length)) > 0)
  176. outputStream.Write (buffer, 0, nr);
  177. }
  178. }
  179. }