Merge pull request #922 from ermshiperete/novell-bug-599689
[mono.git] / mcs / class / System.XML / System.Xml.Schema / XmlSchemaException.cs
1 //
2 // XmlSchemaException.cs
3 //
4 // Author:
5 //      Dwivedi, Ajay kumar Adwiv@Yahoo.com
6 //      Enomoto, Atsushi atsushi@ximian.com
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using System.Globalization;
31 using System.Runtime.Serialization;
32 using System.Security.Permissions;
33
34 namespace System.Xml.Schema
35 {
36         /// <summary>
37         /// Summary description for XmlSchemaException.
38         /// </summary>
39         [Serializable]
40         public class XmlSchemaException : System.SystemException
41         {
42                 //fields
43                 private bool hasLineInfo;
44                 private int lineNumber;
45                 private int linePosition;
46                 private XmlSchemaObject sourceObj;
47                 private string sourceUri;
48
49 #if NET_2_0
50                 public XmlSchemaException ()
51                         : this ("A schema error occured.", null)
52                 {
53                 }
54 #endif
55
56 #if NET_2_0
57                 public
58 #else
59                 internal
60 #endif
61                 XmlSchemaException (string message)
62                         : this (message, null)
63                 {
64                 }
65
66                 protected XmlSchemaException(SerializationInfo info, StreamingContext context)
67                         : base (info, context)
68                 {
69                         this.hasLineInfo = info.GetBoolean ("hasLineInfo");
70                         this.lineNumber = info.GetInt32 ("lineNumber");
71                         this.linePosition = info.GetInt32 ("linePosition");
72                         this.sourceUri = info.GetString ("sourceUri");
73                         this.sourceObj = info.GetValue ("sourceObj", typeof (XmlSchemaObject)) as XmlSchemaObject;
74                 }
75
76 #if NET_2_0
77                 public XmlSchemaException (string message, Exception innerException, int lineNumber, int linePosition)
78                         : this (message, lineNumber, linePosition, null, null, innerException)
79                 {
80                 }
81 #endif
82
83                 internal XmlSchemaException (string message, int lineNumber, int linePosition,
84                         XmlSchemaObject sourceObject, string sourceUri, Exception innerException)
85                         : base (
86                                 GetMessage (message, sourceUri, lineNumber, linePosition, sourceObject),
87                                 innerException)
88                 {
89                         hasLineInfo = true;
90                         this.lineNumber         = lineNumber;
91                         this.linePosition       = linePosition;
92                         this.sourceObj          = sourceObject;
93                         this.sourceUri          = sourceUri;
94                 }
95
96                 internal XmlSchemaException (string message, object sender,
97                         string sourceUri, XmlSchemaObject sourceObject, Exception innerException)
98                         : base (GetMessage (message, sourceUri, sender, sourceObject), innerException)
99                 {
100                         IXmlLineInfo li = sender as IXmlLineInfo;
101                         if (li != null && li.HasLineInfo ()) {
102                                 hasLineInfo = true;
103                                 this.lineNumber = li.LineNumber;
104                                 this.linePosition = li.LinePosition;
105                         }
106                         this.sourceObj = sourceObject;
107                 }
108
109                 internal XmlSchemaException(string message, XmlSchemaObject sourceObject,
110                         Exception innerException)
111                         : base (
112                                 GetMessage (message, null, 0, 0, sourceObject),
113                                 innerException)
114                 {
115                         hasLineInfo = true;
116 #if !MOBILE
117                         this.lineNumber = sourceObject.LineNumber;
118                         this.linePosition = sourceObject.LinePosition;
119                         this.sourceObj  =       sourceObject;
120                         this.sourceUri  =       sourceObject.SourceUri;
121 #endif
122                 }
123
124                 public XmlSchemaException(string message, Exception innerException)
125                         : base (
126                                 GetMessage (message, null, 0, 0, null),
127                                 innerException )
128                 {
129                 }
130
131                 // Properties
132                 public int LineNumber
133                 { 
134                         get{ return this.lineNumber;} 
135                 }
136                 public int LinePosition 
137                 { 
138                         get{ return this.linePosition;} 
139                 }
140                 public XmlSchemaObject SourceSchemaObject 
141                 {
142                         get{ return this.sourceObj; } 
143                 }
144                 public string SourceUri 
145                 { 
146                         get{ return this.sourceUri; } 
147                 }
148
149                 private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
150                 {
151                         IXmlLineInfo li = sender as IXmlLineInfo;
152                         if (li == null)
153                                 return GetMessage (message, sourceUri, 0, 0, sourceObj);
154                         else
155                                 return GetMessage (message, sourceUri, li.LineNumber, li.LinePosition, sourceObj);
156                 }
157
158                 private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
159                 {
160                         string msg = message;
161                         if (lineNumber > 0)
162                                 msg += String.Format (CultureInfo.InvariantCulture, " XML {0} Line {1}, Position {2}.",
163                                         (sourceUri != null && sourceUri != "") ? "URI: " + sourceUri + " ." : "",
164                                         lineNumber,
165                                         linePosition);
166 #if !MOBILE
167                         if (sourceObj != null)
168                                 msg += String.Format (CultureInfo.InvariantCulture, " Related schema item SourceUri: {0}, Line {1}, Position {2}.",
169                                         sourceObj.SourceUri, sourceObj.LineNumber, sourceObj.LinePosition);
170 #endif
171                         return msg;
172                 }
173
174                 public override string Message {
175                         get { return base.Message; }
176                 }
177
178                 // Methods
179
180                 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
181                 public override void GetObjectData(SerializationInfo info, StreamingContext context)
182                 {
183                         base.GetObjectData (info, context);
184                         info.AddValue ("hasLineInfo", hasLineInfo);
185                         info.AddValue ("lineNumber", lineNumber);
186                         info.AddValue ("linePosition", linePosition);
187                         info.AddValue ("sourceUri", sourceUri);
188                         info.AddValue ("sourceObj", sourceObj);
189                 }
190         }
191 }