2
0

MetaData.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. using System.Collections;
  11. using System.IO;
  12. using System.Text;
  13. using System.Xml;
  14. using System.Reflection;
  15. using System.Net;
  16. using System.CodeDom.Compiler;
  17. using Microsoft.CSharp;
  18. namespace System.Runtime.Remoting.MetadataServices
  19. {
  20. public class MetaData
  21. {
  22. internal const string WsdlNamespace = "http://schemas.xmlsoap.org/wsdl/";
  23. internal const string XmlnsNamespace = "http://www.w3.org/2000/xmlns/";
  24. internal const string SchemaNamespace = "http://www.w3.org/2001/XMLSchema";
  25. internal const string SchemaInstanceNamespace = "http://www.w3.org/2001/XMLSchema-instance";
  26. internal const string SudsNamespace = "http://www.w3.org/2000/wsdl/suds";
  27. internal const string SoapEncodingNamespace = "http://schemas.xmlsoap.org/soap/encoding/";
  28. internal const string SoapNamespace = "http://schemas.xmlsoap.org/wsdl/soap/";
  29. public MetaData()
  30. {
  31. }
  32. [MonoTODO ("strong name")]
  33. public static void ConvertCodeSourceFileToAssemblyFile (
  34. string codePath,
  35. string assemblyPath,
  36. string strongNameFilename)
  37. {
  38. CSharpCodeProvider prov = new CSharpCodeProvider ();
  39. ICodeCompiler comp = prov.CreateCompiler ();
  40. CompilerParameters pars = new CompilerParameters ();
  41. pars.OutputAssembly = assemblyPath;
  42. CompilerResults cr = comp.CompileAssemblyFromFile(pars, codePath);
  43. CheckResult (cr);
  44. }
  45. [MonoTODO ("strong name")]
  46. public static void ConvertCodeSourceStreamToAssemblyFile (
  47. ArrayList outCodeStreamList,
  48. string assemblyPath,
  49. string strongNameFilename)
  50. {
  51. CSharpCodeProvider prov = new CSharpCodeProvider ();
  52. ICodeCompiler comp = prov.CreateCompiler ();
  53. CompilerParameters pars = new CompilerParameters ();
  54. pars.OutputAssembly = assemblyPath;
  55. CompilerResults cr = comp.CompileAssemblyFromFileBatch (pars, (string[]) outCodeStreamList.ToArray(typeof(string)));
  56. CheckResult (cr);
  57. }
  58. static void CheckResult (CompilerResults cr)
  59. {
  60. if (cr.Errors.Count > 0)
  61. {
  62. foreach (string s in cr.Output)
  63. Console.WriteLine (s);
  64. string errs = "";
  65. foreach (CompilerError error in cr.Errors)
  66. if (error.FileName != "")
  67. errs += error.ToString () + "\n";
  68. throw new Exception ("There where errors during compilation of the assembly:\n" + errs);
  69. }
  70. }
  71. public static void ConvertSchemaStreamToCodeSourceStream (
  72. bool clientProxy,
  73. string outputDirectory,
  74. Stream inputStream,
  75. ArrayList outCodeStreamList)
  76. {
  77. ConvertSchemaStreamToCodeSourceStream (clientProxy, outputDirectory, inputStream, outCodeStreamList, null, null);
  78. }
  79. public static void ConvertSchemaStreamToCodeSourceStream (
  80. bool clientProxy,
  81. string outputDirectory,
  82. Stream inputStream,
  83. ArrayList outCodeStreamList,
  84. string proxyUrl)
  85. {
  86. ConvertSchemaStreamToCodeSourceStream (clientProxy, outputDirectory, inputStream, outCodeStreamList, proxyUrl, null);
  87. }
  88. public static void ConvertSchemaStreamToCodeSourceStream (
  89. bool clientProxy,
  90. string outputDirectory,
  91. Stream inputStream,
  92. ArrayList outCodeStreamList,
  93. string proxyUrl,
  94. string proxyNamespace)
  95. {
  96. MetaDataCodeGenerator cg = new MetaDataCodeGenerator ();
  97. MemoryStream memStream = new MemoryStream ();
  98. CopyStream (inputStream, memStream);
  99. memStream.Position = 0;
  100. cg.GenerateCode (clientProxy, outputDirectory, memStream, outCodeStreamList, proxyUrl, proxyNamespace);
  101. }
  102. public static void ConvertTypesToSchemaToFile (ServiceType[] servicetypes, SdlType sdltype, string path)
  103. {
  104. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  105. ConvertTypesToSchemaToStream (servicetypes, sdltype, fs);
  106. fs.Close ();
  107. }
  108. public static void ConvertTypesToSchemaToFile (Type[] types, SdlType sdltype, string path)
  109. {
  110. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  111. ConvertTypesToSchemaToStream (types, sdltype, fs);
  112. fs.Close ();
  113. }
  114. public static void ConvertTypesToSchemaToStream (Type[] types, SdlType sdltype, Stream stream)
  115. {
  116. ServiceType[] st = new ServiceType [types.Length];
  117. for (int n=0; n<types.Length; n++)
  118. st [n] = new ServiceType (types[n]);
  119. ConvertTypesToSchemaToStream (st, sdltype, stream);
  120. }
  121. public static void ConvertTypesToSchemaToStream (ServiceType[] servicetypes, SdlType sdltype, Stream stream)
  122. {
  123. MetaDataExporter exporter = new MetaDataExporter ();
  124. MemoryStream memStream = new MemoryStream ();
  125. StreamWriter sw = new StreamWriter (memStream);
  126. XmlTextWriter tw = new XmlTextWriter (sw);
  127. exporter.ExportTypes (servicetypes, sdltype, tw);
  128. tw.Flush ();
  129. memStream.Position = 0;
  130. CopyStream (memStream, stream);
  131. }
  132. public static void RetrieveSchemaFromUrlToFile (string url, string path)
  133. {
  134. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  135. RetrieveSchemaFromUrlToStream (url, fs);
  136. fs.Close ();
  137. }
  138. public static void RetrieveSchemaFromUrlToStream (string url, Stream outputStream)
  139. {
  140. WebRequest req = WebRequest.Create (url);
  141. Stream st = req.GetResponse().GetResponseStream();
  142. CopyStream (st, outputStream);
  143. st.Close ();
  144. }
  145. public static void SaveStreamToFile (Stream inputStream, string path)
  146. {
  147. FileStream fs = new FileStream (path, FileMode.Create, FileAccess.Write);
  148. CopyStream (inputStream, fs);
  149. fs.Close ();
  150. }
  151. static void CopyStream (Stream inputStream, Stream outputStream)
  152. {
  153. byte[] buffer = new byte [1024*5];
  154. int nr = 0;
  155. while ((nr = inputStream.Read (buffer, 0, buffer.Length)) > 0)
  156. outputStream.Write (buffer, 0, nr);
  157. }
  158. }
  159. }