ae2586be93ede93f9ec900d88a1b1d03ce60ed4d
[nit.git] / lib / sax / sax_parse_exception.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # This file is free software, which comes along with NIT. This software is
4 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
5 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
6 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
7 # is kept unaltered, and a notification of the changes is added.
8 # You are allowed to redistribute it and sell it, alone or is a part of
9 # another product.
10
11 # Encapsulates an XML parse error or warning.
12 module sax::sax_parse_exception
13
14 import sax_locator
15
16 # Encapsulates an XML parse error or warning.
17 #
18 # This exception may include information for locating the error
19 # in the original XML document, as if it came from a `Locator`
20 # object. Note that although the application
21 # will receive a `SAXParseException` as the argument to the handlers
22 # in the `ErrorHandler` interface,
23 # the application is not actually required to throw the exception;
24 # instead, it can simply read the information in it and take a
25 # different action.
26 #
27 # Note: The original documentation comes, in part,
28 # from [SAX 2.0](http://www.saxproject.org).
29 #
30 # SEE: `sax::SAXLocator`
31 #
32 # SEE: `sax::ErrorHandler`
33 class SAXParseException
34 super Error
35
36 # The public identifer of the entity that generated
37 # the error or warning.
38 var public_id: nullable String = null
39
40 # The system identifer of the entity that generated
41 # the error or warning.
42 #
43 # If its an URL, it must be fully resolved.
44 var system_id: nullable String = null
45
46 # The line number of the end of the text that
47 # caused the error or warning, or -1.
48 var line_number: Int = -1
49
50 # The column number of the end of the text that
51 # caused the error or warning, or -1.
52 var column_number: Int = -1
53
54 # Create a new SAXParseException from a message and a Locator.
55 #
56 # This constructor is especially useful when an application is
57 # creating its own exception from within a `ContentHandler` callback.
58 #
59 # Parameters:
60 #
61 # * `message`: error or warning message.
62 # * `locator`: locator object for the error or warning.
63 init with_locator(message: String, locator: SAXLocator) do
64 init(message)
65 public_id = locator.public_id
66 system_id = locator.system_id
67 line_number = locator.line_number
68 column_number = locator.column_number
69 end
70
71 # Create a new SAXParseException.
72 #
73 # This constructor is most useful for parser writers.
74 #
75 # All parameters except the message are as if they were provided by a
76 # `Locator`. For example, if the system identifier is a URL (including
77 # relative filename), the caller must resolve it fully before creating the
78 # exception.
79 #
80 # Parameters:
81 #
82 # * `message`: error or warning message.
83 # * `public_id`: public identifer of the entity that generated
84 # the error or warning.
85 # * `system_id`: system identifer of the entity that generated
86 # the error or warning.
87 # * `line_number`: line number of the end of the text that
88 # caused the error or warning.
89 # * `column_number`: column number of the end of the text that
90 # caused the error or warning.
91 init with(message: String, public_id: nullable String,
92 system_id: nullable String, line_number: Int, column_number: Int) do
93 init(message)
94 self.public_id = public_id
95 self.system_id = system_id
96 self.line_number = line_number
97 self.column_number = column_number
98 end
99
100 # Generate a complete message from the exception’s attributes.
101 fun full_message: String do
102 var location = ""
103
104 if public_id != null then
105 location = "PUBLIC \"{public_id.as(not null)}\""
106 end
107 if system_id != null then
108 if location != "" then
109 location += " "
110 end
111 location += "SYSTEM \"{system_id.as(not null)}\""
112 end
113 if line_number >= 0 or column_number >= 0 then
114 if location != "" then
115 location += " at "
116 end
117 location += "{line_number};{column_number}"
118 end
119 if location == "" then
120 return "{message}"
121 else
122 return "[{location}] {message}"
123 end
124 end
125
126 redef fun to_s do
127 return "sax::SAXParseException: {full_message}"
128 end
129
130 # Display `full_message` with a stack trace, then abort.
131 fun throw do
132 assert sax_parse: false else
133 sys.stderr.write "{full_message}\n"
134 end
135 end
136 end