The Labs \ Source Viewer \ SSCLI \ System.Xml.Xsl \ XsltCompileException

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XsltException.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. //------------------------------------------------------------------------------
  15. using System.Globalization;
  16. using System.Resources;
  17. using System.Runtime.Serialization;
  18. using System.Security.Permissions;
  19. using System.Xml.XPath;
  20. namespace System.Xml.Xsl
  21. {
  22.     using Res = System.Xml.Utils.Res;
  23.    
  24.     [Serializable()]
  25.     public class XsltException : SystemException
  26.     {
  27.         string res;
  28.         string[] args;
  29.         string sourceUri;
  30.         int lineNumber;
  31.         int linePosition;
  32.        
  33.         string message;
  34.        
  35.         protected XsltException(SerializationInfo info, StreamingContext context) : base(info, context)
  36.         {
  37.             res = (string)info.GetValue("res", typeof(string));
  38.             args = (string[])info.GetValue("args", typeof(string[]));
  39.             sourceUri = (string)info.GetValue("sourceUri", typeof(string));
  40.             lineNumber = (int)info.GetValue("lineNumber", typeof(int));
  41.             linePosition = (int)info.GetValue("linePosition", typeof(int));
  42.            
  43.             // deserialize optional members
  44.             string version = null;
  45.             foreach (SerializationEntry e in info) {
  46.                 if (e.Name == "version") {
  47.                     version = (string)e.Value;
  48.                 }
  49.             }
  50.            
  51.             if (version == null) {
  52.                 // deserializing V1 exception
  53.                 message = CreateMessage(res, args, sourceUri, lineNumber, linePosition);
  54.             }
  55.             else {
  56.                 // deserializing V2 or higher exception -> exception message is serialized by the base class (Exception._message)
  57.                 message = null;
  58.             }
  59.         }
  60.        
  61.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  62.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  63.         {
  64.             base.GetObjectData(info, context);
  65.             info.AddValue("res", res);
  66.             info.AddValue("args", args);
  67.             info.AddValue("sourceUri", sourceUri);
  68.             info.AddValue("lineNumber", lineNumber);
  69.             info.AddValue("linePosition", linePosition);
  70.             info.AddValue("version", "2.0");
  71.         }
  72.        
  73.         public XsltException() : this(string.Empty, (Exception)null)
  74.         {
  75.         }
  76.        
  77.         public XsltException(string message) : this(message, (Exception)null)
  78.         {
  79.         }
  80.        
  81.         public XsltException(string message, Exception innerException) : this(Res.Xml_UserException, new string[] {message}, null, 0, 0, innerException)
  82.         {
  83.         }
  84.        
  85.         static internal XsltException Create(string res, params string[] args)
  86.         {
  87.             return new XsltException(res, args, null, 0, 0, null);
  88.         }
  89.        
  90.         static internal XsltException Create(string res, string[] args, Exception inner)
  91.         {
  92.             return new XsltException(res, args, null, 0, 0, inner);
  93.         }
  94.        
  95.         internal XsltException(string res, string[] args, string sourceUri, int lineNumber, int linePosition, Exception inner) : base(CreateMessage(res, args, sourceUri, lineNumber, linePosition), inner)
  96.         {
  97.             HResult = HResults.XmlXslt;
  98.             this.res = res;
  99.             this.sourceUri = sourceUri;
  100.             this.lineNumber = lineNumber;
  101.             this.linePosition = linePosition;
  102.         }
  103.        
  104.         public virtual string SourceUri {
  105.             get { return this.sourceUri; }
  106.         }
  107.        
  108.         public virtual int LineNumber {
  109.             get { return this.lineNumber; }
  110.         }
  111.        
  112.         public virtual int LinePosition {
  113.             get { return this.linePosition; }
  114.         }
  115.        
  116.         public override string Message {
  117.             get { return (message == null) ? base.Message : message; }
  118.         }
  119.        
  120.         private static string CreateMessage(string res, string[] args, string sourceUri, int lineNumber, int linePosition)
  121.         {
  122.             try {
  123.                 string message = FormatMessage(res, args);
  124.                 if (res != Res.Xslt_CompileError && lineNumber != 0) {
  125.                     message += " " + FormatMessage(Res.Xml_ErrorFilePosition, sourceUri, lineNumber.ToString(CultureInfo.InvariantCulture), linePosition.ToString(CultureInfo.InvariantCulture));
  126.                 }
  127.                 return message;
  128.             }
  129.             catch (MissingManifestResourceException) {
  130.                 return "UNKNOWN(" + res + ")";
  131.             }
  132.         }
  133.        
  134.         private static string FormatMessage(string key, params string[] args)
  135.         {
  136.             string message = Res.GetString(key);
  137.             if (message != null && args != null) {
  138.                 message = string.Format(CultureInfo.InvariantCulture, message, args);
  139.             }
  140.             return message;
  141.         }
  142.     }
  143.    
  144.     [Serializable()]
  145.     public class XsltCompileException : XsltException
  146.     {
  147.        
  148.         protected XsltCompileException(SerializationInfo info, StreamingContext context) : base(info, context)
  149.         {
  150.         }
  151.        
  152.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  153.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  154.         {
  155.             base.GetObjectData(info, context);
  156.         }
  157.        
  158.         public XsltCompileException() : base()
  159.         {
  160.         }
  161.        
  162.         public XsltCompileException(string message) : base(message)
  163.         {
  164.         }
  165.        
  166.         public XsltCompileException(string message, Exception innerException) : base(message, innerException)
  167.         {
  168.         }
  169.        
  170.         public XsltCompileException(Exception inner, string sourceUri, int lineNumber, int linePosition) : base(lineNumber != 0 ? Res.Xslt_CompileError : Res.Xslt_CompileError2, new string[] {sourceUri, lineNumber.ToString(CultureInfo.InvariantCulture), linePosition.ToString(CultureInfo.InvariantCulture)}, sourceUri, lineNumber, linePosition, inner)
  171.         {
  172.         }
  173.     }
  174. }

Developer Fusion