Project

Profile

Help

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

he / tags / 9.6.0.7 / hen / csource / api / Saxon.Api / Errors.cs @ aa733b18

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
				if (exception.getErrorCodeLocalPart () != null) {
48
					return new QName ("err",
49
						exception.getErrorCodeNamespace (),
50
						exception.getErrorCodeLocalPart ());
51
				} else {
52
					return null;
53
				}
54
			}
55
		}
56

    
57
		/// <summary>
58
		/// Return the message associated with this error
59
		/// </summary>
60

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

    
69

    
70
		/// <summary>
71
		/// Return the message associated with this error concatenated with the message from the causing exception
72
		/// </summary> 
73
		public String InnerMessage
74
		{
75
			get {
76

    
77
				return exception.getMessage() +": " + exception.getCause().Message;
78
			}
79

    
80
		}
81

    
82

    
83

    
84

    
85
		/// <summary>
86
		/// The URI of the query or stylesheet module in which the error was detected
87
		/// (as a string)
88
		/// </summary>
89
		/// <remarks>
90
		/// May be null if the location of the error is unknown, or if the error is not
91
		/// localized to a specific module, or if the module in question has no known URI
92
		/// (for example, if it was supplied as an anonymous Stream)
93
		/// </remarks>
94

    
95
		public String ModuleUri
96
		{
97
			get
98
			{
99
				if (exception.getLocator() == null)
100
				{
101
					return null;
102
				}
103
				return exception.getLocator().getSystemId();
104
			}
105
		}
106

    
107
		/// <summary>
108
		/// The line number locating the error within a query or stylesheet module
109
		/// </summary>
110
		/// <remarks>
111
		/// May be set to -1 if the location of the error is unknown
112
		/// </remarks>        
113

    
114
		public int LineNumber
115
		{
116
			get
117
			{
118
				SourceLocator loc = exception.getLocator();
119
				if (loc == null)
120
				{
121
					if (exception.getException() is TransformerException)
122
					{
123
						loc = ((TransformerException)exception.getException()).getLocator();
124
						if (loc != null)
125
						{
126
							return loc.getLineNumber();
127
						}
128
					}
129
					return -1;
130
				}
131
				return loc.getLineNumber();
132
			}
133
		}
134

    
135

    
136
		/// <summary>
137
		/// The line number locating the error within a query or stylesheet module
138
		/// </summary>
139
		/// <remarks>
140
		/// May be set to -1 if the location of the error is unknown
141
		/// </remarks>        
142

    
143
		public int ColoumnNumber
144
		{
145
			get
146
			{
147
				SourceLocator loc = exception.getLocator();
148
				if (loc == null)
149
				{
150
					if (exception.getException() is TransformerException)
151
					{
152
						loc = ((TransformerException)exception.getException()).getLocator();
153
						if (loc != null)
154
						{
155
							return loc.getColumnNumber();
156
						}
157
					}
158
					return -1;
159
				}
160
				return loc.getColumnNumber();
161
			}
162
		}
163

    
164
		/// <summary>
165
		/// Indicate whether this error is being reported as a warning condition. If so, applications
166
		/// may ignore the condition, though the results may not be as intended.
167
		/// </summary>
168

    
169
		public bool IsWarning
170
		{
171
			get
172
			{
173
				return isWarning;
174
			}
175
			set
176
			{
177
				isWarning = value;
178
			}
179
		}
180

    
181
		/// <summary>
182
		/// Indicate whether this condition is a type error.
183
		/// </summary>
184

    
185
		public bool IsTypeError
186
		{
187
			get
188
			{
189
				return exception.isTypeError();
190
			}
191
		}
192

    
193
		/// <summary>
194
		/// Return the underlying exception. This is unstable as this is an internal object
195
		/// </summary>
196
		/// <returns>XPathException</returns>
197
		public XPathException UnderlyingException
198
		{
199
			get
200
			{
201
				return exception;
202
			}
203
		}
204

    
205
		/// <summary>
206
		/// Return the error message.
207
		/// </summary>
208

    
209
		public override String ToString()
210
		{
211
			return exception.getMessage();
212
		}
213
	}
214

    
215
	/// <summary>
216
	/// The DynamicError class contains information about a dynamic error detected during
217
	/// execution of a stylesheet, query, or XPath expression.
218
	/// </summary>
219

    
220
	[Serializable]
221
	public class DynamicError : Exception
