XmlSchemaException.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. 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. public XmlSchemaException (string message)
  52. : this (message, null)
  53. {
  54. }
  55. #endif
  56. protected XmlSchemaException(SerializationInfo info, StreamingContext context)
  57. : base (info, context)
  58. {
  59. this.hasLineInfo = info.GetBoolean ("hasLineInfo");
  60. this.lineNumber = info.GetInt32 ("lineNumber");
  61. this.linePosition = info.GetInt32 ("linePosition");
  62. this.sourceUri = info.GetString ("sourceUri");
  63. this.sourceObj = info.GetValue ("sourceObj", typeof (XmlSchemaObject)) as XmlSchemaObject;
  64. }
  65. #if NET_2_0
  66. public XmlSchemaException (string message, Exception innerException, int lineNumber, int linePosition)
  67. : this (message, lineNumber, linePosition, null, null, innerException)
  68. {
  69. }
  70. #endif
  71. internal XmlSchemaException (string message, int lineNumber, int linePosition,
  72. XmlSchemaObject sourceObject, string sourceUri, Exception innerException)
  73. : base (
  74. GetMessage (message, sourceUri, lineNumber, linePosition, sourceObject),
  75. innerException)
  76. {
  77. hasLineInfo = true;
  78. this.lineNumber = lineNumber;
  79. this.linePosition = linePosition;
  80. this.sourceObj = sourceObject;
  81. this.sourceUri = sourceUri;
  82. }
  83. internal XmlSchemaException (string message, object sender,
  84. string sourceUri, XmlSchemaObject sourceObject, Exception innerException)
  85. : base (GetMessage (message, sourceUri, sender, sourceObject), innerException)
  86. {
  87. IXmlLineInfo li = sender as IXmlLineInfo;
  88. if (li != null && li.HasLineInfo ()) {
  89. hasLineInfo = true;
  90. this.lineNumber = li.LineNumber;
  91. this.linePosition = li.LinePosition;
  92. }
  93. this.sourceObj = sourceObject;
  94. }
  95. internal XmlSchemaException(string message, XmlSchemaObject sourceObject,
  96. Exception innerException)
  97. : base (
  98. GetMessage (message, null, 0, 0, sourceObject),
  99. innerException)
  100. {
  101. hasLineInfo = true;
  102. this.lineNumber = sourceObject.LineNumber;
  103. this.linePosition = sourceObject.LinePosition;
  104. this.sourceObj = sourceObject;
  105. this.sourceUri = sourceObject.SourceUri;
  106. }
  107. public XmlSchemaException(string message, Exception innerException)
  108. : base (
  109. GetMessage (message, null, 0, 0, null),
  110. innerException )
  111. {
  112. }
  113. // Properties
  114. public int LineNumber
  115. {
  116. get{ return this.lineNumber;}
  117. }
  118. public int LinePosition
  119. {
  120. get{ return this.linePosition;}
  121. }
  122. public XmlSchemaObject SourceSchemaObject
  123. {
  124. get{ return this.sourceObj; }
  125. }
  126. public string SourceUri
  127. {
  128. get{ return this.sourceUri; }
  129. }
  130. private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
  131. {
  132. IXmlLineInfo li = sender as IXmlLineInfo;
  133. if (li == null)
  134. return GetMessage (message, sourceUri, 0, 0, sourceObj);
  135. else
  136. return GetMessage (message, sourceUri, li.LineNumber, li.LinePosition, sourceObj);
  137. }
  138. private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
  139. {
  140. string msg = "XmlSchema error: " + message;
  141. if (lineNumber > 0)
  142. msg += String.Format (CultureInfo.InvariantCulture, " XML {0} Line {1}, Position {2}.",
  143. (sourceUri != null && sourceUri != "") ? "URI: " + sourceUri + " ." : "",
  144. lineNumber,
  145. linePosition);
  146. if (sourceObj != null)
  147. msg += String.Format (CultureInfo.InvariantCulture, " Related schema item SourceUri: {0}, Line {1}, Position {2}.",
  148. sourceObj.SourceUri, sourceObj.LineNumber, sourceObj.LinePosition);
  149. return msg;
  150. }
  151. #if NET_2_0
  152. #else
  153. public override string Message {
  154. get { return base.Message; }
  155. }
  156. #endif
  157. // Methods
  158. #if NET_2_0
  159. [SecurityPermission (SecurityAction.LinkDemand,
  160. Flags=SecurityPermissionFlag.SerializationFormatter)]
  161. #endif
  162. public override void GetObjectData(SerializationInfo info, StreamingContext context)
  163. {
  164. base.GetObjectData (info, context);
  165. info.AddValue ("hasLineInfo", hasLineInfo);
  166. info.AddValue ("lineNumber", lineNumber);
  167. info.AddValue ("linePosition", linePosition);
  168. info.AddValue ("sourceUri", sourceUri);
  169. info.AddValue ("sourceObj", sourceObj);
  170. }
  171. }
  172. }