MetaData.cs 6.8 KB

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