XmlSchemaException.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. //
  2. // XmlSchemaException.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  7. //
  8. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.Globalization;
  30. using System.Runtime.Serialization;
  31. using System.Security.Permissions;
  32. namespace System.Xml.Schema
  33. {
  34. /// <summary>
  35. /// Summary description for XmlSchemaException.
  36. /// </summary>
  37. [Serializable]
  38. public class XmlSchemaException : System.SystemException
  39. {
  40. //fields
  41. private bool hasLineInfo;
  42. private int lineNumber;
  43. private int linePosition;
  44. private XmlSchemaObject sourceObj;
  45. private string sourceUri;
  46. #if NET_2_0
  47. public XmlSchemaException ()
  48. : this ("A schema error occured.", null)
  49. {
  50. }
  51. #endif
  52. #if NET_2_0
  53. public
  54. #else
  55. internal
  56. #endif
  57. XmlSchemaException (string message)
  58. : this (message, null)
  59. {
  60. }
  61. protected XmlSchemaException(SerializationInfo info, StreamingContext context)
  62. : base (info, context)
  63. {
  64. this.hasLineInfo = info.GetBoolean ("hasLineInfo");
  65. this.lineNumber = info.GetInt32 ("lineNumber");
  66. this.linePosition = info.GetInt32 ("linePosition");
  67. this.sourceUri = info.GetString ("sourceUri");
  68. this.sourceObj = info.GetValue ("sourceObj", typeof (XmlSchemaObject)) as XmlSchemaObject;
  69. }
  70. #if NET_2_0
  71. public XmlSchemaException (string message, Exception innerException, int lineNumber, int linePosition)
  72. : this (message, lineNumber, linePosition, null, null, innerException)
  73. {
  74. }
  75. #endif
  76. internal XmlSchemaException (string message, int lineNumber, int linePosition,
  77. XmlSchemaObject sourceObject, string sourceUri, Exception innerException)
  78. : base (
  79. GetMessage (message, sourceUri, lineNumber, linePosition, sourceObject),
  80. innerException)
  81. {
  82. hasLineInfo = true;
  83. this.lineNumber = lineNumber;
  84. this.linePosition = linePosition;
  85. this.sourceObj = sourceObject;
  86. this.sourceUri = sourceUri;
  87. }
  88. internal XmlSchemaException (string message, object sender,
  89. string sourceUri, XmlSchemaObject sourceObject, Exception innerException)
  90. : base (GetMessage (message, sourceUri, sender, sourceObject), innerException)
  91. {
  92. IXmlLineInfo li = sender as IXmlLineInfo;
  93. if (li != null && li.HasLineInfo ()) {
  94. hasLineInfo = true;
  95. this.lineNumber = li.LineNumber;
  96. this.linePosition = li.LinePosition;
  97. }
  98. this.sourceObj = sourceObject;
  99. }
  100. internal XmlSchemaException(string message, XmlSchemaObject sourceObject,
  101. Exception innerException)
  102. : base (
  103. GetMessage (message, null, 0, 0, sourceObject),
  104. innerException)
  105. {
  106. hasLineInfo = true;
  107. #if !MOBILE
  108. this.lineNumber = sourceObject.LineNumber;
  109. this.linePosition = sourceObject.LinePosition;
  110. this.sourceObj = sourceObject;
  111. this.sourceUri = sourceObject.SourceUri;
  112. #endif
  113. }
  114. public XmlSchemaException(string message, Exception innerException)
  115. : base (
  116. GetMessage (message, null, 0, 0, null),
  117. innerException )
  118. {
  119. }
  120. // Properties
  121. public int LineNumber
  122. {
  123. get{ return this.lineNumber;}
  124. }
  125. public int LinePosition
  126. {
  127. get{ return this.linePosition;}
  128. }
  129. public XmlSchemaObject SourceSchemaObject
  130. {
  131. get{ return this.sourceObj; }
  132. }
  133. public string SourceUri
  134. {
  135. get{ return this.sourceUri; }
  136. }
  137. private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
  138. {
  139. IXmlLineInfo li = sender as IXmlLineInfo;
  140. if (li == null)
  141. return GetMessage (message, sourceUri, 0, 0, sourceObj);
  142. else
  143. return GetMessage (message, sourceUri, li.LineNumber, li.LinePosition, sourceObj);
  144. }
  145. private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
  146. {
  147. string msg = message;
  148. if (lineNumber > 0)
  149. msg += String.Format (CultureInfo.InvariantCulture, " XML {0} Line {1}, Position {2}.",
  150. (sourceUri != null && sourceUri != "") ? "URI: " + sourceUri + " ." : "",
  151. lineNumber,
  152. linePosition);
  153. #if !MOBILE
  154. if (sourceObj != null)
  155. msg += String.Format (CultureInfo.InvariantCulture, " Related schema item SourceUri: {0}, Line {1}, Position {2}.",
  156. sourceObj.SourceUri, sourceObj.LineNumber, sourceObj.LinePosition);
  157. #endif
  158. return msg;
  159. }
  160. public override string Message {
  161. get { return base.Message; }
  162. }
  163. // Methods
  164. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  165. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  166. {
  167. base.GetObjectData (info, context);
  168. info.AddValue ("hasLineInfo", hasLineInfo);
  169. info.AddValue ("lineNumber", lineNumber);
  170. info.AddValue ("linePosition", linePosition);
  171. info.AddValue ("sourceUri", sourceUri);
  172. info.AddValue ("sourceObj", sourceObj);
  173. }
  174. }
  175. }