Class: GoobyBaseSaxParser
- Inherits:
-
Object
- Object
- GoobyBaseSaxParser
- Includes:
- LibXML, XML::SaxParser::Callbacks
- Defined in:
- lib/gooby_base_sax_parser.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#current_tagname ⇒ Object
readonly
Returns the value of attribute current_tagname.
-
#end_reached ⇒ Object
readonly
Returns the value of attribute end_reached.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#exception ⇒ Object
readonly
Returns the value of attribute exception.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#mappings ⇒ Object
readonly
Returns the value of attribute mappings.
-
#xml ⇒ Object
readonly
Returns the value of attribute xml.
Instance Method Summary collapse
-
#end_reached? ⇒ Boolean
Indicates if the ending ‘on_end_document’ handler method has been called or not.
- #get_current_text ⇒ Object
-
#has_error? ⇒ Boolean
Indicates if an Error, though not necessarily an Exception, has occurred.
-
#has_exception? ⇒ Boolean
Indicates if an Exception, though not necessarily an Error, has occurred.
-
#initialize ⇒ GoobyBaseSaxParser
constructor
A new instance of GoobyBaseSaxParser.
- #on_cdata_block(cdata) ⇒ Object
- #on_characters(chars) ⇒ Object
- #on_end_document ⇒ Object
- #on_error(msg) ⇒ Object
- #process_file(file) ⇒ Object
- #process_xml(xml_string) ⇒ Object
- #set_error(e) ⇒ Object
- #subclass_initialize ⇒ Object
Constructor Details
#initialize ⇒ GoobyBaseSaxParser
Returns a new instance of GoobyBaseSaxParser.
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/gooby_base_sax_parser.rb', line 23 def initialize @parser = XML::SaxParser.new @parser.callbacks = self @list, @current_text, @current_tagname = [], '', '' @exception, @error, @end_reached = nil, nil, false @mappings = {} subclass_initialize # XML::Error.set_handler do | e | # self.set_error(e) # end end |
Instance Attribute Details
#current_tagname ⇒ Object (readonly)
Returns the value of attribute current_tagname.
20 21 22 |
# File 'lib/gooby_base_sax_parser.rb', line 20 def current_tagname @current_tagname end |
#end_reached ⇒ Object (readonly)
Returns the value of attribute end_reached.
21 22 23 |
# File 'lib/gooby_base_sax_parser.rb', line 21 def end_reached @end_reached end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
21 22 23 |
# File 'lib/gooby_base_sax_parser.rb', line 21 def error @error end |
#exception ⇒ Object (readonly)
Returns the value of attribute exception.
21 22 23 |
# File 'lib/gooby_base_sax_parser.rb', line 21 def exception @exception end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
20 21 22 |
# File 'lib/gooby_base_sax_parser.rb', line 20 def list @list end |
#mappings ⇒ Object (readonly)
Returns the value of attribute mappings.
20 21 22 |
# File 'lib/gooby_base_sax_parser.rb', line 20 def mappings @mappings end |
#xml ⇒ Object (readonly)
Returns the value of attribute xml.
20 21 22 |
# File 'lib/gooby_base_sax_parser.rb', line 20 def xml @xml end |
Instance Method Details
#end_reached? ⇒ Boolean
Indicates if the ending ‘on_end_document’ handler method has been called or not.
70 71 72 |
# File 'lib/gooby_base_sax_parser.rb', line 70 def end_reached? @end_reached end |
#get_current_text ⇒ Object
82 83 84 |
# File 'lib/gooby_base_sax_parser.rb', line 82 def get_current_text (@current_text) ? @current_text.strip : '' end |
#has_error? ⇒ Boolean
Indicates if an Error, though not necessarily an Exception, has occurred.
65 66 67 |
# File 'lib/gooby_base_sax_parser.rb', line 65 def has_error? (@error == nil) ? false : true end |
#has_exception? ⇒ Boolean
Indicates if an Exception, though not necessarily an Error, has occurred.
60 61 62 |
# File 'lib/gooby_base_sax_parser.rb', line 60 def has_exception? (@exception == nil) ? false : true end |
#on_cdata_block(cdata) ⇒ Object
78 79 80 |
# File 'lib/gooby_base_sax_parser.rb', line 78 def on_cdata_block(cdata) @current_text << cdata if cdata end |
#on_characters(chars) ⇒ Object
74 75 76 |
# File 'lib/gooby_base_sax_parser.rb', line 74 def on_characters(chars) @current_text << chars if chars end |
#on_end_document ⇒ Object
90 91 92 93 |
# File 'lib/gooby_base_sax_parser.rb', line 90 def on_end_document @end_reached = true XML::Error.reset_handler end |
#on_error(msg) ⇒ Object
86 87 88 |
# File 'lib/gooby_base_sax_parser.rb', line 86 def on_error(msg) @error = "#{msg}" end |
#process_file(file) ⇒ Object
43 44 45 |
# File 'lib/gooby_base_sax_parser.rb', line 43 def process_file(file) process_xml(File.read(file)) end |
#process_xml(xml_string) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gooby_base_sax_parser.rb', line 47 def process_xml(xml_string) begin @xml = xml_string @parser.string = @xml @parser.parse rescue Exception => e @exception = "#{e.class} #{e.}" raise e end @list end |
#set_error(e) ⇒ Object
39 40 41 |
# File 'lib/gooby_base_sax_parser.rb', line 39 def set_error(e) @error = "#{e}" end |
#subclass_initialize ⇒ Object
35 36 37 |
# File 'lib/gooby_base_sax_parser.rb', line 35 def subclass_initialize # subclasses should override this method. set @mappings and other variables. end |