1 //------------------------------------------------------------------------------
2 // <copyright file="XPathCompileException.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">Microsoft</owner>
6 //------------------------------------------------------------------------------
8 using System.Runtime.Serialization;
9 using System.Security.Permissions;
12 namespace System.Xml.Xsl.XPath {
14 internal class XPathCompileException : XslLoadException {
15 public string queryString;
19 protected XPathCompileException(SerializationInfo info, StreamingContext context)
22 queryString = (string)info.GetValue("QueryString", typeof(string));
23 startChar = (int) info.GetValue("StartChar" , typeof(int ));
24 endChar = (int) info.GetValue("EndChar" , typeof(int ));
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);
35 internal XPathCompileException(string queryString, int startChar, int endChar, string resId, params string[] args)
38 this.queryString = queryString;
39 this.startChar = startChar;
40 this.endChar = endChar;
43 internal XPathCompileException(string resId, params string[] args)
44 : base(resId, args) {} // queryString will be set later
46 private enum TrimType {
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 = "...";
57 if (count <= TrimSize) {
58 sb.Append(value, startIndex, count);
62 sb.Append(TrimMarker);
63 sb.Append(value, startIndex + count - TrimSize, TrimSize);
66 sb.Append(value, startIndex, TrimSize);
67 sb.Append(TrimMarker);
70 sb.Append(value, startIndex, TrimSize / 2);
71 sb.Append(TrimMarker);
72 sb.Append(value, startIndex + count - TrimSize / 2, TrimSize / 2);
78 internal string MarkOutError() {
79 if (queryString == null || queryString.Trim(' ').Length == 0) {
83 int len = endChar - startChar;
84 StringBuilder sb = new StringBuilder();
86 AppendTrimmed(sb, queryString, 0, startChar, TrimType.Left);
89 AppendTrimmed(sb, queryString, startChar, len, TrimType.Middle);
93 AppendTrimmed(sb, queryString, endChar, queryString.Length - endChar, TrimType.Right);
98 internal override string FormatDetailedMessage() {
99 string message = Message;
100 string error = MarkOutError();
102 if (error != null && error.Length > 0) {
103 if (message.Length > 0) {
104 message += Environment.NewLine;