c736a4618235e5304cd2a3a6997d15aa804f1c9b
[nit.git] / lib / sax / helpers / sax_locator_impl.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 # Provides an optional convenience implementation of `Locator`.
12 module sax::helpers::sax_locator_impl
13
14 import sax::sax_locator
15
16 # Provides an optional convenience implementation of `Locator`.
17 #
18 # This class is available mainly for application writers, who
19 # can use it to make a persistent snapshot of a locator at any
20 # point during a document parse:
21 #
22 # module example
23 # #
24 # import sax::helpers::SAXLocatorImpl
25 # import sax::ContentHandler
26 # #
27 # class Example super ContentHandler
28 # private var _locator: nullable SAXLocator = null
29 # private var start_loc: nullable SAXLocator = null
30 # #
31 # fun locator=(Locator locator) do
32 # # note the locator
33 # _locator = locator
34 # end
35 # #
36 # fun start_document do
37 # # save the location of the start of the document
38 # # for future use.
39 # start_loc = new SAXLocatorImpl.from(locator)
40 # end
41 # end
42 #
43 # Normally, parser writers will not use this class, since it
44 # is more efficient to provide location information only when
45 # requested, rather than constantly updating a `Locator` object.
46 #
47 # Note: The original source code and documentation of this class comes, in part,
48 # from [SAX 2.0](http://www.saxproject.org).
49 class SAXLocatorImpl super SAXLocator
50 redef var public_id: nullable String = null is writable
51 redef var system_id: nullable String = null is writable
52 redef var line_number: Int = -1 is writable
53 redef var column_number: Int = -1 is writable
54
55 # Zero-argument constructor.
56 #
57 # This will not normally be useful, since the main purpose
58 # of this class is to make a snapshot of an existing Locator.
59 init do
60 end
61
62 # Copy constructor.
63 #
64 # Create a persistent copy of the current state of a locator.
65 # When the original locator changes, this copy will still keep
66 # the original values (and it can be used outside the scope of
67 # `ContentHandler` methods).
68 #
69 # Parameters:
70 #
71 # * `locator`: locator to copy.
72 init with(locator: SAXLocator) do
73 public_id = locator.public_id
74 system_id = locator.system_id
75 line_number = locator.line_number
76 column_number = locator.column_number
77 end
78 end