XmlSchemaException.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // XmlSchemaException.cs
  3. //
  4. // Author:
  5. // Dwivedi, Ajay kumar [email protected]
  6. // Enomoto, Atsushi [email protected]
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Globalization;
  30. using System.Runtime.Serialization;
  31. namespace System.Xml.Schema
  32. {
  33. /// <summary>
  34. /// Summary description for XmlSchemaException.
  35. /// </summary>
  36. [Serializable]
  37. public class XmlSchemaException : System.SystemException
  38. {
  39. //fields
  40. private bool hasLineInfo;
  41. private int lineNumber;
  42. private int linePosition;
  43. private XmlSchemaObject sourceObj;
  44. private string sourceUri;
  45. #if NET_2_0
  46. public XmlSchemaException ()
  47. : this ("A schema error occured.", null)
  48. {
  49. }
  50. public XmlSchemaException (string message)
  51. : this (message, null)
  52. {
  53. }
  54. #endif
  55. protected XmlSchemaException(SerializationInfo info, StreamingContext context)
  56. : base (info, context)
  57. {
  58. hasLineInfo = true;
  59. this.lineNumber = info.GetInt32 ("lineNumber");
  60. this.linePosition = info.GetInt32 ("linePosition");
  61. this.sourceUri = info.GetString ("sourceUri");
  62. this.sourceObj = info.GetValue ("sourceObj", typeof (XmlSchemaObject)) as XmlSchemaObject;
  63. }
  64. #if NET_2_0
  65. public XmlSchemaException (string message, Exception innerException, int lineNumber, int linePosition)
  66. : this (message, lineNumber, linePosition, null, null, innerException)
  67. {
  68. }
  69. #endif
  70. internal XmlSchemaException (string message, int lineNumber, int linePosition,
  71. XmlSchemaObject sourceObject, string sourceUri, Exception innerException)
  72. : base (
  73. GetMessage (message, sourceUri, lineNumber, linePosition, sourceObject),
  74. innerException)
  75. {
  76. hasLineInfo = true;
  77. this.lineNumber = lineNumber;
  78. this.linePosition = linePosition;
  79. this.sourceObj = sourceObject;
  80. this.sourceUri = sourceUri;
  81. }
  82. internal XmlSchemaException (string message, object sender,
  83. string sourceUri, XmlSchemaObject sourceObject, Exception innerException)
  84. : base (GetMessage (message, sourceUri, sender, sourceObject), innerException)
  85. {
  86. IXmlLineInfo li = sender as IXmlLineInfo;
  87. if (li != null && li.HasLineInfo ()) {
  88. hasLineInfo = true;
  89. this.lineNumber = li.LineNumber;
  90. this.linePosition = li.LinePosition;
  91. }
  92. this.sourceObj = sourceObject;
  93. }
  94. internal XmlSchemaException(string message, XmlSchemaObject sourceObject,
  95. Exception innerException)
  96. : base (
  97. GetMessage (message, null, 0, 0, sourceObject),
  98. innerException)
  99. {
  100. hasLineInfo = true;
  101. this.lineNumber = sourceObject.LineNumber;
  102. this.linePosition = sourceObject.LinePosition;
  103. this.sourceObj = sourceObject;
  104. this.sourceUri = sourceObject.SourceUri;
  105. }
  106. public XmlSchemaException(string message, Exception innerException)
  107. : base (
  108. GetMessage (message, null, 0, 0, null),
  109. innerException )
  110. {
  111. }
  112. // Properties
  113. public int LineNumber
  114. {
  115. get{ return this.lineNumber;}
  116. }
  117. public int LinePosition
  118. {
  119. get{ return this.linePosition;}
  120. }
  121. public XmlSchemaObject SourceSchemaObject
  122. {
  123. get{ return this.sourceObj; }
  124. }
  125. public string SourceUri
  126. {
  127. get{ return this.sourceUri; }
  128. }
  129. private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
  130. {
  131. IXmlLineInfo li = sender as IXmlLineInfo;
  132. if (li == null)
  133. return GetMessage (message, sourceUri, 0, 0, sourceObj);
  134. else
  135. return GetMessage (message, sourceUri, li.LineNumber, li.LinePosition, sourceObj);
  136. }
  137. private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
  138. {
  139. string msg = "XmlSchema error: " + message;
  140. if (lineNumber > 0)
  141. msg += String.Format (CultureInfo.InvariantCulture, " XML {0} Line {1}, Position {2}.",
  142. (sourceUri != null && sourceUri != "") ? "URI: " + sourceUri + " ." : "",
  143. lineNumber,
  144. linePosition);
  145. if (sourceObj != null)
  146. msg += String.Format (CultureInfo.InvariantCulture, " Related schema item SourceUri: {0}, Line {1}, Position {2}.",
  147. sourceObj.SourceUri, sourceObj.LineNumber, sourceObj.LinePosition);
  148. return msg;
  149. }
  150. #if NET_2_0
  151. #else
  152. public override string Message {
  153. get { return base.Message; }
  154. }
  155. #endif
  156. // Methods
  157. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  158. {
  159. base.GetObjectData (info, context);
  160. info.AddValue ("lineNumber", lineNumber);
  161. info.AddValue ("linePosition", linePosition);
  162. info.AddValue ("sourceUri", sourceUri);
  163. info.AddValue ("sourceObj", sourceObj);
  164. }
  165. }
  166. }