This commit was manufactured by cvs2svn to create branch 'mono-1-0'.
[mono.git] / mcs / class / System.Runtime.Serialization.Formatters.Soap / System.Runtime.Serialization.Formatters.Soap / SoapFormatter.cs
1 // created on 07/04/2003 at 17:16\r
2 //\r
3 //      System.Runtime.Serialization.Formatters.Soap.SoapFormatter\r
4 //\r
5 //      Authors:\r
6 //              Jean-Marc Andre (jean-marc.andre@polymtl.ca)\r
7 //\r
8
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29 \r
30 using System;\r
31 using System.IO;\r
32 using System.Reflection;\r
33 using System.Runtime.InteropServices;\r
34 using System.Runtime.Remoting;\r
35 using System.Runtime.Serialization;\r
36 using System.Runtime.Remoting.Messaging;\r
37 using System.Xml.Serialization;\r
38 using System.Threading;\r
39 using System.Globalization;\r
40 \r
41 \r
42 namespace System.Runtime.Serialization.Formatters.Soap {\r
43         enum RemMessageType {\r
44                 MethodCall, MethodResponse, ServerFault, NotRecognize\r
45         }\r
46         \r
47         public sealed class SoapFormatter: IRemotingFormatter, IFormatter {\r
48                 private SoapWriter _soapWriter;\r
49                 private SerializationBinder _binder;\r
50                 private StreamingContext _context;\r
51                 private ISurrogateSelector _selector;\r
52                 private FormatterAssemblyStyle _assemblyFormat = FormatterAssemblyStyle.Full;\r
53                 private FormatterTypeStyle _typeFormat = FormatterTypeStyle.TypesWhenNeeded;\r
54                 private ISoapMessage _topObject = null;\r
55                 \r
56 #if NET_1_1\r
57                 TypeFilterLevel _filterLevel = TypeFilterLevel.Low;\r
58 #endif\r
59 \r
60                 public SoapFormatter() {\r
61                         _selector = null;\r
62                         _context = new StreamingContext(StreamingContextStates.All);\r
63                 }\r
64                 \r
65                 public SoapFormatter(ISurrogateSelector selector, StreamingContext context) {\r
66                         _selector = selector;\r
67                         _context = context;\r
68                 }\r
69                 \r
70                 ~SoapFormatter() {\r
71                 }\r
72 \r
73         \r
74                 public object Deserialize(Stream serializationStream) {\r
75                         return Deserialize(serializationStream, null);\r
76                 }\r
77                 \r
78                 public object Deserialize(Stream serializationStream, HeaderHandler handler) {\r
79                         object objReturn = null;\r
80                         SoapReader soapReader = new SoapReader(_binder, _selector, _context);\r
81                         CultureInfo savedCi = CultureInfo.CurrentCulture;\r
82                         try {\r
83                                 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");\r
84                                 objReturn = soapReader.Deserialize(serializationStream, _topObject);\r
85                         }\r
86                         finally {\r
87                                 Thread.CurrentThread.CurrentCulture = savedCi;\r
88                         }\r
89                         return objReturn;\r
90                 }\r
91                 \r
92                 \r
93                 \r
94                 public void Serialize(Stream serializationStream, object graph) {\r
95                         Serialize(serializationStream, graph, null);\r
96                 }\r
97                 \r
98                 public void Serialize(Stream serializationStream, object graph, Header[] headers) {\r
99                         if(serializationStream == null)\r
100                                 throw new ArgumentNullException("serializationStream");\r
101                         if(!serializationStream.CanWrite)\r
102                                 throw new SerializationException("Can't write in the serialization stream");\r
103                         if(graph == null)\r
104                                 throw new ArgumentNullException("graph");\r
105                         _soapWriter = new SoapWriter(serializationStream, _selector, _context, _topObject);\r
106                         CultureInfo savedCi = CultureInfo.CurrentCulture;\r
107                         try {\r
108                                 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");\r
109                                 _soapWriter.Serialize (graph, headers, _typeFormat, _assemblyFormat);\r
110                         }\r
111                         finally {\r
112                                 Thread.CurrentThread.CurrentCulture = savedCi;\r
113                         }\r
114                         \r
115                 }\r
116                 \r
117                 public ISurrogateSelector SurrogateSelector {\r
118                         get {\r
119                                 return _selector;\r
120                         }\r
121                         set {\r
122                                 _selector = value;\r
123                         }\r
124                 }\r
125                 \r
126                 \r
127                 public SerializationBinder Binder {\r
128                         get {\r
129                                 return _binder;\r
130                         }\r
131                         set {\r
132                                 _binder = value;\r
133                         }\r
134                 }\r
135                 \r
136                 public StreamingContext Context {\r
137                         get {\r
138                                 return _context;\r
139                         }\r
140                         set {\r
141                                 _context = value;\r
142                         }\r
143                 }\r
144                 \r
145                 public ISoapMessage TopObject {\r
146                         get {\r
147                                 return _topObject;\r
148                         }\r
149                         set {\r
150                                 _topObject = value;\r
151                         }\r
152                 }\r
153                 \r
154 #if NET_1_1\r
155                 [MonoTODO ("Interpret this")]\r
156                 [ComVisible(false)]\r
157                 public TypeFilterLevel FilterLevel {\r
158                         get {\r
159                                 return _filterLevel;\r
160                         }\r
161                         set {\r
162                                 _filterLevel = value;\r
163                         }\r
164                 }\r
165 #endif\r
166                 \r
167                 public FormatterAssemblyStyle AssemblyFormat\r
168                 {\r
169                         get {\r
170                                 return _assemblyFormat;\r
171                         }\r
172                         set {\r
173                                 _assemblyFormat = value;\r
174                         }\r
175                 }\r
176 \r
177                 public FormatterTypeStyle TypeFormat\r
178                 {\r
179                         get \r
180                         {\r
181                                 return _typeFormat;\r
182                         }\r
183                         set \r
184                         {\r
185                                 _typeFormat = value;\r
186                         }\r
187                 }\r
188 \r
189         }\r
190 }\r