The Labs \ Source Viewer \ SSCLI \ System.Xml.Schema \ XmlSchemaException

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlSchemaException.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. // <owner current="true" primary="true">priyal</owner>
  15. //------------------------------------------------------------------------------
  16. namespace System.Xml.Schema
  17. {
  18.     using System;
  19.     using System.IO;
  20.     using System.Text;
  21.     using System.Resources;
  22.     using System.Runtime.Serialization;
  23.     using System.Globalization;
  24.     using System.Diagnostics;
  25.     using System.Security.Permissions;
  26.    
  27.     /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException"]/*' />
  28.     [Serializable()]
  29.     public class XmlSchemaException : SystemException
  30.     {
  31.         string res;
  32.         string[] args;
  33.         string sourceUri;
  34.         int lineNumber;
  35.         int linePosition;
  36.        
  37.         [NonSerialized()]
  38.         XmlSchemaObject sourceSchemaObject;
  39.        
  40.         string message;
  41.        
  42.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.XmlSchemaException5"]/*' />
  43.         protected XmlSchemaException(SerializationInfo info, StreamingContext context) : base(info, context)
  44.         {
  45.             res = (string)info.GetValue("res", typeof(string));
  46.             args = (string[])info.GetValue("args", typeof(string[]));
  47.             sourceUri = (string)info.GetValue("sourceUri", typeof(string));
  48.             lineNumber = (int)info.GetValue("lineNumber", typeof(int));
  49.             linePosition = (int)info.GetValue("linePosition", typeof(int));
  50.            
  51.             // deserialize optional members
  52.             string version = null;
  53.             foreach (SerializationEntry e in info) {
  54.                 if (e.Name == "version") {
  55.                     version = (string)e.Value;
  56.                 }
  57.             }
  58.            
  59.             if (version == null) {
  60.                 // deserializing V1 exception
  61.                 message = CreateMessage(res, args);
  62.             }
  63.             else {
  64.                 // deserializing V2 or higher exception -> exception message is serialized by the base class (Exception._message)
  65.                 message = null;
  66.             }
  67.         }
  68.        
  69.        
  70.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.GetObjectData"]/*' />
  71.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  72.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  73.         {
  74.             base.GetObjectData(info, context);
  75.             info.AddValue("res", res);
  76.             info.AddValue("args", args);
  77.             info.AddValue("sourceUri", sourceUri);
  78.             info.AddValue("lineNumber", lineNumber);
  79.             info.AddValue("linePosition", linePosition);
  80.             info.AddValue("version", "2.0");
  81.         }
  82.        
  83.        
  84.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.XmlSchemaException1"]/*' />
  85.         public XmlSchemaException() : this(null)
  86.         {
  87.         }
  88.        
  89.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.XmlSchemaException2"]/*' />
  90.         public XmlSchemaException(string message) : this(message, ((Exception)null), 0, 0)
  91.         {
  92.             #if DEBUG
  93.             Debug.Assert(message == null || !message.StartsWith("Sch_", StringComparison.Ordinal), "Do not pass a resource here!");
  94.             #endif
  95.         }
  96.        
  97.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.XmlSchemaException0"]/*' />
  98.         public XmlSchemaException(string message, Exception innerException) : this(message, innerException, 0, 0)
  99.         {
  100.         }
  101.        
  102.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.XmlSchemaException3"]/*' />
  103.         public XmlSchemaException(string message, Exception innerException, int lineNumber, int linePosition) : this((message == null ? Res.Sch_DefaultException : Res.Xml_UserException), new string[] {message}, innerException, null, lineNumber, linePosition, null)
  104.         {
  105.         }
  106.        
  107.         internal XmlSchemaException(string res, string[] args) : this(res, args, null, null, 0, 0, null)
  108.         {
  109.         }
  110.        
  111.         internal XmlSchemaException(string res, string arg) : this(res, new string[] {arg}, null, null, 0, 0, null)
  112.         {
  113.         }
  114.        
  115.         internal XmlSchemaException(string res, string arg, string sourceUri, int lineNumber, int linePosition) : this(res, new string[] {arg}, null, sourceUri, lineNumber, linePosition, null)
  116.         {
  117.         }
  118.        
  119.         internal XmlSchemaException(string res, string sourceUri, int lineNumber, int linePosition) : this(res, (string[])null, null, sourceUri, lineNumber, linePosition, null)
  120.         {
  121.         }
  122.        
  123.         internal XmlSchemaException(string res, string[] args, string sourceUri, int lineNumber, int linePosition) : this(res, args, null, sourceUri, lineNumber, linePosition, null)
  124.         {
  125.         }
  126.        
  127.         internal XmlSchemaException(string res, XmlSchemaObject source) : this(res, (string[])null, source)
  128.         {
  129.         }
  130.        
  131.         internal XmlSchemaException(string res, string arg, XmlSchemaObject source) : this(res, new string[] {arg}, source)
  132.         {
  133.         }
  134.        
  135.         internal XmlSchemaException(string res, string[] args, XmlSchemaObject source) : this(res, args, null, source.SourceUri, source.LineNumber, source.LinePosition, source)
  136.         {
  137.         }
  138.        
  139.         internal XmlSchemaException(string res, string[] args, Exception innerException, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject source) : base(CreateMessage(res, args), innerException)
  140.         {
  141.            
  142.             HResult = HResults.XmlSchema;
  143.             this.res = res;
  144.             this.args = args;
  145.             this.sourceUri = sourceUri;
  146.             this.lineNumber = lineNumber;
  147.             this.linePosition = linePosition;
  148.             this.sourceSchemaObject = source;
  149.         }
  150.        
  151.         static internal string CreateMessage(string res, string[] args)
  152.         {
  153.             try {
  154.                 return Res.GetString(res, args);
  155.             }
  156.             catch (MissingManifestResourceException) {
  157.                 return "UNKNOWN(" + res + ")";
  158.             }
  159.         }
  160.        
  161.         internal string GetRes {
  162.             get { return res; }
  163.         }
  164.        
  165.         internal string[] Args {
  166.             get { return args; }
  167.         }
  168.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.SourceUri"]/*' />
  169.         public string SourceUri {
  170.             get { return this.sourceUri; }
  171.         }
  172.        
  173.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.LineNumber"]/*' />
  174.         public int LineNumber {
  175.             get { return this.lineNumber; }
  176.         }
  177.        
  178.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.LinePosition"]/*' />
  179.         public int LinePosition {
  180.             get { return this.linePosition; }
  181.         }
  182.        
  183.         /// <include file='doc\XmlSchemaException.uex' path='docs/doc[@for="XmlSchemaException.SourceObject"]/*' />
  184.         public XmlSchemaObject SourceSchemaObject {
  185.             get { return this.sourceSchemaObject; }
  186.         }
  187.        
  188. /*
  189.             internal static XmlSchemaException Create(string res) { //Since internal overload with res string will clash with public constructor that takes in a message
  190.             return new XmlSchemaException(res, (string[])null, null, null, 0, 0, null);
  191.         }*/       
  192.        
  193.         internal void SetSource(string sourceUri, int lineNumber, int linePosition)
  194.         {
  195.             this.sourceUri = sourceUri;
  196.             this.lineNumber = lineNumber;
  197.             this.linePosition = linePosition;
  198.         }
  199.        
  200.         internal void SetSchemaObject(XmlSchemaObject source)
  201.         {
  202.             this.sourceSchemaObject = source;
  203.         }
  204.        
  205.         internal void SetSource(XmlSchemaObject source)
  206.         {
  207.             this.sourceSchemaObject = source;
  208.             this.sourceUri = source.SourceUri;
  209.             this.lineNumber = source.LineNumber;
  210.             this.linePosition = source.LinePosition;
  211.         }
  212.        
  213.         internal void SetResourceId(string resourceId)
  214.         {
  215.             this.res = resourceId;
  216.         }
  217.        
  218.         public override string Message {
  219.             get { return (message == null) ? base.Message : message; }
  220.         }
  221.     }
  222. }
  223. // namespace System.Xml.Schema

Developer Fusion