Project

Profile

Help

How to connect?
Download (10.4 KB) Statistics
| Branch: | Tag: | Revision:

he / tags / 9.4.0.7 / hen / csource / api / Saxon.Api / Errors.cs @ b0b00ab2

1
using System;
2
using System.Collections;
3
using javax.xml.transform;
4
using javax.xml.transform.stream;
5
using XPathException = net.sf.saxon.trans.XPathException;
6

    
7

    
8
namespace Saxon.Api
9
{
10

    
11
    /// <summary>
12
    /// The StaticError class contains information about a static error detected during
13
    /// compilation of a stylesheet, query, or XPath expression.
14
    /// </summary>
15

    
16
    [Serializable]
17
    public class StaticError : Exception
18
    {
19

    
20
        private XPathException exception;
21
        internal bool isWarning;
22

    
23
        /// <summary>
24
        /// Create a new StaticError, wrapping a Saxon XPathException
25
        /// </summary>
26

    
27
        internal StaticError(TransformerException err)
28
        {
29
            if (err is XPathException)
30
            {
31
                this.exception = (XPathException)err;
32
            }
33
            else
34
            {
35
                this.exception = XPathException.makeXPathException(err);
36
            }
37
        }
38

    
39
        /// <summary>
40
        /// The error code, as a QName. May be null if no error code has been assigned
41
        /// </summary>
42

    
43
        public QName ErrorCode
44
        {
45
            get
46
            {
47
                return new QName("err",
48
                        exception.getErrorCodeNamespace(),
49
                        exception.getErrorCodeLocalPart());
50
            }
51
        }
52

    
53
        /// <summary>
54
        /// Return the message associated with this error
55
        /// </summary>
56

    
57
        public override String Message
58
        {
59
            get
60
            {
61
                return exception.getMessage();
62
            }
63
        }
64

    
65
        /// <summary>
66
        /// The URI of the query or stylesheet module in which the error was detected
67
        /// (as a string)
68
        /// </summary>
69
        /// <remarks>
70
        /// May be null if the location of the error is unknown, or if the error is not
71
        /// localized to a specific module, or if the module in question has no known URI
72
        /// (for example, if it was supplied as an anonymous Stream)
73
        /// </remarks>
74

    
75
        public String ModuleUri
76
        {
77
            get
78
            {
79
                if (exception.getLocator() == null)
80
                {
81
                    return null;
82
                }
83
                return exception.getLocator().getSystemId();
84
            }
85
        }
86

    
87
        /// <summary>
88
        /// The line number locating the error within a query or stylesheet module
89
        /// </summary>
90
        /// <remarks>
91
        /// May be set to -1 if the location of the error is unknown
92
        /// </remarks>        
93

    
94
        public int LineNumber
95
        {
96
            get
97
            {
98
                SourceLocator loc = exception.getLocator();
99
                if (loc == null)
100
                {
101
                    if (exception.getException() is TransformerException)
102
                    {
103
                        loc = ((TransformerException)exception.getException()).getLocator();
104
                        if (loc != null)
105
                        {
106
                            return loc.getLineNumber();
107
                        }
108
                    }
109
                    return -1;
110
                }
111
                return loc.getLineNumber();
112
            }
113
        }
114

    
115
        /// <summary>
116
        /// Indicate whether this error is being reported as a warning condition. If so, applications
117
        /// may ignore the condition, though the results may not be as intended.
118
        /// </summary>
119

    
120
        public bool IsWarning
121
        {
122
            get
123
            {
124
                return isWarning;
125
            }
126
            set
127
            {
128
                isWarning = value;
129
            }
130
        }
131

    
132
        /// <summary>
133
        /// Indicate whether this condition is a type error.
134
        /// </summary>
135

    
136
        public bool IsTypeError
137
        {
138
            get
139
            {
140
                return exception.isTypeError();
141
            }
142
        }
143

    
144
        /// <summary>
145
        /// Return the error message.
146
        /// </summary>
147

    
148
        public override String ToString()
149
        {
150
            return exception.getMessage();
151
        }
152
    }
153

    
154
    /// <summary>
155
    /// The DynamicError class contains information about a dynamic error detected during
156
    /// execution of a stylesheet, query, or XPath expression.
157
    /// </summary>
158

    
159
    [Serializable]
160
    public class DynamicError : Exception
161
    {
162

    
163
        private XPathException exception;
164
        internal bool isWarning;
165

    
166
        /// <summary>
167
        /// Create a new DynamicError, specifying the error message
168
        /// </summary>
169
        /// <param name="message">The error message</param>
170

    
171
        public DynamicError(String message)
172
        {
173
            exception = new XPathException(message);
174
        }
175

    
176
        /// <summary>
177
        /// Create a new DynamicError, wrapping a Saxon XPathException
178
        /// </summary>
179
        /// <param name="err">The exception to be wrapped</param>
180

    
181
        internal DynamicError(TransformerException err)
182
        {
183
            if (err is XPathException)
184
            {
185
                this.exception = (XPathException)err;
186
            }
187
            else
188
            {
189
                this.exception = XPathException.makeXPathException(err);
190
            }
191
        }
192

    
193
        /// <summary>
194
        /// The error code, as a QName. May be null if no error code has been assigned
195
        /// </summary>
196

    
197
        public QName ErrorCode
198
        {
199
            get
200
            {
201
                return new QName("err",
202
                        exception.getErrorCodeNamespace(),
203
                        exception.getErrorCodeLocalPart());
204
            }
205
        }
206

    
207
        /// <summary>
208
        /// Return the message associated with this error
209
        /// </summary>
210

    
211
        public override String Message
212
        {
213
            get
214
            {
215
                return exception.getMessage();
216
            }
217
        }
218

    
219
        /// <summary>
220
        /// The URI of the query or stylesheet module in which the error was detected
221
        /// (as a string)
222
        /// </summary>
223
        /// <remarks>
224
        /// May be null if the location of the error is unknown, or if the error is not
225
        /// localized to a specific module, or if the module in question has no known URI
226
        /// (for example, if it was supplied as an anonymous Stream)
227
        /// </remarks>
228

    
229
        public String ModuleUri
230
        {
231
            get
232
            {
233
                if (exception.getLocator() == null)
234
                {
235
                    return null;
236
                }
237
                return exception.getLocator().getSystemId();
238
            }
239
        }
240

    
241
        /// <summary>
242
        /// The line number locating the error within a query or stylesheet module
243
        /// </summary>
244
        /// <remarks>
245
        /// May be set to -1 if the location of the error is unknown
246
        /// </remarks>        
247

    
248
        public int LineNumber
249
        {
250
            get
251
            {
252
                SourceLocator loc = exception.getLocator();
253
                if (loc == null)
254
                {
255
                    if (exception.getException() is TransformerException)
256
                    {
257
                        loc = ((TransformerException)exception.getException()).getLocator();
258
                        if (loc != null)
259
                        {
260
                            return loc.getLineNumber();
261
                        }
262
                    }
263
                    return -1;
264
                }
265
                return loc.getLineNumber();
266
            }
267
        }
268

    
269
        /// <summary>
270
        /// Indicate whether this error is being reported as a warning condition. If so, applications
271
        /// may ignore the condition, though the results may not be as intended.
272
        /// </summary>
273

    
274
        public bool IsWarning
275
        {
276
            get
277
            {
278
                return isWarning;
279
            }
280
            set
281
            {
282
                isWarning = value;
283
            }
284
        }
285

    
286
        /// <summary>
287
        /// Indicate whether this condition is a type error.
288
        /// </summary>
289

    
290
        public bool IsTypeError
291
        {
292
            get
293
            {
294
                return exception.isTypeError();
295
            }
296
        }
297

    
298
        /// <summary>
299
        /// Return the error message.
300
        /// </summary>
301

    
302
        public override String ToString()
303
        {
304
            return exception.getMessage();
305
        }
306

    
307

    
308

    
309
    }
310

    
311
    [Serializable]
312
    internal class ErrorGatherer : javax.xml.transform.ErrorListener
313
    {
314

    
315
        private IList errorList;
316

    
317
        public ErrorGatherer(IList errorList)
318
        {
319
            this.errorList = errorList;
320
        }
321

    
322
        public void warning(TransformerException exception)
323
        {
324
            StaticError se = new StaticError(exception);
325
            se.isWarning = true;
326
            //Console.WriteLine("(Adding warning " + exception.getMessage() + ")");
327
            errorList.Add(se);
328
        }
329

    
330
        public void error(TransformerException error)
331
        {
332
            StaticError se = new StaticError(error);
333
            se.isWarning = false;
334
            //Console.WriteLine("(Adding error " + error.getMessage() + ")");
335
            errorList.Add(se);
336
        }
337

    
338
        public void fatalError(TransformerException error)
339
        {
340
            StaticError se = new StaticError(error);
341
            se.isWarning = false;
342
            errorList.Add(se);
343
            //Console.WriteLine("(Adding fatal error " + error.getMessage() + ")");
344
            throw error;
345
        }
346
    }
347

    
348

    
349

    
350

    
351
}
352

    
353
//
354
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
355
// you may not use this file except in compliance with the License. You may obtain a copy of the
356
// License at http://www.mozilla.org/MPL/
357
//
358
// Software distributed under the License is distributed on an "AS IS" basis,
359
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
360
// See the License for the specific language governing rights and limitations under the License.
361
//
362
// The Original Code is: all this file.
363
//
364
// The Initial Developer of the Original Code is Michael H. Kay.
365
//
366
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
367
//
368
// Contributor(s): none.
369
//
(3-3/12)