The Labs \ Source Viewer \ SSCLI \ System.Runtime.Serialization.Formatters.Soap \ SoapFormatter

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 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. namespace System.Runtime.Serialization.Formatters.Soap
  24. {
  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.    
  37.    
  38.    
  39.     public sealed class SoapFormatter : IRemotingFormatter
  40.     {
  41.         private SoapParser soapParser = null;
  42.         private ISurrogateSelector m_surrogates;
  43.         private StreamingContext m_context;
  44.         private FormatterTypeStyle m_typeFormat = FormatterTypeStyle.TypesWhenNeeded;
  45.         private ISoapMessage m_topObject = null;
  46.         //private FormatterAssemblyStyle m_assemblyFormat = FormatterAssemblyStyle.Simple;
  47.         private FormatterAssemblyStyle m_assemblyFormat = FormatterAssemblyStyle.Full;
  48.         private TypeFilterLevel m_securityLevel = TypeFilterLevel.Full;
  49.         private SerializationBinder m_binder;
  50.         private Stream currentStream = null;
  51.        
  52.        
  53.         // Property which specifies an object of type ISoapMessage into which
  54.         // the SoapTop object is serialized. Should only be used if the Soap
  55.         // top record is a methodCall or methodResponse element.
  56.         public ISoapMessage TopObject {
  57.             get { return m_topObject; }
  58.             set { m_topObject = value; }
  59.         }
  60.        
  61.         // Property which specifies how types are serialized,
  62.         // FormatterTypeStyle Enum specifies options
  63.         public FormatterTypeStyle TypeFormat {
  64.             get { return m_typeFormat; }
  65.             set {
  66.                 // Reset the value if TypesWhenNeeded
  67.                 // Or the value for TypesAlways and XsdString
  68.                 if (value == FormatterTypeStyle.TypesWhenNeeded)
  69.                     m_typeFormat = FormatterTypeStyle.TypesWhenNeeded;
  70.                 else
  71.                     m_typeFormat |= value;
  72.             }
  73.         }
  74.        
  75.         // Property which specifies how types are serialized,
  76.         // FormatterAssemblyStyle Enum specifies options
  77.         public FormatterAssemblyStyle AssemblyFormat {
  78.             get { return m_assemblyFormat; }
  79.             set { m_assemblyFormat = value; }
  80.         }
  81.        
  82.         // Property which specifies the security level of formatter
  83.         // TypeFilterLevel Enum specifies options
  84.         public TypeFilterLevel FilterLevel {
  85.             get { return m_securityLevel; }
  86.             set { m_securityLevel = value; }
  87.         }
  88.        
  89.         // Constructor
  90.         public SoapFormatter()
  91.         {
  92.             m_surrogates = null;
  93.             m_context = new StreamingContext(StreamingContextStates.All);
  94.         }
  95.        
  96.         // Constructor
  97.         public SoapFormatter(ISurrogateSelector selector, StreamingContext context)
  98.         {
  99.             m_surrogates = selector;
  100.             m_context = context;
  101.         }
  102.        
  103.         // Deserialize the stream into an object graph.
  104.         public object Deserialize(Stream serializationStream)
  105.         {
  106.             return Deserialize(serializationStream, null);
  107.         }
  108.        
  109.         // Deserialize the stream into an object graph.
  110.         public object Deserialize(Stream serializationStream, HeaderHandler handler)
  111.         {
  112.             InternalST.InfoSoap("Enter SoapFormatter.Deserialize ");
  113.             if (serializationStream == null) {
  114.                 throw new ArgumentNullException("serializationStream");
  115.             }
  116.            
  117.             if (serializationStream.CanSeek && (serializationStream.Length == 0))
  118.                 throw new SerializationException(SoapUtil.GetResourceString("Serialization_Stream"));
  119.            
  120.             InternalST.Soap(this, "Deserialize Entry");
  121.             InternalFE formatterEnums = new InternalFE();
  122.             formatterEnums.FEtypeFormat = m_typeFormat;
  123.             formatterEnums.FEtopObject = m_topObject;
  124.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Soap;
  125.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  126.             formatterEnums.FEsecurityLevel = m_securityLevel;
  127.             ObjectReader sor = new ObjectReader(serializationStream, m_surrogates, m_context, formatterEnums, m_binder);
  128.            
  129.             // If this is the first call, or a new stream is being used a new Soap parser is created.
  130.             // If this is a continuing call, then the existing SoapParser is used.
  131.             // One stream can contains multiple Soap XML documents. The XMLParser buffers the XML so
  132.             // that the same XMLParser has to be used to continue a stream.
  133.             if ((soapParser == null) || (serializationStream != currentStream)) {
  134.                 soapParser = new SoapParser(serializationStream);
  135.                 currentStream = serializationStream;
  136.             }
  137.             soapParser.Init(sor);
  138.             object obj = sor.Deserialize(handler, soapParser);
  139.             InternalST.InfoSoap("Leave SoapFormatter.Deserialize ");
  140.             return obj;
  141.         }
  142.        
  143.         public void Serialize(Stream serializationStream, object graph)
  144.         {
  145.             Serialize(serializationStream, graph, null);
  146.         }
  147.        
  148.         // Commences the process of serializing the entire graph. All of the data (in the appropriate format)
  149.         // is emitted onto the stream.
  150.        
  151.         public void Serialize(Stream serializationStream, object graph, Header[] headers)
  152.         {
  153.             InternalST.InfoSoap("Enter SoapFormatter.Serialize ");
  154.             if (serializationStream == null) {
  155.                 throw new ArgumentNullException("serializationStream");
  156.             }
  157.            
  158.             InternalST.Soap(this, "Serialize Entry");
  159.             InternalFE formatterEnums = new InternalFE();
  160.             formatterEnums.FEtypeFormat = m_typeFormat;
  161.             formatterEnums.FEtopObject = m_topObject;
  162.             formatterEnums.FEserializerTypeEnum = InternalSerializerTypeE.Soap;
  163.             formatterEnums.FEassemblyFormat = m_assemblyFormat;
  164.             ObjectWriter sow = new ObjectWriter(serializationStream, m_surrogates, m_context, formatterEnums);
  165.             sow.Serialize(graph, headers, new SoapWriter(serializationStream));
  166.             InternalST.InfoSoap("Leave SoapFormatter.Serialize ");
  167.         }
  168.        
  169.         public ISurrogateSelector SurrogateSelector {
  170.             get { return m_surrogates; }
  171.            
  172.             set { m_surrogates = value; }
  173.         }
  174.        
  175.         public SerializationBinder Binder {
  176.             get { return m_binder; }
  177.            
  178.             set { m_binder = value; }
  179.         }
  180.        
  181.         public StreamingContext Context {
  182.             get { return m_context; }
  183.            
  184.             set { m_context = value; }
  185.         }
  186.     }
  187. }

Developer Fusion