2006-11-02 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Web.Services / System.Web.Services.Protocols / Fault.cs
1 // 
2 // System.Web.Services.Protocols.Fault.cs
3 //
4 // Author:
5 //   Lluis Sanchez Gual (lluis@ximian.com)
6 //
7 // Copyright (C) 2004 Novell, Inc.
8 //
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
31 using System;
32 using System.Xml;
33 using System.Xml.Schema;
34 using System.Xml.Serialization;
35 using System.Text;
36 using System.Collections;
37 using System.Globalization;
38
39 namespace System.Web.Services.Protocols
40 {
41         internal class Fault
42         {
43                 static XmlSerializer serializer;
44                 
45                 static Fault ()
46                 {
47                         serializer = new FaultSerializer ();
48                 }
49
50                 public Fault () {}
51                 
52                 public Fault (SoapException ex) 
53                 {
54                         faultcode = ex.Code;
55                         faultstring = ex.Message;
56                         faultactor = ex.Actor;
57                         detail = ex.Detail;
58                 }
59
60                 [XmlElement (Namespace="")]
61                 public XmlQualifiedName faultcode;
62                 
63                 [XmlElement (Namespace="")]
64                 public string faultstring;
65                 
66                 [XmlElement (Namespace="")]
67                 public string faultactor;
68                 
69                 [SoapIgnore]
70                 public XmlNode detail;
71                 
72                 public static XmlSerializer Serializer
73                 {
74                         get { return serializer; }
75                 }
76         }
77
78         internal class FaultSerializer : XmlSerializer 
79         {
80                 protected override void Serialize (object o, XmlSerializationWriter writer)
81                 {
82                         FaultWriter xsWriter = writer as FaultWriter;
83                         xsWriter.WriteRoot_Fault (o);
84                 }
85                 
86                 protected override object Deserialize (XmlSerializationReader reader)
87                 {
88                         FaultReader xsReader = reader as FaultReader;
89                         return xsReader.ReadRoot_Fault ();
90                 }
91                 
92                 protected override XmlSerializationWriter CreateWriter ()
93                 {
94                         return new FaultWriter ();
95                 }
96                 
97                 protected override XmlSerializationReader CreateReader ()
98                 {
99                         return new FaultReader ();
100                 }
101         }       
102         
103         internal class FaultReader : XmlSerializationReader
104         {
105                 public object ReadRoot_Fault ()
106                 {
107                         Reader.MoveToContent();
108                         if (Reader.LocalName != "Fault" || Reader.NamespaceURI != "http://schemas.xmlsoap.org/soap/envelope/")
109                                 throw CreateUnknownNodeException();
110                         return ReadObject_Fault (true, true);
111                 }
112
113                 public System.Web.Services.Protocols.Fault ReadObject_Fault (bool isNullable, bool checkType)
114                 {
115                         System.Web.Services.Protocols.Fault ob = null;
116                         if (isNullable && ReadNull()) return null;
117
118                         if (checkType) 
119                         {
120                                 System.Xml.XmlQualifiedName t = GetXsiType();
121                                 if (t != null) 
122                                 {
123                                         if (t.Name != "Fault" || t.Namespace != "http://schemas.xmlsoap.org/soap/envelope/")
124                                                 throw CreateUnknownTypeException(t);
125                                 }
126                         }
127
128                         ob = new System.Web.Services.Protocols.Fault ();
129
130                         Reader.MoveToElement();
131
132                         while (Reader.MoveToNextAttribute())
133                         {
134                                 if (IsXmlnsAttribute (Reader.Name)) {
135                                 }
136                                 else {
137                                         UnknownNode (ob);
138                                 }
139                         }
140
141                         Reader.MoveToElement();
142                         if (Reader.IsEmptyElement) {
143                                 Reader.Skip ();
144                                 return ob;
145                         }
146
147                         Reader.ReadStartElement();
148                         Reader.MoveToContent();
149
150                         bool b0=false, b1=false, b2=false, b3=false;
151
152                         while (Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
153                         {
154                                 if (Reader.NodeType == System.Xml.XmlNodeType.Element) 
155                                 {
156                                         if (Reader.LocalName == "faultcode" && Reader.NamespaceURI == "" && !b0) {
157                                                 b0 = true;
158                                                 ob.@faultcode = ReadElementQualifiedName ();
159                                         }
160                                         else if (Reader.LocalName == "faultstring" && Reader.NamespaceURI == "" && !b1) {
161                                                 b1 = true;
162                                                 ob.@faultstring = Reader.ReadElementString ();
163                                         }
164                                         else if (Reader.LocalName == "detail" && Reader.NamespaceURI == "http://schemas.xmlsoap.org/soap/envelope/" && !b3) {
165                                                 b3 = true;
166                                                 ob.@detail = ReadXmlNode (true);
167                                         }
168                                         else if (Reader.LocalName == "faultactor" && Reader.NamespaceURI == "" && !b2) {
169                                                 b2 = true;
170                                                 ob.@faultactor = Reader.ReadElementString ();
171                                         }
172                                         else {
173                                                 UnknownNode (ob);
174                                         }
175                                 }
176                                 else
177                                         UnknownNode(ob);
178
179                                 Reader.MoveToContent();
180                         }
181
182                         ReadEndElement();
183
184                         return ob;
185                 }
186
187                 protected override void InitCallbacks ()
188                 {
189                 }
190
191                 protected override void InitIDs ()
192                 {
193                 }
194         }
195
196         internal class FaultWriter : XmlSerializationWriter
197         {
198                 public void WriteRoot_Fault (object o)
199                 {
200                         WriteStartDocument ();
201                         System.Web.Services.Protocols.Fault ob = (System.Web.Services.Protocols.Fault) o;
202                         TopLevelElement ();
203                         WriteObject_Fault (ob, "Fault", "http://schemas.xmlsoap.org/soap/envelope/", true, false, true);
204                 }
205
206                 void WriteObject_Fault (System.Web.Services.Protocols.Fault ob, string element, string namesp, bool isNullable, bool needType, bool writeWrappingElem)
207                 {
208                         if (ob == null)
209                         {
210                                 if (isNullable)
211                                         WriteNullTagLiteral(element, namesp);
212                                 return;
213                         }
214
215                         if (writeWrappingElem) {
216                                 WriteStartElement (element, namesp, ob);
217                         }
218
219                         if (needType) WriteXsiType("Fault", "http://schemas.xmlsoap.org/soap/envelope/");
220
221                         WriteElementQualifiedName ("faultcode", "", ob.@faultcode);
222                         WriteElementString ("faultstring", "", ob.@faultstring);
223                         WriteElementString ("faultactor", "", ob.@faultactor);
224                         WriteElementLiteral (ob.@detail, "detail", "http://schemas.xmlsoap.org/soap/envelope/", false, false);
225                         if (writeWrappingElem) WriteEndElement (ob);
226                 }
227
228                 protected override void InitCallbacks ()
229                 {
230                 }
231         }
232 }
233