SoapFormatter

来源:互联网 发布:电脑ps软件6 编辑:程序博客网 时间:2024/06/04 18:34
  1. // ==++==
  2. // 
  3. //   
  4. //    Copyright (c) 2002 Microsoft Corporation.  All rights reserved.
  5. //   
  6. //    The use and distribution terms for this software are contained in the file
  7. //    named license.txt, which can be found in the root of this distribution.
  8. //    By using this software in any fashion, you are agreeing to be bound by the
  9. //    terms of this license.
  10. //   
  11. //    You must not remove this notice, or any other, from this software.
  12. //   
  13. // 
  14. // ==--==
  15. //============================================================
  16. //
  17. // Class: SoapFormatter
  18. // Purpose: Soap XML Formatter
  19. //
  20. // Date:  June 10, 1999
  21. //
  22. //============================================================
  23. [assembly:System.CLSCompliant(true)]
  24. namespace System.Runtime.Serialization.Formatters.Soap
  25. {
  26.     using System;
  27.     using System.Runtime.Serialization.Formatters;
  28.     using System.IO;
  29.     using System.Reflection;
  30.     using System.Globalization;
  31.     using System.Collections;
  32.     using System.Runtime.Serialization;
  33.     using System.Runtime.Remoting;    
  34.     using System.Runtime.Remoting.Messaging;    
  35.     using System.Text;
  36.     /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter"]/*' />
  37.     sealed public class SoapFormatter : IRemotingFormatter
  38.     {
  39.         private SoapParser soapParser = null;
  40.         private ISurrogateSelector m_surrogates;
  41.         private StreamingContext m_context;
  42.         private FormatterTypeStyle m_typeFormat = FormatterTypeStyle.TypesWhenNeeded;
  43.         private ISoapMessage m_topObject = null;
  44.         //private FormatterAssemblyStyle m_assemblyFormat = FormatterAssemblyStyle.Simple;        
  45.         private FormatterAssemblyStyle m_assemblyFormat = FormatterAssemblyStyle.Full;        
  46.         private SerializationBinder m_binder;
  47.         private Stream currentStream = null;
  48.         // Property which specifies an object of type ISoapMessage into which
  49.         // the SoapTop object is serialized. Should only be used if the Soap 
  50.         // top record is a methodCall or methodResponse element. 
  51.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.TopObject"]/*' />
  52.         public ISoapMessage TopObject
  53.         {
  54.             get {return m_topObject;}
  55.             set {m_topObject = value;}
  56.         }
  57.         // Property which specifies how types are serialized,
  58.         // FormatterTypeStyle Enum specifies options
  59.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.TypeFormat"]/*' />
  60.         public FormatterTypeStyle TypeFormat
  61.         {
  62.             get {return m_typeFormat;}
  63.             set
  64.             {
  65.                 // Reset the value if TypesWhenNeeded
  66.                 // Or the value for TypesAlways and XsdString
  67.                 if (value == FormatterTypeStyle.TypesWhenNeeded)
  68.                     m_typeFormat = FormatterTypeStyle.TypesWhenNeeded;
  69.                 else
  70.                     m_typeFormat |= value;
  71.             }
  72.         }        
  73.         // Property which specifies how types are serialized,
  74.         // FormatterAssemblyStyle Enum specifies options
  75.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.AssemblyFormat"]/*' />
  76.         public FormatterAssemblyStyle AssemblyFormat
  77.         {
  78.             get {return m_assemblyFormat;}
  79.             set {m_assemblyFormat = value;}
  80.         }    
  81.         
  82.         // Constructor
  83.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.SoapFormatter"]/*' />
  84.         public SoapFormatter()
  85.         {
  86.             m_surrogates=null;
  87.             m_context = new StreamingContext(StreamingContextStates.All);
  88.         }
  89.         // Constructor
  90.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.SoapFormatter1"]/*' />
  91.         public SoapFormatter(ISurrogateSelector selector, StreamingContext context)
  92.         {
  93.             m_surrogates = selector;
  94.             m_context = context;
  95.         }
  96.         // Deserialize the stream into an object graph.
  97.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Deserialize"]/*' />
  98.         public Object Deserialize(Stream serializationStream)
  99.         {
  100.             return Deserialize(serializationStream, null);
  101.         }
  102.         // Deserialize the stream into an object graph.
  103.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Deserialize1"]/*' />
  104.         public Object Deserialize(Stream serializationStream, HeaderHandler handler) {
  105.             InternalST.InfoSoap("Enter SoapFormatter.Deserialize ");            
  106.             if (serializationStream==null) {
  107.                 throw new ArgumentNullException("serializationStream");
  108.             }
  109.             if (serializationStream.CanSeek && (serializationStream.Length == 0))
  110.                 throw new SerializationException(SoapUtil.GetResourceString("Serialization_Stream"));                                
  111.             InternalST.Soap( this"Deserialize Entry");
  112.             InternalFE formatterEnums = new InternalFE();
  113.             formatterEnums.FEtypeFormat = m_typeFormat;
  114.             formatterEnums.FEtopObject = m_topObject;
  115.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Soap;
  116.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  117.             ObjectReader sor = new ObjectReader(serializationStream, m_surrogates, m_context, formatterEnums, m_binder);
  118.             // If this is the first call, or a new stream is being used a new Soap parser is created.
  119.             // If this is a continuing call, then the existing SoapParser is used.
  120.             // One stream can contains multiple Soap XML documents. The XMLParser buffers the XML so
  121.             // that the same XMLParser has to be used to continue a stream.
  122.             if ((soapParser == null) || (serializationStream != currentStream))
  123.             {
  124.                 soapParser = new SoapParser(serializationStream);
  125.                 currentStream = serializationStream;
  126.             }
  127.             soapParser.Init(sor);
  128.             Object obj = sor.Deserialize(handler, soapParser);
  129.             InternalST.InfoSoap("Leave SoapFormatter.Deserialize ");
  130.             return obj;
  131.         }
  132.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Serialize"]/*' />
  133.         public void Serialize(Stream serializationStream, Object graph)
  134.         {
  135.             Serialize(serializationStream, graph, null);
  136.         }
  137.         // Commences the process of serializing the entire graph.  All of the data (in the appropriate format)
  138.         // is emitted onto the stream.
  139.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Serialize1"]/*' />
  140.         public void Serialize(Stream serializationStream, Object graph, Header[] headers)
  141.         {
  142.             InternalST.InfoSoap("Enter SoapFormatter.Serialize ");
  143.             if (serializationStream==null) {
  144.                 throw new ArgumentNullException("serializationStream");
  145.             }
  146.             InternalST.Soap( this"Serialize Entry");
  147.             InternalFE formatterEnums = new InternalFE();
  148.             formatterEnums.FEtypeFormat = m_typeFormat;
  149.             formatterEnums.FEtopObject = m_topObject;
  150.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Soap;
  151.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  152.             ObjectWriter sow = new ObjectWriter(serializationStream, m_surrogates, m_context, formatterEnums);
  153.             sow.Serialize(graph, headers, new SoapWriter(serializationStream));
  154.             InternalST.InfoSoap("Leave SoapFormatter.Serialize ");            
  155.         }
  156.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.SurrogateSelector"]/*' />
  157.         public ISurrogateSelector SurrogateSelector {
  158.             get {
  159.                 return m_surrogates;
  160.             }
  161.             set {
  162.                 m_surrogates = value;
  163.             }
  164.         }
  165.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Binder"]/*' />
  166.         public SerializationBinder Binder {
  167.             get {
  168.                 return m_binder;
  169.             }
  170.             set {
  171.                 m_binder=value;
  172.             }
  173.         }
  174.         /// <include file='doc/SoapFormatter.uex' path='docs/doc[@for="SoapFormatter.Context"]/*' />
  175.         public StreamingContext Context {
  176.             get {
  177.                 return m_context;
  178.             }
  179.             set {
  180.                 m_context = value;
  181.             }
  182.         }
  183.     }
  184. }