New test.
[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 \r
9 //\r
10 // Permission is hereby granted, free of charge, to any person obtaining\r
11 // a copy of this software and associated documentation files (the\r
12 // "Software"), to deal in the Software without restriction, including\r
13 // without limitation the rights to use, copy, modify, merge, publish,\r
14 // distribute, sublicense, and/or sell copies of the Software, and to\r
15 // permit persons to whom the Software is furnished to do so, subject to\r
16 // the following conditions:\r
17 // \r
18 // The above copyright notice and this permission notice shall be\r
19 // included in all copies or substantial portions of the Software.\r
20 // \r
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
28 //\r
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 SerializationBinder _binder;\r
49                 private StreamingContext _context;\r
50                 private ISurrogateSelector _selector;\r
51                 private FormatterAssemblyStyle _assemblyFormat = FormatterAssemblyStyle.Full;\r
52                 private FormatterTypeStyle _typeFormat = FormatterTypeStyle.TypesWhenNeeded;\r
53                 private ISoapMessage _topObject = null;\r
54                 \r
55 #if NET_1_1\r
56                 TypeFilterLevel _filterLevel = TypeFilterLevel.Low;\r
57 #endif\r
58 \r
59                 public SoapFormatter() {\r
60                         _selector = null;\r
61                         _context = new StreamingContext(StreamingContextStates.All);\r
62                 }\r
63                 \r
64                 public SoapFormatter(ISurrogateSelector selector, StreamingContext context) {\r
65                         _selector = selector;\r
66                         _context = context;\r
67                 }\r
68                 \r
69                 public object Deserialize(Stream serializationStream) {\r
70                         return Deserialize(serializationStream, null);\r
71                 }\r
72                 \r
73                 public object Deserialize(Stream serializationStream, HeaderHandler handler) {\r
74                         object objReturn = null;\r
75                         SoapReader soapReader = new SoapReader(_binder, _selector, _context);\r
76                         CultureInfo savedCi = CultureInfo.CurrentCulture;\r
77                         try {\r
78                                 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");\r
79                                 objReturn = soapReader.Deserialize(serializationStream, _topObject);\r
80                         }\r
81                         finally {\r
82                                 Thread.CurrentThread.CurrentCulture = savedCi;\r
83                         }\r
84                         return objReturn;\r
85                 }\r
86                 \r
87                 \r
88                 \r
89                 public void Serialize(Stream serializationStream, object graph) {\r
90                         Serialize(serializationStream, graph, null);\r
91                 }\r
92                 \r
93                 public void Serialize(Stream serializationStream, object graph, Header[] headers) {\r
94                         if(serializationStream == null)\r
95                                 throw new ArgumentNullException("serializationStream");\r
96                         if(!serializationStream.CanWrite)\r
97                                 throw new SerializationException("Can't write in the serialization stream");\r
98                         if(graph == null)\r
99                                 throw new ArgumentNullException("graph");\r
100                         SoapWriter soapWriter = new SoapWriter(serializationStream, _selector, _context, _topObject);\r
101                         CultureInfo savedCi = CultureInfo.CurrentCulture;\r
102                         try {\r
103                                 Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");\r
104                                 soapWriter.Serialize (graph, headers, _typeFormat, _assemblyFormat);\r
105                         }\r
106                         finally {\r
107                                 Thread.CurrentThread.CurrentCulture = savedCi;\r
108                         }\r
109                         \r
110                 }\r
111                 \r
112                 public ISurrogateSelector SurrogateSelector {\r
113                         get {\r
114                                 return _selector;\r
115                         }\r
116                         set {\r
117                                 _selector = value;\r
118                         }\r
119                 }\r
120                 \r
121                 \r
122                 public SerializationBinder Binder {\r
123                         get {\r
124                                 return _binder;\r
125                         }\r
126                         set {\r
127                                 _binder = value;\r
128                         }\r
129                 }\r
130                 \r
131                 public StreamingContext Context {\r
132                         get {\r
133                                 return _context;\r
134                         }\r
135                         set {\r
136                                 _context = value;\r
137                         }\r
138                 }\r
139                 \r
140                 public ISoapMessage TopObject {\r
141                         get {\r
142                                 return _topObject;\r
143                         }\r
144                         set {\r
145                                 _topObject = value;\r
146                         }\r
147                 }\r
148                 \r
149 #if NET_1_1\r
150                 [MonoTODO ("Interpret this")]\r
151 #if ONLY_1_1\r
152                 [ComVisible(false)]\r
153 #endif\r
154                 public TypeFilterLevel FilterLevel {\r
155                         get {\r
156                                 return _filterLevel;\r
157                         }\r
158                         set {\r
159                                 _filterLevel = value;\r
160                         }\r
161                 }\r
162 #endif\r
163                 \r
164                 public FormatterAssemblyStyle AssemblyFormat\r
165                 {\r
166                         get {\r
167                                 return _assemblyFormat;\r
168                         }\r
169                         set {\r
170                                 _assemblyFormat = value;\r
171                         }\r
172                 }\r
173 \r
174                 public FormatterTypeStyle TypeFormat\r
175                 {\r
176                         get \r
177                         {\r
178                                 return _typeFormat;\r
179                         }\r
180                         set \r
181                         {\r
182                                 _typeFormat = value;\r
183                         }\r
184                 }\r
185 \r
186         }\r
187 }\r