Project

Profile

Help

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

he / latest9.1 / bn / csource / api / Saxon.Api / Errors.cs @ e090dcb1

1
using System;
2
using System.Collections;
3
using javax.xml.transform;
4
using javax.xml.transform.stream;
5
using JStaticError = net.sf.saxon.trans.StaticError;
6
using JDynamicError = net.sf.saxon.trans.DynamicError;
7
using XPathException = net.sf.saxon.trans.XPathException;
8

    
9

    
10
namespace Saxon.Api
11
{
12

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

    
18
    [Serializable]
19
    public class StaticError : Exception
20
    {
21

    
22
        private XPathException exception;
23
        internal bool isWarning;
24

    
25
        /// <summary>
26
        /// Create a new StaticError, wrapping a Saxon XPathException
27
        /// </summary>
28

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

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

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

    
55
        /// <summary>
56
        /// Return the message associated with this error
57
        /// </summary>
58

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

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

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

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

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

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

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

    
134
        /// <summary>
135
        /// Indicate whether this condition is a type error.
136
        /// </summary>
137

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

    
146
        /// <summary>
147
        /// Return the error message.
148
        /// </summary>
149

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

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

    
161
    [Serializable]
162
    public class DynamicError : Exception
163
    {
164

    
165
        private XPathException exception;
166
        internal bool isWarning;
167

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

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

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

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

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

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

    
209
        /// <summary>
210
        /// Return the message associated with this error
211
        /// </summary>
212

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

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

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

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

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

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

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

    
288
        /// <summary>
289
        /// Indicate whether this condition is a type error.
290
        /// </summary>
291

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

    
300
        /// <summary>
301
        /// Return the error message.
302
        /// </summary>
303

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

    
309

    
310

    
311
    }
312

    
313
    [Serializable]
314
    internal class ErrorGatherer : javax.xml.transform.ErrorListener
315
    {
316

    
317
        private IList errorList;
318

    
319
        public ErrorGatherer(IList errorList)
320
        {
321
            this.errorList = errorList;
322
        }
323

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

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

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

    
350

    
351

    
352

    
353
}
354

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