BindingFlags.Public needed here as Exception.HResult is now public in .NET 4.5. This...
[mono.git] / mcs / class / System.XML / System.Xml.Xsl / XsltException.cs
1 //\r
2 // System.Xml.Xsl.XsltException.cs\r
3 //\r
4 // Authors:\r
5 //  Tim Coleman (tim@timcoleman.com)\r
6 //  Andreas Nahr (ClassDevelopment@A-SoftTech.com)\r
7 //\r
8 // (C) Copyright 2002 Tim Coleman\r
9 // (C) 2003 Andreas Nahr\r
10 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)\r
11 //\r
12 // Permission is hereby granted, free of charge, to any person obtaining\r
13 // a copy of this software and associated documentation files (the\r
14 // "Software"), to deal in the Software without restriction, including\r
15 // without limitation the rights to use, copy, modify, merge, publish,\r
16 // distribute, sublicense, and/or sell copies of the Software, and to\r
17 // permit persons to whom the Software is furnished to do so, subject to\r
18 // the following conditions:\r
19 // \r
20 // The above copyright notice and this permission notice shall be\r
21 // included in all copies or substantial portions of the Software.\r
22 // \r
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
30 //\r
31 \r
32 using System.Globalization;\r
33 using System.Runtime.Serialization;\r
34 using System.Xml.XPath;\r
35 using System.Security.Permissions;\r
36 \r
37 namespace System.Xml.Xsl\r
38 {\r
39         [Serializable]\r
40         public class XsltException : SystemException\r
41         {\r
42                 static string CreateMessage (string message, XPathNavigator nav)\r
43                 {\r
44                         IXmlLineInfo li = nav as IXmlLineInfo;\r
45                         int lineNumber = li != null ? li.LineNumber : 0;\r
46                         int linePosition = li != null ? li.LinePosition : 0;\r
47                         string sourceUri = nav != null ? nav.BaseURI : string.Empty;\r
48 \r
49                         if (lineNumber != 0) {\r
50                                 return CreateMessage ("{0} at {1}({2},{3}).", message, lineNumber, linePosition, sourceUri);\r
51                         }\r
52                         return CreateMessage ("{0}.", message, lineNumber, linePosition, sourceUri);\r
53                 }\r
54 \r
55                 static string CreateMessage (string msgFormat, string message, int lineNumber, int linePosition, string sourceUri)\r
56                 {\r
57                         return string.Format (CultureInfo.InvariantCulture, msgFormat,\r
58                                 message, sourceUri, lineNumber.ToString (CultureInfo.InvariantCulture),\r
59                                 linePosition.ToString (CultureInfo.InvariantCulture));\r
60                 }\r
61 \r
62                 #region Fields\r
63 \r
64                 int lineNumber;\r
65                 int linePosition;\r
66                 string sourceUri;\r
67                 string templateFrames;\r
68 \r
69                 #endregion\r
70 \r
71                 #region Constructors\r
72 \r
73 #if NET_2_0\r
74                 public XsltException ()\r
75                         : this (string.Empty, (Exception) null)\r
76                 {\r
77                 }\r
78 \r
79                 public XsltException (string message)\r
80                         : this (message, (Exception) null)\r
81                 {\r
82                 }\r
83 #endif\r
84 \r
85                 public XsltException (string message, Exception innerException)\r
86                         : this ("{0}", message, innerException, 0, 0, (string) null)\r
87                 {\r
88                 }\r
89 \r
90                 protected XsltException (SerializationInfo info, StreamingContext context)\r
91                 {\r
92                         lineNumber = info.GetInt32 ("lineNumber");\r
93                         linePosition = info.GetInt32 ("linePosition");\r
94                         sourceUri = info.GetString ("sourceUri");\r
95                         templateFrames = info.GetString ("templateFrames");\r
96                 }\r
97 \r
98                 internal XsltException (string msgFormat, string message, Exception innerException, int lineNumber, int linePosition, string sourceUri)\r
99                         : base (CreateMessage (msgFormat, message, lineNumber, linePosition, sourceUri), innerException)\r
100                 {\r
101                         this.lineNumber = lineNumber;\r
102                         this.linePosition = linePosition;\r
103                         this.sourceUri = sourceUri;\r
104                 }\r
105 \r
106                 internal XsltException (string message, Exception innerException, XPathNavigator nav)\r
107                         : base (CreateMessage (message, nav), innerException)\r
108                 {\r
109                         IXmlLineInfo li = nav as IXmlLineInfo;\r
110                         this.lineNumber = li != null ? li.LineNumber : 0;\r
111                         this.linePosition = li != null ? li.LinePosition : 0;\r
112                         this.sourceUri = nav != null ? nav.BaseURI : string.Empty;\r
113                 }\r
114 \r
115                 #endregion\r
116 \r
117                 #region Properties\r
118 \r
119 #if NET_2_0\r
120                 public virtual int LineNumber {\r
121 #else\r
122                 public int LineNumber {\r
123 #endif\r
124                         get { return lineNumber; }\r
125                 }\r
126 \r
127 #if NET_2_0\r
128                 public virtual int LinePosition {\r
129 #else\r
130                 public int LinePosition {\r
131 #endif\r
132                         get { return linePosition; }\r
133                 }\r
134 \r
135                 public override string Message {\r
136                         get {\r
137                                 return templateFrames != null ? base.Message + templateFrames : base.Message;\r
138                         }\r
139                 }\r
140 \r
141 #if NET_2_0\r
142                 public virtual string SourceUri {\r
143 #else\r
144                 public string SourceUri {\r
145 #endif\r
146                         get { return sourceUri; }\r
147                 }\r
148 \r
149                 #endregion\r
150 \r
151                 #region Methods\r
152 \r
153                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]\r
154                 public override void GetObjectData (SerializationInfo info, StreamingContext context)\r
155                 {\r
156                         base.GetObjectData (info, context);\r
157                         info.AddValue ("lineNumber", lineNumber);\r
158                         info.AddValue ("linePosition", linePosition);\r
159                         info.AddValue ("sourceUri", sourceUri);\r
160                         info.AddValue ("templateFrames", templateFrames);\r
161                 }\r
162 \r
163                 internal void AddTemplateFrame (string frame)\r
164                 {\r
165                         templateFrames += frame;\r
166                 }\r
167 \r
168                 #endregion\r
169         }\r
170 }\r