222
	{
223

    
224
		private XPathException exception;
225
		internal bool isWarning;
226

    
227
		/// <summary>
228
		/// Create a new DynamicError, specifying the error message
229
		/// </summary>
230
		/// <param name="message">The error message</param>
231

    
232
		public DynamicError(String message)
233
		{
234
			exception = new XPathException(message);
235
		}
236

    
237
		/// <summary>
238
		/// Create a new DynamicError, wrapping a Saxon XPathException
239
		/// </summary>
240
		/// <param name="err">The exception to be wrapped</param>
241

    
242
		internal DynamicError(TransformerException err)
243
		{
244
			if (err is XPathException)
245
			{
246
				this.exception = (XPathException)err;
247
			}
248
			else
249
			{
250
				this.exception = XPathException.makeXPathException(err);
251
			}
252
		}
253

    
254
		/// <summary>
255
		/// The error code, as a QName. May be null if no error code has been assigned
256
		/// </summary>
257

    
258
		public QName ErrorCode
259
		{
260
			get
261
			{
262
				return new QName("err",
263
					exception.getErrorCodeNamespace(),
264
					exception.getErrorCodeLocalPart());
265
			}
266
		}
267

    
268
		/// <summary>
269
		/// Return the message associated with this error
270
		/// </summary>
271

    
272
		public override String Message
273
		{
274
			get
275
			{
276
				return exception.getMessage();
277
			}
278
		}
279

    
280
		/// <summary>
281
		/// The URI of the query or stylesheet module in which the error was detected
282
		/// (as a string)
283
		/// </summary>
284
		/// <remarks>
285
		/// May be null if the location of the error is unknown, or if the error is not
286
		/// localized to a specific module, or if the module in question has no known URI
287
		/// (for example, if it was supplied as an anonymous Stream)
288
		/// </remarks>
289

    
290
		public String ModuleUri
291
		{
292
			get
293
			{
294
				if (exception.getLocator() == null)
295
				{
296
					return null;
297
				}
298
				return exception.getLocator().getSystemId();
299
			}
300
		}
301

    
302
		/// <summary>
303
		/// The line number locating the error within a query or stylesheet module
304
		/// </summary>
305
		/// <remarks>
306
		/// May be set to -1 if the location of the error is unknown
307
		/// </remarks>        
308

    
309
		public int LineNumber
310
		{
311
			get
312
			{
313
				SourceLocator loc = exception.getLocator();
314
				if (loc == null)
315
				{
316
					if (exception.getException() is TransformerException)
317
					{
318
						loc = ((TransformerException)exception.getException()).getLocator();
319
						if (loc != null)
320
						{
321
							return loc.getLineNumber();
322
						}
323
					}
324
					return -1;
325
				}
326
				return loc.getLineNumber();
327
			}
328
		}
329

    
330
		/// <summary>
331
		/// Indicate whether this error is being reported as a warning condition. If so, applications
332
		/// may ignore the condition, though the results may not be as intended.
333
		/// </summary>
334

    
335
		public bool IsWarning
336
		{
337
			get
338
			{
339
				return isWarning;
340
			}
341
			set
342
			{
343
				isWarning = value;
344
			}
345
		}
346

    
347
		/// <summary>
348
		/// Indicate whether this condition is a type error.
349
		/// </summary>
350

    
351
		public bool IsTypeError
352
		{
353
			get
354
			{
355
				return exception.isTypeError();
356
			}
357
		}
358

    
359
		/// <summary>
360
		/// Return the error message.
361
		/// </summary>
362

    
363
		public override String ToString()
364
		{
365
			return exception.getMessage();
366
		}
367

    
368
		/// <summary>
369
		/// Return the underlying exception. This is unstable as this is an internal object
370
		/// </summary>
371
		/// <returns>XPathException</returns>
372
		public XPathException UnderlyingException
373
		{
374
			get
375
			{
376
				return exception;
377
			}
378
		}
379

    
380

    
381
	}
382

    
383
	/// <summary>
384
	/// Error gatherer. This class To provide customized error handling
385
	/// <p>If an application does <em>not</em> register its own custom
386
	/// <para><code>ErrorListener</code>, the default <code>ErrorGatherer</code>
387
	/// is used which keeps track of all warnings and errors in a list.
388
	/// and does not throw any <code>Exception</code>s.
389
	/// Applications are <em>strongly</em> encouraged to register and use
390
	/// <code>ErrorListener</code>s that insure proper behavior for warnings and
391
	/// errors.</para>
392
	/// </summary>
393
	[Serializable]
394
	internal class ErrorGatherer : javax.xml.transform.ErrorListener
395
	{
396

    
397
		private IList errorList;
398

    
399

    
400
		/// <summary>
401
		/// Initializes a new instance of the <see cref="Saxon.Api.ErrorGatherer"/> class.
402
		/// </summary>
403
		/// <param name="errorList">Error list.</param>
404
		public ErrorGatherer(IList errorList)
405
		{
406
			this.errorList = errorList;
407
		}
408

    
409
		/// <summary>
410
		/// Warning the specified exception.
411
		/// </summary>
412
		/// <param name="exception">TransformerException.</param>
413
		public void warning(TransformerException exception)
414
		{
415
			StaticError se = new StaticError(exception);
416
			se.isWarning = true;
417
			//Console.WriteLine("(Adding warning " + exception.getMessage() + ")");
418
			errorList.Add(se);
419
		}
420

    
421
		/// <summary>
422
		/// Report a Transformer exception thrown.
423
		/// </summary>
424
		/// <param name="error">Error.</param>
425
		public void error(TransformerException error)
426
		{
427
			StaticError se = new StaticError(error);
428
			se.isWarning = false;
429
			//Console.WriteLine("(Adding error " + error.getMessage() + ")");
430
			errorList.Add(se);
431
		}
432

    
433
		/// <summary>
434
		/// Report a fatal exception thrown.
435
		/// </summary>
436
		/// <param name="error">TransformerException.</param>
437
		public void fatalError(TransformerException error)
438
		{
439
			StaticError se = new StaticError(error);
440
			se.isWarning = false;
441
			errorList.Add(se);
442
			//Console.WriteLine("(Adding fatal error " + error.getMessage() + ")");
443
		}
444

    
445

    
446
		/// <summary>
447
		/// Gets the error list.
448
		/// </summary>
449
		/// <returns>Returns the error list</returns>
450
		public IList ErrorList{
451
			get { return errorList;}
452
		}
453
	}
454

    
455

    
456

    
457

    
458
}
459

    
460
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
461
// Copyright (c) 2013 Saxonica Limited.
462
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
463
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
464
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
465
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
(3-3/13)