merge -r 61110:61111
[mono.git] / mcs / class / Commons.Xml.Relaxng / Commons.Xml.Nvdl / NvdlException.cs
1 using System;
2 using System.Xml;
3
4 namespace Commons.Xml.Nvdl
5 {
6         public class NvdlException : Exception
7         {
8                 public NvdlException (string message)
9                         : base (message)
10                 {
11                 }
12
13                 public NvdlException (string message, Exception inner)
14                         : base (message ,inner)
15                 {
16                 }
17
18                 internal static string FormatMessage (string message,
19                         IXmlLineInfo lineInfo)
20                 {
21                         NvdlElementBase source = lineInfo as NvdlElementBase;
22                         XmlReader reader = lineInfo as XmlReader;
23                         if (source != null && source.HasLineInfo ())
24                                 return String.Format ("{0}. {1} ({2},{3})",
25                                         message, source.SourceUri,
26                                         source.LineNumber, source.LinePosition);
27                         else if (lineInfo != null && lineInfo.HasLineInfo ())
28                                 return String.Format ("{0}. {3}({1},{2})",
29                                         message,
30                                         lineInfo.LineNumber,
31                                         lineInfo.LinePosition,
32                                         reader != null ? reader.BaseURI + ' ' : String.Empty);
33                         else
34                                 return message;
35                 }
36         }
37
38         public class NvdlCompileException : NvdlException
39         {
40                 public NvdlCompileException (string message,
41                         IXmlLineInfo source)
42                         : this (message, null, source)
43                 {
44                 }
45
46                 public NvdlCompileException (string message, Exception inner,
47                         IXmlLineInfo source)
48                         : base (FormatMessage (message, source), inner)
49                 {
50                 }
51         }
52
53         public class NvdlValidationException : NvdlException
54         {
55                 public NvdlValidationException (string message,
56                         IXmlLineInfo source)
57                         : this (message, null, source)
58                 {
59                 }
60
61                 public NvdlValidationException (string message, Exception inner,
62                         IXmlLineInfo source)
63                         : base (FormatMessage (message, source), inner)
64                 {
65                 }
66         }
67
68         public class NvdlInstanceValidationException : NvdlException
69         {
70                 public NvdlInstanceValidationException (string message,
71                         NvdlValidatorGenerator generator,
72                         string nvdlLocation)
73                         : this (message, null, generator, nvdlLocation)
74                 {
75                 }
76
77                 public NvdlInstanceValidationException (string message, Exception inner,
78                         NvdlValidatorGenerator generator,
79                         string nvdlLocation)
80                         : base (FormatMessageWithDefinition (message, nvdlLocation), inner)
81                 {
82                 }
83
84                 // assuming that wrapped exception message usually 
85                 // contains the actual instance location info.
86                 static string FormatMessageWithDefinition (string message, string nvdlLocation)
87                 {
88                         return String.Format ("{0}. Related NVDL script: {1}", message, nvdlLocation);
89                 }
90         }
91 }
92