2010-04-06 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.XML / System.Xml.Schema / XmlSchemaException.cs
blobaa902796b82c6da2aec655a2db43f7acbf088a20
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.
30 using System.Globalization;
31 using System.Runtime.Serialization;
32 using System.Security.Permissions;
34 namespace System.Xml.Schema
36 /// <summary>
37 /// Summary description for XmlSchemaException.
38 /// </summary>
39 [Serializable]
40 public class XmlSchemaException : System.SystemException
42 //fields
43 private bool hasLineInfo;
44 private int lineNumber;
45 private int linePosition;
46 private XmlSchemaObject sourceObj;
47 private string sourceUri;
49 #if NET_2_0
50 public XmlSchemaException ()
51 : this ("A schema error occured.", null)
54 #endif
56 #if NET_2_0
57 public
58 #else
59 internal
60 #endif
61 XmlSchemaException (string message)
62 : this (message, null)
66 protected XmlSchemaException(SerializationInfo info, StreamingContext context)
67 : base (info, context)
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;
76 #if NET_2_0
77 public XmlSchemaException (string message, Exception innerException, int lineNumber, int linePosition)
78 : this (message, lineNumber, linePosition, null, null, innerException)
81 #endif
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)
89 hasLineInfo = true;
90 this.lineNumber = lineNumber;
91 this.linePosition = linePosition;
92 this.sourceObj = sourceObject;
93 this.sourceUri = sourceUri;
96 internal XmlSchemaException (string message, object sender,
97 string sourceUri, XmlSchemaObject sourceObject, Exception innerException)
98 : base (GetMessage (message, sourceUri, sender, sourceObject), innerException)
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;
106 this.sourceObj = sourceObject;
109 internal XmlSchemaException(string message, XmlSchemaObject sourceObject,
110 Exception innerException)
111 : base (
112 GetMessage (message, null, 0, 0, sourceObject),
113 innerException)
115 hasLineInfo = true;
116 #if !MONOTOUCH
117 this.lineNumber = sourceObject.LineNumber;
118 this.linePosition = sourceObject.LinePosition;
119 this.sourceObj = sourceObject;
120 this.sourceUri = sourceObject.SourceUri;
121 #endif
124 public XmlSchemaException(string message, Exception innerException)
125 : base (
126 GetMessage (message, null, 0, 0, null),
127 innerException )
131 // Properties
132 public int LineNumber
134 get{ return this.lineNumber;}
136 public int LinePosition
138 get{ return this.linePosition;}
140 public XmlSchemaObject SourceSchemaObject
142 get{ return this.sourceObj; }
144 public string SourceUri
146 get{ return this.sourceUri; }
149 private static string GetMessage (string message, string sourceUri, object sender, XmlSchemaObject sourceObj)
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);
158 private static string GetMessage (string message, string sourceUri, int lineNumber, int linePosition, XmlSchemaObject sourceObj)
160 string msg = "XmlSchema error: " + 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 !MONOTOUCH
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;
174 public override string Message {
175 get { return base.Message; }
178 // Methods
180 [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
181 public override void GetObjectData(SerializationInfo info, StreamingContext context)
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);