Update Reference Sources to .NET Framework 4.6.1
[mono.git] / mcs / class / referencesource / System.Data.SqlXml / System / Xml / Xsl / XPath / XPathCompileException.cs
1 //------------------------------------------------------------------------------
2 // <copyright file="XPathCompileException.cs" company="Microsoft">
3 //     Copyright (c) Microsoft Corporation.  All rights reserved.
4 // </copyright>
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
7
8 using System.Runtime.Serialization;
9 using System.Security.Permissions;
10 using System.Text;
11
12 namespace System.Xml.Xsl.XPath {
13     [Serializable]
14     internal class XPathCompileException : XslLoadException {
15         public string  queryString;
16         public int     startChar;
17         public int     endChar;
18
19         protected XPathCompileException(SerializationInfo info, StreamingContext context)
20             : base(info, context)
21         {
22             queryString = (string)info.GetValue("QueryString", typeof(string));
23             startChar   = (int)   info.GetValue("StartChar"  , typeof(int   ));
24             endChar     = (int)   info.GetValue("EndChar"    , typeof(int   ));
25         }
26
27         [SecurityPermissionAttribute(SecurityAction.LinkDemand, SerializationFormatter = true)]
28         public override void GetObjectData(SerializationInfo info, StreamingContext context) {
29             base.GetObjectData(info, context);
30             info.AddValue("QueryString", queryString);
31             info.AddValue("StartChar"  , startChar);
32             info.AddValue("EndChar"    , endChar);
33         }
34
35         internal XPathCompileException(string queryString, int startChar, int endChar, string resId, params string[] args)
36             : base(resId, args)
37         {
38             this.queryString = queryString;
39             this.startChar   = startChar;
40             this.endChar     = endChar;
41         }
42
43         internal XPathCompileException(string resId, params string[] args)
44             : base(resId, args) {} // queryString will be set later
45
46         private enum TrimType {
47             Left,
48             Right,
49             Middle,
50         }
51
52         // This function is used to prevent long quotations in error messages, SQLBUDT 222626
53         private static void AppendTrimmed(StringBuilder sb, string value, int startIndex, int count, TrimType trimType) {
54             const int    TrimSize   = 32;
55             const string TrimMarker = "...";
56
57             if (count <= TrimSize) {
58                 sb.Append(value, startIndex, count);
59             } else {
60                 switch (trimType) {
61                 case TrimType.Left:
62                     sb.Append(TrimMarker);
63                     sb.Append(value, startIndex + count - TrimSize, TrimSize);
64                     break;
65                 case TrimType.Right:
66                     sb.Append(value, startIndex, TrimSize);
67                     sb.Append(TrimMarker);
68                     break;
69                 case TrimType.Middle:
70                     sb.Append(value, startIndex, TrimSize / 2);
71                     sb.Append(TrimMarker);
72                     sb.Append(value, startIndex + count - TrimSize / 2, TrimSize / 2);
73                     break;
74                 }
75             }
76         }
77
78         internal string MarkOutError() {
79             if (queryString == null || queryString.Trim(' ').Length == 0) {
80                 return null;
81             }
82
83             int len = endChar - startChar;
84             StringBuilder sb = new StringBuilder();
85
86             AppendTrimmed(sb, queryString, 0, startChar, TrimType.Left);
87             if (len > 0) {
88                 sb.Append(" -->");
89                 AppendTrimmed(sb, queryString, startChar, len, TrimType.Middle);
90             }
91
92             sb.Append("<-- ");
93             AppendTrimmed(sb, queryString, endChar, queryString.Length - endChar, TrimType.Right);
94
95             return sb.ToString();
96         }
97
98         internal override string FormatDetailedMessage() {
99             string message = Message;
100             string error = MarkOutError();
101
102             if (error != null && error.Length > 0) {
103                 if (message.Length > 0) {
104                     message += Environment.NewLine;
105                 }
106                 message += error;
107             }
108             return message;
109         }
110     }
111 }