Class: GoobyBaseSaxParser

Inherits:
Object
  • Object
show all
Includes:
LibXML, XML::SaxParser::Callbacks
Defined in:
lib/gooby_base_sax_parser.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGoobyBaseSaxParser

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_tagnameObject (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_reachedObject (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

#errorObject (readonly)

Returns the value of attribute error.



21
22
23
# File 'lib/gooby_base_sax_parser.rb', line 21

def error
  @error
end

#exceptionObject (readonly)

Returns the value of attribute exception.



21
22
23
# File 'lib/gooby_base_sax_parser.rb', line 21

def exception
  @exception
end

#listObject (readonly)

Returns the value of attribute list.



20
21
22
# File 'lib/gooby_base_sax_parser.rb', line 20

def list
  @list
end

#mappingsObject (readonly)

Returns the value of attribute mappings.



20
21
22
# File 'lib/gooby_base_sax_parser.rb', line 20

def mappings
  @mappings
end

#xmlObject (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.

Returns:

  • (Boolean)


70
71
72
# File 'lib/gooby_base_sax_parser.rb', line 70

def end_reached?
  @end_reached
end

#get_current_textObject



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


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_documentObject



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.message}"
    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_initializeObject



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