XmlSchemaException.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. this.lineNumber = sourceObject.LineNumber;
  108. this.linePosition = sourceObject.LinePosition;
  109. this.sourceObj = sourceObject;
  110. this.sourceUri = sourceObject.SourceUri;
  111. }
  112. public XmlSchemaException(string message, Exception innerException)
  113. : base (
  114. GetMessage (message, null, 0, 0, null),
  115. innerException )
  116. {
  117. }
  118. // Properties
  119. public int LineNumber
  120. {
  121. get{ return this.lineNumber;}
  122. }
  123. public int LinePosition
  124. {
  125. get{ return this.linePosition;}
  126. }
  127. public XmlSchemaObject SourceSchemaObject
  128. {
  129. get{ return this.sourceObj; }
  130. }
  131. public string SourceUri
  132. {
  133. get{ return this.sourceUri; }
  134. }
  135. private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
  136. {
  137. IXmlLineInfo li = sender as IXmlLineInfo;
  138. if (li == null)
  139. return GetMessage (message, sourceUri, 0, 0, sourceObj);
  140. else
  141. return GetMessage (message, sourceUri, li.LineNumber, li.LinePosition, sourceObj);
  142. }
  143. private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
  144. {
  145. string msg = "XmlSchema error: " + message;
  146. if (lineNumber > 0)
  147. msg += String.Format (CultureInfo.InvariantCulture, " XML {0} Line {1}, Position {2}.",
  148. (sourceUri != null && sourceUri != "") ? "URI: " + sourceUri + " ." : "",
  149. lineNumber,
  150. linePosition);
  151. if (sourceObj != null)
  152. msg += String.Format (CultureInfo.InvariantCulture, " Related schema item SourceUri: {0}, Line {1}, Position {2}.",
  153. sourceObj.SourceUri, sourceObj.LineNumber, sourceObj.LinePosition);
  154. return msg;
  155. }
  156. public override string Message {
  157. get { return base.Message; }
  158. }
  159. // Methods
  160. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  161. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  162. {
  163. base.GetObjectData (info, context);
  164. info.AddValue ("hasLineInfo", hasLineInfo);
  165. info.AddValue ("lineNumber", lineNumber);
  166. info.AddValue ("linePosition", linePosition);
  167. info.AddValue ("sourceUri", sourceUri);
  168. info.AddValue ("sourceObj", sourceObj);
  169. }
  170. }
  171. }