XmlSchemaException.cs 5.5 KB

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