Fault.cs 5.9 KB

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