File: XslException.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (193 lines) | stat: -rw-r--r-- 7,647 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//------------------------------------------------------------------------------
// <copyright file="XslException.cs" company="Microsoft">
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// </copyright>
// <owner current="true" primary="true">Microsoft</owner>
//------------------------------------------------------------------------------

using System.CodeDom.Compiler;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using System.Runtime.Serialization;
using System.Security.Permissions;
using System.Text;

namespace System.Xml.Xsl {
    using Res = System.Xml.Utils.Res;

    [Serializable]
    internal class XslTransformException : XsltException {

        protected XslTransformException(SerializationInfo info, StreamingContext context)
            : base(info, context) {}

        public XslTransformException(Exception inner, string res, params string[] args)
            : base(CreateMessage(res, args), inner) {}

        public XslTransformException(string message)
            : base(CreateMessage(message, null), null) {}

        internal XslTransformException(string res, params string[] args)
            : this(null, res, args) {}

        internal static string CreateMessage(string res, params string[] args) {
            string message = null;

            try {
                message = Res.GetString(res, args);
            }
            catch (MissingManifestResourceException) {
            }

            if (message != null) {
                return message;
            }

            StringBuilder sb = new StringBuilder(res);
            if (args != null && args.Length > 0) {
                Debug.Fail("Resource string '" + res + "' was not found");
                sb.Append('(');
                sb.Append(args[0]);
                for (int idx = 1; idx < args.Length; idx++) {
                    sb.Append(", ");
                    sb.Append(args[idx]);
                }
                sb.Append(')');
            }
            return sb.ToString();
        }

        internal virtual string FormatDetailedMessage() {
            return Message;
        }

        public override string ToString() {
            string result = this.GetType().FullName;
            string info = FormatDetailedMessage();
            if (info != null && info.Length > 0) {
                result += ": " + info;
            }
            if (InnerException != null) {
                result += " ---> " + InnerException.ToString() + Environment.NewLine + "   " + CreateMessage(Res.Xml_EndOfInnerExceptionStack);
            }
            if (StackTrace != null) {
                result += Environment.NewLine + StackTrace;
            }
            return result;
        }
    }

    [Serializable]
    internal class XslLoadException : XslTransformException {
        ISourceLineInfo lineInfo;

        protected XslLoadException (SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            bool hasLineInfo = (bool) info.GetValue("hasLineInfo", typeof(bool));

            if (hasLineInfo) {
                string  uriString;
                int     startLine, startPos, endLine, endPos;

                uriString   = (string)   info.GetValue("Uri"        , typeof(string ));
                startLine   = (int)      info.GetValue("StartLine"  , typeof(int    ));
                startPos    = (int)      info.GetValue("StartPos"   , typeof(int    ));
                endLine     = (int)      info.GetValue("EndLine"    , typeof(int    ));
                endPos      = (int)      info.GetValue("EndPos"     , typeof(int    ));

                lineInfo = new SourceLineInfo(uriString, startLine, startPos, endLine, endPos);
            }
        }

        [SecurityPermissionAttribute(SecurityAction.LinkDemand, SerializationFormatter=true)]
        public override void GetObjectData(SerializationInfo info, StreamingContext context) {
            base.GetObjectData(info, context);
            info.AddValue("hasLineInfo"  , lineInfo != null);

            if (lineInfo != null) {
                info.AddValue("Uri"      , lineInfo.Uri);
                info.AddValue("StartLine", lineInfo.Start.Line);
                info.AddValue("StartPos" , lineInfo.Start.Pos );
                info.AddValue("EndLine"  , lineInfo.End.Line);
                info.AddValue("EndPos"   , lineInfo.End.Pos );
            }
        }

        internal XslLoadException(string res, params string[] args)
            : base(null, res, args) {}

        internal XslLoadException(Exception inner, ISourceLineInfo lineInfo)
            : base(inner, Res.Xslt_CompileError2, null)
        {
            SetSourceLineInfo(lineInfo);
        }

#if !DISABLE_XSLT_SCRIPT
        internal XslLoadException(CompilerError error)
            : base(Res.Xml_UserException, new string[] { error.ErrorText })
        {
            int errorLine = error.Line;
            int errorColumn = error.Column;

            if (errorLine == 0) {
                // If the compiler reported error on Line 0 - ignore columns, 
                //   0 means it doesn't know where the error was and our SourceLineInfo
                //   expects either all zeroes or all non-zeroes
                errorColumn = 0;
            }
            else {
                if (errorColumn == 0) {
                    // In this situation the compiler returned for example Line 10, Column 0.
                    // This means that the compiler knows the line of the error, but it doesn't
                    //   know (or support) the column part of the location.
                    // Since we don't allow column 0 (as it's invalid), let's turn it into 1 (the begining of the line)
                    errorColumn = 1;
                }
            }

            SetSourceLineInfo(new SourceLineInfo(error.FileName, errorLine, errorColumn, errorLine, errorColumn));
        }
#endif

        internal void SetSourceLineInfo(ISourceLineInfo lineInfo) {
            Debug.Assert(lineInfo == null || lineInfo.Uri != null);
            this.lineInfo = lineInfo;
        }

        public override string SourceUri {
            get { return lineInfo != null ? lineInfo.Uri : null; }
        }

        public override int LineNumber {
            get { return lineInfo != null ? lineInfo.Start.Line : 0; }
        }

        public override int LinePosition {
            get { return lineInfo != null ? lineInfo.Start.Pos : 0; }
        }

        private static string AppendLineInfoMessage(string message, ISourceLineInfo lineInfo) {
            if (lineInfo != null) {
                string fileName = SourceLineInfo.GetFileName(lineInfo.Uri);
                string lineInfoMessage = CreateMessage(Res.Xml_ErrorFilePosition, fileName, lineInfo.Start.Line.ToString(CultureInfo.InvariantCulture), lineInfo.Start.Pos.ToString(CultureInfo.InvariantCulture));
                if (lineInfoMessage != null && lineInfoMessage.Length > 0) {
                    if (message.Length > 0 && !XmlCharType.Instance.IsWhiteSpace(message[message.Length - 1])) {
                        message += " ";
                    }
                    message += lineInfoMessage;
                }
            }
            return message;
        }

        internal static string CreateMessage(ISourceLineInfo lineInfo, string res, params string[] args) {
            return AppendLineInfoMessage(CreateMessage(res, args), lineInfo);
        }

        internal override string FormatDetailedMessage() {
            return AppendLineInfoMessage(Message, lineInfo);
        }
    }
}